<?php
namespace app\admin\controller;

use app\admin\BaseController;
use app\common\model\User as UserModel;
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\BrokerForm as BrokerFormModel;

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

class Broker extends BaseController
{
	
	public function brokerList()
	{
		$workerlist = WorkerModel::order(['id'=>'desc'])->select();
		$agentlist = AgentModel::order(['id'=>'desc'])->select();
		return view('broker/brokerlist',[
			'workerlist'	=> $workerlist,
			'agentlist'		=> $agentlist
		]);
	}
	
	public function brokerForm()
	{
		$id = input('id/d, 0');
		$broker = BrokerModel::findOrEmpty($id);
		$workerlist = WorkerModel::with(['agent','muser'])->order(['id'=>'desc'])->select();
		return view('broker/brokerform',[
			'workerlist'	=> $workerlist,
			'broker'		=> $broker
		]);
	}
	
	public function editBroker()
	{
		$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()
			)));
		}
		$muser = UserModel::where(['mobile'=>input('musermobile/s', '')])->findOrEmpty();
		if ($muser->isEmpty()){
			exit(json_encode(array(
				'code' 	=> 1,
				'msg'	=> "关联的用户不存在。"
			)));
		}
		$workeragentarr = explode(",", input('workeragent/s'));
		$workerid = isset($workeragentarr[0]) ? $workeragentarr[0] : 0;
		$agentid = isset($workeragentarr[1]) ? $workeragentarr[1] : 0;
		if ( empty($workerid) || empty($agentid) ){
			exit(json_encode(array(
				'code' 	=> 1,
				'msg'	=> "请选择劳务公司和代理门店。"
			)));
		}
		$data = [
			'userid'		=> $muser->id,
			'workerid'		=> $workerid,
			'agentid'		=> $agentid,
			'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')==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);
		}
		$muser->save([
			'brokerid'	=> $broker->id
		]);
		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()
	{
		$access_admin = session('access_admin');
		$password = input('password');
		if ( $access_admin['password'] !== md5($password) ){
			exit(json_encode(array(
				'code' 	=> 1,
				'msg'	=> "操作密码验证失败"
			)));
		}
		$idarr = input('idarr/a');
        $result = Db::name('broker')->whereIn('id',$idarr)->update(['deletetime'=>time()]);
		if ($result){
			exit(json_encode(array(
				'code' 	=> 0,
				'msg'	=> ""
			)));
		}
		exit(json_encode(array(
			'code' 	=> 1,
			'msg'	=> "删除失败，请稍后重试"
		)));
	}
	
	public function listBroker()
	{
		$limit = input('limit/d',20);
		$page = input('page/d',1);
		$map = array();
		$keywords = input('keywords/s');
		if (!empty($keywords)){
			$map[] =['title', 'like', '%'.$keywords.'%'];
		}
		$status = input('status/d');
		if (!empty($status)){
			$map[] = ['status', '=', $status];
		}
		$list = BrokerModel::with(['worker','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
		)));
	}
	
	
	// 申请注册职业顾问
	public function fbrokerList()
	{
		return view('broker/fbrokerlist');
	}
	
	public function fbrokerForm()
	{
		$id = input('id/d, 0');
		$fbroker = BrokerFormModel::findOrEmpty($id);
		return view('broker/fbrokerform',[
			'fbroker'	=> $fbroker
		]);
	}
	
	public function editFbroker()
	{
		$id = input('id/d');
		$fbroker = BrokerFormModel::findOrEmpty($id);
		$fbroker->save([
			'realname'  	=> input('realname/s', ""),
			'mobile'    	=> input('mobile/s', ""),
			'address'		=> input('address/s', ""),
			'idcard'		=> input('idcard/s', ""),
			'recommender'	=> input('recommender/s', ""),
			'status'		=> input('status/d', 1),
			'remark'		=> input('remark/s', ""),
			'createtime'	=> input('createtime/s', ""),
		]);
		exit(json_encode(array(
			'code' 	=> 0
		)));
	}
	
	public function fieldFbroker()
	{
		$id = input('id/d',0); 
		$info = BrokerFormModel::findOrEmpty($id);
		if ($info->isEmpty()){
			exit(json_encode(array(
				'code' 	=> 1,
				'msg'	=> "信息不存在"
			)));
		}else{
			$info->save([
				input('field/s')  => input('value')
			]);
		}
		exit(json_encode(array(
			'code' 	=> 0
		)));
	}
	
	public function delFbroker()
	{
		$idarr = input('idarr/a');
		$fbroker = BrokerFormModel::whereIn('id',$idarr)->select();
		$result = $fbroker->delete();
		if ($result){
			exit(json_encode(array(
				'code' 	=> 0,
				'msg'	=> ""
			)));
		}
		exit(json_encode(array(
			'code' 	=> 1,
			'msg'	=> "删除失败，请稍后重试"
		)));
	}
	
	public function listFbroker()
	{
		$limit = input('limit');
		$page = input('page');
		$map = array();
		$keywords = input('keywords/s');
		if (!empty($keywords)){
			$map[] =['realname|mobile', 'like', '%'.$keywords.'%'];
		}
		$status = input('status/d');
		if (!empty($status)){
			$map[] = ['status', '=', $status];
		}
		$list = BrokerFormModel::where($map)->order('id','DESC')->limit($limit)->page($page)->append(['status_text','powerreport_text'])->select();
		$count = BrokerFormModel::where($map)->count();
		if ($count==0){
			exit(json_encode(array(
				'code' 	=> 1,
				'msg'	=> "未查询到数据"
			)));
		}
		exit(json_encode(array(
			'code' 	=> 0,
			'msg'	=> "",
			'count' => $count,
			'data'  => $list
		)));
	}
	
	

}
