<?php

namespace app\home\controller;

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

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

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

        $limit = 10;
        $where = [
            ['status', '=', NoticeModel::STATUS_PUBLISH],
            ['title', 'like', "%$keyword%"],
        ];
        $total = NoticeModel::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', '=', NoticeModel::STATUS_PUBLISH],
        ];
        if (!empty($keyword)) {
            $where[] = ['title', 'like', "%$keyword%"];
        }
        $list  = NoticeModel::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 = NoticeModel::where('status', NoticeModel::STATUS_PUBLISH)->find($id);
        if (empty($info)) {
            jump('该系统通知不存在或已经删除');
        }

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