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

use app\BaseController;
use app\model\api\ServiceLabel as LabelModel;
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 ServiceLabel extends  BaseController{
    
    /**
     * 标签列表
     * @param Request $request
     * @return type
     */
    public function list(Request $request) {
        $post =  UtilService::getMore([
            ['page',1],
            ['pageSize', 50],
            ['title',''],
            ['status',''],
            
        ],$request);
        $data = (new LabelModel)->getList($post);
        return app('json')->success([
            'list'      => $data["list"],
            'pageCount' => $data["totalCount"],
            'pageSize'  => $data["pageSize"],
            'page'      => $data["page"],
        ]);
    }

    /**
     * 标签添加
     */
    public function add(Request $request){
        $post = UtilService::getMore([
            ['id','0'],
            ['title','','empty','请输入标签名称'],
            ['seq','0'],
            ['type','common'],
            ['status','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("分类删除成功");
    }

}