<?php

namespace App\Web\Controllers;

use App\Http\Controllers\BaseController;
use App\Web\Services\HelpService;
use Illuminate\Http\Request;


class HelpController extends BaseController
{

    protected $service = null;

    /**
     * 构造函数
     * HelpController constructor.
     * @param HelpService $service
     */
    public function __construct(HelpService $service)
    {
        $this->service = $service;
    }

    /**
     * 获取帮助类型列表
     * */
    public function getHelpTypeList(Request $request){
        $params=$request->only(['page_size','page','status','keyword']);
        $ret=$this->service->getHelpTypeList($params);
        return $this->jsonResponse(
            'ok',
            $ret
        );
    }

    /**
     * 帮助类型保存
     * */
    public function saveHelpType(Request $request){
        $this->validate($request, [
            'type_name' => 'required',
        ], [
            'type_name.required' => '类型名称不能为空',
        ]);
        $params=$request->only(['id','type_name','type_img','type_img_alt','status','sort','seo_data', 'seo_id','expand_content']);
        $ret=$this->service->saveHelpType($params);
        return $this->jsonResponse(
            'ok',
            $ret
        );
    }

    /**
     * 变更帮助类型状态
     * */
    public function changeHelpTypeStatus(Request $request){
        $this->validate($request, [
            'id' => 'required',
        ], [
            'id.required' => 'id不能为空',
        ]);
        $params=$request->only(['id','status']);
        $ret=$this->service->saveHelpType($params);
        return $this->jsonResponse(
            'ok',
            $ret
        );
    }

    /**
     * 保存帮助类型关系
     * */
    public function typeRelationSave(Request $request){
        $this->validate($request, [
            'help_id' => 'required',
        ], [
            'help_id.required' => '帮助id不能为空',
        ]);
        $params=$request->only(['help_id','type_ids']);
        $ret=$this->service->typeRelationSave($params);
        return $this->jsonResponse(
            'ok',
            $ret
        );
    }

    /**
     * 根据帮助类型 保存帮助关系
     * */
    public function addRelationByTypeId(Request $request){
        $this->validate($request, [
            'help_ids' => 'required',
            'type_id' => 'required',
        ], [
            'help_ids.required' => '帮助id不能为空',
            'type_id.required' => '帮助类型id不能为空',
        ]);
        $typeId=$request->input('type_id',0);
        $helpIds=$request->input('help_ids',[]);
        $ret=$this->service->addRelationByTypeId($typeId,$helpIds);
        return $this->jsonResponse(
            'ok',
            $ret
        );
    }

    /**
     * 根据类型id及帮助id 删除关联关系
     * */
    public function delTypeRelation(Request $request){
        $this->validate($request, [
            'help_id' => 'required',
            'type_id' => 'required',
        ], [
            'help_id.required' => '帮助id不能为空',
            'type_id.required' => '类型id不能未空',
        ]);
        $helpId=$request->input('help_id','');
        $typeId=$request->input('type_id','');
        $ret=$this->service->delTypeRelation($typeId,$helpId);
        return $this->jsonResponse(
            'ok',
            $ret
        );
    }


    /**
     * 获取帮助标签列表
     * */
    public function getHelpTagList(Request $request){
        $params=$request->only(['page_size','page','status','keyword']);
        $ret=$this->service->getHelpTagList($params);
        return $this->jsonResponse(
            'ok',
            $ret
        );
    }

    /**
     * 帮助标签保存
     * */
    public function saveHelpTag(Request $request){
        $this->validate($request, [
            'tag_name' => 'required',
        ], [
            'tag_name.required' => '类型名称不能为空',
        ]);
        $params=$request->only(['id','tag_name','is_hot','status','sort','seo_id','seo_data']);
        $ret=$this->service->saveHelpTag($params);
        return $this->jsonResponse(
            'ok',
            $ret
        );
    }

    /**
     * 变更帮助类型状态
     * */
    public function changeHelpTagStatus(Request $request){
        $this->validate($request, [
            'id' => 'required',
        ], [
            'id.required' => 'id不能为空',
        ]);
        $params=$request->only(['id','status']);
        $ret=$this->service->saveHelpTag($params);
        return $this->jsonResponse(
            'ok',
            $ret
        );
    }

