<?php
namespace app\agent\controller;

use think\facade\Session;
use app\agent\BaseController;
use app\common\model\ReportFactory as ReportFactoryModel;
use app\common\model\ReportEntry as ReportEntryModel;
use app\common\model\Broker as BrokerModel;
use app\common\model\ReportOrder as ReportOrderModel;
use app\common\model\ReportOrderlog as ReportOrderlogModel;

use PHPExcel_IOFactory;
use PHPExcel;

class Report extends BaseController
{
	
	// 订单信息
	public function orderList()
	{
		$factorylist = ReportFactoryModel::order(['id'=>'desc'])->select();
		return view('report/orderlist',[
			'factorylist'	=> $factorylist
		]);
	}
	
	public function listOrder()
	{
		$agentid = $this->access_agent['id'];
		$limit = input('limit/d',20);
		$page = input('page/d',1);
		$map = array();
		$map[] = ['ReportOrder.status', '>', 1];
		$keywords = input('keywords/s', "");
		if (!empty($keywords)){
			$map[] =['title', 'like', '%'.$keywords.'%'];
		}
		$factoryid = input('factoryid/d', 0);
		if (!empty($factoryid)){
			$map[] = ['factoryid', '=', $factoryid];
		}
		$reftype = input('reftype/d', 0);
		if (!empty($reftype)){
			$map[] = ['reftype', '=', $reftype];
		}
		$status = input('status/d', 0);
		if (!empty($status)){
			$map[] = ['status', '=', $status];
		}
		$list = ReportOrderModel::hasWhere('reportOrderlog',['agentid'=>$agentid])->with(['reportFactory'])->where($map)->order('id','DESC')->limit($limit)->page($page)->append(['reftype_text','status_text'])->select();
		$count = ReportOrderModel::hasWhere('reportOrderlog',['agentid'=>$agentid])->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 orderDetails()
	{
		$id = input('id/d', 0);
		$order = ReportOrderModel::with(['reportFactory'])->findOrEmpty($id);
		return view('report/orderdetails',[
			'order'			=> $order
		]);
	}

	public function listOrderlog()
	{
		$agentid = $this->access_agent['id'];
		$orderid = input('orderid/d', 0);
		$limit = input('limit/d',20);
		$page = input('page/d',1);
		$map = array();
		$map[] = ['agentid', '=', $agentid];
		$map[] = ['orderid', '=', $orderid];
		$list = ReportOrderlogModel::with(['reportOrder'])->where($map)->order('id','DESC')->limit($limit)->page($page)->select();
		$count = ReportOrderlogModel::where($map)->count();
		ReportOrderlogModel::update(['status'=>2], ['agentid'=>$agentid,'orderid'=>$orderid]);
		if ($count==0){
			exit(json_encode(array(
				'code' 	=> 1,
				'msg'	=> "未查询到数据"
			)));
		}
		exit(json_encode(array(
			'code' 	=> 0,
			'msg'	=> "",
			'count' => $count,
			'data'  => $list
		)));
	}
	
	// 报备名单
	public function entryImport()
	{
		$agent = $this->access_agent;
		return view('report/entryimport',[
			'agent'	=> $agent
		]);
	}
	
	public function importEntry()
	{
		$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'			=> $this->access_agent['workerid'],
					'agentid'			=> $this->access_agent['id'],
					'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'			=> 1,
					'remark'			=> "",
					'createtime'		=> $createtime
				);
				$sucnumber++;
			}
		}
		$entry->saveAll($data);
		exit(json_encode(array(
			'code' 	=> 0,
			'sucnumber'	=> $sucnumber,
			'errnumber'	=> $errnumber
		)));
	}
	
	public function entryForm()
	{
		$agentid = $this->access_agent['id'];
		$id = input('id/d, 0');
		$entry = ReportEntryModel::where('agentid', '=', $agentid)->findOrEmpty($id);
		$brokerlist = BrokerModel::where('agentid', '=', $agentid)->order(['id'=>'desc'])->select();
		$factorylist = ReportFactoryModel::order(['id'=>'desc'])->select();
		return view('report/entryform',[
			'brokerlist'	=> $brokerlist,
			'factorylist'	=> $factorylist,
			'entry'			=> $entry
		]);
	}
	
	public function editEntry()
	{
		$agentid = $this->access_agent['id'];
		$id = input('id/d');
		$entry = ReportEntryModel::where('agentid', '=', $agentid)->findOrEmpty($id);
		$data = array(
			'workerid'			=> $this->access_agent['workerid'],
			'agentid'			=> $this->access_agent['id'],
			'brokerid'			=> input('brokerid/d', 0),
			'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')
		);
		if ($entry->isEmpty()){
			$data['createtime'] = time();
			$data['remark'] = "";
			ReportEntryModel::create($data);
		}else{
			$entry->save($data);
		}
		exit(json_encode(array(
			'code' 	=> 0
		)));
	}
	
	public function delEntry()
	{
		$agentid = $this->access_agent['id']; 
		$idarr = input('idarr/a');
		$entry = ReportEntryModel::where('agentid', '=', $agentid)->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()
	{
		$agentid = $this->access_agent['id']; 
		$brokerlist = BrokerModel::where('agentid', '=', $agentid)->order(['id'=>'desc'])->select();
		$factorylist = ReportFactoryModel::order(['id'=>'desc'])->select();
		return view('report/entrylist',[
			'brokerlist'	=> $brokerlist,
			'factorylist'	=> $factorylist
		]);
	}
	
	public function listEntry()
	{
		$agentid = $this->access_agent['id']; 
		$limit = input('limit/d',20);
		$page = input('page/d',1);
		$map = array();
		$map[] = ['agentid', '=', $agentid];
		$keywords = input('keywords/s');
		if (!empty($keywords)){
			$map[] =['realname|idcard', 'like', '%'.$keywords.'%'];
		}
		$factoryid = input('factoryid/d');
		if (!empty($factoryid)){
			$map[] = ['factoryid', '=', $factoryid];
		}
		$brokerid = input('brokerid/d');
		if (!empty($brokerid)){
			$map[] = ['brokerid', '=', $brokerid];
		}
		$status = input('status/d');
		if (!empty($status)){
			$map[] = ['status', '=', $status];
		}
		$list = ReportEntryModel::with(['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()
	{
		$agentid = $this->access_agent['id'];
		$map = array();
		$map[] = ['agentid', '=', $agentid];
		$keywords = input('keywords/s');
		if (!empty($keywords)){
			$map[] =['realname|idcard', 'like', '%'.$keywords.'%'];
		}
		$factoryid = input('factoryid/d');
		if (!empty($factoryid)){
			$map[] = ['factoryid', '=', $factoryid];
		}
		$brokerid = input('brokerid/d');
		if (!empty($brokerid)){
			$map[] = ['brokerid', '=', $brokerid];
		}
		$status = input('status/d');
		if (!empty($status)){
			$map[] = ['status', '=', $status];
		}
		$xlsData = ReportEntryModel::with(['broker','reportFactory'])->where($map)->order('id','DESC')->select()->toArray();
		$xlsCell = array(
			array('id','表ID'),
			array('broker.title','职业顾问'),
			array('reportFactory.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
		)));
	}
	
}
