<?php
namespace app\admin\controller;

use app\admin\BaseController;
use app\common\model\Worker as WorkerModel;
use app\common\model\Demand as DemandModel;
use app\common\model\DemandCate as DemandCateModel;
use app\common\model\DemandLog as DemandLogModel;

use app\common\validate\Demand as DemandValidate;
use think\exception\ValidateException;
use think\facade\Db;

class Demand extends BaseController
{
	
	
	// 报名记录
	public function logList()
	{
		$workerlist = WorkerModel::with('demand')->order(['id'=>'desc'])->select();
		return view('demand/loglist',[
			'workerlist'		=> $workerlist
		]);
	} 
	
	public function delLog()
	{
		$idarr = input('idarr/a');
		$log = DemandLogModel::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 = DemandLogModel::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();
		$workerdemandarr = explode(",", input('workerdemand/s'));
		$workerid = isset($workerdemandarr[0]) ? $workerdemandarr[0] : 0;
		$demandid = isset($workerdemandarr[1]) ? $workerdemandarr[1] : 0;
		if (!empty($workerid)){
			$map[] = ['workerid', '=', $workerid];
		}
		if (!empty($demandid)){
			$map[] = ['demandid', '=', $demandid];
		}
		$list = DemandLogModel::with(['gworker','worker','demand'])->where($map)->order('id','DESC')->limit($limit)->page($page)->select();
		$count = DemandLogModel::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();
		$workerdemandarr = explode(",", input('workerdemand/s'));
		$workerid = isset($workerdemandarr[0]) ? $workerdemandarr[0] : 0;
		$demandid = isset($workerdemandarr[1]) ? $workerdemandarr[1] : 0;
		if (!empty($workerid)){
			$map[] = ['workerid', '=', $workerid];
		}
		if (!empty($demandid)){
			$map[] = ['demandid', '=', $demandid];
		}
		$xlsData = DemandLogModel::with(['gworker','worker','demand'])->where($map)->order('id','DESC')->select()->toArray();
		$xlsCell = array(
			array('id','表ID'),
			array('gworker.title','接单公司名称'),
			array('gworker.realname','接单公司联系人姓名'),
			array('gworker.mobile','接单公司联系人手机号'),
			array('worker.title','发单公司'),
			array('demand.title','订单标题'),
			array('createtime','接单时间'),
		);
		export_excel("接单报名记录",$xlsCell,$xlsData);
	}
	
	
	// 企业招聘
	public function demandList()
	{
		$workerlist = WorkerModel::order(['id'=>'desc'])->select();
		$catelist = DemandCateModel::order(['priority'=>'desc','id'=>'desc'])->select();
		return view('demand/demandlist',[
			'workerlist'	=> $workerlist,
			'catelist'		=> $catelist
		]);
	}
	
	public function demandForm()
	{
		$id = input('id/d, 0');
		$demand = DemandModel::findOrEmpty($id);
		$workerlist = WorkerModel::order(['id'=>'desc'])->select();
		$catelist = DemandCateModel::order(['priority'=>'desc','id'=>'desc'])->select();
		return view('demand/demandform',[
			'catelist'		=> $catelist,
			'workerlist'	=> $workerlist,
			'demand'		=> $demand
		]);
	}
	
	public function editDemand()
	{
		$id = input('id/d', 0);
		$wtype = input('wtype/d', 1);
		$zwagall = $wtype==1 ? input('zwagall/s', "") : '';
		$data = [
			'workerid'			=> input('workerid/d', 0),
			'title'				=> input('title/s', ""),
			'cateid'			=> input('cateid/d', 0),
			'province'			=> input('province/s', ""),
			'city'				=> input('city/s', ""),
			'district'			=> input('district/s', ""),
			'agegroup'			=> input('agegroup/s', ""),
			'tags'				=> input('tags/a', array()),
			'enddate'			=> input('enddate/s', ""),
			'requirement'		=> input('requirement/s', ""),
			'comdetails'		=> input('comdetails/s', ""),
			'wtype'				=> $wtype,
			'bwagall'			=> input('bwagall/s', ""),
			'zwagall'			=> $zwagall,
			'ftype'				=> input('ftype/d', 1),
			'fwagall'			=> input('fwagall/s', ""),
			'telephone'			=> input('telephone/s', ""),
			'remark'			=> input('remark/s', ""),
			'status'			=> input('status/d', 1),
			'isfree'			=> input('isfree/d', 1),
			'priority'			=> input('priority/d', 0),
			'updatetime'		=> input('updatetime/s', ""),
			'createtime'		=> input('createtime/s', ""),
			'volume'			=> input('volume/d', 0)
		];
		try {
		    validate(DemandValidate::class)->check($data);
		} catch (ValidateException $e) {
		    exit(json_encode(array(
		        'code' 	=> 1,
		        'msg'	=> $e->getError()
		    )));
		}
		if (empty($id)){
			$data['telearr'] = array();
            $data['video'] = input('new_video/s', "");
			$demand = DemandModel::create($data);
		}else{

            $old_video = input('old_video/s','');
            $new_video = input('new_video/s','');
            if(!empty($new_video)){
                $data['video'] = $new_video;
            }else{
                if(!empty($old_video))
                {
                    $data['video'] = $old_video;
                }
            }

			$demand = DemandModel::find($id);
			DemandLogModel::update([ 'workerid' => input('workerid/d', 0) ],['demandid'=>$demand->id]); 
			$demand->save($data);
		}
		exit(json_encode(array(
			'code' 	=> 0
		)));
	}
	
