<?php

namespace app\admin\controller;

use app\admin\AdminBaseController;
use app\common\model\NoticeModel;
use app\admin\validate\NoticeValidate;
use think\exception\ValidateException;

class Notice extends AdminBaseController
{

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

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

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

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

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

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

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

        ajax_return();
    }
}