<?php

namespace App\Http\Controllers\Manage\Notice;

use App\Http\Controllers\Manage\Notice\Transformers\NoticeTypeTransformer;
use App\Modules\Notice\Services\NoticeService;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class NoticeTypesController extends Controller
{
    /**
     * @apiDefine Notice 公告
     */

    /**
     * @apiVersion 1.0.0
     * @apiDescription 获取公告分类列表
     * @api {GET} noticeTypes 获取公告分类列表
     * @apiGroup Notice
     * @apiName noticeTypes
     * @apiSuccess {Number}  id 公告分类id.
     * @apiSuccess {String}  name 分类名称.
     * @apiSuccessExample {json} Success-Response:
     *
     *     {
     *         "code": 0,
     *         "msg": "",
     *         "data":
     *         [
     *             {
     *                 "id": 121,
     *                 "name": "教程",
     *             }
     *         ]
     *     }
     */
    function get_list(Request $request)
    {
        $notic_types = NoticeService::getAllNoticeTypeList();

        return response()->collection(new NoticeTypeTransformer(),$notic_types);
    }

    /**
     * @apiVersion 1.0.0
     * @apiDescription 添加公告分类
     * @api {POST} addNoticeType 添加公告分类
     * @apiGroup Notice
     * @apiName addNoticeType
     * @apiParam   {String}  name 公告分类名称
     * @apiSuccessExample {json} Success-Response:
     *
     *     {
     *         "code": 0,
     *         "msg": "",
     *         "data":{
     *              "id":1,
     *              "name":"通告"
     *          }
     *     }
     */
    function add_type(Request $request)
    {
        $data = $request->all();
        $name = $request->has('name') ? $request->input('name') : '';
        if(empty($name)) {
            return response()->error("PARAM_EMPTY");
        }
        if(NoticeService::existNoticeTypeName($name)) {
            return response()->error("NAME_EXIST");
        }

        $notice_types = NoticeService::addNoticeType($data);
        $id = $notice_types['id'];
        $name = $notice_types['name'];
        return response()->success(compact('id', 'name'));
    }


    /**
     * @apiVersion 1.0.0
     * @apiDescription 删除公告分类
     * @api {POST} rmNoticeType 删除公告分类
     * @apiGroup Notice
     * @apiName rmNoticeType
     * @apiParam   {Number}  id 公告分类ID
     * @apiSuccessExample {json} Success-Response:
     *
     *     {
     *         "code": 0,
     *         "msg": "",
     *         "data":{}
     *     }
     */
    function rm_type(Request $request) {
        $id = $request->has('id') ? $request->input('id') : '';
        if(!is_numeric($id)) {
            return response()->error("PARAM_ERROR");
        }
        NoticeService::rmNoticeType($id);

        return response()->success();
    }
    
}