<?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 postForm()
    {
        $id   = input('id/d, 0');
        $info = RecruitModel::find($id);
        if (empty($info)) {
            return '该招考不存在';
        }

        $list = RecruitPostModel::field(['id', 'code', 'name', 'number', 'limit', 'special'])
            ->where('recruit_id', $id)
            ->select();
        if ($list->isEmpty()) {
            $list = json_encode([['id' => 0, 'code' => '', 'name' => '', 'number' => 1, 'limit' => '{}', 'special' => '{}']]);
        }

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

    public function editPost()
    {
        $data       = input('post.');
        $recruit_id = $data['recruit_id'];
        $list       = $data['list'];
        if (empty($recruit_id)) {
            ajax_return(1, '请选择招考场次');
        }
        if (empty($list)) {
            ajax_return(1, '请添加岗位');
        }

        //数据分离
        $insert     = [];
        $update     = [];
        $ids        = RecruitPostModel::where('recruit_id', $recruit_id)->column('id');
        $data_ids   = array_column($list, 'id');
        $delete_ids = array_diff($ids, $data_ids);
        $check      = RecruitAppointInfoModel::whereIn('post_id', $delete_ids)->find();
        if (!empty($check)) {
            ajax_return(1, "删除的岗位已有人报名，无法删除，请刷新页面重新录入");
        }
        foreach ($list as $v) {
            if (empty($v['id'])) {
                $insert[] = $v;
            } else {
                if (!in_array($v['id'], $delete_ids))
                    $update[] = $v;
            }
        }

        if (!empty($insert)) {
            foreach ($insert as $v) {
                $v['recruit_id'] = $recruit_id;
                RecruitPostModel::create($v);
            }
        }
        if (!empty($update)) {
            foreach ($update as $v) {
                RecruitPostModel::update($v);
            }
        }
        if (!empty($delete_ids)) {
            RecruitPostModel::destroy(array_values($delete_ids));
        }

        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)->toArray();

        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();
    }
}