<?php

namespace app\admin\controller;

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

//品牌授权
class Keepbrand extends Base
{
    public function __construct(App $app)
    {
        parent::__construct($app);
    }
    public function create(){
        $brand_book = isset($this->post['brand_book']) && $this->post['brand_book'] !=="" ? trim($this->post['brand_book']):"";
//        if($brand_book==""){
//            return error_show(1003,"参数brand_book不能为空");
//        }
        $gyscode = isset($this->post['gyscode']) && $this->post['gyscode'] !=="" ? trim($this->post['gyscode']):"";
//        if($gyscode==""){
//            return error_show(1003,"参数gyscode不能为空");
//        }
        $brand_id = isset($this->post['brand_id']) && $this->post['brand_id'] !=="" ? trim($this->post['brand_id']):"";
        if($brand_id==""){
            return error_show(1003,"参数brand_id不能为空");
        }
        $rename = Db::name('brand_book')->where(['brand_id' => $brand_id,"gyscode"=>$gyscode,'is_del' => 0])->find();
        if (!empty($rename)) {
            return error_show(1002, "品牌名称已存在");
        }
//        $is_book = isset($this->post['is_book']) && $this->post['is_book'] !=="" ? trim($this->post['is_book']):"";
//        if($is_book==""){
//            return error_show(1003,"参数is_book不能为空");
//        }
        $token = isset($this->post['token'])&& $this->post['token']!='' ? trim($this->post['token']):"";
        if($token==''){
            return error_show(105,"参数token不能为空");
        }
//        $user =GetUserInfo($token);
//        if(empty($user)||$user['code']!=0){
//            return error_show(1002,"创建人数据不存在");
//        }
        $createrid= $this->uid;//isset($user["data"]['id']) ?  $user["data"]['id'] : "";
        $creater= $this->uname;//isset($user["data"]['nickname']) ?  $user["data"]['nickname'] : "";
        $long = isset($this->post['long']) && $this->post['long'] !=="" ? intval($this->post['long']):"0";

        $status = isset($this->post['status']) && $this->post['status'] !=="" ? intval($this->post['status']) :"0";
            $data=[
                "brand_book"=>$brand_book,
                "gyscode"=>$gyscode,
               // "is_book"=>$is_book,
                "brand_id"=>$brand_id,
                "createrid"=>$createrid,
                "creater"=>$creater,
                "long"=>$long,
                "is_del"=>0,
                "status"=>$status,
                "addtime"=>date("Y-m-d H:i:s"),
                "updatetime"=>date("Y-m-d H:i:s")
            ];
        if($long===1){
            $starttime = isset($this->post['starttime']) && $this->post['starttime'] !==""
            ?$this->post['starttime']:null;
//            if($starttime==""){
//                return error_show(1005,'参数starttime不能为空');
//            }
            $endtime= isset($this->post['endtime']) && $this->post['endtime'] !=="" ?$this->post['endtime']:null;
//            if($endtime==""){
//                return error_show(1005,'修改时间不能为空');
//            }
            $data['starttime']=$starttime;
            $data['endtime']=$endtime;
        }
            $info = Db::name("brand_book")->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]];
        $gyscode = isset($this->post['gyscode']) && $this->post['gyscode'] !=="" ? trim($this->post['gyscode']):"";
        if($gyscode!=""){
            $where[]=['a.gyscode',"like","%$gyscode%"];
        }
        $name = isset($this->post['name']) && $this->post['name'] !=="" ? trim($this->post['name']):"";
        if($name!=""){
            $where[]=['b.name',"like","%$name%"];
        }
        $registerCode = isset($this->post['registerCode']) && $this->post['registerCode'] !=="" ? trim($this->post['registerCode']):"";
        if($registerCode!=""){
            $where[]=['b.registerCode',"like","%$registerCode%"];
        }
        $count = Db::name('brand_book')
            ->alias('a')->join("supplier b","a.gyscode=b.code","left")
            ->where($where)->count();
        $total = ceil($count / $size);
        $page = $page >= $total ? $total : $page;
        $list = Db::name('brand_book')->alias('a')
            ->join("supplier b","a.gyscode=b.code","left")
            ->join("brand c","a.brand_id=c.id","left")
            ->where($where)->page($page,$size)->field("a.*,c.brand_name,b.name as supplier_name")->order("addtime desc")
            ->select();
        return app_show(0,"获取成功",['list'=>$list,'count'=>$count]);
    }
    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("brand_book")->where(["id"=>$id])->find();
        if(!$info) {
            return error_show(1002, "未找到对应数据");
        }
        $data = Db::name("brand")->alias('a')
            ->join("brand_book b","b.brand_id=a.id")
            ->join("supplier c","b.gyscode=c.code","left")->field("b.*,a.brand_name,c.name")
            ->where(["a.id"=>$info['brand_id']])->find();
        if($data){
            return app_show(0,"获取成功",$data);
        }else{
            return app_show(1002,"获取失败",$data);
        }
    }
    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("brand_book")->where([["id","=",$id]])->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?"启用":"禁用";
        $update = Db::name("brand_book")->save($info);
        return  $update? error_show(0,"{$msg}成功"):error_show(1004,"{$msg}失败");
    }
    public function del(){
        $id = $this->post['id'] && $this->post['id'] !=="" ? intval($this->post['id']) :"";
        if($id==""){
            return  error_show(1004,"参数id不能为空");
        }
        $str= Db::name('brand_book')->where(['id'=>$id,'is_del'=>0])->find();
        if(empty($str)){
            return error_show(1002,"未找到数据");
        }
        $end = Db::name('brand_book')->update(['id'=>$id,'is_del'=>1]);
        if($end){
            return error_show(0,"删除成功");
        }else{
            return error_show(1002,"删除失败");
        }
    }
    public function edit(){
        $id = isset($this->post['id']) && $this->post['id'] !=="" ? intval($this->post['id']):"";
        if($id == ""){
            return error_show(1002,"参数id不能为空");
        }
        $info = Db::name('brand_book')->where(['id'=>$id,'is_del'=>0])->find();
        if($info==""){
            return error_show(1002,"未找到数据");
        }
        $brand_book = isset($this->post['brand_book']) && $this->post['brand_book'] !=="" ? trim($this->post['brand_book']):"";
//        if($brand_book==""){
//            return error_show(1002,"参数brand_book不能为空");
//        }
//        $gyscode = isset($this->post['gyscode']) && $this->post['gyscode'] !== "" ? trim($this->post['gyscode']):"";
//        if($gyscode==""){
//            return error_show(1002,"参数gyscode不能为空");
//        }
        $brand_id = isset($this->post['brand_id']) && $this->post['brand_id'] !== "" ? intval($this->post['brand_id']):"";
        if($brand_id==""){
            return error_show(1002,"参数brand_id不能为空");
        }
        $rename = Db::name('brand_book')->where(['brand_id' => $brand_id, 'is_del' => 0])->find();
        if (empty($rename)) {
            return error_show(1002, "品牌名称已存在");
        }
//        $is_book = isset($this->post['is_book']) && $this->post['is_book'] !== "" ? intval($this->post['is_book']):"";
//        if($is_book==""){
//            return error_show(1002,"参数is_book不能为空");
//        }
        $long = isset($this->post['long']) && $this->post['long'] !=="" ? intval($this->post['long']):"0";
        $status = isset($this->post['status']) && $this->post['status'] !=="" ? intval($this->post['status']) :"0";
        if($status==""){
            return error_show(1002,"参数status不能为空");
        }
        $data=[
            "id"=>$id,
            "brand_book"=>$brand_book,
           // "gyscode"=>$gyscode,
           // "is_book"=>$is_book,
            "brand_id"=>$brand_id,
            "long"=>$long,
            "is_del"=>0,
            "status"=>$status,
            "updatetime"=>date("Y-m-d H:i:s")
        ];
        if($long===1){
            $starttime = isset($this->post['starttime']) && $this->post['starttime'] !=="" ?$this->post['starttime']:null;
//            if($starttime==""){
//                return error_show(1005,'参数starttime不能为空');
//            }
            $endtime= isset($this->post['endtime']) && $this->post['endtime'] !=="" ?$this->post['endtime']:null;
//            if($endtime==""){
//                return error_show(1005,'修改时间不能为空');
//            }
            $data['starttime']=$starttime;
            $data['endtime']=$endtime;
        }
        $info = Db::name("brand_book")->save($data);
        if($info){
            return error_show(0,"更新成功");
        }else{
            return error_show(1002,"更新失败");
        }
    }
}