<?php
namespace app\mainapp\controller;

use app\mainapp\BaseController;
use think\facade\Session;

use app\common\model\Param as ParamModel;

use app\common\model\User as UserModel;
use app\common\model\UserFollow as UserFollowModel;
use app\common\model\UserPart as UserPartModel;

use app\common\model\Agent as AgentModel;
use app\common\model\Broker as BrokerModel;
use app\common\model\BrokerForm as BrokerFormModel;

use app\common\model\ReportEntry as ReportEntryModel;
use app\common\model\ReportFactory as ReportFactoryModel;

use app\common\model\ReportOrder as ReportOrderModel;
use app\common\model\ReportOrderlog as ReportOrderlogModel;

use echowx\WxProgram;

class Broker extends BaseController
{
	
	
	
	public function getListBroker()
	{
		$ppage = input('ppage/d', 1);
		$psize = input('psize/d', 20);
		$map = array();
		$map[] = ['status','=',1];
		$searchval = input('searchval/s', "");
		if (!empty($searchval)){
			$map[] =['title', 'like', '%'.$searchval.'%'];
		}
		$district = input('district/s', "");
		if (!empty($district)){
			$map[] = ['district', '=', $district];
		}
		$plist = BrokerModel::with(['muser'])->where($map)->order(['id'=>'desc'])->page($ppage)->limit($psize)->select();
		page_result(0, "", array(
			'plist' => $plist,
			'pstatus' => $psize > count($plist) ? 'noMore' : 'more'
			));
	}
	
	
	
	
	
	// 申请注册职业顾问
	public function pageForm()
	{
		$param = ParamModel::where(1)->find();
		page_result(0, "", array(
			'param'		=> $param
		));
	}
	
	public function regBroker()
	{
		$realname = input('realname/s', "");
		$mobile = input('mobile/s', "");
		$address = input('address/s', "");
		$idcard = input('idcard/s', "");
		$recommender = input('recommender/s', "");
		if ( empty($realname) || empty($mobile) || empty($address) || empty($idcard) ){
			page_result(1, "姓名、手机号、当前住址、身份证号均不能为空。");
		}
		$form = new BrokerFormModel;
		$form->save([
			'realname'			=> $realname,
			'mobile'			=> $mobile,
			'address'			=> $address,
			'idcard'			=> $idcard,
			'recommender'		=> $recommender,
			'status'			=> 1,
			'remark'			=> "",
			'createtime'		=> time()
		]);
		page_result(0, "", array());
	}
	
	
	

	// 报备信息
	public function getOrder()
	{
		$orderid = input('orderid/d',0);
		$agentid = input('agentid/d',0);
		$order = ReportOrderModel::with(['reportFactory'])->where(['id'=>$orderid])->find();
		$orderlog = ReportOrderlogModel::where(['agentid'=>$agentid,'orderid'=>$orderid])->order(['id'=>"DESC"])->select();
		ReportOrderlogModel::update(['status' => 2], ['agentid'=>$agentid,'orderid'=>$orderid]);
		page_result(0, "", array(
			'order'			=> $order,
			'orderlog'		=> $orderlog
		));
	}
	
	// 订单信息	
	public function listOrder()
	{
		$brokerid = input('brokerid/d',0);
		$broker = BrokerModel::findOrEmpty($brokerid);
		$agentid = $broker->agentid;
		$ppage = input('ppage/d', 1);
		$psize = input('psize/d', 20);
		$map = array();
		$map[] = ['ReportOrder.status', '>', 1];
		$plist = ReportOrderModel::hasWhere('reportOrderlog',['agentid'=>$agentid])->with(['reportFactory'])->where($map)->order(['status'=>'ASC','id'=>'DESC'])->page($ppage)->limit($psize)->select()->toArray();	
		foreach($plist as $key=>$val){
			$plist[$key]['log'] = ReportOrderlogModel::where(['agentid'=>$agentid,'orderid'=>$val['id']])->order(['id'=>'DESC'])->findOrEmpty()->toArray();
		}
		page_result(0, "", array(
			'plist' => $plist,
			'pstatus' => $psize > count($plist) ? 'noMore' : 'more'
			));
	}
	
	
	
