<?php
namespace app\workerapi\controller;

use Exception;
use app\workerapi\lists\GroupServiceWorkLists;
use app\adminapi\logic\works\GroupServiceWorkLogic;
use app\workerapi\validate\GroupServiceWorkValidate;

/**
 * 拼团工单系统
 */
class GroupWorksController extends BaseApiController
{
    /**
     * 服务工单列表-全部
     * @return \think\response\Json
     */
    public function serviceWorkList()
    {
        return $this->dataLists(new GroupServiceWorkLists());
    }

    /**
     * 工单详情
     * @return \think\response\Json
     */
    public function detail()
    {
        try {
            $params = (new GroupServiceWorkValidate())->goCheck('detail',[
                'master_worker_id' => $this->userId,
                'user_id' => $this->userId,
                'user_info' => $this->userInfo,
            ]);
            if(empty($params['id']) && empty($params['work_sn'])){
                $this->fail('参数错误');
            }
            $result = GroupServiceWorkLogic::detail($params);
            if (false === $result) {
                return $this->fail(GroupServiceWorkLogic::getError());
            }
            return $this->data($result);
        } catch (Exception $e) {
            return $this->fail($e->getMessage());
        }
    }

    /**
     * 工程师确认上门
     * @return \think\response\Json
     */
    public function confirmDoor()
    {
        $params = (new GroupServiceWorkValidate())->post()->goCheck('door', [
            'user_id' => $this->userId,
            'user_info' => $this->userInfo
        ]);
        $result = GroupServiceWorkLogic::confirmDoor($params);
        if (false === $result) {
            return $this->fail(GroupServiceWorkLogic::getError());
        }
        return $this->success('操作成功,工程师已上门', [], 1, 1);
    }

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

    /**
     * 再次上门
     * @return \think\response\Json
     */
    public function againDoor()
    {
        $params = (new GroupServiceWorkValidate())->post()->goCheck('againDoor', [
            'user_id' => $this->userId,
            'user_info' => $this->userInfo
        ]);
        $result = GroupServiceWorkLogic::againDoor($params);
        if (false === $result) {
            return $this->fail(GroupServiceWorkLogic::getError());
        }
        return $this->success('操作成功，已确定新的预约时间', [], 1, 1);
    }

}