<?php

namespace App\Http\Controllers\Manage\Help;

use App\Http\Controllers\Manage\Help\Transformers\HelpTransformer;
use App\Modules\Help\Services\HelpService;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class HelpController extends Controller
{
    /**
     * @apiDefine Help 帮助
     */

    /**
     * @apiVersion 1.0.0
     * @apiDescription 获取帮助
     * @api {get} help/getAllHelpList 获取帮助
     * @apiGroup Help
     * @apiName getAllHelpList
     * @apiParam   {string}      status状态(enbale,disable)
     * @apiSuccess {int}         code 状态码
     * @apiSuccess {String}      msg  信息
     * @apiSuccess {object}      data 结果集
     * @apiSuccess {object}      data.id id
     * @apiSuccess {object}      data.title 标题
     * @apiSuccess {object}      data.content 内容
     * @apiSuccess {object}      data.created_at 创建时间
     * @apiSuccess {object}      data.updated_at 更新时间
     * @apiSuccess {object}      data.is_enabled 是否启用(0不启用,1启用)
     * @apiSuccessExample {json} Success-Response:
     *      HTTP/1.1 200 OK
     *     {
     *       code: 0,
     *       msg: "",
     *       data: {}
     *     }
     */
    public function getAllHelpList(Request $request){
        $status = $request->get('is_enabled');
        $status_array = ['enbale'=>1,'disable'=>0];
        $where = [];
        if($status && isset($status_array[$status])){
            $where[] = ['is_enabled'=>$status_array[$status]];
        }
        $res = HelpService::getAllHelpList($where);

        return response()->pagination(new HelpTransformer(),$res);
    }

    /**
     * @apiVersion 1.0.0
     * @apiDescription 获取帮助
     * @api {get} help/getHelp/{id} 获取帮助
     * @apiGroup Help
     * @apiName getHelp
     * @apiSuccess {int}         code 状态码
     * @apiSuccess {String}      msg  信息
     * @apiSuccess {object}      data 结果集
     * @apiSuccess {object}      data.id id
     * @apiSuccess {object}      data.title 标题
     * @apiSuccess {object}      data.content 内容
     * @apiSuccess {object}      data.created_at 创建时间
     * @apiSuccess {object}      data.updated_at 更新时间
     * @apiSuccess {object}      data.is_enabled 是否启用(0不启用,1启用)
     * @apiSuccessExample {json} Success-Response:
     *      HTTP/1.1 200 OK
     *     {
     *       code: 0,
     *       msg: "",
     *       data: {}
     *     }
     */
    public function getHelp($id){
        return response()->item(new HelpTransformer(),HelpService::getById($id));
    }

    /**
     * @apiVersion 1.0.0
     * @apiDescription 添加帮助
     * @api {post} help/createHelp 添加帮助
     * @apiGroup Help
     * @apiName createHelp
     * @apiSuccess {int}         code 状态码
     * @apiSuccess {String}      msg  信息
     * @apiSuccess {object}      data 结果集
     * @apiParam    {string}     content内容
     * @apiParam    {string}     title标题
     * @apiSuccessExample {json} Success-Response:
     *       HTTP/1.1 200 OK
     *     {
     *       code: 0,
     *       msg: "",
     *       data: {}
     *     }
     */
    public function createHelp(Request $request){
        $content = $request->post('content');
        $title = $request->post('title');
        if(empty($content) || empty($title)){
            return response()->error('PARAM_EMPTY');
        }
        $data = compact('title','content');
        if(HelpService::create($data)){
            return response()->success();
        }else{
            return response()->error('UNKNOWN_ERROR');
        }
    }


    /**
     * @apiVersion 1.0.0
     * @apiDescription 编辑帮助
     * @api {post} help/editHelp 编辑帮助
     * @apiGroup Help
     * @apiName editHelp
     * @apiParam   {string}     content 内容
     * @apiParam   {string}     title  标题
     * @apiParam   {int}        id     id
     * @apiSuccess {int}         code 状态码
     * @apiSuccess {String}      msg  信息
     * @apiSuccess {object}      data 结果集
     * @apiSuccessExample {json} Success-Response:
     *       HTTP/1.1 200 OK
     *     {
     *       code: 0,
     *       msg: "",
     *       data: {}
     *     }
     */
    public function editHelp(Request $request){
        $content = $request->post('content');
        $title = $request->post('title');
        $id = $request->post('id');
        if(empty($content) || empty($title) || empty($id)){
            return response()->error('PARAM_EMPTY');
        }

        HelpService::updateHelp($id,compact('content','title'));
        return response()->success();
    }


    /**
     * @apiVersion 1.0.0
     * @apiDescription 编辑帮助状态
     * @api {post} help/editStatus 编辑帮助状态
     * @apiGroup Help
     * @apiName editStatus
     * @apiSuccess {int}         code 状态码
     * @apiSuccess {String}      msg  信息
     * @apiSuccess {object}      data 结果集
     * @apiParam   {string}     status状态(enbale,disable)
     * @apiParam   {int}        id
     * @apiSuccessExample {json} Success-Response:
     *      HTTP/1.1 200 OK
     *     {
     *       code: 0,
     *       msg: "",
     *       data: {}
     *     }
     */
    public function editStatus(Request $request){
        $status = $request->post('status');
        $id = $request->post('id');
        $status_array = ['enbale'=>1,'disable'=>0];
        if(empty($status) || empty($id) || !isset($status_array[$status])) {
            return response()->error('PARAM_EMPTY');
        }

        HelpService::enableHelpStatus($id,$status_array[$status]);
        return response()->success();
    }

}
