<?php
namespace app\admin\controller;

use app\admin\BaseController;
use app\common\model\ComjobsReport as ComjobsReportModel;
use app\common\model\ReportLog as ReportLogModel;

use app\common\model\Comjobs as ComjobsModel;
use app\common\model\Agent as AgentModel;

use app\common\validate\Report as ReportValidate;
use think\exception\ValidateException;
use think\facade\Db;

class Comjobsreport extends BaseController
{


	// 报备记录
	public function reportList()
	{
		$comjobslist = ComjobsModel::select();
		$agentlist = AgentModel::order(['id'=>'desc'])->select();
		$statusarr = array(1=>'待审核', 2=>'待面试', 3=>'已入职', 4=>'无效报备');
		return view('comjobsreport/reportlist',[
			'comjobslist'	=> $comjobslist,
			'agentlist'		=> $agentlist,
			'statusarr'		=> $statusarr
		]);
	}

    // 报备记录(待审核)
    public function reportExamine()
    {
        $comjobslist = ComjobsModel::select();
        $agentlist = AgentModel::order(['id'=>'desc'])->select();
        $statusarr = array(1=>'待审核', 2=>'待面试', 3=>'已入职', 4=>'无效报备');
        return view('comjobsreport/reportexamine',[
            'comjobslist'	=> $comjobslist,
            'agentlist'		=> $agentlist,
            'statusarr'		=> $statusarr
        ]);
    }
	
