<?php

namespace App\Http\Controllers\Admin;

use App\Exports\ContractExport;
use App\Exports\ContractTypeExport;
use App\Http\Controllers\Controller;
use App\Http\Response\ResponseJson;
use App\Models\AppVersion;
use App\Models\Contract;
use App\Models\ContractType;
use App\Models\PublicConfig;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Validator;
use Maatwebsite\Excel\Facades\Excel;

class ContractController extends Controller
{
    use ResponseJson;


    public function addContract(Request $request){
        try {
            $data = $request->all();
            $validated = Validator::make($data, [
                'party_a_id' => 'required',
                'party_a_name' => 'required',
                'party_b_id' => 'required',
                'party_b_name' => 'required',
                'number' => 'required|unique:contract',
                'name' => 'required',
                'sign_date' => 'required',
//                'type_id' => 'required',
            ], [
                'party_a_id.required' => '请选择甲方',
                'party_a_name.required' => '请填写甲方名',
                'number.required' => '请填写合同编号',
                'number.unique' => '合同编号重复',
                'name.required' => '请填写合同名',
                'sign_date.required' => '请填写签署日期',
                'party_b_id.required' => '请选择乙方',
                'party_b_name.required' => '请填写乙方名',
//                'type_id.required' => '请选择合同类型',
            ]);

            if ($validated->fails()) {
                $errors = $validated->errors()->all();
                $error = count($errors) ? $errors[0] : null;
                return $this->failure($error);
            }

            Contract::create($data);
            return $this->success('ok');
        }catch (\Exception $e){
            return $this->failure('file:'.$e->getFile().'|line:'.$e->getLine().'|message:'.$e->getMessage());
        }
    }

    public function getContract(Request $request){
        try {
            $keyword = $request->get('keyword');
            $type_id = $request->get('type_id');
            $begin_date = $request->get('begin_date');
            $end_date = $request->get('end_date');
            $no_page = $request->get('no_page');
            $is_export = $request->get('is_export');
            if($is_export){
                $no_page = true;
            }
            $query = Contract::query()->with('type')->when($keyword,function ($query) use ($keyword){
                $query->where('number','like',"%{$keyword}%")->orWhere('name','like',"%{$keyword}%");
            });

            $query->when($type_id,function ($query) use ($type_id){
               $query->where('type_id',$type_id);
            });

            $query->when($begin_date,function ($query) use ($begin_date,$end_date){
                $query->where('sign_date','>=',$begin_date)->where('sign_date','<=',$end_date);
            });
            $query->orderByDesc('id');
            if($no_page){
                $list = $query->get();
            }else{
                $list = $query->paginate();
            }

            if($is_export){
                if($list->isEmpty()){
                    return $this->failure('暂无数据');
                }
                return Excel::download(new ContractExport($list), 'contract.xlsx');
            }
            return $this->success('ok',$list);
        }catch (\Exception $e){
            return $this->failure('file:'.$e->getFile().'|line:'.$e->getLine().'|message:'.$e->getMessage());
        }
    }

    public function deleteContract($id){
        try {
            Contract::where('id',$id)->delete();
            return $this->success('ok');
        }catch (\Exception $e){
            return $this->failure('file:'.$e->getFile().'|line:'.$e->getLine().'|message:'.$e->getMessage());
        }
    }

    public function updateContract(Request $request,$id){
        try {
            $detail = Contract::findOrFail($id);
            $data = $request->only(['party_a_id','party_a_name','party_b_id','party_b_name','number','name','img','sign_date','type_id']);

            $detail->update($data);
            return $this->success('ok');
        }catch (\Exception $e){
            return $this->failure('file:'.$e->getFile().'|line:'.$e->getLine().'|message:'.$e->getMessage());
        }
    }

    public function addContractType(Request $request){
        try {
            $data = $request->only(['name']);
            $validated = Validator::make($data, [
                'name' => 'required|unique:contract_type',
            ], [
                'name.required' => '请输入合同类型名称',
                'name.unique' => '合同类型已存在',
            ]);

            if ($validated->fails()) {
                $errors = $validated->errors()->all();
                $error = count($errors) ? $errors[0] : null;
                return $this->failure($error);
            }
            ContractType::create($data);
            return $this->success('ok');
        }catch (\Exception $e){
            return $this->failure('addContractType file:'.$e->getFile().'|line:'.$e->getLine().'|message:'.$e->getMessage());
        }
    }

    public function getContractTypeList(Request $request){
        try {
            $keyword = $request->get('keyword');
            $no_page = $request->get('no_page');
            $is_export = $request->get('is_export');
            if($is_export){
                $no_page = true;
            }
            $query = ContractType::query();

            $query->when($keyword,function ($query) use ($keyword){
                $query->where('name','like',"%{$keyword}%");
            });
            $query->orderByDesc('id');
            if($no_page){
                $list = $query->get();
            }else{
                $list = $query->paginate();
            }

            if($is_export){
                if($list->isEmpty()){
                    return $this->failure('暂无数据');
                }
                return Excel::download(new ContractTypeExport($list), 'contract_type.xlsx');
            }
            return $this->success('ok',$list);
        }catch (\Exception $e){
            return $this->failure('getContractTypeList file:'.$e->getFile().'|line:'.$e->getLine().'|message:'.$e->getMessage());
        }
    }

    public function updateContractType(Request $request,$id){
        try {
            $detail = ContractType::findOrFail($id);
            $data = $request->only(['name']);
            $validated = Validator::make($data, [
                'name' => 'required|unique:contract_type',
            ], [
                'name.required' => '请输入合同类型名称',
                'name.unique' => '合同类型已存在',
            ]);

            if ($validated->fails()) {
                $errors = $validated->errors()->all();
                $error = count($errors) ? $errors[0] : null;
                return $this->failure($error);
            }

            $detail->update($data);
            return $this->success('ok');
        }catch (\Exception $e){
            return $this->failure('updateContractType file:'.$e->getFile().'|line:'.$e->getLine().'|message:'.$e->getMessage());
        }
    }

    public function deleteContractType($id){
        try {
            $exists = Contract::where('type_id',$id)->exists();
            if($exists){
                return $this->failure('该类型已绑定合同,不可删除');
            }
            ContractType::where('id',$id)->delete();
            return $this->success('ok');
        }catch (\Exception $e){
            return $this->failure('file:'.$e->getFile().'|line:'.$e->getLine().'|message:'.$e->getMessage());
        }
    }
}
