<?php
namespace app\agent\controller;

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

use app\common\model\User as UserModel;
use app\common\model\Broker as BrokerModel;

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

class Home extends BaseController
{
	
	public function index()
	{
		return view('index', [
			'access_agent'  => session('access_agent')
		]);
	}
	
	public function console()
	{
		
		$agentid = $this->access_agent['id'];
		$brokeridarr = BrokerModel::where('agentid','=',$agentid)->column('id');
				
		$stime = strtotime(date("Y-m-d"),time());
		
		$tuserCount = UserModel::whereIn('brokerid',$brokeridarr)->where('brokerid','<>', 0)->where('createtime','between',[$stime,$stime+86400])->count();
		$yuserCount = UserModel::whereIn('brokerid',$brokeridarr)->where('brokerid','<>', 0)->where('createtime','between',[$stime-86400,$stime])->count();
		
		$userCount = UserModel::whereIn('brokerid',$brokeridarr)->where('brokerid','<>', 0)->count();
		$brolerCount = BrokerModel::where('agentid', '=', $agentid)->count();
		$entryCount = ReportEntryModel::where('agentid', '=', $agentid)->count();
		
		$entryfactorylist = ReportEntryModel::with('reportFactory')->where('agentid', '=', $agentid)->field('agentid,factoryid,COUNT(id) AS entrytol')->group('factoryid')->order('entrytol','desc')->select()->toArray();
		$entryfnamearr = array();
		$entryfvaluearr = array();
		if (!empty($entryfactorylist)){
			foreach($entryfactorylist as $k=>$v){
				$entryfnamearr[$k] = $v['reportFactory']['title'] ." - ".$v['reportFactory']['idnumber'];
				$entryfvaluearr[$k]['name'] = $v['reportFactory']['title'] ." - ".$v['reportFactory']['idnumber'];
				$entryfvaluearr[$k]['value'] = $v['entrytol'];
			}
		}
		
		$userlist = UserModel::whereIn('brokerid',$brokeridarr)->where('brokerid','<>', 0)->where('createtime','between',[$stime-86400*30, $stime+86399])->withAttr('createtime',function ($value,$data) { return date("Y-m-d",$value); })->select()->toArray();
		$userarr = periodDate( date('Y-m-d', time()-86400*30), date("Y-m-d") );
		if (!empty($userlist)){
			foreach($userlist as $k=>$v){
				$userarr[$v['createtime']] += 1;
			}
		}
		$usertolarr = arrKeyVal($userarr);
		
		$entrylist = ReportEntryModel::where('agentid', '=', $agentid)->where('createtime','between',[$stime-86400*30, $stime+86399])->withAttr('createtime',function ($value,$data) { return date("Y-m-d",$value); })->select()->toArray();
		$entryarr = periodDate( date('Y-m-d', time()-86400*30), date("Y-m-d") );
		if (!empty($entrylist)){
			foreach($entrylist as $k=>$v){
				$entryarr[$v['createtime']] += 1;
			}
		}
		$entrytolarr = arrKeyVal($entryarr);
		
		return view('home/console',[
			'tuserCount'		=> $tuserCount,
			'yuserCount'		=> $yuserCount,
			'userCount'			=> $userCount,
			'brolerCount'		=> $brolerCount,
			'entryCount'		=> $entryCount,
			
			'entryfnamearr'		=> json_encode($entryfnamearr),
			'entryfvaluearr'	=> json_encode($entryfvaluearr),
			
			'usernamearr'	=> json_encode($usertolarr['keyarr']),
			'uservaluearr'	=> json_encode($usertolarr['valarr']), 
			
			'entrynamearr'	=> json_encode($entrytolarr['keyarr']), 
			'entryvaluearr'	=> json_encode($entrytolarr['valarr'])
		]);
	}
	

}