<?php
namespace app\worker\controller;

use think\facade\Session;
use app\worker\BaseController;
use app\common\model\ReportFactory as ReportFactoryModel;
use app\common\model\ReportEntry as ReportEntryModel;
use app\common\model\Agent as AgentModel;
use app\common\model\Broker as BrokerModel;

use PHPExcel_IOFactory;
use PHPExcel;

class Report extends BaseController
{
	
	// 报备名单
	public function entryImport()
	{
		$workerid = $this->access_worker['id'];
		$agentlist = AgentModel::where('workerid', '=', $workerid)->order(['id'=>'asc'])->select();
		return view('report/entryimport',[
			'agentlist'	=> $agentlist
		]);
	}
	
	public function importEntry()
	{
		$workerid = $this->access_worker['id'];
		$agentid = input('agentid/d', 0);
		if (empty($agentid)){
			exit(json_encode(array(
				'code' 	=> 1,
				'msg'	=> "请选择报备代理门店。"
			)));
		}
		$execlfile = input('execlfile/s', "");
		if (empty($execlfile)){
			exit(json_encode(array(
				'code' 	=> 1,
				'msg'	=> "请上传导入文件。"
			)));
		}	
		$filedata = read_excel( $execlfile );
		$factorylist = ReportFactoryModel::where(['status'=>1])->column('idnumber,id,title','idnumber');
		$createtime = input('createtime/s', "");
		$entry = new ReportEntryModel;
		$data = array();
		$sucnumber = 0;
		$errnumber = 0;
		foreach( $filedata as $k=>$v ){ 
			$idnumber = trim($v[7]);
			if (empty($factorylist[$idnumber])){
				$errnumber++;
				continue;
			}else{
				$data[] = array(
					'workerid'			=> $workerid,
					'agentid'			=> $agentid,
					'brokerid'			=> 0,
					'realname'			=> $v[1],
					'mobile'			=> $v[2],
					'idcard'			=> $v[3],
					'gender'			=> $v[4],
					'nation'			=> $v[5],
					'address'			=> $v[6],
					'factoryid'			=> $factorylist[$idnumber]['id'],
					'startdate'			=> $v[8],
					'enddate'			=> $v[9],
					'reftype'			=> $v[10],
					'refpolicy'			=> $v[11],
					'agentremark'		=> $v[12],
					'status'			=> 2,
					'remark'			=> "",
					'createtime'		=> $createtime
				);
				$sucnumber++;
			}
		}
		$entry->saveAll($data);
		exit(json_encode(array(
			'code' 	=> 0,
			'sucnumber'	=> $sucnumber,
			'errnumber'	=> $errnumber
		)));
	}
	
	public function entryForm()
	{
		$workerid = $this->access_worker['id'];
		$id = input('id/d', 0);
		$entry = ReportEntryModel::where('workerid', '=', $workerid)->findOrEmpty($id);
		$agentlist = AgentModel::with('broker')->where('workerid', '=', $workerid)->order(['id'=>'asc'])->select();
		$factorylist = ReportFactoryModel::order(['id'=>'desc'])->select();
		return view('report/entryform',[
			'agentlist'		=> $agentlist,
			'factorylist'	=> $factorylist,
			'entry'			=> $entry
		]);
	}
	
	public function editEntry()
	{
		$workerid = $this->access_worker['id'];
		$id = input('id/d', 0);
		$entry = ReportEntryModel::where('workerid', '=', $workerid)->findOrEmpty($id);
		$agentbrokerarr = explode(",", input('agentbroker/s'));
		$agentid = isset($agentbrokerarr[0]) ? $agentbrokerarr[0] : 0;
		$brokerid = isset($agentbrokerarr[1]) ? $agentbrokerarr[1] : 0;
		$agent = AgentModel::findOrEmpty($agentid);
		$data = array(
			'workerid'			=> $workerid,
			'agentid'			=> $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', 1),
			'remark'			=> input('remark/s', ""),
			'createtime'		=> input('createtime/s', "")
		);
		if ($entry->isEmpty()){
			ReportEntryModel::create($data);
		}else{
			$entry->save($data);
		}
		exit(json_encode(array(
			'code' 	=> 0
		)));
	}
	
