<?php
namespace app\agent\controller;

use app\agent\BaseController;
use app\common\model\Partjob as PartjobModel;
use app\common\model\PartjobLog as PartjobLogModel;

class Partjob extends BaseController
{
	
	// 报名记录
	public function logList()
	{
		$agentid = $this->access_agent['id'];
		$partjoblist = PartjobModel::where('agentid', '=', $agentid)->order('id','DESC')->select();
		return view('partjob/loglist',[
			'partjoblist'	=> $partjoblist
		]);
	}
	
	public function delLog()
	{
		$idarr = input('idarr/a');
		$agentid = $this->access_agent['id'];
		$log = PartjobLogModel::where('agentid', '=', $agentid)->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()
	{
		$agentid = $this->access_agent['id']; 
		$limit = input('limit/d',20);
		$page = input('page/d',1);
		$map = array();
		$map[] = ['agentid', '=', $agentid];
		$partjobid = input('partjobid/d');
		if (!empty($partjobid)){
			$map[] = ['partjobid', '=', $partjobid];
		}
		$list = PartjobLogModel::with(['user','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()
	{
		$agentid = $this->access_agent['id'];
		$map = array();
		$map[] = ['agentid', '=', $agentid];
		$partjobid = input('partjobid/d');
		if (!empty($partjobid)){
			$map[] = ['partjobid', '=', $partjobid];
		}
		$xlsData = PartjobLogModel::with(['user','partjob'])->where($map)->order('id','DESC')->select()->toArray();
		$xlsCell = array(
			array('id','表ID'),
			array('user.nickname','昵称'),
			array('user.realname','姓名'),
			array('user.mobile','手机号'),
			array('partjob.title','兼职标题'),
			array('remark','反馈备注'),
			array('createtime','报名时间'),
		);
		export_excel("兼职报名记录",$xlsCell,$xlsData);
	}
	
	
	// 兼职招聘
	public function partjobList()
	{
		return view('partjob/partjoblist');
	}
	
	public function partjobForm()
	{
		$id = input('id/d, 0');
		$agentid = $this->access_agent['id'];
		$partjob = PartjobModel::where('agentid', '=', $agentid)->findOrEmpty($id);
		$agent = $this->access_agent;
		return view('partjob/partjobform',[
			'partjob'	=> $partjob,
			'agent'		=> $agent
		]);
	}
	
	public function editPartjob()
	{
		$id = input('id/d');
		$agentid = $this->access_agent['id'];
		$data = [
			'agentid'			=> $agentid,
			'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'),
			'createtime'		=> input('createtime/s')
		];
		if (empty($id)){
			$data['priority'] = 0;
			$partjob = PartjobModel::create($data);
		}else{
			$partjob = PartjobModel::find($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::where('agentid', '=', $agentid)->whereIn('id',$idarr)->select();
		PartjobLogModel::where('agentid', '=', $agentid)->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()
	{
		$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[] =['title', 'like', '%'.$keywords.'%'];
		}
		$status = input('status/d');
		if (!empty($status)){
			$map[] = ['status', '=', $status];
		}
		$list = PartjobModel::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
		)));
	}
	

}
