<?php

namespace app\admin\controller;

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

//商品规格
class Specs extends Base
{
    public $post="";
    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->post=$this->request->post();
    }
    public function create(){
        $spec_name = isset($this->post['spec_name']) && $this->post['spec_name'] !==""? trim($this->post['spec_name']):"";
        if($spec_name==""){
            return error_show(1002,"参数spec_name不能为空");
        }
//        $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'] : "";
        $exam_status = isset($this->post['exam_status']) &&$this->post['exam_status'] !==""? intval($this->post['exam_status']):"0";
        $status = isset($this->post['status']) &&$this->post['status'] !==""? intval($this->post['status']):"1";

        $tmp = Db::name('specs')
            ->field('id')
            ->where(['is_del'=>0,'spec_name'=>$spec_name])
            ->findOrEmpty();
        if(!empty($tmp)) return json_show(1004,'该规格值已存在');

        $data = [
            "spec_name"=>$spec_name,
            "creater"=>$creater,
            "createrid"=>$createrid,
            "exam_status"=>$exam_status,
            "status"=>$status,
            "is_del"=>0,
            "addtime"=>date("Y-m-d H:i:s"),
            "updatetime"=>date("Y-m-d H:i:s")
        ];
        $datainfo = Db::name('specs')->insert($data);

        return $datainfo?json_show(0,"新建成功"):json_show(1002,"创建失败");
//        if($datainfo){
//            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=[["s.is_del","=",0]];
        $spec_name = isset($this->post['spec_name']) && $this->post['spec_name'] !=="" ? trim($this->post['spec_name']):"";
        if($spec_name!=""){
            $where[]=['s.spec_name',"like","%$spec_name%"];
        }
        $status = isset($this->post['status']) && $this->post['status'] !=="" ? intval($this->post['status']) :"";
        if($status!==""){
            $where[]=['s.status',"=",$status];
        }
        $creater = isset($this->post['creater']) && $this->post['creater'] !=="" ? intval($this->post['creater']):"";
        if($creater!=""){
            $where[]=['s.creater',"like","%$creater%"];
        }
        $start = isset($this->post['start']) && $this->post['start']!=="" ? $this->post['start']:"";
        if($start!==""){
            $where[]=['s.addtime',">=",date('Y-m-d H:i:s',strtotime($start))];
        }
        $end = isset($this->post['end']) && $this->post['end']!=="" ? $this->post['end']:"";
        if($end!==""){
            $where[]=['s.addtime',"<",date('Y-m-d H:i:s',strtotime($end)+24*3600)];
        }
        $company_name = isset($this->post['company_name']) && $this->post['company_name'] !== "" ? trim($this->post['company_name']) : "";
        if ($company_name !== "") $where[] = ["s.createrid", 'in', get_company_item_user_by_name($company_name)];

        $count = Db::name('specs')
            ->alias('s')
            ->where($where)
            ->count();
        $total = ceil($count / $size);
        $page = $page >= $total ? $total : $page;
        $list = Db::name('specs')
            ->alias('s')
            ->where($where)
            ->page($page,$size)
            ->order("addtime desc")
            ->select()
            ->toArray();

        $all_createrid = array_column($list,'createrid');
        $item = get_company_name_by_uid($all_createrid);

        foreach ($list as &$val){
            $val['company_name']=$item[$val['createrid']]??'';
        }
        return app_show(0,"获取成功",['list'=>$list,'count'=>$count]);
    }
    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("specs")->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?"启用":"禁用";
        $update = Db::name("specs")->save($info);
        return  $update? error_show(0,"{$msg}成功"):error_show(1004,"{$msg}失败");
    }
    public function title(){
        $where=[["is_del","=",0]];
        $spec_name = isset($this->post['spec_name']) && $this->post['spec_name'] !=="" ? trim($this->post['spec_name']):"";
        if($spec_name!=""){
            $where[]=['spec_name',"like","%$spec_name%"];
        }
        $status = isset($this->post['status']) && $this->post['status'] !=="" ? intval($this->post['status']):"";
        if($status!=""){
            $where[]=['status',"=",$status];
        }
        $creater = isset($this->post['creater']) && $this->post['creater'] !=="" ? intval($this->post['creater']):"";
        if($creater!=""){
            $where[]=['creater',"like","%$creater%"];
        }
        $start = isset($this->post['start']) && $this->post['start']!=="" ? $this->post['start']:"";
        if($start!==""){
            $where[]=['addtime',">=",date('Y-m-d H:i:s',strtotime($start))];
        }
        $end = isset($this->post['end']) && $this->post['end']!=="" ? $this->post['end']:"";
        if($end!==""){
            $where[]=['addtime',"<",date('Y-m-d H:i:s',strtotime($end)+24*3600)];
        }
        $list = Db::name('specs')->where($where)->order("addtime desc")->select();
        return app_show(0,"获取成功",$list);
    }
    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("specs")->where(['id'=>$id,"is_del"=>0])->find();
        if($info==""){
            return error_show(1002,"未找到数据");
        }
        if($info['status']==1){
            return error_show(1002,"状态是启用状态,无法编辑");
        }
        $spec_name = isset($this->post['spec_name']) && $this->post['spec_name'] !==""? trim($this->post['spec_name']):"";
        if($spec_name==""){
            return error_show(1002,"参数spec_name不能为空");
        }
        $exam_status = isset($this->post['exam_status']) &&$this->post['exam_status'] !==""? intval($this->post['exam_status']):"0";
        $status = isset($this->post['status']) &&$this->post['status'] !==""? intval($this->post['status']):"0";

        $tmp = Db::name('specs')
            ->field('id')
            ->where(['is_del'=>0,'spec_name'=>$spec_name])
            ->where('id','<>',$id)
            ->findOrEmpty();
        if(!empty($tmp)) return json_show(1004,'该规格值已存在');

        $data = [
            "id"=>$id,
            "spec_name"=>$spec_name,
            "exam_status"=>$exam_status,
            "status"=>$status,
            "is_del"=>0,
            "updatetime"=>date("Y-m-d H:i:s")
        ];
        $datainfo=Db::name('specs')->save($data);
        $item = array_diff($data,$info);
        $json = json_encode($item,JSON_UNESCAPED_UNICODE);
        $jsp = json_encode($info,JSON_UNESCAPED_UNICODE);
        if($datainfo){
            ChangeLog::logAdd(5,$info['id'],$jsp,$json,['id' => $this->uid, 'nickname' => $this->uname]);
            return error_show(0,"更新成功");
        }else{
            return error_show(1002,"更新失败");
        }
    }
    public function del(){
        $id = $this->post['id'] && $this->post['id'] !=="" ? intval($this->post['id']) :"";
        if($id===""){
            return  error_show(1004,"参数id不能为空");
        }
        $str= Db::name('specs')->where(['id'=>$id,'is_del'=>0])->find();
        if(empty($str)){
            return error_show(1002,"未找到数据");
        }
        $end = Db::name('specs')->update(['id'=>$id,'is_del'=>1]);
        if($end){
            return error_show(0,"删除成功");
        }else{
            return error_show(1002,"删除失败");
        }
    }
}