<?php

namespace app\adminapi\controller\contract;

use app\adminapi\controller\BaseAdminController;
use app\common\enum\notice\NoticeEnum;
use app\common\model\contract\VehicleContract;

class VehicleContractController extends BaseAdminController
{

//    private string $url = 'http://www.lst.local';
    private string $url = 'https://logistics.lihaink.cn';

    //风控中心上传合同
    public function uploadContract() {
        //获取参数
        $params = $this->request->post(['id','file']);
        if(empty($params['id']) || empty($params['file'])){
            return $this->fail('缺少必要参数');
        }
        //获取合同信息
        $vehicle_contract = VehicleContract::where('id',$params['id'])->find();
        if(empty($vehicle_contract)){
            return $this->fail('合同信息错误');
        }
        if($vehicle_contract['status'] != 0){
            return $this->fail('合同状态错误');
        }
        //更新本地
        try {
            //判断合同类型
            if(!empty($vehicle_contract['contract_logistic_id'])){
                //更新物流系统
                curl_post($this->url.'/api/updateContract',[],[
                    'id' => $vehicle_contract['contract_logistic_id'],
                    'file' => $params['file'],
                    'status' => 1
                ]);
            }
            VehicleContract::where('id', $params['id'])->update(['file' => $params['file'],'status'=>1]);
        }catch (\Exception $e){
            return $this->fail($e->getMessage());
        }
        //发送短信
        $sms = [
            'mobile' => $vehicle_contract['company_a_phone'],
            'name' => $vehicle_contract['company_user'],
            'scene' => 'WQTZ'
        ];
        $this->rentNoticeSms($sms);
        return $this->success('上传成功', [], 1, 1);
    }

    //发送通知短信
    public function rentNoticeSms($params) {
        try {
            $scene = NoticeEnum::getSceneByTag($params['scene']);
            if (empty($scene)) {
                throw new \Exception('场景值异常');
            }
            $result = event('Notice',  [
                'scene_id' => $scene,
                'params' => $params
            ]);
            return $result[0];
        } catch (\Exception $e) {
            return false;
        }
    }

    public function lists() {
        $param = $this->request->get();
        $where = [];
        if(isset($param['company_name'])){
            $where[] = ['company_b_name','like','%'.$param['company_name'].'%'];
        }
        if(isset($param['contract_no'])){
            $where[] = ['contract_no','like','%'.$param['contract_no'].'%'];
        }
        if(isset($param['status']) && in_array($param['status'],[0,1])){
            if($param['status'] == 1){
                $where[] = ['status','in', '1,2,3'];
            }else{
                $where[] = ['status','=', $param['status']];
            }
        }else{
            $where[] = ['status','in', '0,1,2,3'];
        }
        $pageNo = !empty($param['page_no']) ? $param['page_no'] : 1;
        $pageSize = !empty($param['page_size']) ? $param['page_size'] : 15;
        $data = VehicleContract::where($where)
            ->page($pageNo, $pageSize)
            ->order('create_time desc')
            ->select()->each(function($item){
                $item['cars_info'] = json_decode($item['cars_info'],true);
            });
        return $this->success('success',['lists'=>$data->toArray(),'page_no'=>$pageNo,'page_size'=>$pageSize,'count'=>$data->count()]);
    }

    public function detail() {
        $id = $this->request->get('id');
        if(empty($id)){
            $this->fail('参数错误');
        }
        $data = VehicleContract::where('id',$id)->find();
        $data['cars_info'] = json_decode($data['cars_info'],true);
        return $this->success('success',$data->toArray());
    }
}