<?php

namespace app\admin\controller;

use app\admin\AdminBaseController;
use app\common\model\PreviousModel;
use app\common\validate\PreviousValidate;
use think\exception\ValidateException;

class Previous extends AdminBaseController
{

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

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

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

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

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

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

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

        ajax_return();
    }
}