<?php
namespace app\agent\controller;

use app\agent\BaseController;
use app\common\model\Agent as AgentModel;
use app\common\model\Broker as BrokerModel;
use app\common\model\User as UserModel;

use app\common\validate\Broker as BrokerValidate;
use think\exception\ValidateException;

class Broker extends BaseController
{
	
	public function brokerList()
	{
		return view('broker/brokerlist');
	}
	
	public function brokerForm()
	{
		$id = input('id/d, 0');
		$broker = BrokerModel::findOrEmpty($id);
		$agent = $this->access_agent;
		return view('broker/brokerform',[
			'agent'			=> $agent,
			'broker'		=> $broker
		]);
	}
	
	public function editBroker()
	{
		$agent = $this->access_agent;
		$id = input('id/d');
		$vdata = array(
			'id'		=> $id,
			'mobile'    => input('mobile/s')
		);
		try {
			validate(BrokerValidate::class)->check($vdata);
		} catch (ValidateException $e) {
			exit(json_encode(array(
				'code' 	=> 1,
				'msg'	=> $e->getError()
			)));
		}
		$musermap = array();
		$brokeridarr = BrokerModel::where('agentid','=',$agent['id'])->column('id');
		$musermap[] = ['brokerid', 'in', $brokeridarr];
		$musermap[] = ['brokerid', '<>', 0];
		$musermap[] = ['mobile', '=', input('musermobile/s', '')];
		$muser = UserModel::where($musermap)->findOrEmpty();
		if ($muser->isEmpty()){
			exit(json_encode(array(
				'code' 	=> 1,
				'msg'	=> "关联的用户不存在。"
			)));
		}
		$data = [
			'userid'		=> $muser->id,
			'workerid'		=> $agent['workerid'],
			'agentid'		=> $agent['id'],
			'title'			=> input('title/s', ""),
			'mobile'    	=> input('mobile/s', ""),
			'weixin'    	=> input('weixin/s', ""),
			'qq'			=> input('qq/s', ""),
			'province'		=> input('province/s', ""),
			'city'			=> input('city/s', ""),
			'district'		=> input('district/s', ""),
			'details'		=> input('details/s', ""),
			'powerreport'	=> input('powerreport/d', 0)==1 ? 1 : 2,
			'status'		=> input('status/d')==1 ? 1 : 2
		];
		if (empty($id)){
			$data['createtime'] = time();
			$broker = BrokerModel::create($data);
		}else{
			$broker = BrokerModel::find($id);
			$broker->save($data);
		}
		exit(json_encode(array(
			'code' 	=> 0
		)));
	}
	
	public function fieldBroker()
	{
		$id = input('id/d',0); 
		$broker = BrokerModel::findOrEmpty($id);
		if ($broker->isEmpty()){
			exit(json_encode(array(
				'code' 	=> 1,
				'msg'	=> "信息不存在"
			)));
		}else{
			$broker->save([
				input('field/s')  => input('value')
			]);
		}
		exit(json_encode(array(
			'code' 	=> 0
		)));
	}
	
	public function delBroker()
	{
		$idarr = input('idarr/a');
		$broker = BrokerModel::where('role',2)->whereIn('id',$idarr)->select();
		$result = $broker->delete();
		if ($result){
			exit(json_encode(array(
				'code' 	=> 0,
				'msg'	=> ""
			)));
		}
		exit(json_encode(array(
			'code' 	=> 1,
			'msg'	=> "删除失败，请稍后重试"
		)));
	}
	
	public function listBroker()
	{
		$agentid = $this->access_agent['id'];
		$limit = input('limit/d',20);
		$page = input('page/d',1);
		$map = array();
		$map[] = ['agentid', '=', $agentid];
		$keywords = input('keywords/s');
		if (!empty($keywords)){
			$map[] =['title', 'like', '%'.$keywords.'%'];
		}
		$status = input('status/d');
		if (!empty($status)){
			$map[] = ['status', '=', $status];
		}
		$list = BrokerModel::with(['agent','muser'])->withCount(['user'])->where($map)->order('id','DESC')->limit($limit)->page($page)->append(['status_text','powerreport_text'])->select();
		$count = BrokerModel::where($map)->count();
		if ($count==0){
			exit(json_encode(array(
				'code' 	=> 1,
				'msg'	=> "未查询到数据"
			)));
		}
		exit(json_encode(array(
			'code' 	=> 0,
			'msg'	=> "",
			'count' => $count,
			'data'  => $list
		)));
	}
	

}
