<?php
namespace app\worker\controller;

use app\worker\BaseController;
use app\common\model\Worker as WorkerModel;
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()
	{
		$workerid = $this->access_worker['id'];
		$agentlist = AgentModel::where('workerid','=',$workerid)->order(['id'=>'desc'])->select();
		return view('broker/brokerlist',[
			'agentlist'		=> $agentlist 
		]);
	}
	
	public function brokerForm()
	{
		$workerid = $this->access_worker['id'];
		$id = input('id/d', 0);
		$broker = BrokerModel::where('workerid','=',$workerid)->findOrEmpty($id);
		$agentlist = AgentModel::where('workerid','=',$workerid)->order(['id'=>'desc'])->select();
		return view('broker/brokerform',[
			'agentlist'		=> $agentlist,
			'broker'		=> $broker,
			'worker'		=> $this->access_worker
		]);
	}
	
	public function editBroker()
	{
		$workerid = $this->access_worker['id'];
		$id = input('id/d', 0);
		$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()
			)));
		}
		$muser = UserModel::where(['mobile'=>input('musermobile/s', '')])->findOrEmpty();
		if ($muser->isEmpty()){
			exit(json_encode(array(
				'code' 	=> 1,
				'msg'	=> "关联的用户不存在。"
			)));
		}
		$data = [
			'userid'		=> $muser->id,
			'workerid'		=> $workerid,
			'agentid'		=> input('agentid/d', 0),
			'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', 0)==1 ? 1 : 2
		];
		if (empty($id)){
			$data['createtime'] = time();
			$broker = BrokerModel::create($data);
		}else{
			$broker = BrokerModel::find($id);
			$broker->save($data);
		}
		$muser->save([
			'brokerid'	=> $broker->id
		]);
		exit(json_encode(array(
			'code' 	=> 0
		)));
	}
	
	public function fieldBroker()
	{
		$workerid = $this->access_worker['id'];
		$id = input('id/d',0); 
		$broker = BrokerModel::where('workerid','=',$workerid)->findOrEmpty($id);
		if ($broker->isEmpty()){
			exit(json_encode(array(
				'code' 	=> 1,
				'msg'	=> "信息不存在"
			)));
		}else{
			$broker->save([
				input('field/s')  => input('value/s', "")
			]);
		}
		exit(json_encode(array(
			'code' 	=> 0
		)));
	}
	
	public function delBroker()
	{
		$workerid = $this->access_worker['id'];
		$password = input('password');
		if ( $this->access_worker['password'] !== md5($password) ){
			exit(json_encode(array(
				'code' 	=> 1,
				'msg'	=> "操作密码验证失败"
			)));
		}
		$idarr = input('idarr/a');
		$broker = BrokerModel::where('workerid','=',$workerid)->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()
	{
		$workerid = $this->access_worker['id'];
		$limit = input('limit/d',20);
		$page = input('page/d',1);
		$map = array();
		$map[] = ['workerid', '=', $workerid];
		$keywords = input('keywords/s');
		if (!empty($keywords)){
			$map[] =['title', 'like', '%'.$keywords.'%'];
		}
		$status = input('status/d');
		if (!empty($status)){
			$map[] = ['status', '=', $status];
		}
		$agentid = input('agentid/d');
		if (!empty($agentid)){
			$map[] = ['agentid', '=', $agentid];
		}
		$list = BrokerModel::with(['muser','worker','agent'])->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
		)));
	}
	

}
