<?php
namespace app\api\controller;

use app\api\lists\recharge\ServiceOrderLists;
use app\api\logic\ServiceOrderLogic;
use app\api\validate\ServiceOrderValidate;
use think\facade\Log;

/**
 * 订单类
 */
class ServiceOrderController extends BaseApiController
{
    /**
     * 订单列表
     * @return \think\response\Json
     */
    public function lists()
    {
        return $this->dataLists(new ServiceOrderLists());
    }

    /**
     * 订单详情
     * @return \think\response\Json
     */
    public function detail()
    {
        $params = (new ServiceOrderValidate())->goCheck('detail',[
            'user_id' => $this->userId,
        ]);
        $result = ServiceOrderLogic::detail($params);
        if (false === $result) {
            return $this->fail(ServiceOrderLogic::getError());
        }
        return $this->data($result);
    }

    /**
     * 订单支付详情
     * @return \think\response\Json
     */
    public function orderPayInfo()
    {
        $params = (new ServiceOrderValidate())->goCheck('detail',[
            'user_id' => $this->userId,
        ]);
        $result = ServiceOrderLogic::orderPayInfo($params);
        if (false === $result) {
            return $this->fail(ServiceOrderLogic::getError());
        }
        return $this->data($result);
    }

    public function getMasterWorker()
    {
        $params = (new ServiceOrderValidate())->goCheck('worker',[
            'user_id' => $this->userId,
        ]);
        $result = ServiceOrderLogic::getMasterWorker($params);
        if (false === $result) {
            return $this->fail(ServiceOrderLogic::getError());
        }
        return $this->data($result);
    }

    /**
     * 提交订单
     * @return \think\response\Json
     */
    public function submitOrder()
    {
        $params = (new ServiceOrderValidate())->post()->goCheck('add', [
            'user_id' => $this->userId,
            'terminal' => $this->userInfo['terminal'],
            'user_info' => $this->userInfo
        ]);
        Log::write(json_encode($params,JSON_UNESCAPED_UNICODE));
        $result = ServiceOrderLogic::submitOrder($params);
        if (false === $result) {
            return $this->fail(ServiceOrderLogic::getError());
        }
        return $this->data($result);
    }

    /**
     * 取消订单
     * @return \think\response\Json
     */
    public function cancelOrder()
    {
        $params = (new ServiceOrderValidate())->post()->goCheck('cancel', [
            'user_id' => $this->userId,
            'terminal' => $this->userInfo['terminal'],
            'user_info' => $this->userInfo
        ]);
        $result = ServiceOrderLogic::cancelOrder($params);
        if (false === $result) {
            return $this->fail(ServiceOrderLogic::getError());
        }
        return $this->success('取消成功', [], 1, 1);
    }

    /**
     * 确认报价订单
     * @return \think\response\Json
     */
    public function confirmOrder()
    {
        $params = (new ServiceOrderValidate())->post()->goCheck('price', [
            'user_id' => $this->userId,
            'user_info' => $this->userInfo
        ]);
        $result = ServiceOrderLogic::confirmOrder($params);
        if (false === $result) {
            return $this->fail(ServiceOrderLogic::getError());
        }
        return $this->success('已确认报价，师傅即将开始服务', [], 1, 1);
    }

    /**
     * 用户确认服务完成
     * @return \think\response\Json
     */
    public function confirmServiceFinish()
    {
        $params = (new ServiceOrderValidate())->post()->goCheck('finished', [
            'user_id' => $this->userId,
            'user_info' => $this->userInfo
        ]);
        $result = ServiceOrderLogic::confirmServiceFinish($params);
        if (false === $result) {
            return $this->fail(ServiceOrderLogic::getError());
        }
        return $this->success('已确认服务完成', [], 1, 1);
    }

    /**
     * 提交尾款订单
     * @return \think\response\Json
     */
    public function submitFinalOrder()
    {
        $params = (new ServiceOrderValidate())->post()->goCheck('final', [
            'user_id' => $this->userId,
            'terminal' => $this->userInfo['terminal'],
            'user_info' => $this->userInfo
        ]);
        Log::write(json_encode($params,JSON_UNESCAPED_UNICODE));
        $result = ServiceOrderLogic::submitFinalOrder($params);
        if (false === $result) {
            return $this->fail(ServiceOrderLogic::getError());
        }
        return $this->data($result);
    }

    /**
     * 重置订单优惠券
     * @return \think\response\Json
     */
    public function cancelOrderCoupon()
    {
        $params = (new ServiceOrderValidate())->post()->goCheck('cancelOrderCoupon', [
            'user_id' => $this->userId,
            'user_info' => $this->userInfo
        ]);
        $result = ServiceOrderLogic::cancelOrderCoupon($params);
        if (false === $result) {
            return $this->fail(ServiceOrderLogic::getError());
        }
        return $this->success('保存成功', [], 1, 1);
    }

    public function firmOrderLists()
    {
        return $this->dataLists(new ServiceOrderLists());
    }

    public function firmOrderSave()
    {
        $params = (new ServiceOrderValidate())->post()->goCheck('firmOrderSave', [
            'user_id' => $this->userId,
            'user_info' => $this->userInfo
        ]);
        $result = ServiceOrderLogic::firmOrderSave($params);
        if (false === $result) {
            return $this->fail(ServiceOrderLogic::getError());
        }
        return $this->success('保存成功', [], 1, 1);
    }
}