<?php
namespace app\admin\controller;

use app\admin\BaseController;
use app\common\model\Worker as WorkerModel;
use app\common\model\Supply as SupplyModel;

use app\common\validate\Supply as SupplyValidate;
use think\exception\ValidateException;
use think\facade\Db;

class Supply extends BaseController
{
	
	// 企业招聘
	public function supplyList()
	{
		$workerlist = WorkerModel::order(['id'=>'desc'])->select();
		return view('supply/supplylist',[
			'workerlist'	=> $workerlist
		]);
	}
	
	public function supplyForm()
	{
		$id = input('id/d, 0');
		$supply = SupplyModel::findOrEmpty($id);
		$workerlist = WorkerModel::order(['id'=>'desc'])->select();
		$provincelist = array(
			['value'=>"全国", 'title'=>"全国"],
			['value'=>"北京市", 'title'=>"北京市"],
			['value'=>"天津市", 'title'=>"天津市"],
			['value'=>"河北省", 'title'=>"河北省"],
			['value'=>"山西省", 'title'=>"山西省"],
			['value'=>"内蒙古自治区", 'title'=>"内蒙古自治区"],
			['value'=>"辽宁省", 'title'=>"辽宁省"],
			['value'=>"吉林省", 'title'=>"吉林省"],
			['value'=>"黑龙江省", 'title'=>"黑龙江省"],
			['value'=>"上海市", 'title'=>"上海市"],
			['value'=>"江苏省", 'title'=>"江苏省"],
			['value'=>"浙江省", 'title'=>"浙江省"],
			['value'=>"安徽省", 'title'=>"安徽省"],
			['value'=>"福建省", 'title'=>"福建省"],
			['value'=>"江西省", 'title'=>"江西省"],
			['value'=>"山东省", 'title'=>"山东省"],
			['value'=>"河南省", 'title'=>"河南省"],
			['value'=>"湖北省", 'title'=>"湖北省"],
			['value'=>"湖南省", 'title'=>"湖南省"],
			['value'=>"广东省", 'title'=>"广东省"],
			['value'=>"广西壮族自治区", 'title'=>"广西壮族自治区"],
			['value'=>"海南省", 'title'=>"海南省"],
			['value'=>"重庆市", 'title'=>"重庆市"],
			['value'=>"四川省", 'title'=>"四川省"],
			['value'=>"贵州省", 'title'=>"贵州省"],
			['value'=>"云南省", 'title'=>"云南省"],
			['value'=>"西藏自治区", 'title'=>"西藏自治区"],
			['value'=>"陕西省", 'title'=>"陕西省"],
			['value'=>"甘肃省", 'title'=>"甘肃省"],
			['value'=>"青海省", 'title'=>"青海省"],
			['value'=>"宁夏回族自治区", 'title'=>"宁夏回族自治区"],
			['value'=>"新疆维吾尔自治区", 'title'=>"新疆维吾尔自治区"],
			['value'=>"台湾省", 'title'=>"台湾省"],
			['value'=>"香港特别行政区", 'title'=>"香港特别行政区"],
			['value'=>"澳门特别行政区", 'title'=>"澳门特别行政区"]
		);
		return view('supply/supplyform',[
			'provincelist'	=> $provincelist,
			'workerlist'	=> $workerlist,
			'supply'		=> $supply
		]);
	}
	
	public function editSupply()
	{
		$id = input('id/d', 0);
		$descity = input('descity/a', []);
		if (in_array('全国', $descity)){
			$descity = ['全国'];
		}
		$data = [
			'workerid'			=> input('workerid/d', 0),
			'mnumber'			=> input('mnumber/d', 0),
			'wnumber'			=> input('wnumber/d', 0),
			'agegroup'			=> input('agegroup/s', ""),
			'province'			=> input('province/s', ""),
			'city'				=> input('city/s', ""),
			'district'			=> input('district/s', ""),
			'descity'			=> implode(",", $descity),
			'candate'			=> input('candate/s', ""),
			'telephone'			=> input('telephone/s', ""),
			'remark'			=> input('remark/s', ""),
			'status'			=> input('status/d', 1),
			'priority'			=> input('priority/d', 0),
			'updatetime'		=> input('updatetime/s', ""),
			'createtime'		=> input('createtime/s', ""),
			'volume'			=> input('volume/d', 0)
		];
		try {
		    validate(SupplyValidate::class)->check($data);
		} catch (ValidateException $e) {
		    exit(json_encode(array(
		        'code' 	=> 1,
		        'msg'	=> $e->getError()
		    )));
		}
		if (empty($id)){
			$data['telearr'] = array();
			$supply = SupplyModel::create($data);
		}else{
			$supply = SupplyModel::find($id);
			$supply->save($data);
		}
		exit(json_encode(array(
			'code' 	=> 0
		)));
	}
	
	public function fieldSupply()
	{
		$id = input('id/d',0); 
		$supply = SupplyModel::findOrEmpty($id);
		$supply->save([
			input('field/s')  => input('value/s', "")
		]);
		exit(json_encode(array(
			'code' 	=> 0
		)));
	}
	
	public function delSupply()
	{
		$idarr = input('idarr/a');
        $result = Db::name('supply')->whereIn('id',$idarr)->update(['deletetime'=>time()]);
		if ($result){
			exit(json_encode(array(
				'code' 	=> 0,
				'msg'	=> ""
			)));
		}
		exit(json_encode(array(
			'code' 	=> 1,
			'msg'	=> "删除失败，请稍后重试"
		)));
	}
	
	public function listSupply()
	{
		$limit = input('limit/d',20);
		$page = input('page/d',1);
		$map = array();
		$keywords = input('keywords/s', "");
		if (!empty($keywords)){
			$map[] =['province|city|district|descity', 'like', '%'.$keywords.'%'];
		}
		$workerid = input('workerid/d', 0);
		if (!empty($workerid)){
			$map[] = ['workerid', '=', $workerid];
		}
		$status = input('status/d');
		if (!empty($status)){
			$map[] = ['status', '=', $status];
		}
		$daterange = input('daterange/s', "");
		if (!empty($daterange)){
			$daterangearr = explode("~", $daterange);
			$map[] = ['createtime', '>= time', trim($daterangearr[0])];
			$map[] = ['createtime', '<= time', trim($daterangearr[1])];
		}
		$list = SupplyModel::with(['worker'])->where($map)->order(['priority'=>'desc','id'=>'desc',])->limit($limit)->page($page)->append(['status_text'])->select();
		$count = SupplyModel::where($map)->count();
		if ($count==0){
			exit(json_encode(array(
				'code' 	=> 1,
				'msg'	=> "未查询到数据"
			)));
		}
		exit(json_encode(array(
			'code' 	=> 0,
			'msg'	=> "",
			'count' => $count,
			'data'  => $list
		)));
	}
	
	
}
	