	public function delEntry()
	{
		$workerid = $this->access_worker['id']; 
		$idarr = input('idarr/a');
		$entry = ReportEntryModel::where('workerid', '=', $workerid)->where('status', '=', 1)->whereIn('id',$idarr)->select();
		$result = $entry->delete();
		if ($result){
			exit(json_encode(array(
				'code' 	=> 0,
				'msg'	=> ""
			)));
		}
		exit(json_encode(array(
			'code' 	=> 1,
			'msg'	=> "删除失败，请稍后重试"
		)));
	}
	
	public function entryList()
	{
		$workerid = $this->access_worker['id']; 
		$agentlist = AgentModel::where('workerid', '=', $workerid)->order(['id'=>'asc'])->select();
		$factorylist = ReportFactoryModel::order(['id'=>'desc'])->select();
		return view('report/entrylist',[
			'worker'		=> $this->access_worker,
			'agentlist'		=> $agentlist,
			'factorylist'	=> $factorylist
		]);
	}
	
	public function listEntry()
	{
		$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[] =['realname|idcard', 'like', '%'.$keywords.'%'];
		}
		$factoryid = input('factoryid/d');
		if (!empty($factoryid)){
			$map[] = ['factoryid', '=', $factoryid];
		}
		$agentid = input('agentid/d');
		if (!empty($agentid)){
			$map[] = ['agentid', '=', $agentid];
		}
		$status = input('status/d');
		if (!empty($status)){
			$map[] = ['status', '=', $status];
		}
		$list = ReportEntryModel::with(['agent','broker','reportFactory'])->where($map)->order('id','DESC')->limit($limit)->page($page)->append(['status_text'])->select();
		$count = ReportEntryModel::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 exportEntry()
	{
		$workerid = $this->access_worker['id'];
		$map = array();
		$map[] = ['workerid', '=', $workerid];
		$keywords = input('keywords/s');
		if (!empty($keywords)){
			$map[] =['realname|idcard', 'like', '%'.$keywords.'%'];
		}
		$factoryid = input('factoryid/d');
		if (!empty($factoryid)){
			$map[] = ['factoryid', '=', $factoryid];
		}
		$agentid = input('agentid/d');
		if (!empty($agentid)){
			$map[] = ['agentid', '=', $agentid];
		}
		$status = input('status/d');
		if (!empty($status)){
			$map[] = ['status', '=', $status];
		}
		$xlsData = ReportEntryModel::with(['worker','agent','broker','reportFactory'])->where($map)->order('id','DESC')->select()->toArray();
		$xlsCell = array(
			array('id','表ID'),
			array('agent.title','代理门店'),
			array('broker.title','职业顾问'),
			array('report_factory.title','意向工厂'),
			array('realname','姓名'),
			array('mobile','手机号'),
			array('idcard','身份证号'),
			array('gender','性别'),
			array('nation','民族'),
			array('address','现居住地'),
			array('startdate','出发日期'),
			array('enddate','预计到达日期'),
			array('reftype','返费模式'),
			array('refpolicy','返费政策'),
			array('agentremark','报备备注'),
			array('status','状态',array(1=>'待确认',2=>'已确认',3=>'已入职',4=>'未通过')),
			array('remark','状态备注'),
			array('createtime','注册时间'),
		);
		export_excel("名单报备",$xlsCell,$xlsData);
	}
	
	// 报备工厂
	public function factoryList()
	{
		return view('report/factorylist');
	}

	public function listFactory()
	{
		$limit = input('limit/d',20);
		$page = input('page/d',1);
		$map = array();
		$map[] = ['status', '=', 1];
		$keywords = input('keywords/s');
		if (!empty($keywords)){
			$map[] =['title', 'like', '%'.$keywords.'%'];
		}
		$list = ReportFactoryModel::where($map)->order('id','DESC')->limit($limit)->page($page)->select();
		$count = ReportFactoryModel::where($map)->count();
		if ($count==0){
			exit(json_encode(array(
				'code' 	=> 1,
				'msg'	=> "未查询到数据"
			)));
		}
		exit(json_encode(array(
			'code' 	=> 0,
			'msg'	=> "",
			'count' => $count,
			'data'  => $list
		)));
	}
	
}
