<?php
	
	namespace app\middleapi\controller;
	
	use app\adminapi\logic\ShopContractLogic;
	use app\adminapi\validate\ShopContractValidate;
	use app\api\controller\JunziqianController;
	use app\api\logic\SmsLogic;
	use app\common\controller\BaseLikeAdminController;
	use app\common\logic\contract\ContractLogic;
	use app\common\model\ShopContract;
	use app\common\model\ShopMerchant;
	use think\response\Json;
	
	class ShopContractController extends BaseLikeAdminController
	{
		//商户合同列表
		public function lists(): Json
		{
			if(!$this->request->isPost()){
				return $this->fail('请求方式错误');
			}
			$params = $this->request->post(['page_no','page_size','contract_no','party_a','party_b','status']);
			$pageNo = !empty($params['page_no']) ? $params['page_no'] : 1;
			$pageSize = !empty($params['page_size']) ? $params['page_size'] : 20;
			$where = [];
			if(!empty($params['contract_no'])){
				$where[] = ['contract_no','like','%'.$params['contract_no'].'%'];
			}
			if(isset($params['party_a']) && $params['party_a']!=''){
				$arr= ShopMerchant::where('company_name','like','%'.$params['party_a'].'%')->column('id');
				if($arr){
					$where[]=['party_a','in',$arr];
				}
			}
			if(isset($params['party_b']) && $params['party_b']!=''){
				$arr= ShopMerchant::where('company_name','like','%'.$params['party_b'].'%')->column('id');
				if($arr){
					$where[]=['party_b','in',$arr];
				}
			}
			if(isset($params['status']) && in_array($params['status'],[0,1])){
				$where[] = ['status','=',$params['status']];
			}
			
			$data = ShopContract::where($where)
				->field(['id', 'contract_no', 'party_a', 'party_b', 'area_manager', 'type', 'evidence_url', 'check_status', 'status', 'notes'])
				->page($pageNo, $pageSize)->order(['id' => 'desc'])
				->with(['partyA', 'partyB'])->select()->toArray();
			$count = ShopContract::where($where)->count();
			$result = [
				'lists' => $data,
				'count' => $count,
				'page_no' => $pageNo,
				'page_size' => $pageSize
			];
			return $this->success('请求成功',$result);
		}
		
		//商户合同详情
		public function detail(): Json
		{
			$params = (new ShopContractValidate())->post()->goCheck('detail');
			$result = ShopContractLogic::detail($params);
			return $this->data($result);
		}
		
		//商户合同审核后
		public function check(): Json
		{
			if(!$this->request->isPost()){
				return $this->fail('请求方式错误');
			}
			$params = $this->request->post(['id','file']);
			if(empty($params['id']) || empty($params['file'])){
				return $this->fail('参数错误');
			}
			$res = ShopContract::where('id', $params['id'])->update(['file' => $params['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 note(): Json
		{
			if(!$this->request->isPost()){
				return $this->fail('请求方式错误');
			}
			$params = $this->request->post(['id','notes']);
			if(empty($params['id']) || empty($params['notes'])){
				return $this->fail('参数错误');
			}
			$shopContract = ShopContract::where('id', $params['id'])->find();
			if (empty($shopContract)) {
				return $this->fail('合同不存在');
			}
			$shopContract->notes = $params['notes'];
			$shopContract->save();
			return $this->success('成功');
		}
		
		//发送合同
		public function draftContract(): Json
		{
			if(!$this->request->isPost()){
				return $this->fail('请求方式错误');
			}
			$params = $this->request->post(['id']);
			if(empty($params['id'])){
				return $this->fail('参数错误');
			}
			$result = ShopContractLogic::Draftingcontracts($params);
			if ($result) {
				return $this->success('生成合同成功', [], 1, 1);
			}
			return $this->fail(ContractLogic::getError());
		}
		
		//发送短信
		public function sendSms(): Json
		{
			if(!$this->request->isPost()){
				return $this->fail('请求方式错误');
			}
			$params = $this->request->post(['id']);
			if(empty($params['id'])){
				return $this->fail('参数错误');
			}
			$re = ShopContractLogic::postsms($params);
			if (!$re) {
				return  $this->fail(ShopContractLogic::getError());
			}
			return $this->success('成功');
		}
		
		//下载证据包
		public function evidence()
		{
			if(!$this->request->isPost()){
				return $this->fail('请求方式错误');
			}
			$params = $this->request->post(['id']);
			if(empty($params['id'])){
				return $this->fail('参数错误');
			}
			$detail=ShopContract::where('id',$params['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);
		}
	}