<?php
/**
 * 经济人表
 *  id
 *  store_id ------门店id关联门店表
 *  district_id  --区域id关联区域表
 *  level ---------等级 10业务员 20店长  30总监
 *  name ----------经济人姓名
 *  phone ---------手机号
 *  password ------登录密码
 *  sex -----------性别 0保密 1男 2女
 *  img -----------头像
 *  status --------账号状态，0正常 1冻结 2离职
 *  create_time ---创建时间
 *  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\AAgents;
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();
        //获取操作权限
        $userRule=Session::get('userRule');
        $data['userRule']=$userRule;
        $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)){
           $where.=" and a.id='$search' or a.name like '%$search%' or a.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=s.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为新增
     *
     * */
    /**
     * @return \think\Response
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function saveAgent(){
        $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('status',$data['status'],$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);
        }


        }


    //



   
}