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

use app\BaseController;
use app\model\api\AdverPage;
use app\model\api\Label as LabelModel;
use app\model\api\GoodsCate;
use app\Request;
use library\services\UtilService;

// +----------------------------------------------------------------------
// | [ WE CAN DO IT MORE SIMPLE  ]
// +----------------------------------------------------------------------
// | Copyright (c) 2018-2020 rights reserved.
// +----------------------------------------------------------------------
// | [ 标签管理 ]
// +----------------------------------------------------------------------
// | Date: 2020-09-06 21:53
// +----------------------------------------------------------------------

class Label extends  BaseController{
    
    /**
     * 标签列表
     * @param Request $request
     * @return type
     */
    public function list(Request $request) {
        $pageSize = 50;
        $post =  UtilService::getMore([
                ['page',1],
                ['name',''],
                ['is_show',''],
        ],$request);
        $where=[];
        if(in_array((string)$post["is_show"],['0','1'])){
            $where[]=["is_show","=",(int)$post["is_show"]];
        }
        if(!empty($post["name"])){
            $where[]=["name","like","%{$post["name"]}%"];
        }
        $data = (new LabelModel)
                ->where($where)
                ->page((int)$post["page"], (int)$pageSize)
                ->order("seq","desc")
                ->order("id","desc")
                ->select()
                ->toArray();
        $pageCount = (new LabelModel)->where($where)->count();
        $data = empty($data)?[]:$data;
        return app('json')->success([
            'list'      => $data,
            'pageCount' => $pageCount,
            'pageSize'  => $pageSize,
            'page'      => $post["page"]
        ]);
    }

    /**
     * 标签添加
     */
    public function add(Request $request){
        $post = UtilService::getMore([
            ['id','0'],
            ['name','','empty','请输入标签名称'],
            ['seq','0'],
            ['type','activity'],
            ['is_show','1']
        ],$request);
        $id = $post["id"];
        unset($post["id"]);
        $r=0;
        if(empty($id) || $id=="0"){
            $r = (new LabelModel)->insert($post);
        }else{
            $r = (new LabelModel)->where("id",$id)->update($post);
        }
        
        if($r){
            return app('json')->success("数据保存成功");
        }else{
            return app('json')->fail("数据保存失败");
        }
    }

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

}