<?php
// +----------------------------------------------------------------------
// | likeadmin快速开发前后端分离管理后台(PHP版)
// +----------------------------------------------------------------------
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
// | 开源版本可自由商用,可去除界面版权logo
// | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
// | github下载:https://github.com/likeshop-github/likeadmin
// | 访问官网:https://www.likeadmin.cn
// | likeadmin团队 版权所有 拥有最终解释权
// +----------------------------------------------------------------------
// | author: likeadminTeam
// +----------------------------------------------------------------------


namespace app\adminapi\controller;


use app\adminapi\controller\BaseAdminController;
use app\adminapi\lists\ShopContractLists;
use app\adminapi\logic\ShopContractLogic;
use app\adminapi\validate\ShopContractValidate;
use app\api\controller\JunziqianController;
use app\api\logic\SmsLogic;
use app\common\logic\contract\ContractLogic;
use app\common\model\Company;
use app\common\model\contract\Contract;
use app\common\model\ShopContract;
use app\common\model\ShopMerchant;
use app\Request;
use think\facade\Db;


/**
 * ShopContract控制器
 * Class ShopContractController
 * @package app\adminapi\controller
 */
class ShopContractController extends BaseAdminController
{


    /**
     * @notes 获取列表
     * @return \think\response\Json
     * @author likeadmin
     * @date 2023/09/13 17:01
     */
    public function lists()
    {
        return $this->dataLists(new ShopContractLists());
    }


    /**
     * @notes 添加
     * @return \think\response\Json
     * @author likeadmin
     * @date 2023/09/13 17:01
     */
    public function add()
    {
        $params = (new ShopContractValidate())->post()->goCheck('add');
        $result = ShopContractLogic::add($params);
        if (true === $result) {
            return $this->success('添加成功', [], 1, 1);
        }
        return $this->fail(ShopContractLogic::getError());
    }


    /**
     * @notes 编辑
     * @return \think\response\Json
     * @author likeadmin
     * @date 2023/09/13 17:01
     */
    public function edit()
    {
        $params = (new ShopContractValidate())->post()->goCheck('edit');
        $result = ShopContractLogic::edit($params);
        if (true === $result) {
            return $this->success('编辑成功', [], 1, 1);
        }
        return $this->fail(ShopContractLogic::getError());
    }


    /**
     * @notes 删除
     * @return \think\response\Json
     * @author likeadmin
     * @date 2023/09/13 17:01
     */
    public function delete()
    {
        $params = (new ShopContractValidate())->post()->goCheck('delete');
        ShopContractLogic::delete($params);
        return $this->success('删除成功', [], 1, 1);
    }


    /**
     * @notes 获取详情
     * @return \think\response\Json
     * @author likeadmin
     * @date 2023/09/13 17:01
     */
    public function detail()
    {
        $params = (new ShopContractValidate())->goCheck('detail');
        $result = ShopContractLogic::detail($params);
        return $this->data($result);
    }

    // 上传合同
    public function wind_control()
    {
        $params = $this->request->param();
        $file = $params['file'];
        $res = ShopContract::where('id', $params['id'])->update(['file' => $file, 'check_status' => 2]);
        if ($res) {
            $find = ShopContract::where('id', $params['id'])->field('type,party_b,party_a')
                ->find()->toArray();

            $find['party_a_info'] = ShopMerchant::where('id', $find['party_a'])->field('company_name name,master_phone phone')->find()->toArray();
            $find['party_b_info'] = ShopMerchant::where('id', $find['party_b'])->field('company_name name,master_phone phone')->find()->toArray();

            $a = [
                'mobile' => $find['party_a_info']['phone'],
                'name' => $find['party_a_info']['name'],
                'scene' => 'WQTZ'
            ];
            SmsLogic::contractUrl($a);
            $b = [
                'mobile' => $find['party_b_info']['phone'],
                'name' => $find['party_b_info']['name'],
                'scene' => 'WQTZ'
            ];
            SmsLogic::contractUrl($b);
            return $this->success('上传成功', [], 1, 1);
        } else {
            if ($res == 0) {
                return $this->success('没有更新', [], 1, 1);
            }
            return $this->fail('上传失败');
        }
    }

    // 发送合同
    public function Draftingcontracts()
    {
        $params = $this->request->param();
        $result = ShopContractLogic::Draftingcontracts($params);
        if ($result == true) {
            return $this->success('生成合同成功', [], 1, 1);
        }
        return $this->fail(ContractLogic::getError());
    }

    //**发送短信 */ 接口可能要做调整

    public function postsms()
    {
        $params = $this->request->param();
        $re = ShopContractLogic::postsms($params);
        if (!$re) {
            return  $this->fail(ShopContractLogic::getError());
        }
        return $this->success('成功');
    }

    public function evidence()
    {
        $id = $this->request->param('id');
        $detail=ShopContract::where('id',$id)->find();
        if(!empty($detail['evidence_url'])){
            return $this->success('获取成功', ['url' => env('url.url_prefix').$detail['evidence_url']]);
        }
        $company=ShopMerchant::where('id',$detail['party_a'])->find();
        $request = array(
            "applyNo" => $detail['contract_no'],
            "fullName" => $company['company_name'],
            "identityCard" => $company['organization_code'],
            "identityType" => 12,
        );
        return  app(JunziqianController::class)->EvidenceShopDownload($request);
    }

    public function addNote()
    {
        $params = $this->request->param(); // id notes
        $shopContract = ShopContract::where('id', $params['id'])->find();
        if (empty($shopContract)) {
            return $this->fail('合同不存在');
        }
        $shopContract->notes = $params['notes'];
        $shopContract->save();
        return $this->success('成功');
    }
}