	public function fieldDemand()
	{
		$id = input('id/d',0); 
		$demand = DemandModel::findOrEmpty($id);
		if ($demand->isEmpty()){
			exit(json_encode(array(
				'code' 	=> 1,
				'msg'	=> "信息不存在"
			)));
		}else{
			$demand->save([
				input('field/s')  => input('value')
			]);
		}
		exit(json_encode(array(
			'code' 	=> 0
		)));
	}
	
	public function delDemand()
	{
		$idarr = input('idarr/a');
		DemandLogModel::whereIn('demandid',$idarr)->delete();
        $result = Db::name('demand')->whereIn('id',$idarr)->update(['deletetime'=>time()]);
		if ($result){
			exit(json_encode(array(
				'code' 	=> 0,
				'msg'	=> ""
			)));
		}
		exit(json_encode(array(
			'code' 	=> 1,
			'msg'	=> "删除失败，请稍后重试"
		)));
	}
	
	public function listDemand()
	{
		$limit = input('limit/d',20);
		$page = input('page/d',1);
		$map = array();
		$keywords = input('keywords/s', "");
		if (!empty($keywords)){
			$map[] =['title', 'like', '%'.$keywords.'%'];
		}
		$workerid = input('workerid/d', 0);
		if (!empty($workerid)){
			$map[] = ['workerid', '=', $workerid];
		}
		$cateid = input('cateid/d', 0);
		if (!empty($cateid)){
			$map[] = ['cateid', '=', $cateid];
		}
		$wtype = input('wtype/d');
		if (!empty($wtype)){
			$map[] = ['wtype', '=', $wtype];
		}
		$ftype = input('ftype/d');
		if (!empty($ftype)){
			$map[] = ['ftype', '=', $ftype];
		}
		$status = input('status/d');
		if (!empty($status)){
			$map[] = ['status', '=', $status];
		}
		$isfree = input('isfree/d',0);
		if (!empty($isfree)){
			$map[] = ['isfree', '=', $isfree];
		}
		$daterange = input('daterange/s', "");
		if (!empty($daterange)){
			$daterangearr = explode("~", $daterange);
			$map[] = ['createtime', '>= time', trim($daterangearr[0])];
			$map[] = ['createtime', '<= time', trim($daterangearr[1])];
		}
		$list = DemandModel::with(['worker','demandCate'])->withCount(['demandLog'])->where($map)->order(['priority'=>'desc','id'=>'desc',])->limit($limit)->page($page)->append(['wtype_text','ftype_text','status_text','isfree_text'])->select();
		$count = DemandModel::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 cateList()
	{
		return view('demand/catelist');
	}
	
	public function cateForm()
	{
		$id = input('id/d', 0);
		$cate = DemandCateModel::findOrEmpty($id);
		return view('demand/cateform',[
			'cate'			=> $cate
		]);
	}
	
	public function listCate()
	{
		$limit = input('limit');
		$page = input('page');
		$list = DemandCateModel::order(['priority'=>'desc','id'=>'desc'])->limit($limit)->page($page)->select();
		$count = DemandCateModel::count();
		if ($count==0){
			exit(json_encode(array(
				'code' 	=> 1,
				'msg'	=> "未查询到数据"
			)));
		}
		exit(json_encode(array(
			'code' 	=> 0,
			'msg'	=> "",
			'count' => $count,
			'data'  => $list
		)));
	}	
	
	public function fieldCate()
	{
		$id = input('id/d', 0);
		$info = DemandCateModel::find($id);
		$info->save([
			input('field/s')  => input('value/s', "")
		]);
		exit(json_encode(array(
			'code' 	=> 0
		)));
	}
	
	public function editCate()
	{
		$id = input('id/d');
		if (empty($id)){
			$cate = DemandCateModel::create([
				'title' 	=> input('title/s', ""),
				'priority'  => input('priority/d', 0)
			]);
		}else{
			$cate = DemandCateModel::find($id);
			$cate->save([
				'title' 	=> input('title/s', ""),
				'priority'  => input('priority/d', 0)
			]);
		}
		exit(json_encode(array(
			'code' 	=> 0
		)));
	}
	
	public function delCate()
	{
		$id = input('id/d');
		$cate = DemandCateModel::where('id',$id)->select();
		$result = $cate->delete();
		if ($result){
			exit(json_encode(array(
				'code' 	=> 0,
				'msg'	=> ""
			)));
		}
		exit(json_encode(array(
			'code' 	=> 1,
			'msg'	=> "删除失败，请稍后重试"
		)));
	}
		

}
