<?php
declare (strict_types = 1);

namespace app\admin\controller;
use app\admin\BaseController;
use think\App;
use think\facade\Db;

class Supplier extends BaseController
{
    public function __construct(App $app) {parent::__construct($app);}
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function SupplierList()
    {
        $post = $this->post;
        $condition = "1=1 ";
       $name= isset($post['name'])&&$post['name']!="" ? trim($post['name']) :"";
       if($name!=""){
           $condition .=" and (name like '%{$name}%' or contector like '%{$name}%')";
       }
        $page =  isset($post['page'])&&$post['page']!="" ? intval($post['page']) :0;
        $size =  isset($post['size'])&&$post['size']!="" ? intval($post['size']) :10;
        $count = Db::name("supplier_info")->where($condition)->count();
        $total = ceil($count/$size)>1 ? ceil($count/$size) : 1;
        $page = $page>=$total?intval($total):$page;
        $list =  Db::name("supplier_info")->where($condition)->page($page,$size)->field("id,code,name,contector,nature,mobile,address,post,addtime")->select();
        return app_show(0,"获取成功",["list"=>$list,"count"=>$count]);
    }

    /**
     * 获取供应商信息
     *
     * @return \think\Response
     */
    public function SupplierInfo()
    {
        $post = $this->post;
        $sid = isset($post['id'])&& $post['id']!="" ? intval($post['id']) :"";
        if($sid==""){
            return error_show(1002,"参数d不能为空");
        }
        $supplier = Db::name("supplier_info")->where("id","=",$sid)->find();
        if($supplier==false){
            return error_show(1003,"供应商信息不存在");
        }
        $cgd = Db::name("pay")->where([["supplierNo","=",$supplier['code']],["status","<>",0]])
            ->field("id,payNo,pay_fee,wait_fee,status")->select();

        $list=["payment"=>["wait"=>["total"=>0,"num"=>0],"already"=>["total"=>0,"num"=>0]],"invoice"=>["wait"=>["total"=>0,"num"=>0],"already"=>["total"=>0,"num"=>0]]];

        foreach ($cgd as $key=>$value){

            $list['payment']['wait']['total']+= $value['wait_fee']*100;
            $list['payment']['already']['total']+= $value['pay_fee']*100;

            $paystage = Db::name("pay_stages")->where([["payNo","=",$value['payNo']],["is_del","=",0],['status',">","4"],['status',"<>","9"]])
                ->sum('pay_fee');

            $paywait = Db::name("pay_stages")->where([["payNo","=",$value['payNo']],["is_del","=",0],['status',"<","4"],
                ['status',"<>","9"]])
                ->sum('pay_fee');

            $list['invoice']['wait']['total']+= $paywait*100;
            $list['invoice']['already']['total']+= $paystage*100;
            if($value['status']==2){
                $list['payment']['already']["num"]++;
            }else{
                $list['payment']['wait']["num"]++;
            }
        }
        array_walk($list,function (&$v){
            $v['wait']['total'] = $v['wait']['total']/100;
            $v['already']['total'] = $v['already']['total']/100;
        });
        return app_show(0,"获取成功",$list);
    }

    /**
     * 修改供应商信息
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function SupplierSave()
    {
        $post = $this->post;
        $sid = isset($post['id'])&& $post['id']!="" ? intval($post['id']) :"";
        if($sid==""){
            return error_show(1002,"参数d不能为空");
        }
        $supplier = Db::name("supplier_info")->where("id","=",$sid)->find();
        if($supplier==false){
            return error_show(1003,"供应商信息不存在");
        }
        $name= isset($post['name'])&& $post['name']!="" ? trim($post['name']) :"";
        if($name==""){
            return error_show(1002,"供货商名称不能为空");
        }
        $contector= isset($post['contector'])&& $post['contector']!="" ? trim($post['contector']) :"";
        if($contector==""){
            return error_show(1002,"联系人名称不能为空");
        }

        $mobile= isset($post['mobile'])&& $post['mobile']!="" ? trim($post['mobile']) :"";
        if($mobile==""){
            return error_show(1002,"联系人手机号不能为空");
        }
        if(!checkMobile($mobile)){
            return error_show(1002,"联系人手机号格式错误");
        }
        $pay_method= isset($post['pay_method'])&& $post['pay_method']!="" ? trim($post['pay_method']) :"";
        if($pay_method==""){
            return error_show(1002,"结账方式不能为空");
        }
        $paydays= isset($post['paydays'])&& $post['paydays']!="" ? trim($post['paydays']) :"";
        $return_ticket= isset($post['return_ticket'])&& $post['return_ticket']!="" ? trim($post['return_ticket']) :"";
        $data=[
            "id"=>$supplier['id'],
            "name"=>$name,
            "contector"=>$contector,
            "mobile"=>$mobile,
            "pay_method"=>$pay_method,
            "paydays"=>$paydays,
            "return_ticket"=>$return_ticket,
            "updatetime"=>date("Y-m-d H:i:s"),
        ];
        $save = Db::name("supplier_info")->save($data);
        return $save ? app_show(0,"修改成功") :error_show(1003,"修改失败");
    }

    /**
     * 供应商状态修改
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function SupplierStatus()
    {
        $post = $this->post;
        $sid = isset($post['id'])&& $post['id']!="" ? intval($post['id']) :"";
        if($sid==""){
            return error_show(1002,"参数d不能为空");
        }
        $supplier = Db::name("supplier_info")->where("id","=",$sid)->find();
        if($supplier==false){
            return error_show(1003,"供应商信息不存在");
        }
        $status= isset($post['status'])&& $post['status']!="" ? intval($post['status']) :"";
        if($status==""){
            return error_show(1002,"参数status不能为空");
        }
        $data=[
            "id"=>$sid,
            "status"=>$status,
            "updatetime"=>date("Y-m-d H:i:s")
        ];
        $udap = DB::name("supplier_ifno")->save($data);
        return $udap ? app_show(0,"修改成功") :error_show(1003,"修改失败");
    }


}