<?php
namespace app\worker\controller;

use app\worker\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\Session;
use think\facade\Db;
use think\facade\Request;

class Supply extends BaseController
{
	
	// 企业招聘
	public function supplyList()
	{
        $access_worker = Session::get('access_worker');
        $is_released = 1;
        if($access_worker['status'] == 1)
        {
            $is_released = is_released($access_worker['id']);
        }

		return view('supply/supplylist',[
            'is_released' => $is_released['code']
        ]);
	}
	
	public function supplyForm()
	{
		$id = input('id/d, 0');
		$supply = SupplyModel::findOrEmpty($id);
        $access_worker = Session::get('access_worker');
		$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,
			'title'	=> $access_worker['title'],
			'supply'		=> $supply
		]);
	}
	
	public function editSupply()
	{
		$id = input('id/d', 0);
        $workerid = $this->access_worker['id'];
		$descity = input('descity/a', []);
		if (in_array('全国', $descity)){
			$descity = ['全国'];
		}
		$data = [
			'workerid'			=> $workerid,
			'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'			=> (int)2
		];
		try {
		    validate(SupplyValidate::class)->check($data);
		} catch (ValidateException $e) {
		    exit(json_encode(array(
		        'code' 	=> 1,
		        'msg'	=> $e->getError()
		    )));
		}
		if (empty($id)){

            if($this->access_worker['status'] == 1)
            {
                $is_released = is_released($workerid);

                if($is_released['code'] == 1001)
                {
                    return $is_released;

                }

            }
            $data['createtime'] = $data['updatetime']  = time();
			$data['telearr'] = array();
			$supply = SupplyModel::create($data);
		}else{
            $data['updatetime'] = time();
			$supply = SupplyModel::find($id);
			$supply->save($data);
		}
		exit(json_encode(array(
			'code' 	=> 0
		)));
	}

    public function editSupplyStatus()
    {

        $id = input('id/d', 0);
        $workerid = $this->access_worker['id'];
        if (Request::isAjax()){

            $data = [
                'workerid'			=> $workerid,
                'status'			=> input('status/d', 0),
            ];

            if (empty($id)){
                $data['createtime'] = $data['updatetime'] = time();
                $data['telearr'] = array();
                $supply = SupplyModel::create($data);
            }else{
                $data['updatetime'] = time();
                Db::name('supply')->where(['workerid'=>$workerid,'id'=>$id])->update($data);
            }
            exit(json_encode(array(
                'code' 	=> 0
            )));

        }else{

            $data = Db::name('supply')
                    ->field('a.id,a.status,b.title')
                    ->alias('a')
                    ->where('a.workerid',$workerid)
                    ->where('a.id',$id)
                    ->join('worker b','a.workerid = b.id','LEFT')
                    ->find();

            if($data['status'] == 2){
                $data['status_text'] = '待审核';
            }elseif($data['status'] == 3){
                $data['status_text'] = '已上架';
            }elseif($data['status'] == 4){
                $data['status_text'] = '已停招';
            }elseif($data['status'] == 5){
                $data['status_text'] = '已下架';
            }


            return view('supply/editsupplystatus',[
                'data' => $data
            ]);


        }

    }
	
	public function fieldSupply()
	{
		$id = input('id/d',0); 
		$supply = SupplyModel::findOrEmpty($id);
		$supply->save([
			 'telephone'=> input('value/s', "")
		]);
		exit(json_encode(array(
			'code' 	=> 0
		)));
	}
	
	public function delSupply()
	{
		$idarr = input('idarr/a');
        $workerid = $this->access_worker['id'];
        $result = Db::name('supply')
                 ->where('workerid',$workerid)
                 ->whereIn('id',$idarr)
                 ->update(['status'=>(int)6]);
		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', "");
        $workerid = $this->access_worker['id'];
        $map[] = ['workerid', '=', $workerid];
		if (!empty($keywords)){
			$map[] =['province|city|district|descity', 'like', '%'.$keywords.'%'];
		}
		$status = input('status/d');
		if (!empty($status)){
			$map[] = ['status', '=', $status];
		}else{
            $map[] = ['status', '<', 6];
        }
		$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
		)));
	}
	
	
}
	