<?php
// +----------------------------------------------------------------------
// | 奇乐自媒体内容管理系统 免费版 2019
// +----------------------------------------------------------------------
// | 官方网址：http://www.qilecms.com
// +----------------------------------------------------------------------
// | 官方论坛：http://bbs.qilecms.com
// +----------------------------------------------------------------------
// | Author:奇乐网络
// +----------------------------------------------------------------------
// | 版权说明：本产品为免费版，使用请保留底部版权
// +----------------------------------------------------------------------
namespace app\admin\controller;
class AdminNav extends Base
{
  public function index(){
//显示导航
    $AdminNav =  model('AdminNav');
    $list  = $AdminNav->getNavTree(); 
    // print_r($list);
    list_sort_by($list,'sort','desc'); 

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

  public function add(){
     $param = $this->request->param();
     if($this->request->isPost()){  
        //验证
        $rule =[
          'title'=>'require',
        ];
        $msg= [
            'title.require' =>'导航标题不能为空',  
        ];
         $error = $this->checkSubmit($param,$rule,$msg);  
       if($error){
             return error_json($error);
         }
         //表单数据
          $data['title'] = $param['title'];
          $data['name']  = $param['name'];
          $data['icon']  = $param['icon'];
          $data['pid']   = $param['pid'];
          $data['sort']  = $param['sort'];
          $data['create_time'] = time();
          $data['display'] = !empty($param['display'])?1:0;
       //判断是否存在相同菜单
       if(model('admin_nav')->getAdminNavInfoByTitle($data['title'])){
          return error_json('导航标题已经存在');    
       }

       if(model('admin_nav')->addData($data)){
         return success_json('添加成功'); 
        }
        
      }else{
        return $this->fetch();    
      }
     
  }

  public function edit(){
    $param = $this->request->param();
    if($this->request->isPost()){
         //验证
        $rule =[
          'title'=>'require',
        ];
        $msg= [
         'title.require' =>'导航标题不能为空',  
        ];
         $error = $this->checkSubmit($param,$rule,$msg);  
       if($error){
             return error_json($error);
         }

         
         //表单数据
          $data['title'] = $param['title'];
          $data['name'] = $param['name'];
          $data['icon'] = $param['icon'];
          $data['pid'] = $param['pid'];
          $data['sort'] = $param['sort'];
          $data['create_time'] = time();
          $data['display'] = !empty($param['display'])?1:0;
      //判断是否存在相同菜单
       if(model('admin_nav')->getAdminNavInfoByTitle($data['title'])){
          return error_json('导航标题已经存在');    
       }


          $where['id'] = inval($param['id']);
          $res = model('admin_nav')->edit($data,$where);
          return success_json('编辑成功'); 
    }else{
         $adminNav = model('admin_nav')->getAdminNavInfoById($where['id']);
         $this->assign('adminNav',$adminNav);
         return $this->fetch();


    }  
  }

  public function update()
    {
      if($this->request->isAjax()){ 
           $param = $this->request->param();
           $where['title'] = $param['title'];

          if(model('adminNav')->updateNav($param)){
            return success_json('菜单更新成功');
          }else{
            return error_json('菜单更新失败');
          }
      }

  }



  public function del()
    {
     $param = $this->request->param();
     if($this->request->isAjax()){
       if(!$param['id']){  return error_json('请求参数错误');    }
       //判断是否子类
       $map[] = ['pid','IN',$param['id']];
       $adminNav = model('adminNav')->listData($map);
       if(!empty($adminNav)){
        return success_json('请先删除下级导航');
       }
       return $this->delData('adminNav',"id");
  }

}










}
