<?php
namespace app\workerapi\controller\shops;

use app\common\logic\PaymentLogic;
use app\workerapi\controller\BaseApiController;
use app\workerapi\lists\shops\ShopOrderLists;
use app\workerapi\logic\shops\ShopOrderLogic;
use app\workerapi\validate\shops\ShopOrderValidate;
use app\workerapi\validate\shops\ShopPayValidate;

/**
 * 商城订单
 */
class OrderController extends BaseApiController
{

    public function submitOrder()
    {
        $params = (new ShopOrderValidate())->post()->goCheck('submit', [
            'user_id' => $this->userId,
            'terminal' => $this->userInfo['terminal'],
            'user_info' => $this->userInfo
        ]);
        $result = ShopOrderLogic::submitOrder($params);
        if (false === $result) {
            return $this->fail(ShopOrderLogic::getError());
        }
        return $this->data($result);
    }

    public function lists()
    {
        return $this->dataLists(new ShopOrderLists());
    }

    public function detail()
    {
        $params = (new ShopOrderValidate())->goCheck('detail',[
            'worker_id' => $this->userId,
        ]);
        $result = ShopOrderLogic::detail($params);
        if (false === $result) {
            return $this->fail(ShopOrderLogic::getError());
        }
        return $this->data($result);
    }

    /**
     * 取消订单
     */
    public function cancelOrder()
    {
        $params = (new ShopOrderValidate())->goCheck('detail',[
            'worker_id' => $this->userId,
        ]);
        $result = ShopOrderLogic::cancelOrder($params);
        if (false === $result) {
            return $this->fail(ShopOrderLogic::getError());
        }
        return $this->success('取消成功', [], 1, 1);
    }

    /**
     * @notes 预支付
     * @return \think\response\Json
     */
    public function prepay()
    {
        $params = (new ShopPayValidate())->post()->goCheck('pay');
        //订单信息
        $order = PaymentLogic::getPayShopOrderInfo($params);
        if (false === $order) {
            return $this->fail(PaymentLogic::getError(), $params);
        }
        //支付流程
        $redirectUrl = $params['redirect'] ?? '/pages/payment/payment';
        $result = PaymentLogic::shopPay($params['pay_way'], 'shop_goods', $order, $this->userInfo, $redirectUrl);
        if (false === $result) {
            return $this->fail(PaymentLogic::getError(), $params);
        }
        $result['sn'] = $order['sn'];
        return $this->success('', $result);
    }

    public function logisticDetail()
    {
        $params = (new ShopOrderValidate())->goCheck('detail',[
            'worker_id' => $this->userId,
        ]);
        $result = ShopOrderLogic::logisticDetail($params);
        if (false === $result) {
            return $this->fail(ShopOrderLogic::getError());
        }
        return $this->data($result);
    }

    public function confirmReceipt()
    {
        $params = (new ShopOrderValidate())->goCheck('detail',[
            'worker_id' => $this->userId,
        ]);
        $result = ShopOrderLogic::confirmReceipt($params);
        if (false === $result) {
            return $this->fail(ShopOrderLogic::getError());
        }
        return $this->success('确认收货成功', [], 1, 1);
    }

    public function deleteOrder()
    {
        $params = (new ShopOrderValidate())->goCheck('detail',[
            'worker_id' => $this->userId,
        ]);
        $result = ShopOrderLogic::deleteOrder($params);
        if (false === $result) {
            return $this->fail(ShopOrderLogic::getError());
        }
        return $this->success('删除成功', [], 1, 1);
    }
}