<?php
namespace app\admin\controller;
use think\Controller;
use think\Db;
use util\Tree;
use authruletree\Authruletree;
use app\admin\model\Authrule as AuthruleModel;

class Authrule extends Base
{
    public function index($tab = 1, $id = 0){
        if(request()->isPost()){
            if(!checkAuth()){
            echo "<script>parent.window.location.href='/admin/index/index';</script>";
            exit;
        }
            foreach (input('post.listorder/a') as $key => $value) {
                Db::name('auth_rule')->where('id',$key)->update(['listorder'=>$value]);
            }
            return success('排序更新成功!',url('index',['tab'=>$tab]));
        }else{
            $authruleArray = AuthruleModel::order('listorder')->select();
            foreach ($authruleArray as $key => $value) {
                $authruleList[] = $value->toArray(); //对象转数组
            }
            $tree = new Tree();
            $tree->tree($authruleList,'id','pid','title');
            $authrule = $tree->getArray();
            $this->assign('authrule',$authrule);

            // 编辑规则
            if( 3 == $tab ){
                // 获取所要编辑菜单的信息
                $info = Db::name('auth_rule')->where('id',$id)->find();
                if($info!=null && is_array($info)){
                    $this->assign('info',$info);
                }
            }
        }
        return view();
    }

    public function add($requireField = '', $requireFieldName = ''){
        if(request()->isPost()){
            $authruleModel = new AuthruleModel;
            if($authruleModel->allowField(true)->save(input('post.'))){
                return success('新规则添加成功!',url('index',['tab'=>1]));
            }else{
                return error('规则添加失败!');
            }
        }
    }
public function getcharl($cate_id){
            static $re=array();
$re='';
         $data=Db::name('auth_rule')->where('pid', $cate_id)->select();
      for($i=0;$i<count($data);$i++){
        $re[]=$data[$i];
      }
      return $re;
}
public function deletet($id = 0){
        $data=$this->getcharl($id);

        if($data){

       Db::name('auth_rule')->where('id', $id)->delete();

        for($i=0;$i<count($data);$i++){
        $newid=$data[$i]['id'];
        $this->deletet($newid);

        }


        }else{
       Db::name('auth_rule')->where('id', $id)->delete();
        }
       
    }
    public function delete($id = 0){
        $data=$this->getcharl($id);
         // var_dump($data);
         // die;
        if($data){

       Db::name('auth_rule')->where('id', $id)->delete();

        for($i=0;$i<count($data);$i++){
        $newid=$data[$i]['id'];
        $this->deletet($newid);

        }
        $result=1;
        }else{
       $result=Db::name('auth_rule')->where('id', $id)->delete();
        }
        if($result){
            return success('删除成功!',url('index',['tab'=>1]));
        }else{
            return error('删除失败!');
        }
    }

    public function edit($id = 0, $requireField = '', $requireFieldName = ''){
        if(request()->isPost()){
            $authrule_form = input('post.');
            //判断child状态
            if(input('post.child')==null){
                $authrule_form['child'] = 0;
                $count = Db::name('auth_rule')->where('pid',$id)->count();
                if($count){
                    return error('该规则拥有子项，无法取消勾选!');
                }
            }
            // 编辑规则
            $authruleModel = new AuthruleModel;
            if($authruleModel->allowField(true)->isUpdate()->save($authrule_form)){
                return success('规则编辑成功!',url('index',['tab'=>1]));
            }else{
                return error('规则信息未修改或编辑失败!');
            }
        }
    }

    public function sort(){
        $id=input('param.id');

        $sort=input('param.sort');
        db('auth_rule')->where(array('id'=>$id))->update(array('listorder'=>$sort));
        $this->success('更新排序成功！',url('index',['tab'=>1]));
    }

     public function status(){
       $id=input('param.id');
       $statusres=db('auth_rule')->field('id,status')->find($id);
        $artshow=$statusres['status'];
        if($artshow==1){
            db('auth_rule')->where(array('id'=>$id))->update(['status'=>0]);
        }else{
            db('auth_rule')->where(array('id'=>$id))->update(['status'=>1]);
        }
       $this->success('操作成功');  
    }

    public function ajaxlst(){
        if(request()->isAjax()){
            $id=input('id');
            $authrule=db('auth_rule');
            $authruleTree=new authruletree();
            
            $sonids=$authruleTree->childrenids($id,$authrule);
            echo json_encode($sonids);
        }else{
            $this->error('非法操作！');
        }
    }

}