    /**
     * 保存帮助标签关系
     * */
    public function tagRelationSave(Request $request){
        $this->validate($request, [
            'help_id' => 'required',
        ], [
            'help_id.required' => '帮助id不能为空',
        ]);
        $params=$request->only(['help_id','tag_ids']);
        $ret=$this->service->tagRelationSave($params);
        return $this->jsonResponse(
            'ok',
            $ret
        );
    }

    /**
     * 根据帮助标签 保存帮助关系
     * */
    public function addRelationByTagId(Request $request){
        $this->validate($request, [
            'help_ids' => 'required',
            'tag_id' => 'required',
        ], [
            'help_ids.required' => '帮助id不能为空',
            'tag_id.required' => '帮助标签id不能为空',
        ]);
        $tagId=$request->input('tag_id',0);
        $helpIds=$request->input('help_ids',[]);
        $ret=$this->service->addRelationByTagId($tagId,$helpIds);
        return $this->jsonResponse(
            'ok',
            $ret
        );
    }


    /**
     * 根据标签id及帮助id 删除关联关系
     * */
    public function delTagRelation(Request $request){
        $this->validate($request, [
            'help_id' => 'required',
            'tag_id' => 'required',
        ], [
            'help_id.required' => '帮助id不能为空',
            'tag_id.required' => '标签id不能未空',
        ]);
        $helpId=$request->input('help_id','');
        $tagId=$request->input('tag_id','');
        $ret=$this->service->delTagRelation($tagId,$helpId);
        return $this->jsonResponse(
            'ok',
            $ret
        );
    }

    /**
     * 获取帮助板块列表
     * */
    public function getHelpPlateList(Request $request){
        $params=$request->only(['page_size','page','status','keyword']);
        $ret=$this->service->getHelpPlateList($params);
        return $this->jsonResponse(
            'ok',
            $ret
        );
    }

    /**
     * 帮助板块保存
     * */
    public function saveHelpPlate(Request $request){
        $this->validate($request, [
            'plate_name' => 'required',
        ], [
            'plate_name.required' => '类型名称不能为空',
        ]);
        $params=$request->only(['id','plate_name','status','sort','seo_id','seo_data']);
        $ret=$this->service->saveHelpPlate($params);
        return $this->jsonResponse(
            'ok',
            $ret
        );
    }

    /**
     * 变更帮助板块状态
     * */
    public function changeHelpPlateStatus(Request $request){
        $this->validate($request, [
            'id' => 'required',
        ], [
            'id.required' => 'id不能为空',
        ]);
        $params=$request->only(['id','status']);
        $ret=$this->service->saveHelpPlate($params);
        return $this->jsonResponse(
            'ok',
            $ret
        );
    }


    /**
     * 获取帮助列表
     * */
    public function getHelpList(Request $request){
        $params=$request->only(['page_size','page','status','keyword','plate_id','sort']);
        $ret=$this->service->getHelpList($params);
        return $this->jsonResponse(
            'ok',
            $ret
        );
    }

    /**
     * 获取帮助详情
     * */
    public function getHelpInfo(Request $request){
        $this->validate($request, [
            'id' => 'required',
        ], [
            'id.required' => '帮助id不能为空',
        ]);
        $id=$request->input('id',0);
        $ret=$this->service->getHelpInfo(['id'=>$id]);
        return $this->jsonResponse(
            'ok',
            $ret
        );
    }

    /**
     * 获取帮助分类未关联的帮助
     * */
    public function getUnrelatedHelpByTypeIds(Request $request){
        $this->validate($request, [
            'type_id' => 'required',
        ], [
            'type_id.required' => 'id不能为空',
        ]);
        $params=$request->only(['page_size','page','status','keyword','type_id']);
        $ret=$this->service->getUnrelatedHelpByTypeIds($params);
        return $this->jsonResponse(
            'ok',
            $ret
        );
    }

    /**
     * 获取帮助标签未关联的帮助
     * */
    public function getUnrelatedHelpByTagIds(Request $request){
        $this->validate($request, [
            'tag_id' => 'required',
        ], [
            'tag_id.required' => 'id不能为空',
        ]);
        $params=$request->only(['page_size','page','status','keyword','tag_id']);
        $ret=$this->service->getUnrelatedHelpByTagIds($params);
        return $this->jsonResponse(
            'ok',
            $ret
        );
    }

