<?php
namespace app\admin\controller;

use app\admin\BaseController;
use app\common\model\Agent as AgentModel;
use app\common\model\Partjob as PartjobModel;
use app\common\model\PartjobLog as PartjobLogModel;

class Partjob extends BaseController
{
	
	// 报名记录
	public function logList()
	{
		$agentlist = AgentModel::with('partjob')->order(['id'=>'desc'])->select();
		return view('partjob/loglist',[
			'agentlist'		=> $agentlist
		]);
	}
	
	public function delLog()
	{
		$idarr = input('idarr/a');
		$log = PartjobLogModel::whereIn('id',$idarr)->select();
		$result = $log->delete();
		if ($result){
			exit(json_encode(array(
				'code' 	=> 0,
				'msg'	=> ""
			)));
		}
		exit(json_encode(array(
			'code' 	=> 1,
			'msg'	=> "删除失败，请稍后重试"
		)));
	}
	
	public function fieldLog()
	{
		$id = input('id/d',0); 
		$log = PartjobLogModel::findOrEmpty($id);
		if ($log->isEmpty()){
			exit(json_encode(array(
				'code' 	=> 1,
				'msg'	=> "信息不存在"
			)));
		}else{
			$log->save([
				input('field/s')  => input('value')
			]);
		}
		exit(json_encode(array(
			'code' 	=> 0
		)));
	}
	
	public function listLog()
	{
		$limit = input('limit/d',20);
		$page = input('page/d',1);
		$map = array();
		$agentpartjobarr = explode(",", input('agentpartjob/s'));
		$agentid = isset($agentpartjobarr[0]) ? $agentpartjobarr[0] : 0;
		$partjobid = isset($agentpartjobarr[1]) ? $agentpartjobarr[1] : 0;
		if (!empty($agentid)){
			$map[] = ['agentid', '=', $agentid];
		}
		if (!empty($partjobid)){
			$map[] = ['partjobid', '=', $partjobid];
		}
		$list = PartjobLogModel::with(['user','agent','partjob'])->where($map)->order('id','DESC')->limit($limit)->page($page)->select();
		$count = PartjobLogModel::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 exportLog()
	{
		$map = array();
		$agentpartjobarr = explode(",", input('agentpartjob/s'));
		$agentid = isset($agentpartjobarr[0]) ? $agentpartjobarr[0] : 0;
		$partjobid = isset($agentpartjobarr[1]) ? $agentpartjobarr[1] : 0;
		if (!empty($agentid)){
			$map[] = ['agentid', '=', $agentid];
		}
		if (!empty($partjobid)){
			$map[] = ['partjobid', '=', $partjobid];
		}
		$xlsData = PartjobLogModel::with(['user','agent','partjob'])->where($map)->order('id','DESC')->select()->toArray();
		$xlsCell = array(
			array('id','表ID'),
			array('user.nickname','昵称'),
			array('user.realname','姓名'),
			array('user.mobile','手机号'),
			array('agent.title','代理门店'),
			array('partjob.title','兼职标题'),
			array('remark','反馈备注'),
			array('createtime','报名时间'),
		);
		export_excel("兼职报名记录",$xlsCell,$xlsData);
	}
	
	
	// 兼职招聘
	public function partjobList()
	{
		$agentlist = AgentModel::order(['id'=>'desc'])->select();
		return view('partjob/partjoblist',[
			'agentlist'		=> $agentlist
		]);
	}
	
	public function partjobForm()
	{
		$id = input('id/d, 0');
		$partjob = PartjobModel::findOrEmpty($id);
		$agentlist = AgentModel::order(['id'=>'desc'])->select();
		return view('partjob/partjobform',[
			'agentlist'		=> $agentlist,
			'partjob'		=> $partjob
		]);
	}
	
	public function editPartjob()
	{
		$id = input('id/d');
		$data = [
			'agentid'			=> input('agentid/d', 0),
			'title'				=> input('title/s', ""),
			'tilpic'			=> input('tilpic/s', ""),
			'tags'				=> input('tags/a', array()),
			'telephone'			=> input('telephone/s', ""),
			'address'			=> input('address/s', ""),
			'wagall'			=> input('wagall/s', ""),
			'endtime'			=> input('endtime/s', ""),
			'recruitnum'		=> input('recruitnum/d', 0),
			'reqdetails'		=> input('reqdetails/s', ""),
			'jobdetails'		=> input('jobdetails/s', ""),
			'status'			=> input('status/d', 0),
			'priority'			=> input('priority/d', 0),
			'createtime'		=> input('createtime/s', "")
		];
		if (empty($id)){
			$partjob = PartjobModel::create($data);
		}else{
			$partjob = PartjobModel::find($id);
			PartjobLogModel::update([ 'agentid' => input('agentid/d', 0) ],['partjobid'=>$partjob->id]); 
			$partjob->save($data);
		}
		exit(json_encode(array(
			'code' 	=> 0
		)));
	}
	
	public function fieldPartjob()
	{
		$id = input('id/d',0); 
		$partjob = PartjobModel::findOrEmpty($id);
		if ($partjob->isEmpty()){
			exit(json_encode(array(
				'code' 	=> 1,
				'msg'	=> "信息不存在"
			)));
		}else{
			$partjob->save([
				input('field/s')  => input('value')
			]);
		}
		exit(json_encode(array(
			'code' 	=> 0
		)));
	}
	
	public function delPartjob()
	{
		$idarr = input('idarr/a');
		$partjob = PartjobModel::whereIn('id',$idarr)->select();
		PartjobLogModel::whereIn('partjobid',$idarr)->delete();
		$result = $partjob->delete();
		if ($result){
			exit(json_encode(array(
				'code' 	=> 0,
				'msg'	=> ""
			)));
		}
		exit(json_encode(array(
			'code' 	=> 1,
			'msg'	=> "删除失败，请稍后重试"
		)));
	}
	
	public function listPartjob()
	{
		$limit = input('limit/d',20);
		$page = input('page/d',1);
		$map = array();
		$keywords = input('keywords/s');
		if (!empty($keywords)){
			$map[] =['title', 'like', '%'.$keywords.'%'];
		}
		$agentid = input('agentid/d');
		if (!empty($agentid)){
			$map[] = ['agentid', '=', $agentid];
		}
		$status = input('status/d');
		if (!empty($status)){
			$map[] = ['status', '=', $status];
		}
		$list = PartjobModel::with(['agent'])->withCount(['partjobLog'])->where($map)->order('id','DESC')->limit($limit)->page($page)->append(['status_text'])->select();
		$count = PartjobModel::where($map)->count();
		if ($count==0){
			exit(json_encode(array(
				'code' 	=> 1,
				'msg'	=> "未查询到数据"
			)));
		}
		exit(json_encode(array(
			'code' 	=> 0,
			'msg'	=> "",
			'count' => $count,
			'data'  => $list
		)));
	}
	

}
