<?php
namespace app\admin\controller;

use think\Db;

class AssetCate extends Auth
{

    public function index(){
        $tree = model('AssetCate')->showAllTree();
        $this->assign('tree',$tree);
        return $this->fetch();
    }

    /**
     * 新增
     */
    public function add($pid=0){
        if(request()->isPost()){
            $res = model('AssetCate')->addSave();
            if($res){
                $this->success('操作成功',url('index'));
            }else{
                $this->error(model('AssetCate')->getError());
            }
        }else{

            $this->assign('pid',$pid);
            return $this->fetch();
        }
    }

    /**
     * 编辑
     */
    public function edit($id=0){
        if(request()->isPost()){
            $res = model('AssetCate')->editSave();
            if($res){
                $this->success('操作成功',url('index'));
            }else{
                $this->error(model('AssetCate')->getError());
            }
        }else{
            $info = db('asset_cate')->where('id',$id)->find();

            $this->assign('info',$info);

            return $this->fetch();
        }
    }

    /**
     * 删除记录
     * @param int $id
     */
    public function del($id=0){
        if(!$id){
            $this->error('参数错误');
        }
        // 检查是否有子级
        $ret = Db::name('asset_cate')->where('parent_id',$id)->find();
        if($ret){
            $this->error('有子级不能删除');
        }
        $res = Db::name('asset_cate')->where('id',$id)->delete();
        if($res){
            $this->success('删除成功');
        }else{
            $this->error('删除失败');
        }
    }


}