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

use app\BaseController;
use app\model\admin\AdverPage;
use app\model\admin\Advert;
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 Adver extends  BaseController{

    /**
     * 分类列表数据
     */
    public function page(Request $request){
        [$cate_name,$pid,$is_show] = UtilService::getMore([
            ['cate_name',''],
            ['pid','0'],
            ['is_show','0']
        ],$request,true);
        $menuMenu = new AdverPage();
        $menus = $menuMenu->getArMenu($cate_name,$pid,$is_show);
        return app('json')->success($menus);
    }

    /**
     * 获取树级分类
     */
    public function page_tree() {
        $menuAr = (new AdverPage())->order("sort","desc")->select()->toArray();
        $data = sort_list_tier($menuAr, '顶级', 'pid', 'id');
        return app('json')->success($data);
    }
    public function list(Request $request) {
        $pageSize = 50;
        [$page,$page_id,$title] =  UtilService::getMore(
            [
                ['page',1],
                ['page_id','0'],
                ['title','']
            ],$request,true
        );
        $sassid = $request->site['sassid'];
        $where = compact('page_id','title','sassid');
        list($pageCount,$data) = (new Advert)
            ->getList($page,$where,$pageSize,'id desc');
        return app('json')->success([
            'list'      => $data,
            'pageCount' => $pageCount,
            'pageSize'  => $pageSize,
            'page'      => $page
        ]);
    }
    /**
     * 获取详情栏目数据
     */
    public function info(Request $request){
        [$id] = UtilService::getMore([
            ['id',0,'empty','参数错误']
        ],$request,true);
        $data = (new Advert())->find(compact('id'))->toArray();
        return app('json')->success($data);
    }

    /**
     * 保存数据
     */
    public function save(Request $request){
        $post = UtilService::getMore([
            ['title','','empty','商品分类'],
            ['page_id','0'],
            ['sort','0'],
            ['id','0'],
            ['text',''],
            ['url',''],
            ['img',''],
            ['data',''],
            ['is_show','0']
        ],$request);
        $post['sassid'] = $request->site["sassid"];
        (new Advert())->savePage($post);
        return app('json')->success("数据保存成功");
    }

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

}