<?php
/*
 本代码由 焕岱科技 创建
 技术支持 WX：15285007200
 严禁反编译、逆向等任何形式的侵权行为，违者将追究法律责任
*/

namespace app\api\controller\merchant_admin;

use app\api\controller\merchant_admin\MerchantBase;
use think\Db;

class Category extends MerchantBase
{
    protected $noNeedLogin = [];
    protected $noMerchantNeedRight = [];
    protected $noNeedRight = ['*'];
    
    public function getList(){
        
        $params = $this->param();
        
        $pindex = max(1, intval($params["page"]));
        $psize = intval($params["limit"])?intval($params["limit"]):20;
        $where = ["uniacid"=>UNIACID,'pid'=>0,'mid'=>$this->mid];
        
        $all = Db::name("bbfx_category")->where($where)->order('sort desc')->select();
    	$total = Db::name("bbfx_category")->where($where)->count();
    	$list = [];
    	foreach ($all as $key => &$val) {
    	    $val['thumb'] = cdnurl($val['thumb']);
    	    $val['level'] = 1;
    	    $val['stitle'] = $val['title'];
    	    $list[] = $val;
    	    $child = Db::name("bbfx_category")->where(["uniacid"=>UNIACID,'pid'=>$val['id'],'mid'=>$this->mid])->order('sort desc')->select();
    	    if(!empty($child)){
    	        foreach ($child as $k => $v){
    	            $v['level'] = 2;
    	            if($k == count($child) - 1){
    	                $v['stitle'] = "&nbsp;&nbsp;&nbsp;&nbsp;└&nbsp;&nbsp;".$v['title'];
    	            }
    	            else{
    	                $v['stitle'] = "&nbsp;&nbsp;&nbsp;&nbsp;├&nbsp;&nbsp;".$v['title'];
    	            }
    	            $list[] = $v;
    	            
    	            $three_child = Db::name("bbfx_category")->where(["uniacid"=>UNIACID,'pid'=>$v['id'],'mid'=>$this->mid])->order('sort desc')->select();
            	    if(!empty($three_child)){
            	        foreach ($three_child as $k1 => $v1){
            	            $v1['level'] = 3;
            	            if($k1 == count($three_child) - 1){
            	                $v1['stitle'] = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;└&nbsp;&nbsp;".$v1['title'];
            	            }
            	            else{
            	                $v1['stitle'] = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;├&nbsp;&nbsp;".$v1['title'];
            	            }
            	            $list[] = $v1;
            	            
            	            
            	        }
            	    }
    	        }
    	    }
    	}
    	unset($val);
      	
        __success('操作成功',['total'=>$total,'list'=>$list]);
    }
    
    public function all(){
        
        $list = Db::name("bbfx_category")->where(["uniacid"=>UNIACID,'pid'=>0,'mid'=>$this->mid])->order('sort desc')->select();
        foreach ($list as $key => &$val) {
    	    $val['thumb'] = cdnurl($val['thumb']);
    	    $val['level'] = 1;
    	    $child = Db::name("bbfx_category")->where(["uniacid"=>UNIACID,'pid'=>$val['id'],'mid'=>$this->mid])->order('sort desc')->select();
    	    
    	    if(!empty($child)){
    	        foreach ($child as $k => &$v){
        	        $v['thumb'] = cdnurl($v['thumb']);
        	        $v['level'] = 2;
        	        $three_child = Db::name("bbfx_category")->where(["uniacid"=>UNIACID,'pid'=>$v['id'],'mid'=>$this->mid])->order('sort desc')->select();
        	        if(!empty($three_child)){
        	            foreach ($three_child as $k1 => &$v1){
            	            $v1['thumb'] = cdnurl($v1['thumb']);
            	            $v1['level'] = 3;
            	        }
            	        unset($v1);
            	        $v['child'] = $three_child;
        	        };
        	        
        	    }
        	    unset($v);
        	    
        	    $val['child'] = $child;
    	    }
    	}
    	unset($val);
      	
        __success('操作成功',$list);
    }
    
    public function parent(){
        $pid = $this->param("pid/d");
        $where = ["uniacid"=>UNIACID,'pid'=>0,'status'=>1,'mid'=>$this->mid];
        if($pid > 0){
            $where['id'] = $pid;
        }
        
        $list = Db::name("bbfx_category")->where($where)->order('sort desc')->select();
        foreach ($list as $key => &$val) {
    	    $val['thumb'] = cdnurl($val['thumb']);
    	}
    	unset($val);
      	
        __success('操作成功',$list);
    }
    
    public function detail(){
        $id = $this->param("id/d");
        $info = Db::name("bbfx_category")->where(['id'=>$id,'mid'=>$this->mid])->find();
        if(empty($info)){
            __error("分类不存在");
        }
        $info['thumb'] = cdnurl($info['thumb']);
        __success("操作成功",['info'=>$info]);
    }
    
    public function post(){
        $params = $this->param(['title/s','pid/d','status/d','thumb/s','sort/d']);
        $id = intval($params['id']);
        
        $item = Db::name("bbfx_category")->where(['id'=>$id,'mid'=>$this->mid])->find();
        
        $data = array(
        	"uniacid"=>UNIACID,
        	"mid"=>$this->mid,
          	"pid"=>intval($params['pid']),
          	"title"=>$params['title'],
          	"status"=>$params['status'],
          	"thumb"=>$params['thumb'],
          	"sort"=>$params['sort']
        );
      	$data['thumb'] = replaceSiteroot($data['thumb']);
      	if(!empty($item)){
            $res = Db::name("bbfx_category")->where(['id'=>$id,'mid'=>$this->mid])->update($data);
        }
      	else{
            $res = Db::name("bbfx_category")->insertGetId($data);
            $data['id'] = $res;
        }
      	
      	if($res){
        	__success("编辑成功");
        }
      	else{
        	__error("操作失败");
        }
    }
    
    public function delete(){
        $params = $this->param();
        $ids = $params['id'];
        if(empty($ids)){
            __error("请选择需要删除的数据");
        }
        
        $ids = explode(",",$ids);
        
        foreach ($ids as $key => &$val){
            $val = intval($val);
        }
        unset($val);
        
        $list = Db::name("bbfx_category")->where(['id'=>['in',$ids],'mid'=>$this->mid])->where()->select();
        if(empty($list)){
            __error("删除的数据不存在");
        }
        $titles = array_column($list, 'title');
        
      	$res = Db::name("bbfx_category")->where(['id'=>['in',$ids],'mid'=>$this->mid])->delete();
      	if($res){
        	__success("删除成功");
        }
    	else{
        	__error("操作失败");
        }
    }
}