<?php

namespace app\workerapi\controller;

use app\common\command\AddAgreementPdf;
use app\common\logic\MasterWorkerExamineLogic;
use app\common\model\master_worker\MasterWorkerAgree;
use app\workerapi\logic\MasterWorkerAgreeLogic;
use app\workerapi\validate\MasterWorkerAgreeValidate;

class MasterWorkerAgreeController extends BaseApiController
{
    public array $notNeedLogin = ['policy'];

    public function ceshi()
    {
        AddAgreementPdf::sendMq('202408181742334617','https://weixiu.zhongdunzhizhao.com/index/agreement/index?code=202408181742334617');
        dd(1);
    }

    public function policy()
    {
        $type = $this->request->get('type/s', '');
        $result = MasterWorkerAgreeLogic::getPolicyByType($type);
        return $this->data($result);
    }

    /**
     * 我的协议列表
     * @return \think\response\Json
     */
    public function agreement()
    {
        //服务合作协议是否存在pdf
        $pdf = MasterWorkerAgree::where(['agree_type'=>'master_service_content','worker_id'=>$this->userId])->value('pdf_url');
        $result = [
            [
                'type'=>'master_service',
                'value'=>'服务合作协议',
                'pdf'=> !empty($pdf)?$pdf:''
            ],
            [
                'type'=>'master_bill',
                'value'=>'开票授权协议'
            ],
            [
                'type'=>'master_privacy',
                'value'=>'隐私政策'
            ],
            [
                'type'=>'master_allow',
                'value'=>'工程师软件许可协议'
            ],
        ];
        return $this->data($result);
    }

    /**
     * 协议详情
     * @return \think\response\Json
     */
    public function agreement_detail()
    {
        $type = $this->request->get('type/s', '');
        $result = MasterWorkerAgreeLogic::getAgreeByType($type,$this->userId);
        return $this->data($result);
    }

    /**
     * 协议签名
     * @return \think\response\Json
     */
    public function agreeSign()
    {
        $params = (new MasterWorkerAgreeValidate())->post()->goCheck('sign', [
            'user_id' => $this->userId,
            'user_info' => $this->userInfo
        ]);
        $result = MasterWorkerAgreeLogic::sign($params);
        if (false === $result) {
            return $this->fail(MasterWorkerAgreeLogic::getError());
        }
        MasterWorkerExamineLogic::updateEngineerInformation($params['user_id']);
        return $this->success('签名成功', [], 1, 1);
    }
}