<?php
// +----------------------------------------------------------------------
// | 奇乐自媒体内容管理系统 免费版 2019
// +----------------------------------------------------------------------
// | 官方网址：http://www.qilecms.com
// +----------------------------------------------------------------------
// | 官方论坛：http://bbs.qilecms.com
// +----------------------------------------------------------------------
// | Author:奇乐网络
// +----------------------------------------------------------------------
// | 版权说明：本产品为免费版，使用请保留底部版权
// +----------------------------------------------------------------------
namespace app\admin\controller;
use think\controller;
use think\Db;
class Follow extends Base
{
	public function index(){
      $alias        = 'a';  //需要别名时写别名，不需要的时候留空 
      $defaultWhere = '';   //默认where条件
      $defaultOrder = 'a.create_time desc';  //默认排序
//接收搜索表单数据
      $SearchFormData = $this->getSearchFormData($defaultWhere,$defaultOrder);
      $where          = $SearchFormData['where'];    //返回的条件sql
      $order          = $SearchFormData['order'];    //返回排序sql
      $pageNum        = $SearchFormData['pageNum']; //返回的分页数量

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

              ];
//单个字段筛选
    $customSingleField =[
                         [
                          'name'  => '审核状态',
                          'field' => 'status', 
                          'data'  => [ 0 =>'待审核',1=>'审核通过',2=>'审核失败'],
                          'alias' => $alias,
                         ]
                       ]; 
// 组件字段组筛选
    $groupData = [
                    [
                     'name'      => '用户组',     //用户组
                     'table'     => 'user_level', //表名
                     'field_id'  => 'level_id',   //字段ID
                     'field_name'=> 'level_name',  //字段名
                     'alias'     =>  $alias,
                    ],
        
              ];
     $orderData   = [
                 [
                 'name'     => '余额多到少',
                 'field'    => 'money',  //字段名
                 'orderby'  => 'desc',     //注册排序
                 'alias'    => $alias,         //数据表别名
                 ],
                 [
                 'name'     => '余额少到多',
                 'field'    => 'money',
                 'orderby'  => 'asc',
                 'alias'    => $alias,
                 ],
                [
                 'name'     => '积分多到少',
                 'field'    => 'credit',  //字段名
                 'orderby'  => 'desc',     //注册排序
                 'alias'    => $alias,         //数据表别名
                 ],
                 [
                 'name'     => '积分少到多',
                 'field'    => 'credit',
                 'orderby'  => 'asc',
                 'alias'    => $alias,
                 ],
                                [
                 'name'     => '保证金多到少',
                 'field'    => 'deposit',  //字段名
                 'orderby'  => 'desc',     //注册排序
                 'alias'    => $alias,    //数据表别名
                 ],
                 [
                 'name'     => '保证金少到多',
                 'field'    => 'deposit',
                 'orderby'  => 'asc',
                 'alias'    => $alias,
                 ],
               ];  

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

     $list = DB::name('user')->alias('a')->join('user_level b','a.level_id = b.level_id',"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|chsAlpha',
          'name'=>'require',
        ];
        $msg= [
         'title.require' =>'规则标题不能为空',
         'title.chsAlpha' =>'规则标题只能是汉字或字母',
         'name.require' =>'规则名称不能为空',   
        ];
         $error = $this->checkSubmit($param,$rule,$msg);  
       if($error){
              return error_json($error);
         }
         //表单数据
          $data['title'] = $param['title'];
          $data['name'] = $param['name'];
          $data['type'] = $param['type'];
          $data['condition'] = $param['condition'];
          $data['status'] = !empty($param['status'])?1:0;

       if(model('Other')->addData($data)){
         return success_json('添加成功'); 
       }
   }else{
        return $this->fetch();
   }      
  }
  public function edit(){
    $param = $this->request->param();
    if($this->request->isPost()){

        //验证
        $rule =[
          'title'=>'require|chsAlpha',
          'name'=>'require',
        ];
        $msg= [
         'title.require' =>'规则标题不能为空',
         'title.chsAlpha' =>'规则标题只能是汉字或字母',
         'name.require' =>'规则名称不能为空',  
        ];
         $error = $this->checkSubmit($param,$rule,$msg);  
       if($error){
              return error_json($error);
         }
    
        //表单数据       
          $data['title'] = $param['title'];
          $data['name'] = $param['name'];
          $data['type'] = $param['type'];
          $data['condition'] = $param['condition'];
          $data['status'] = !empty($param['status'])?1:0;
          $where['id'] = $param['id'];
          $res = model('Other')->editData($data,$where);
          return success_json('编辑成功'); 
    }else{
         $rule = model('Other')->getRuleInfoById($param['id']);
         $this->assign('rule',$rule);
         return $this->fetch();


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

     $res= model('Other')->delData('id',$param['id']);
       if($res){
         return success_json('删除成功');
       }else{
         return error_json($res);
       }
    }
      

}