<?php
// +----------------------------------------------------------------------
// | 奇乐自媒体内容管理系统 免费版 2019
// +----------------------------------------------------------------------
// | 官方网址：http://www.qilecms.com
// +----------------------------------------------------------------------
// | 官方论坛：http://bbs.qilecms.com
// +----------------------------------------------------------------------
// | Author:奇乐网络
// +----------------------------------------------------------------------
// | 版权说明：本产品为免费版，使用请保留底部版权
// +----------------------------------------------------------------------
namespace app\admin\controller;
use think\DB;
use think\facade\Request;
class Info extends Category
{

	public function set(){
		return $this->fetch();
	}

	public function index(){
      $alias = 'a';        //需要别名时写别名，不需要的时候留空 
      $defaultWhere = '';  //默认where
      $defaultOrder = 'a.id asc';  //默认排序
    //接收搜索表单数据
      $SearchFormData = $this->getSearchFormData($defaultWhere,$defaultOrder);
      $where = $SearchFormData['where'];
      $order = $SearchFormData['order'];
      $pageNum = $SearchFormData['pageNum'];

//组建关键词数据
     $keywordsData  =[
                  [
                  'field'   =>'id', //字段名
                  'name'    =>'文章ID', //文本显示名称
                  'alias'   => $alias,  //字段对应的别名
                  ],
                  [
                  'field'   =>'title',
                  'name'    =>'文章标题',
                  'alias'   => $alias,
                  ]
     ];
//组建日期筛选   
       $dateData = [
                  [
                  'title'        =>'创建时间',  //标签显示名称
                  'field'        =>'create_time', //筛选字段
                  'start_name'   =>'开始时间', //开始时间
                  'end_name'     =>'结束时间', //结束时间
                  'alias'        => $alias,  //字段别名
                  ],

              ];
//单个字段筛选
    $customSingleField =[
                       ]; 
// 组件字段组筛选
     $groupData    = [];
     $orderData   = [];  

     $this->assign('keywordsData',$keywordsData);
     $this->assign('customSingleField',$customSingleField);
     $this->assign('groupData',$groupData);
     $this->assign('dateData',$dateData);
     $this->assign('orderData',$orderData);



     $list = DB::name('info')->alias('a')
     ->field('a.id,a.title,a.create_time,b.cname,b.alias')
     ->join('info_category b','a.cid = b.cid',"LEFT")
     ->where($where)
     ->order($order)
     ->paginate($pageNum);
     $page = $list->render();    
     $this->assign('page', $page);
     $this->assign('list',$list->toArray()['data']);
	  return $this->fetch();
	} 


	public function add(){
  $param =$this->request->param();

     if($this->request->isPost()){
    

           //表单验证
           $rule =[
              'title'=>'require|max:150',
              'content'=>'require',
            ];
            $msg= [
             'title.require' =>'站内文章标题不能为空',
             'title.max' =>'站内文章标题不能超过150个字',
             'content.require' =>'站内文章内容不能为空',
            
            ];
             $error = $this->checkSubmit($param,$rule,$msg);  
            if($error){
                return error_json($error);
             }
        //三级分类判断    
                      if(!empty($param['cate']['three'])){
                            $param['cid'] = $param['cate']['three'];
                      }else{
                         if(!empty($param['cate']['two'])){
                             $param['cid'] = $param['cate']['two'];
                         }else{
                            if(!empty($param['cate']['one'])){
                             $param['cid'] = $param['cate']['one']; 
                            }
                         }
                      }

            $data['create_time'] = time();
            $data['title'] = $param['title'];
            $data['content'] = $param['content'];
            $data['hits'] = $param['hits'];
            $data['cid'] = $param['cid'];
            $result =  model('info')->addData($data);
          
       if($result){
              return success_json('添加成功');
             
            }else{
               return error_json('添加失败'); 
            }
           
   
        }else{

           return $this->fetch();

        }

	}
	public function edit(){
      $param = $this->request->param();

     if($this->request->isPost()){
   
               //表单验证
               $rule =[
                  'title'=>'require|max:150',
                  'content'=>'require',
                ];
                $msg= [
                 'title.require' =>'站内文章标题不能为空',
                 'title.max' =>'站内文章标题不能超过150个字',
                 'content.require' =>'站内文章内容不能为空',
                
                ];
                 $error = $this->checkSubmit($param,$rule,$msg);  
                if($error){
                    return error_json($error);
                 }
                      if(!empty($param['cate']['three'])){
                            $param['cid'] = $param['cate']['three'];
                      }else{
                         if(!empty($param['cate']['two'])){
                             $param['cid'] = $param['cate']['two'];
                         }else{
                            if(!empty($param['cate']['one'])){
                             $param['cid'] = $param['cate']['one']; 
                            }
                         }
                      }
                $data['create_time'] = time();
                $data['title'] =$param['title'];
                $data['hits'] = $param['hits'];
                $data['content'] = $param['content'];
                $data['cid'] = $param['cid'];

                $where['id'] = $param['id'];
                $result =  model('info')->editData($data,$where);
                if($result){
                    return success_json('更新成功');
                     
                  }else{
                     return error_json('更新失败');
                  }
                  

         }else{
             if(!$param['id']){  return error_json('请求参数错误');}
              $info = model('info')->getInfoById($param['id']);

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

	}
	public function del(){
     $param = input();
     if(!$param['id']){  return error_json('请求参数错误');    }

     $res= $this->delData('info',"id");
     if($res){
      return success_json('删除成功');
     }else{
      return error_json($res);
     }

	}
  
   public  function category(){

      return parent::category();
   }

   public  function categoryAdd(){

      return parent::categoryAdd();
   }
   public  function categoryEdit(){

      return parent::categoryEdit();
   }
   public  function categoryUpdate(){

      return parent::updateCategoryCache();
   }
   public  function categoryDel(){

      return parent::del();
   }




}