<?php

namespace app\home\controller;

use app\common\model\RecruitNewsModel;
use app\home\HomeBaseController;

class News extends HomeBaseController
{
    protected function init()
    {
        $this->tab = 'news';
    }

    public function index()
    {
        $keyword = input('keyword', '');

        $limit = 10;
        $where = [
            ['status', '=', RecruitNewsModel::STATUS_PUBLISH],
            ['title', 'like', "%$keyword%"],
        ];
        $total = RecruitNewsModel::where($where)
            ->count();
        return view('', [
            'keyword' => $keyword,
            'total'   => $total,
            'limit'   => $limit,
        ]);
    }

    public function list()
    {
        $page    = input('page', 1);
        $limit   = input('limit', 10);
        $keyword = input('keyword', '');

        $where = [
            ['status', '=', RecruitNewsModel::STATUS_PUBLISH],
        ];
        if (!empty($keyword)) {
            $where[] = ['title', 'like', "%$keyword%"];
        }
        $list  = RecruitNewsModel::with(['recruit'])
            ->where($where)
            ->page($page, $limit)
            ->order(['priority' => 'desc', 'update_time' => 'desc'])
            ->append(['summary', 'update_show'])
            ->select();

        ajax_success($list);
    }

    public function detail()
    {
        $id = input('id', 0);
        if (empty($id)) {
            jump('该招聘公告不存在或已经删除');
        }

        $info = RecruitNewsModel::with(['recruit'])
            ->where('status', RecruitNewsModel::STATUS_PUBLISH)
            ->find($id);
        if (empty($info)) {
            jump('该招聘公告不存在或已经删除');
        }
        $info->volume++;
        $info->save();

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