<?php

namespace app\mobile\controller;

use app\common\model\OutRecruit as OutRecruitModel;
use app\common\model\OutRecruitReport;
use app\mobile\MobileBaseController;
use app\mobile\validate\RecruitValidate;
use think\App;
use think\exception\ValidateException;
use think\facade\View;

class Recruit extends MobileBaseController
{
    private $_broker = null;

    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->_broker = get_broker();
        View::assign('broker', $this->_broker);
    }

    /**
     * 招聘信息
     */
    public function index()
    {
        return view('recruit/index');
    }

    /**
     * 招聘信息列表
     */
    public function listRecruit()
    {
        $map   = $this->dealLikeInput(['keyword' => 'title|company_name']);
        $map[] = ['status', '=', 1];

        $list = OutRecruitModel::where($map)
            ->order(['priority' => 'desc', 'id' => 'desc'])
            ->limit(input('limit', 10))
            ->page(input('page', 1))
            ->select();

        ajax_success($list);
    }

    /**
     * 详情
     */
    public function detail()
    {
        $id = input('id/d', 0);

        $info = OutRecruitModel::find($id);
        if (empty($info) || $info['status'] != 1) {
            jump('该信息不存在或已下架');
        }

        return view('recruit/detail', ['info' => $info]);
    }

    /**
     * 报备
     */
    public function report()
    {
        $id = input('id/d', 0);

        $info = OutRecruitModel::find($id);
        if (empty($info) || $info['status'] != 1) {
            jump('该信息不存在或已下架');
        }

        return view('recruit/report', ['info' => $info]);
    }

    /**
     * 报备提交
     */
    public function reportPost()
    {
        $data = input('post.');
        try {
            validate(RecruitValidate::class)->check($data);
        } catch (ValidateException $e) {
            ajax_return(1, $e->getError());
        }

        $mobile_check = OutRecruitReport::where('mobile', $data['mobile'])->where('recruit_id', $data['recruit_id'])->find();
        $mobile_check && ajax_return(1, '该手机号已报备，请勿重复报备！');

        $data['workerid']   = $this->_broker['workerid'];
        $data['agentid']    = $this->_broker['agentid'];
        $data['brokerid']   = $this->_broker['id'];
        $data['createtime'] = time();

        OutRecruitReport::create($data);

        ajax_return();
    }
}
