<?php
declare (strict_types = 1);

namespace app\Admin\controller;

use app\BaseController;
use think\facade\Db;
use think\Request;

class Supplier extends BaseController
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function SupplierList()
    {
        $post  =$this->request->post();
        $token = isset($post['token']) ? trim($post['token']) : "";
        if($token==""){
            return error_show(101,'token不能为空');
        }
        $effetc = VerifyTokens($token);
        if(!empty($effetc) && $effetc['code']!=0){
            return error_show($effetc['code'],$effetc['message']);
        }
        $condition = "1=1 ";
//        $userinfo = GetUserInfo($token);
//        if(!isset($userinfo['code'])|| $userinfo['code']!=0){
//            return error_show(101,'未能获取用户信息');
//        }
//        $check = checkRole($userinfo['data']['roleid'],40);
//        if($check){
//            $condition .=" and (staffer = {$userinfo['data']['nickname']} or staffer ='')";
//
//        }
       $name= isset($post['name'])&&$post['name']!="" ? trim($post['name']) :"";
       //$contector= isset($post['contector'])&&$post['contector']!="" ? trim($post['contector']) :"";
     //  $staff= isset($post['staff'])&&$post['staff']!="" ? trim($post['staff']) :"";
       if($name!=""){
           $condition .=" and (name like '%{$name}%' or contector like '%{$name}%')";
       }
//        if($contector!=""){
//            $condition .=" and contector like '%{$contector}%'";
//        }
//        if($staff!=""){
//            $condition .="name 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->request->post();
        $token = isset($post['token']) ? trim($post['token']) : "";
        if($token==""){
            return error_show(101,'token不能为空');
        }
        $effetc = VerifyTokens($token);
        if(!empty($effetc) && $effetc['code']!=0){
            return error_show($effetc['code'],$effetc['message']);
        }
        $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->request->post();
        $token = isset($post['token']) ? trim($post['token']) : "";
        if($token==""){
            return error_show(101,'token不能为空');
        }
        $effetc = VerifyTokens($token);
        if(!empty($effetc) && $effetc['code']!=0){
            return error_show($effetc['code'],$effetc['message']);
        }
        $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->request->post();
        $token = isset($post['token']) ? trim($post['token']) : "";
        if($token==""){
            return error_show(101,'token不能为空');
        }
        $effetc = VerifyTokens($token);
        if(!empty($effetc) && $effetc['code']!=0){
            return error_show($effetc['code'],$effetc['message']);
        }
        $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,"修改失败");
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function SupplierAdd($id)
    {
        //
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        //
    }
}