<?php

namespace app\admin\controller;

use app\admin\AdminBaseController;
use app\common\model\SlideModel;
use app\common\validate\SlideValidate;
use think\exception\ValidateException;

class Slide extends AdminBaseController
{

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

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

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

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

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

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

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

        ajax_return();
    }
}