	public function listReport()
	{
		$limit = input('limit/d',20);
		$page = input('page/d',1);

		$map = array();
        $date6 = input('date6');

        if(!empty($date6))
        {
            $date6 =explode('~',$date6);
            $startTime = strtotime($date6[0]);
            $endTime = strtotime($date6[1]);
            $map[] = ['createtime', 'between', [$startTime,$endTime]];
        }

		$comjobsid = input('comjobsid/d', 0);
		if (!empty($comjobsid)){
			$map[] = ['comjobsid', '=', $comjobsid];
		}
		$agentid = input('agentid/s', "");
		if ($agentid!=""){
			$map[] = ['agentid', '=', intval($agentid)];
		}
		$status = input('status/d', 0);
		if (!empty($status)){
			$map[] = ['status', '=', $status];
		}
		$list = ComjobsReportModel::with(['comjobs','agent','broker'])->where($map)->order('id','DESC')->limit($limit)->page($page)->append(['status_text'])->select();
		$count = ComjobsReportModel::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 exportReport()
	{
		$map = array();
		$comjobsid = input('comjobsid/d', 0);
		if (!empty($comjobsid)){
			$map[] = ['comjobsid', '=', $comjobsid];
		}
		$agentid = input('agentid/d', 0);
		if (!empty($agentid)){
			$map[] = ['agentid', '=', $agentid];
		}
		$status = input('status/d', 0);
		if (!empty($status)){
			$map[] = ['status', '=', $status];
		}
		$xlsData = ComjobsReportModel::with(['comjobs','agent','broker'])->where($map)->order('id','DESC')->append(['status_text'])->select()->toArray();
		$xlsCell = array(
			array('id','表ID'),
			array('comjobs.title','招聘标题'),
			array('agent.title','代理门店'),
			array('broker.title','经纪人'),
			array('realname','姓名'),
			array('mobile','手机号'),
			array('idcard','身份证号'),
			array('arrivetime','预计到达时间'),
			array('remark','报名备注'),
			array('status_text','状态'),
			array('createtime','接单时间')
		);
		export_excel("报备记录",$xlsCell,$xlsData);
	}
	
	public function setComtype()
	{
		$idarr = input('idarr/a');
		$comjobsid = input('comjobsid/d', 0);
		$comjobs = ComjobsModel::with(['company'])->findOrEmpty($comjobsid);
		if ($comjobs->isEmpty()){
			exit(json_encode(array(
				'code' 	=> 1,
				'msg'	=> "请选择招聘信息和代理门店"
			)));
		}
		$data = [
			'comjobsid'			=> $comjobs->id,
			'comtype'			=> input('comtype/s', ""),
			'comprice'			=> input('comprice/s', ""),
			'comremark'			=> input('comremark/s', ""),
			'updatetime'		=> input('updatetime/s', date("Y-m-d H:i"))
		];
		ComjobsReportModel::whereIn('id',$idarr)->update($data);
		$reportall = ComjobsReportModel::whereIn('id',$idarr)->select()->toArray();
		$logdata = array();
		$statusarr = array(1=>'待审核', 2=>'待面试', 3=>'已入职', 4=>'无效报备');
		$agentall = AgentModel::column('realname', 'id');
		foreach($reportall as $key=>$item){
			$logdata[] = array(
				'reportid'		=> $item['id'],
				'logstatus'		=> $statusarr[$item['status']],
				'editrealname'	=> "管理员",
				'comjobstil'	=> $comjobs->isEmpty() ? "" : $comjobs->title,
				'agenttil'		=> empty($agentall[$item['agentid']]) ? "未报备" : $agentall[$item['agentid']],
				'realname'		=> $item['realname'],
				'mobile'		=> $item['mobile'],
				'idcard'		=> $item['idcard'],
				'comtype'		=> input('comtype/s', ""),
				'comprice'		=> input('comprice/s', ""),
				'comremark'		=> input('comremark/s', ""),
				'arrivetime'	=> $item['arrivetime'],
				'remark'		=> $item['remark'],
				'createtime'	=> date("Y-m-d H:i")
			);
		}
		$reportlog = new ReportLogModel;
		$reportlog->saveAll($logdata);
		exit(json_encode(array(
			'code' 	=> 0,
			'msg'	=> ""
		)));
	}
	
	public function setStatus()
	{
		$idarr = input('idarr/a');
		$status = input('status/d', 1);
		ComjobsReportModel::whereIn('id',$idarr)->update(['status'=>$status]);
		exit(json_encode(array(
			'code' 	=> 0,
			'msg'	=> ""
		)));
	}
	
	public function delReport()
	{
		$idarr = input('idarr/a');

        $ComjobsReport = ComjobsReportModel::whereIn('id',$idarr)->select();
        $result = $ComjobsReport->delete();

		if ($result){
			exit(json_encode(array(
				'code' 	=> 0,
				'msg'	=> ""
			)));
		}
		exit(json_encode(array(
			'code' 	=> 1,
			'msg'	=> "删除失败，请稍后重试"
		)));
	}
	
	public function reportImport()
	{
		$id = input('id/d, 0');
		$comjobslist = ComjobsModel::with(['comjobsRefund.agentGroups'])->order(['id'=>'desc'])->select();
		$agentlist = AgentModel::order(['id'=>'asc'])->select();
		return view('comjobsreport/reportimport',[
			'comjobslist'	=> $comjobslist,
			'agentlist'		=> $agentlist
		]);
	}
	
	public function importReport()
	{
		$execlfile = input('execlfile/s', "");
		if (empty($execlfile)){
			exit(json_encode(array(
				'code' 	=> 1,
				'msg'	=> "请上传导入文件"
			)));
		}
		$comjobsid = input('comjobsid/d', 0);
		$comjobs = ComjobsModel::findOrEmpty($comjobsid);
		$agentid = input('agentid/d', 0);
		$agent = AgentModel::findOrEmpty($agentid);
		$filedata = read_excel( $execlfile );
		$sucnumber = 0;
		$errnumber = 0;
		$errarr = "";
		$statusarr = array(1=>'待审核', 2=>'待面试', 3=>'已入职', 4=>'无效报备');
		foreach( $filedata as $k=>$v ){
			$tabcomjobsid = intval($v[1]);
			if (empty($tabcomjobsid)){
				$tabcomjobs = $comjobs;
			}else{
				$tabcomjobs = ComjobsModel::findOrEmpty($tabcomjobsid);
			}
			$tabagentid = intval($v[2]);
			if (empty($tabagentid)){
				$tabagent = $agent;
			}else{
				$tabagent = AgentModel::findOrEmpty($tabagentid);
			}
			if( $tabcomjobs->isEmpty() ){
				$errarr .= $v[0] . "，";
				$errnumber++;
				continue;
			}else{
				$data = array(
					'comjobsid'			=> $tabcomjobs->id,
					'agentid'			=> $tabagent->isEmpty() ? 0 : $tabagent->id,
					'brokerid'			=> 0,
					'realname'			=> trim($v[3]),
					'mobile'			=> trim($v[4]),
					'idcard'			=> trim($v[5]),
					'gender'			=> trim($v[6])=='男' ? 1 : 2,
					'comtype'			=> empty(trim($v[7])) ? trim($v[7]) : input('comtype/s', ""),
					'comprice'			=> empty(trim($v[8])) ? trim($v[8]) : input('comprice/s', ""),
					'comremark'			=> empty(trim($v[9])) ? trim($v[9]) : input('comremark/s', ""),
					'arrivetime'		=> trim($v[10]),
					'remark'			=> trim($v[11]),
					'status'			=> intval($v[12]),
					'updatetime'		=> date("Y-m-d H:i"),
					'createtime'		=> date("Y-m-d H:i")
				);
				try {
				    validate(ReportValidate::class)->check($data);
				} catch (ValidateException $e) {
					$errarr .= $v[0] . "，";
					$errnumber++;
					continue;
				}
				$report = new ComjobsReportModel;
				$report->save($data);
				$logdata = array(
					'reportid'		=> $report->id,
					'logstatus'		=> $statusarr[$report->status],
					'editrealname'	=> "管理员",
					'comjobstil'	=> $tabcomjobs->title,
					'agenttil'		=> $tabagent->isEmpty() ? "未报备" : $tabagent->title,
					'realname'		=> trim($v[3]),
					'mobile'		=> trim($v[4]),
					'idcard'		=> trim($v[5]),
					'comtype'		=> empty(trim($v[7])) ? trim($v[7]) : input('comtype/s', ""),
					'comprice'		=> empty(trim($v[8])) ? trim($v[8]) : input('comprice/s', ""),
					'comremark'		=> empty(trim($v[9])) ? trim($v[9]) : input('comremark/s', ""),
					'arrivetime'	=> trim($v[10]),
					'remark'		=> trim($v[11]),
					'createtime'	=> date("Y-m-d H:i")
				);
				ReportLogModel::create($logdata);
				$sucnumber++;
			}
		}
		exit(json_encode(array(
			'code' 			=> 0,
			'sucnumber'		=> $sucnumber,
			'errnumber'		=> $errnumber,
			'errarr'		=> $errarr
		)));
	}
	
	
	public function reportForm()
	{
		$id = input('id/d, 0');
		$comjobslist = ComjobsModel::field('id,title,workerid')->order(['id'=>'desc'])->select();
		$agentlist = AgentModel::with('broker')->order(['id'=>'asc'])->select();
		$report = ComjobsReportModel::with(['comjobs','agent','broker'])->append(['gender_text','status_text'])->findOrEmpty($id);
		$statusarr = array(1=>'待审核', 2=>'待面试', 3=>'已入职', 4=>'无效报备');
		return view('comjobsreport/reportform',[
			'comjobslist'	=> $comjobslist,
			'agentlist'		=> $agentlist,
			'report'		=> $report,
			'statusarr'		=> $statusarr
		]);
	}
	 
	public function editReport()
	{
		$id = input('id/d', 0);
		$comjobsid = input('comjobsid/d', 0);
		$agentbrokerarr = explode(",", input('agentbroker/s'));
		$agentid = isset($agentbrokerarr[0]) ? $agentbrokerarr[0] : 0;
		$brokerid = isset($agentbrokerarr[1]) ? $agentbrokerarr[1] : 0;

		$comjobs_data = Db::name("comjobs")
                        ->field('id,workerid,title')
                        ->where('id',$comjobsid)
                        ->find();

		$data = array(
			'comjobsid'			=> $comjobsid,
			'agentid'			=> $agentid,
			'brokerid'			=> $brokerid,
			'workerid'			=> $comjobs_data['workerid'],
			'realname'			=> input('realname/s', ""),
			'mobile'			=> input('mobile/s', ""),
			'idcard'			=> input('idcard/s', ""),
			'arrivetime'		=> input('arrivetime/s', ""),
			'remark'			=> input('remark/s', ""),
			'status'			=> input('status/d', 1),
			'updatetime'		=> input('updatetime/s', date("Y-m-d H:i")),
			'createtime'		=> input('createtime/s', date("Y-m-d H:i"))
		);
		if (empty($id)){
			$report = new ComjobsReportModel;
			$report->save($data); 
		}else{
			$report = ComjobsReportModel::find($id);
			$report->save($data); 
		}
		exit(json_encode(array(
			'code' 	=> 0
		)));
	}
	
	// 记录
	public function logList()
	{
		$reportid = input('reportid/d',0);
		$report = ComjobsReportModel::findOrEmpty($reportid);
		return view('comjobsreport/loglist',[
			'reportid'		=> $reportid,
			'report'		=> $report
		]); 
	}
	
	public function listLog()
	{
		$limit = input('limit/d',50);
		$page = input('page/d',1);
		$map = array();
		$map[] = ['reportid', '=', input('reportid/d',0)];
        $map[] = ['delstatus', '<>', 2];
		$list = ReportLogModel::with(['report'])->where($map)->order(['id'=>'desc'])->limit($limit)->page($page)->select();
		$count = ReportLogModel::where($map)->count();
		if ($count==0){
			exit(json_encode(array(
				'code' 	=> 1,
				'msg'	=> "未查询到数据"
			)));
		}
		exit(json_encode(array(
			'code' 	=> 0,
			'msg'	=> "",
			'count' => $count,
			'data'  => $list
		)));
	}
	
	

}