	// 数据信息
	public function pageBroker()
	{
		$brokerid = input('brokerid/d', 0);
		$stime = strtotime(date("Y-m-d"),time());
		
		$tuserCount = UserModel::where('brokerid', '=', $brokerid)->where('createtime','between',[$stime,$stime+86400])->count();
		$userCount = UserModel::where('brokerid', '=', $brokerid)->count();
		
		$tentryCount = ReportEntryModel::where('brokerid', '=', $brokerid)->where('createtime','between',[$stime,$stime+86400])->count();
		$entryCount = ReportEntryModel::where('brokerid', '=', $brokerid)->count();
		
		page_result(0, "", array('countobj' => array(
			'tuser'		=> $tuserCount,
			'user'		=> $userCount,
			'tentry'	=> $tentryCount,
			'entry'		=> $entryCount
			)));
	}
		
	// 报备信息
	public function getEntry()
	{
		$entryid = input('entryid/d',0);
		$brokerid = input('brokerid/d',0);
		$entry = ReportEntryModel::with(['reportFactory'])->where(['brokerid'=>$brokerid,'id'=>$entryid])->find();
		$broker = BrokerModel::with('agent')->findOrEmpty($brokerid);
		$factorylist = ReportFactoryModel::order(['priority'=>'desc','id'=>'desc'])->select()->toArray();
		$factoryidarr = ReportFactoryModel::order(['priority'=>'desc','id'=>'desc'])->column('title');
		page_result(0, "", array(
			'entry'			=> $entry,
			'broker'		=> $broker,
			'factorylist'	=> $factorylist,
			'factoryidarr'	=> array_values($factoryidarr),
			'today'			=> date("Y-m-d")
		));
	}
	
	public function statusEntry()
	{
		$entryid = input('entryid/d',0);
		$brokerid = input('brokerid/d',0);
		$entry = ReportEntryModel::with(['reportFactory'])->where('brokerid','=',$brokerid)->findOrEmpty($entryid);
		if ($entry->isEmpty()){
			page_result(1, "报备信息不存在。");
		}
		if ($entry->status > 1){
			page_result(1, "报备信息已确认过。");
		}
		$entry->save(['status'=>2]);
		page_result(0, "", array(
			'entry'		=> $entry
		));
	}
	
	public function deleteEntry()
	{
		$entryid = input('entryid/d',0);
		$brokerid = input('brokerid/d',0);
		$entry = ReportEntryModel::with(['reportFactory'])->where('brokerid','=',$brokerid)->findOrEmpty($entryid);
		if ($entry->isEmpty()){
			page_result(1, "报备信息不存在。");
		}
		if ($entry->status > 1){
			page_result(1, "报备信息已确认过，不能删除。");
		}
		$entry->delete();
		page_result(0, "", array());
	}
	
	public function addEntry()
	{
		$brokerid = input('brokerid/d',0);
		$broker = BrokerModel::with('agent')->findOrEmpty($brokerid);
		if ($broker->isEmpty()){
			page_result(1, "职业顾问信息不存在。");
		}
		$data = array(
			'workerid'			=> $broker->workerid,
			'agentid'			=> $broker->agentid,
			'brokerid'			=> $brokerid,
			'factoryid'			=> input('factoryid/d', 0),
			'realname'			=> input('realname/s', ""),
			'mobile'			=> input('mobile/s', ""),
			'idcard'			=> input('idcard/s', ""),
			'gender'			=> input('gender/s', ""),
			'nation'			=> input('nation/s', ""),
			'address'			=> input('address/s', ""),
			'startdate'			=> input('startdate/s', ""),
			'enddate'			=> input('enddate/s', ""),
			'reftype'			=> input('reftype/s', ""),
			'refpolicy'			=> input('refpolicy/s', ""),
			'agentremark'		=> input('agentremark/s', ""),
			'status'			=> input('status/d', ""),
			'remark'			=> "",
			'createtime'		=> time()
		);
		$entry = new ReportEntryModel;
		$entry->save($data);
		$newentry = ReportEntryModel::with(['reportFactory'])->where('brokerid','=',$brokerid)->find($entry->id);
		page_result(0, "", array(
			'entry'		=> $newentry
		));
	}
	
