<?php

namespace app\admin\controller;

use app\admin\AdminBaseController;
use app\common\model\NavigationModel;
use app\common\validate\NavigationValidate;
use think\exception\ValidateException;

class Navigation extends AdminBaseController
{

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

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

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

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

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

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

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

        ajax_return();
    }
}