<?php

namespace app\mobile\controller;

use app\common\model\Agent as AgentModel;
use app\common\model\Config;
use app\common\model\OutRecruit as OutRecruitModel;
use app\common\model\Broker as BrokerModel;
use app\common\model\OutRecruitReport;
use app\common\model\OutResume as OutResumeModel;
use app\mobile\MobileBaseController;
use app\mobile\validate\WorkerReportValidate;
use think\exception\ValidateException;
use think\facade\Db;

class Worker extends MobileBaseController
{
    /**
     * 招聘信息
     */
    public function index()
    {
        $agent_id = session('mobile.agent_id');
        if (empty($agent_id)) {
            $agent_id = input('id/d', 0);
            if (empty($agent_id)) {
                jump('门店不存在');
            }

            session('mobile.agent_id', $agent_id);
        }

        $agent = AgentModel::where([
            ['id', '=', $agent_id],
            ['type', '=', 2],
            ['status', '=', 1],
        ])->find();

        if (empty($agent)) {
            session('mobile.agent_id', null);
            jump('门店不存在');
        }

        return view('worker/index', [
            'agent' => $agent,
        ]);
    }

    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 recruitDetail()
    {
        $this->get_agent();

        $id = input('id/d', 0);
        if (empty($id)) {
            jump('招聘不存在');
        }

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

        return view('worker/recruit_detail', ['info' => $info]);
    }

    /**
     * 报备
     */
    public function report()
    {
        $id = input('id/d', 0);
        if (empty($id)) {
            jump('招聘不存在');
        }

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

        $agent  = $this->get_agent();
        $broker = BrokerModel::field(['title as text', 'id as value'])->where([
            ['agentid', '=', $agent['id']],
            ['status', '=', 1],
            ['type', '=', 3],
        ])->select();

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

    public function reportPost()
    {
        $data  = input('post.');
        $agent = $this->get_agent();
        try {
            validate(WorkerReportValidate::class)->check($data);
        } catch (ValidateException $e) {
            ajax_return(1, $e->getError());
        }

        //简历
        $resume = OutResumeModel::with(['broker'])->where('mobile', $data['mobile'])->find();
        $resume && ajax_return(1, '您已有经纪人，请找经纪人报备！经纪人信息' . $resume['broker']['title'] . '(<a href="tel:' . $resume['broker']['mobile'] . '">' . $resume['broker']['mobile'] . '</a>)');
        //报备记录
        $report = OutRecruitReport::with(['broker'])->where('mobile', $data['mobile'])->where('recruit_id', $data['recruit_id'])->find();
        $report && ajax_return(1, '您已有经纪人，请找经纪人报备！经纪人信息' . $report['broker']['title'] . '(<a href="tel:' . $report['broker']['mobile'] . '">' . $report['broker']['mobile'] . '</a>)');

        //添加报备信息
        if (empty($data['brokerid'])) {
            $broker = BrokerModel::where([
                ['agentid', '=', $agent['id']],
                ['status', '=', 1],
                ['type', '=', 3],
            ])->order(Db::raw('RAND()'))->find();

            if (empty($broker)) {
                $data['brokerid'] = Config::getConfigValue('default_broker');
                $broker = BrokerModel::find($data['brokerid']);
            } else {
                $data['brokerid'] = $broker['id'];
            }
        } else {
            $broker = BrokerModel::find($data['brokerid']);
        }

        $data['workerid']   = $broker['workerid'];
        $data['agentid']    = $broker['agentid'];
        $data['createtime'] = time();
        OutRecruitReport::create($data);

        ajax_return(0, '报名成功，经纪人：' . $broker['title'] . '(<a href="tel:' . $broker['mobile'] . '">' . $broker['mobile'] . '</a>)', '/mobile/worker/index');
    }

    /**
     * 经纪人
     */
    public function broker()
    {
        return view('worker/broker');
    }

    public function listBroker()
    {
        $agent = $this->get_agent();

        $list = BrokerModel::where('agentid', $agent['id'])
            ->where('status', 1)
            ->limit(input('limit', 10))
            ->page(input('page', 1))
            ->select();

        ajax_success($list);
    }

    /**
     * 报备查询
     */
    public function reportFind()
    {
        $mobile = input('mobile');
        if (empty($mobile)) {
            return view('worker/report_find');
        }

        $report = OutRecruitReport::where('mobile', $mobile)->find();
        if (empty($report)) {
            ajax_return(1, '暂无信息');
        }

        $broker = BrokerModel::find($report['brokerid']);
        ajax_success($broker);
    }

    public function listReport()
    {
        $map = $this->dealEqualInput(['mobile']);

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

        ajax_success($list);
    }

    private function get_agent()
    {
        $agent_id = session('mobile.agent_id');
        if (empty($agent_id)) {
            jump('门店不存在');
        }

        $agent = AgentModel::where([
            ['id', '=', $agent_id],
            ['type', '=', 2],
            ['status', '=', 1],
        ])->find();
        if (empty($agent)) {
            session('mobile.agent_id', null);
            jump('门店不存在');
        }

        return $agent;
    }
}
