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

namespace app\admin\controller\bbfxshop\goods;

use app\admin\controller\bbfxshop\Base;
use think\Db;


class Category extends Base
{

    public function _initialize(){
        parent::_initialize();
    }
    
    public function list(){
        
        $params = $this->param();
        
        
        $pindex = max(1, intval($params["page"]));
        $psize = intval($params["limit"])?intval($params["limit"]):20;
        $where = ["uniacid"=>UNIACID,'pid'=>0,'mid'=>0];
        
        $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'=>0])->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'=>0])->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'=>0])->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'=>0])->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'=>0])->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'=>0];
        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 status(){
        $params = $this->param();
        
        $id = intval($params['id']);
        $status = intval($params['status']);
        if($status != 1){
            $status = 0;
        }
        $data = ["status"=>$status];
        $res = Db::name("bbfx_category")->where(['id'=>$id,'mid'=>0])->update($data);
        if($res){
            $data['id'];
            \app\admin\model\AdminLog::record("","",$data,"bbfx_category");
            __success("操作成功");
        }
        else{
            __success("操作成功");
        }
    }
    public function post(){
        $params = $this->param();
        $id = intval($params['id']);
        
        $item = Db::name("bbfx_category")->where(['id'=>$id,'mid'=>0])->find();
        
        $data = array(
        	"uniacid"=>UNIACID,
          	"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])->update($data);
            $data['id'] = $id;
        }
      	else{
            $res = Db::name("bbfx_category")->insertGetId($data);
            $data['id'] = $res;
        }
      	
      	if($res){
      	    
      	    \app\admin\model\AdminLog::record("","",$data,"bbfx_category");
      	    
        	__success("编辑成功");
        }
      	else{
        	__success("操作成功");
        }
    }
    
    public function delete(){
        $this->commonDelete('bbfx_category');
    }
}