<?php
declare (strict_types = 1);
namespace app\system\controller\v1;

use app\BaseController;
use app\model\system\Category as CategoryModel;
use app\Request;
use library\services\UtilService;

// +----------------------------------------------------------------------
// | [ WE CAN DO IT MORE SIMPLE  ]
// +----------------------------------------------------------------------
// | Copyright (c) 2018-2020 rights reserved.
// +----------------------------------------------------------------------
// | Author: TABLE ME
// +----------------------------------------------------------------------
// | Date: 2020-09-06 21:53
// +----------------------------------------------------------------------

class Category extends  BaseController{

    /**
     * 基本设置
     */
    public function list(Request $request){
    [$cate_name,$pid,$is_show] = UtilService::getMore([
        ['cate_name',''],
        ['pid','0'],
        ['is_show','0']
    ],$request,true);
    $menuMenu = new CategoryModel();
    $menus = $menuMenu->getArMenu($cate_name,$pid,$is_show);
    return app('json')->success($menus);
}

    /**
     * 系统分类显示 | 关闭
     */
    public function status(Request $request) {
    [$id,$is_show] = UtilService::getMore([
        ['id','0','empty','参数错误'],
        ['is_show','0']
    ],$request,true);
    $bool = (new CategoryModel())->setStatus($id,$is_show);
    if($bool) {
        return app('json')->success('操作成功');
    } else {
        return app('json')->fail('提交失败');
    }
}


    /**
     * 获取树级分类
     */
    public function treeList() {
    $menuAr = (new CategoryModel())->order("sort","desc")->select()->toArray();
    $data = sort_list_tier($menuAr, '顶级', 'pid', 'id');
    return app('json')->success($data);
}

    /**
     * 获取详情栏目数据
     */
    public function info(Request $request){
    [$id] = UtilService::getMore([
        ['id',0,'empty','参数错误']
    ],$request,true);
    $data = (new CategoryModel())->find(compact('id'))->toArray();
    return app('json')->success($data);
}

    /**
     * 保存数据
     */
    public function save(Request $request){
    $post = UtilService::getMore([
        ['cate_name','','empty','商品分类'],
        ['pid','0'],
        ['sort','0'],
        ['pic',''],
        ['is_show','0'],
        ['id','0']
    ],$request);
    (new CategoryModel())->saveMenu($post);
    return app('json')->success("数据保存成功");
}

    /**
     * 栏目删除
     * @param Request $request
     */
    public function del(Request $request) {
    [$id] = UtilService::getMore([
        ['id',0,'empty','参数错误']
    ],$request,true);
    $bool =   (new CategoryModel())->delMenu($id);
    return app('json')->success("栏目删除成功");
}

}