<?php
/**
 * 经济人表
 * @param id
 * @param store_id ------门店id关联门店表
 * @param district_id  --区域id关联区域表
 * @param level ---------等级 10业务员 20店长  30总监
 * @param name ----------经济人姓名
 * @param phone ---------手机号
 * @param password ------登录密码
 * @param sex -----------性别 0保密 1男 2女
 * @param img -----------头像
 * @param status --------账号状态，0正常 1冻结 2离职
 * @param inviter_id  ---邀请人id
 * @param create_time ---创建时间
 * @param update_time ---修改时间
 * Created by PhpStorm.
 * User: zfc
 * Date: 2018/2/26
 * Time: 13:51
 */

namespace app\index\controller;

use app\index\extend\Basic;
use app\model\AuthGroup;
use app\model\AuthRule;
use app\model\AAgents;
use app\model\AuthGroupAccess;
use think\Db;
use think\Session;

class Agent extends Basic
{
   protected $authGroupModel;
    protected $authRuleModel;
   
       function _initialize()
    {


    }

  //经纪人显示页面
    public function agent(){
        return view('agent');

    }
    //编辑页面
    public function saveAgentIndex(){
        return view('save_agent');
    }
    /*********************接口******************************/
    //首页显示列表
    public  function AgentList(){
        $table= new AAgents;
        $data['status'] = 200;
        $data['msg'] = '';
        $params = $this->request->param();
        $pageNo = empty($params['pageNo']) ? 1 : $params['pageNo'];
        $pageSize = empty($params['pageSize']) ? 15 : $params['pageSize'];
        //条件
        $where='a.status in(0,1)';
        $search=$params['search'];
         // prt($data);
        if(!empty($search)){

           // Session::set("search.agent",$search);
           $where.=" and a.id='$search' or a.name like '%$search%' or a.phone like '%$search%' ";

            $data['search']=$search;
        }else{
//            $search=Session::get("search.agent");
//            if(!empty($search)){
//                $where.=" id='$search' or name like '%$search%' or phone like '%$search%' ";
//                $data['search']=$search;
//            }
        }
        $field="a.id,a.store_id,a.district_id,a.`level`,a.`name`,a.phone,a.sex,a.img,a.`status`,a.inviter_id,a.create_time,
               ifnull(s.store_name,'')store_name,ifnull(d.district_name,'')district_name";
        $order='a.id asc';
        $join=[
            ['a_store s', ' a.store_id=a.id','left'],
            ['a_district d','a.district_id=d.id','left']
        ];
        $data['list']  = $table->getListJoin($pageNo, $pageSize,$order,$field,$join, $where);
        $data['total'] = $table->getTotal2($where);
        // prt($list);//转化arr
        //prt(collection($list)->toArray());//转化arr
        return $this->response(200,'成功',$data);

    }



    /*
     * 新增or修改or查看
     * $group_id !=0为查看
     * post存在id为新增
     *
     * */

    public function saveAgent($group_id=0){
        $group_id=$this->request->param('id');
        $table= New AAgents;

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

            $data = input('post.');

            //新增或者编辑数据
            if(empty($data['id'])){
                $data['create_time']=  date( 'Y-m-d H:i:s',time());
                $data['update_time']=  date( 'Y-m-d H:i:s',time());
                $id='';
            }else{
                $data['update_time']=  date( 'Y-m-d H:i:s',time());
                $id=$data['id'];
            }
            //判断name是否重复

            if($table->repetition('phone',$data['phone'])&& empty($id)){
                return $this->response(100, '存在重复值');
            }



            if ($table->editData($data,$id)) {
                return $this->response(200, '成功');

            } else {
                return $this->response(100, '无修改');

            }

        } else {
            //查看
            $info= $table->where('id',$group_id)->find();
            //echo $table->getLastSql();
            return $this->response(200, '查看', $info);


        }


    }
    //删除(冻结)
    public  function updateStatus(){
        $data=$this->request->param();
        $ids=$data['ids'];
        //排除超级管理员
            if (is_array($ids)) {
                if (in_array(1, $ids)) {
                    return $this->response(100,'超级管理员不允许操作',$data);
                }
            } else {
                if ($ids === 1) {
                    return $this->response(100,'超级管理员不允许操作',$data);
                }
            }
        $table= New AAgents;

        if($table->saveStatus($data['name'],$data['k'],$ids)){
            return $this->response(200,'成功',$data);
        }else{
            return $this->response(100,'失败',$data);

        }

    }
    /**
     * 批量变更角色
     * @post ids 用户组id
     * @post group_id 变更的角色组id
    */
    public function updateRole(){
        $table=New AAgents;
        $data=$this->request->param();
        $ids=trim($data['ids'],',');

        if(!empty($ids)&&!empty($data['group_id'])){

         if($table->saveStatus('auth_group_id',$data['group_id'],$ids)){
             return $this->response(200,'成功',$data);
               }
             return $this->response(100,'失败',$data);

         }else{
            return $this->response(100,'数据为空',$data);
        }


        }


    //



   
}