	public function setIdcard()
	{
		$picpath = input('picpath/s',"");
		$picpath = root_path()."public".$picpath;
		$idcard = aliyun_ocr_idcard( $picpath );
		if ($idcard==false){
			page_result(1, "身份证信息识别失败。");
		}
		page_result(0, "", array(
			'idcard'		=> $idcard
		));
	}
	
	public function pageEntry()
	{
		$brokerid = input('brokerid/d',0);
		$statuslist = array(
			array('value'=>"", 'title'=>'状态不限'),
			array('value'=>1, 'title'=>'待确认'),
			array('value'=>2, 'title'=>'已确认'),
			array('value'=>3, 'title'=>'已入职'),
			array('value'=>4, 'title'=>'未通过')
			);
		$factorylist = ReportFactoryModel::field('id as value, title, priority')->order(['priority'=>'desc','id'=>'desc'])->select()->toArray();
		array_unshift($factorylist, array('value'=>"", 'title'=>'工厂不限'));
		page_result(0, "", array(
			'statuslist' => $statuslist,
			'factorylist' => $factorylist
			));
	}
	
	public function listEntry()
	{
		$ppage = input('ppage/d', 1);
		$psize = input('psize/d', 20);
		$map = array();
		$brokerid = input('brokerid/d', 0);
		$map[] = ['brokerid', '=', $brokerid];
		$status = input('status/d', 0);
		if (!empty($status)){
			$map[] = ['status', '=', $status];
		}
		$factoryid = input('factoryid/d', 0);
		if (!empty($factoryid)){
			$map[] = ['factoryid', '=', $factoryid];
		}
		$searchval = input('searchval/s', "");
		if (!empty($searchval)){
			$map[] =['realname|mobile', 'like', '%'.$searchval.'%', 'OR'];
		}
		$plist = ReportEntryModel::with(['reportFactory'])->where($map)->order(['id'=>'desc'])->page($ppage)->limit($psize)->select();
		page_result(0, "", array(
			'plist' => $plist,
			'pstatus' => $psize > count($plist) ? 'noMore' : 'more'
			));
	}
	
	
	// 我的邀请
	public function getMyuser()
	{
		$userid = input('userid/d',0);
		$brokerid = input('brokerid/d',0);
		$user = UserModel::with(['userFollow'])->where('brokerid','=',$brokerid)->findOrEmpty($userid);
		if ($user->isEmpty()){
			page_result(1, "用户信息不存在");
		}
		page_result(0, "", array(
			'user' => $user,
		));
	}
	
	public function sendFollow()
	{
		$userid = input('userid/d',0);
		$ftype = input('ftype/s',"");
		$remark = input('remark/s',"");
		if (empty($remark)){
			page_result(1, "请输入跟进的具体内容。");
		}
		$followstatus = input('followstatus/d',1);
		UserModel::update(['followstatus'=>$followstatus],['id'=>$userid]);
		$data = array(
			'userid'		=> $userid,
			'ftype'			=> $ftype,
			'remark'		=> $remark,
			'createtime'	=> time()
		);
		$follow = new UserFollowModel;
		$follow->save($data);
		
		$user = UserModel::with(['userFollow'])->findOrEmpty($userid);
		page_result(0, "", array(
			'user' => $user,
		));
	}
	
	public function listUser()
	{
		$ppage = input('ppage/d', 1);
		$psize = input('psize/d', 20);
		$map = array();
		$brokerid = input('brokerid/d', 0);
		$map[] = ['brokerid', '=', $brokerid];
		$followstatus = input('followstatus/d', 0);
		if (!empty($followstatus)){
			$map[] = ['followstatus', '=', $followstatus];
		}
		$plist = UserModel::with(['userFollow'])->where($map)->order(['id'=>'desc'])->page($ppage)->limit($psize)->select();
		page_result(0, "", array(
			'plist' => $plist,
			'pstatus' => $psize > count($plist) ? 'noMore' : 'more'
			));
	}
	
}