<?php

namespace app\admin\controller;

use app\admin\AdminBaseController;
use app\admin\validate\RecruitNewsValidate;
use app\common\model\RecruitAppointInfoModel;
use app\common\model\RecruitModel;
use app\common\model\RecruitNewsModel;
use app\common\model\RecruitPostModel;
use think\exception\ValidateException;

class Recruit extends AdminBaseController
{

    /**
     * 列表
     */
    public function index()
    {
        return view('', [
            'status_list' => RecruitModel::STATUS_INT,
        ]);
    }

    public function listRecruit()
    {
        $map   = $this->dealEqualInput(['status'], $this->dealLikeInput(['name']));
        $list  = RecruitModel::where($map)
            ->order(['priority' => 'desc', 'update_time' => 'desc'])
            ->limit(input('limit'))
            ->page(input('page'))
            ->append(['status_text'])
            ->select();
        $count = RecruitModel::where($map)->count();
        if ($count == 0) {
            ajax_return(1, '未查询到数据');
        }
        list_return($list, $count);
    }

    public function delRecruit()
    {
        $id_arr = input('id_arr/a');
        RecruitModel::destroy($id_arr);
        ajax_return();
    }

    /**
     * 编辑
     */
    public function recruitForm()
    {
        $id   = input('id/d', 0);
        $info = RecruitModel::find($id);

        return view('', [
            'info'                      => $info,
            'forms'                     => RecruitModel::FORMS,
            'status_list'               => RecruitModel::STATUS_INT,
            'status_step_list'          => RecruitModel::STATUS_STEP,
            'status_ticket_type_list'   => RecruitModel::STATUS_TICKET_TYPE,
            'status_ticket_status_list' => RecruitModel::STATUS_TICKET_STATUS,
            'status_score_list'         => RecruitModel::STATUS_SCORE,
        ]);
    }

    public function editRecruit()
    {
        $data = input('post.');
        if (empty($data['forms'])) {
            ajax_return(1, '请选择表单模块');
        }
        if (empty($data['introduction'])) {
            ajax_return(1, '请输入招考正文');
        }
        if ($data['pen_confirm'] == RecruitModel::STATUS_YES) {
            if (empty($data['pen_confirm_start']) || empty($data['pen_confirm_end'])) {
                ajax_return(1, '请选择确认笔试时间');
            }
        }

        $data['step'] = array_values($data['step']);
        $data         = del_empty($data);
        if (empty($data['id'])) {
            $data['name_en'] = uniqid();
            RecruitModel::create($data);
        } else {
            RecruitModel::update($data, ['id' => $data['id']]);
        }

        ajax_return();
    }

    public function postList()
    {
        $id = input('id/d', 0);;
        $info = RecruitModel::find($id);
        if (empty($info)) {
            return '该招考不存在';
        }

        return view('', [
            'id' => $id,
        ]);
    }

    public function listPost()
    {
        $id = input('id/d', 0);;
        $map   = [
            ['recruit_id', '=', $id],
        ];
        $list  = RecruitPostModel::where($map)
            ->limit(input('limit'))
            ->page(input('page'))
            ->select();
        $count = RecruitPostModel::where($map)->count();
        if ($count == 0) {
            ajax_return(1, '未查询到数据');
        }
        list_return($list, $count);
    }

    /**
     * 编辑
     */
    public function postForm()
    {
        $limit_condition = [
            ['text' => '不限', 'value' => ''],
            ['text' => '专科及以上', 'value' => '专科'],
            ['text' => '本科及以上', 'value' => '本科'],
            ['text' => '硕士及以上', 'value' => '硕士'],
            ['text' => '博士及以上', 'value' => '博士'],
        ];
        $limit_degree    = [
            ['text' => '不限', 'value' => ''],
            ['text' => '学士学位及以上', 'value' => '学士学位'],
            ['text' => '硕士学位及以上', 'value' => '硕士学位'],
            ['text' => '博士学位及以上', 'value' => '博士学位'],
        ];
        $data            = [
            'limit_condition' => $limit_condition,
            'limit_degree'    => $limit_degree,
        ];

        $recruit_id = input('recruit_id/d', 0);
        if (!empty($recruit_id)) {
            $data['info'] = [
                'recruit_id' => $recruit_id,
                'limit'      => [],
                'special'    => [],
            ];
            return view('', $data);
        }

        $id              = input('id/d', 0);
        $info            = RecruitPostModel::where('id', $id)
            ->find();
        $info['limit']   = empty($info['limit']) ? [] : json_decode($info['limit'], true);
        $info['special'] = empty($info['special']) ? [] : json_decode($info['special'], true);
        $data['info']    = $info;

        return view('', $data);
    }

    public function editPost()
    {
        //基础数据
        $post = input('post.');
        $data = [
            'recruit_id' => $post['recruit_id'],
            'code'       => $post['code'],
            'name'       => $post['name'],
            'number'     => $post['number'],
        ];

        //限制条件
        $limit = [];
        foreach ($post['limit'] as $k => $v) {
            if (!empty($v)) {
                $limit[$k] = $v;
            }
        }
        $data['limit'] = json_encode($limit);

        //加分项目
        $data['special'] = '[]';
        if (!empty($post['special_list'])) {
            $data['special'] = json_encode(['multi' => $post['special']['multi'], 'list' => $post['special_list']]);
        }

        //数据库操作
        if (empty($post['id'])) {
            RecruitPostModel::create($data);
        } else {
            RecruitPostModel::update($data, ['id' => $post['id']]);
        }

        ajax_return();
    }

    public function delPost()
    {
        $id = input('id/d', 0);

        $check = RecruitAppointInfoModel::whereIn('post_id', $id)->find();
        if (!empty($check)) {
            ajax_return(1, "删除的岗位已有人报名，无法删除");
        }

        RecruitPostModel::destroy($id);

        ajax_return();
    }

    /**
     * 列表
     */
    public function news()
    {
        return view('', [
            'status_list' => RecruitNewsModel::STATUS,
        ]);
    }

    public function newsForm()
    {
        $id   = input('id/d', 0);
        $info = RecruitNewsModel::with('recruit')->find($id);

        return view('', [
            'info'        => $info,
            'status_list' => RecruitNewsModel::STATUS,
        ]);
    }

    public function listNews()
    {
        $map = $this->dealEqualInput(['status', 'recruit_id'], $this->dealLikeInput(['title']));

        $list  = RecruitNewsModel::with('recruit')
            ->where($map)
            ->order(['priority' => 'desc', 'id' => 'desc'])
            ->limit(input('limit'))
            ->page(input('page'))
            ->append(['status_text'])
            ->select();
        $count = RecruitNewsModel::where($map)->count();

        if ($count == 0) {
            ajax_return(1, '未查询到数据');
        }
        list_return($list, $count);
    }

    public function delNews()
    {
        $id_arr = input('id_arr/a');
        RecruitNewsModel::destroy($id_arr);
        ajax_return();
    }

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

        if (empty($data['id'])) {
            RecruitNewsModel::create($data);
        } else {
            RecruitNewsModel::update($data, ['id' => $data['id']]);
        }

        ajax_return();
    }
}