    /**
     * 根据类型id 获取帮助列表
     * */
    public function getHelpListByType(Request $request){
        $params=$request->only(['page_size','page','status','keyword','type_id']);
        $ret=$this->service->getHelpByTypeIds($params);
        return $this->jsonResponse(
            'ok',
            $ret
        );
    }

    /**
     * 根据标签id 获取帮助列表
     * */
    public function getHelpListByTag(Request $request){
        $params=$request->only(['page_size','page','status','keyword','tag_id']);
        $ret=$this->service->getHelpByTagIds($params);
        return $this->jsonResponse(
            'ok',
            $ret
        );
    }

    /**
     * 变更帮助板块状态
     * */
    public function changeHelpStatus(Request $request){
        $this->validate($request, [
            'id' => 'required',
        ], [
            'id.required' => 'id不能为空',
        ]);
        $params=$request->only(['id','status']);
        $ret=$this->service->saveHelp($params);
        return $this->jsonResponse(
            'ok',
            $ret
        );
    }

    /**
     * 保存帮助数据
     * */
    public function saveHelp(Request $request){
        $this->validate($request, [
            'title' => 'required',
            'content' => 'required|string',
            'type_ids' => 'array',
            'tag_ids' => 'array',
        ]);
        $params = $request->only(['id','pub_date','title','content','outline_bar','description',
            'image_url',  'image_alt',  'main_image',  'main_image_alt',  'virtual_view',
            'download_url','seo_id','sort','seo_data','status','tag_ids','type_ids', 'is_top']);
        $ret=$this->service->saveHelp($params);
        return $this->jsonResponse(
            'ok',
            $ret
        );
    }



    /**
     * 帮助推荐设置
     * */
    public function recommendSet(Request $request){
        $this->validate($request, [
            'id' => 'required',
        ]);
        $params = $request->only(['id','is_recommend']);
        $ret=$this->service->recommendSet($params);
        return $this->jsonResponse(
            'ok',
            $ret
        );
    }


    /**
     * 更新帮助的浏览量
     * */
    public function upHelpPv(Request $request){
        $this->validate($request, [
            'id' => 'required',
        ], [
            'id.required' => 'id不能为空',
        ]);
        $helpId=$request->input('id',0);
        $incValue=$request->input('inc_val',1);
        $ret=$this->service->upHelpPv($helpId,$incValue);
        return $this->jsonResponse(
            'ok',
            $ret
        );
    }

    /**
     * 帮助预览
     * */
    public function previewHelp(Request $request){
        $this->validate($request, [
            'id' => 'required',
        ], [
            'id.required' => 'id不能为空',
        ]);
        $helpId=$request->input('id',0);
        $ret=$this->service->getHelpRenderData(['id'=>$helpId]);
        return $this->jsonResponse(
            'ok',
            $ret
        );
    }

    /**
     * 根据类型id获取帮助
     * */
    public function getPublishHelpListByTypeId(Request $request){
        $params=$request->only(['page_size','page','type_id']);
        $ret=$this->service->getPublishHelpListByTypeId($params);
        return $this->jsonResponse(
            'ok',
            $ret
        );
    }

    /**
     * 根据标签id获取帮助
     * */
    public function getPublishHelpListByTagId(Request $request){
        $params=$request->only(['page_size','page','tag_id', 'plate_id']);
        $ret=$this->service->getPublishHelpListByTagId($params);
        return $this->jsonResponse(
            'ok',
            $ret
        );
    }

    /**
     * 搜索获取已发布的帮助
     * */
    public function getSearchHelpList(Request $request){
        $params=$request->only(['page_size','keyword']);
        $ret=$this->service->getPublishHelpList($params);
        return $this->jsonResponse(
            'ok',
            $ret
        );
    }

    /**
     * 新闻排序上下移
     * @url /api/help/change-sort
     */
    public function changeHelpSort(Request $request)
    {
        $this->validate($request, [
            'id' => 'required',
            'sort' => 'required',
        ]);
        $ret = $this->service->changeHelpSort($request->input('id'), $request->input('sort'));
        return $this->jsonResponse('ok', $ret);
    }
}
