<?php

namespace app\admin\controller;

use app\admin\model\ChangeLog;
use app\BaseController;
use think\App;
use think\facade\Db;

//客户的营业执照相关信息
class Title extends BaseController
{
    public $post = "";
    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->post= $this->request->post();
    }
    public function create(){
        $companyNo = isset($this->post['companyNo']) && $this->post['companyNo'] !==""? trim($this->post['companyNo']):"";
        if($companyNo==""){
            return error_show(1002,"参数companyNo不能为空");
        }
        $companyinfo =Db::name('customer_title')->where(['is_del' => 0, 'companyNo'=>$companyNo])->find();
        if (!empty($companyinfo)) {
            return error_show(1002, "统一社会信用代码名称已存在");
        }
        $invoice_title = isset($this->post['invoice_title']) && $this->post['invoice_title'] !=="" ? trim($this->post['invoice_title']):"";
        if($invoice_title==""){
            return error_show(1002,"参数invoice_title不能为空");
        }
//        $invoice_people = isset($this->post['invoice_people']) && $this->post['invoice_people'] !== ""? trim($this->post['invoice_people']):"";
//        if($invoice_people==""){
//            return error_show(1002,"参数invoice_people不能为空");
//        }
        $invoice_code = isset($this->post['invoice_code']) && $this->post['invoice_code'] !==""? trim($this->post['invoice_code']):"";
        if($invoice_code==""){
            return error_show(1002,"参数invoice_code不能为空");
        }
        $invoice_bank = isset($this->post['invoice_bank']) && $this->post['invoice_bank'] !==""? trim($this->post['invoice_bank']):"";
        if($invoice_bank==""){
            return error_show(1002,"参数invoice_bank不能为空");
        }
        $invoice_bankNo = isset($this->post['invoice_bankNo']) && $this->post['invoice_bankNo'] !==""? trim($this->post['invoice_bankNo']):"";
        if($invoice_bankNo==""){
            return error_show(1002,"参数invoice_bankNo不能为空");
        }
        $invoice_addr = isset($this->post['invoice_addr']) && $this->post['invoice_addr'] !==""? trim($this->post['invoice_addr']):"";
        if($invoice_addr==""){
            return error_show(1002,"参数invoice_addr不能为空");
        }
        $invoice_mobile = isset($this->post['invoice_mobile']) && $this->post['invoice_mobile'] !==""? trim($this->post['invoice_mobile']):"";
        if($invoice_mobile==""){
            return error_show(1002,"参数invoice_mobile不能为空");
        }
        $status = isset($this->post['status']) && $this->post['status'] !==""? trim($this->post['status']):"1";
        $data =[
            "companyNo"=>$companyNo,
            "invoice_title"=>$invoice_title,
           // "invoice_people"=>$invoice_people,
            "invoice_code"=>$invoice_code,
            "invoice_bank"=>$invoice_bank,
            "invoice_bankNo"=>$invoice_bankNo,
            "invoice_addr"=>$invoice_addr,
            "invoice_mobile"=>$invoice_mobile,
            "status"=>$status,
            "is_del"=>0,
            "addtime"=>date("Y-m-d H:i:s"),
            "updatetime"=>date("Y-m-d H:i:s")
        ];
        $info = Db::name('customer_title')->insert($data);
        if($info){
            return error_show(0,"新建成功");
        }else{
            return error_show(1002,"新建失败");
        }
    }
    public function list(){
        $page = isset($this->post['page']) && $this->post['page'] !=="" ? intval($this->post['page']) :"1";
        $size = isset($this->post['size']) && $this->post['size'] !=="" ? intval($this->post['size']) :"10";
        $where =[["a.is_del","=",0]];
        $invoice_title = isset($this->post['invoice_title']) && $this->post['invoice_title'] !=="" ? trim($this->post['invoice_title']):"";
        if($invoice_title!=""){
            $where[]=['a.invoice_title',"like","%$invoice_title%"];
        }
        $invoice_code = isset($this->post['invoice_code']) && $this->post['invoice_code'] !=="" ? trim($this->post['invoice_code']):"";
        if($invoice_code!=""){
            $where[]=['a.invoice_code',"like","%$invoice_code%"];
        }
        $invoice_mobile = isset($this->post['invoice_mobile']) && $this->post['invoice_mobile'] !=="" ? trim($this->post['invoice_mobile']):"";
        if($invoice_mobile !=""){
            $where[] =['a.invoice_code',"like","%$invoice_mobile%"];
        }
        $status = isset($this->post['status']) && $this->post['status'] !=="" ? intval($this->post['status'])
            :"";
        if($status!==""){
            $where[]=['a.status',"=",$status];
        }
        $companyName = isset($this->post['companyName']) && $this->post['companyName'] !=="" ? trim($this->post['companyName']):"";
        if($companyName!=""){
            $where[]=['b.companyName',"like","%$companyName%"];
        }
        $creater = isset($this->post['creater']) && $this->post['creater'] !=="" ? trim($this->post['creater']):"";
        if($creater!=""){
            $where[]=['b.creater',"like","%$creater%"];
        }
        $start = isset($this->post['start']) && $this->post['start']!=="" ? $this->post['start']:"";
        if($start!==""){
            $where[]=['a.addtime',">=",date('Y-m-d H:i:s',strtotime($start))];
        }
        $end = isset($this->post['end']) && $this->post['end']!=="" ? $this->post['end']:"";
        if($end!==""){
            $where[]=['a.addtime',"<",date('Y-m-d H:i:s',strtotime($end)+24*3600)];
        }
        $count = Db::name('customer_title')->alias("a")
            ->join("customer_info b","b.companyNo=a.companyNo","left")->where($where)->count();
        $total = ceil($count / $size);
        $page = $page >= $total ? $total : $page;
        $list = Db::name('customer_title')->alias("a")
            ->join("customer_info b","b.companyNo=a.companyNo","left")
            ->where($where)->page($page,$size)->field("a.*,b.companyName,b.creater")->select();
        return app_show(0,"获取成功",['list'=>$list,'count'=>$count]);
    }
    public function edit(){
        $id = isset($this->post['id']) && $this->post['id'] !=="" ? intval($this->post['id']):"";
        if($id ==""){
            return error_show(1002,"参数id不能为空");
        }
        $token = isset($this->post['token']) ? trim($this->post['token']) : "";
        if($token==""){
            return error_show(101,'token不能为空');
        }
        $info= Db::name('customer_title')->where(['id'=>$id,'is_del'=>0])->find();
        if($info==""){
            return error_show(1002,"未找到数据");
        }
//        $companyNo = isset($this->post['companyNo']) && $this->post['companyNo'] !==""? trim($this->post['companyNo']):"";
//        if($companyNo==""){
//            return error_show(1002,"参数companyNo不能为空");
//        }
        $invoice_title = isset($this->post['invoice_title']) && $this->post['invoice_title'] !=="" ? trim($this->post['invoice_title']):"";
        if($invoice_title==""){
            return error_show(1002,"参数invoice_title不能为空");
        }
//        $invoice_people = isset($this->post['invoice_people']) && $this->post['invoice_people'] !== ""? trim($this->post['invoice_people']):"";
//        if($invoice_people==""){
//            return error_show(1002,"参数invoice_people不能为空");
//        }
        $invoice_code = isset($this->post['invoice_code']) && $this->post['invoice_code'] !==""? trim($this->post['invoice_code']):"";
        if($invoice_code==""){
            return error_show(1002,"参数invoice_code不能为空");
        }
        $invoice_bank = isset($this->post['invoice_bank']) && $this->post['invoice_bank'] !==""? trim($this->post['invoice_bank']):"";
        if($invoice_bank==""){
            return error_show(1002,"参数invoice_bank不能为空");
        }
        $invoice_bankNo = isset($this->post['invoice_bankNo']) && $this->post['invoice_bankNo'] !==""? trim($this->post['invoice_bankNo']):"";
        if($invoice_bankNo==""){
            return error_show(1002,"参数invoice_bankNo不能为空");
        }
        $invoice_addr = isset($this->post['invoice_addr']) && $this->post['invoice_addr'] !==""? trim($this->post['invoice_addr']):"";
        if($invoice_addr==""){
            return error_show(1002,"参数invoice_addr不能为空");
        }
        $invoice_mobile = isset($this->post['invoice_mobile']) && $this->post['invoice_mobile'] !==""? trim($this->post['invoice_mobile']):"";
        if($invoice_mobile==""){
            return error_show(1002,"参数invoice_mobile不能为空");
        }
        $status = isset($this->post['status']) && $this->post['status'] !==""? trim($this->post['status']):"1";
        $data =[
            "id"=>$id,
            "invoice_title"=>$invoice_title,
          //  "invoice_people"=>$invoice_people,
            "invoice_code"=>$invoice_code,
            "invoice_bank"=>$invoice_bank,
            "invoice_bankNo"=>$invoice_bankNo,
            "invoice_addr"=>$invoice_addr,
            "invoice_mobile"=>$invoice_mobile,
            "status"=>$status,
            "is_del"=>0,
            "updatetime"=>date("Y-m-d H:i:s")
        ];
        $temp = Db::name('customer_title')->save($data);
        $titn = array_diff($data,$info);
        $json = json_encode($titn,JSON_UNESCAPED_UNICODE);
        $jsp = json_encode($info,JSON_UNESCAPED_UNICODE);
        if($temp){
            ChangeLog::logAdd(2,$info['companyNo'],$jsp,$json,$this->post['token'],$this->post);
            return error_show(0,"更新成功");
        }else{
            return error_show(1002,"更新失败");
        }
    }
    public function info(){
        $id = isset($this->post['id']) && $this->post['id'] !=="" ? intval($this->post['id']):"";
        if($id ==""){
            return error_show(1002,"参数id不能为空");
        }
        $info= Db::name('customer_title')->where(['id'=>$id,'is_del'=>0])->find();
        if($info==""){
            return error_show(1002,"未找到数据");
        }
        $temp = Db::name("customer_info")->where(['companyNo'=>$info['companyNo'],'is_del'=>0])->find();
        $info['companyName']=$temp['companyName'];
        return app_show(0,"获取成功",$info);
    }
    public function del(){
        $id = isset($this->post['id']) && $this->post['id'] !=="" ? intval($this->post['id']):"";
        if($id ==""){
            return error_show(1002,"参数id不能为空");
        }
        $info= Db::name('customer_title')->where(['id'=>$id,'is_del'=>0])->find();
        if($info==""){
            return error_show(1002,"未找到数据");
        }
        $end = Db::name('customer_title')->update(['id'=>$id,'is_del'=>1,'updatetime'=>date("Y-m-d H:i:s")]);
        if($end){
            return error_show(0,"删除成功");
        }else{
            return error_show(1002,"删除失败");
        }
    }
    public function status(){
        $id = isset($this->post['id']) && $this->post['id'] !=="" ? intval($this->post['id']):"";
        if($id==""){
            return error_show(1002,"参数id不能为空");
        }
        $info= Db::name('customer_title')->where(['id'=>$id,'is_del'=>0])->find();
        if($info==""){
            return error_show(1002,"未找到数据");
        }
        $status = isset($this->post['status']) && $this->post['status'] !=="" ? intval($this->post['status']):"";
        if($status===""){
            return error_show(1002,"参数status不能为空");
        }
        if(!in_array($status,[0,1])){
            return error_show(1002,"参数status无效");
        }
        $info['status']=$status;
        $info['updatetime']=date("Y-m-d H:i:s");
        $msg = $status==1?"启用":"禁用";
        $temp = Db::name("customer_title")->save($info);
        return $temp ? error_show(0,"{$msg}成功"):error_show(1002,"{$msg}失败");
    }
}