<?php
namespace app\workerapi\controller;

use app\workerapi\lists\ReturnWorkLists;
use app\workerapi\logic\ReturnWorkLogic;
use app\workerapi\validate\ReturnWorkValidate;

/**
 * 返修工单系统
 */
class ReturnWorkController extends BaseApiController
{
    /**
     * 返修工单列表
     * @return \think\response\Json
     */
    public function lists()
    {
        return $this->dataLists(new ReturnWorkLists());
    }

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

    public function appointWork()
    {
        $params = (new ReturnWorkValidate())->post()->goCheck('appointWork',[
            'user_id' => $this->userId,
        ]);
        $result = ReturnWorkLogic::appointWork($params);
        if (false === $result) {
            return $this->fail(ReturnWorkLogic::getError());
        }
        return $this->success('预约成功，等待上门', [], 1, 1);
    }

    public function confirmDoor()
    {
        $params = (new ReturnWorkValidate())->post()->goCheck('confirmDoor',[
            'user_id' => $this->userId,
        ]);
        $result = ReturnWorkLogic::confirmDoor($params);
        if (false === $result) {
            return $this->fail(ReturnWorkLogic::getError());
        }
        return $this->success('操作成功,工程师已上门', [], 1, 1);
    }

    public function updateReturn()
    {
        $params = (new ReturnWorkValidate())->post()->goCheck('update',[
            'user_id' => $this->userId,
        ]);
        $result = ReturnWorkLogic::updateReturn($params);
        if (false === $result) {
            return $this->fail(ReturnWorkLogic::getError());
        }
        return $this->success('操作成功,工程师已确认服务完成，等待用户确认中', [], 1, 1);
    }

    public function getWorkStatus()
    {
        $params = (new ReturnWorkValidate())->goCheck('detail',[
            'user_id' => $this->userId,
        ]);
        $result = ReturnWorkLogic::detail($params);
        if (empty($result)) {
            return $this->fail('返修单不存在');
        }
        return $this->success('', ['return_work_status'=>$result['return_work_status']], 1, 1);
    }


}