<?php

namespace app\admin\controller;

use app\admin\AdminBaseController;
use app\common\model\ReservedModel;
use app\common\validate\ReservedValidate;
use think\exception\ValidateException;

class Reserved extends AdminBaseController
{

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

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

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

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

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

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

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

        ajax_return();
    }
}