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

namespace app\admin\controller\bbfxshop\smaterial;

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


class Category extends Base
{
    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];
        
        $all = Db::name("bbfx_smaterial_category")->where($where)->order('sort desc')->select();
    	$total = Db::name("bbfx_smaterial_category")->where($where)->count();
    	$list = [];
    	foreach ($all as $key => &$val) {
    	    $val['image'] = cdnurl($val['image']);
    	    $val['level'] = 1;
    	    $val['stitle'] = $val['title'];
    	    $list[] = $val;
    	    $child = Db::name("bbfx_smaterial_category")->where(["uniacid"=>UNIACID,'pid'=>$val['id']])->order('sort desc')->select();
    	    if(!empty($child)){
    	        foreach ($child as $k => $v){
                    $v['image'] = cdnurl($v['image']);
    	            $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;
    	        }
    	    }
    	}
    	unset($val);
      	
        __success('操作成功',['total'=>$total,'list'=>$list]);
    }
    public function all(){
        
        $list = Db::name("bbfx_smaterial_category")->where(["uniacid"=>UNIACID,'pid'=>0])->order('sort desc')->select();
        foreach ($list as $key => &$val) {
    	    $val['image'] = cdnurl($val['image']);
    	    $val['level'] = 1;
    	    $child = Db::name("bbfx_smaterial_category")->where(["uniacid"=>UNIACID,'pid'=>$val['id']])->order('sort desc')->select();
    	    
    	    if(!empty($child)){
    	        foreach ($child as $k => &$v){
        	        $v['image'] = cdnurl($v['image']);
        	        $v['level'] = 2;
        	    }
        	    unset($v);
        	    
        	    $val['child'] = $child;
    	    }
    	}
    	unset($val);
      	
        __success('操作成功',$list);
    }
    
    public function parent(){
        $pid = $this->param("pid/d");
        $where = ["uniacid"=>UNIACID,'pid'=>0];
        if($pid > 0){
            $where['id'] = $pid;
        }
        
        $list = Db::name("bbfx_smaterial_category")->where($where)->order('sort desc')->select();
        foreach ($list as $key => &$val) {
    	    $val['image'] = cdnurl($val['image']);
    	}
    	unset($val);
      	
        __success('操作成功',$list);
    }

    public function detail(){
        $id = $this->param("id/d");
        $info = Db::name("bbfx_smaterial_category")->where(['id'=>$id])->find();
        if(!empty($info)){
            __success("操作成功",['info'=>$info]);
        }
        else{
            __error("分类不存在");
        }
    }
    
    public function post(){
        $params = $this->param();
        $id = intval($params['id']);
        
        $item = Db::name("bbfx_smaterial_category")->where(['id'=>$id])->find();
        
        $data = array(
        	"uniacid"=>UNIACID,
          	"pid"=>intval($params['pid']),
          	"title"=>$params['title'],
          	"image"=>$params['image'],
          	"sort"=>$params['sort']
        );
      	$data['image'] = replaceSiteroot($data['image']);
      	if(!empty($item)){
            $res = Db::name("bbfx_smaterial_category")->where(['id'=>$id])->update($data);
            $data['id'] = $id;
        }
      	else{
            $res = Db::name("bbfx_smaterial_category")->insertGetId($data);
            $data['id'] = $res;
        }
      	
      	if($res){
      	    
      	    \app\admin\model\AdminLog::record("","",$data,"bbfx_smaterial_category");
      	    
        	__success("编辑成功");
        }
      	else{
        	__success("操作成功");
        }
    }
    
    public function delete(){
        $this->commonDelete('bbfx_smaterial_category');
    }
}