<?php

namespace App\Web\Controllers;

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



class MeetingController extends BaseController
{

    protected $service = null;

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

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

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

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

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

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

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


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

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

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

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

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


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

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

    /**
     * 产品板块保存
     * */
    public function saveMeetingPlate(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->saveMeetingPlate($params);
        return $this->jsonResponse(
            'ok',
            $ret
        );
    }

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


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

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

    /**
     * 获取产品分类未关联的产品
     * */
    public function getUnrelatedMeetingByTypeIds(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->getUnrelatedMeetingByTypeIds($params);
        return $this->jsonResponse(
            'ok',
            $ret
        );
    }

    /**
     * 获取产品标签未关联的产品
     * */
    public function getUnrelatedMeetingByTagIds(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->getUnrelatedMeetingByTagIds($params);
        return $this->jsonResponse(
            'ok',
            $ret
        );
    }

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

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

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

    /**
     * 保存产品数据
     * */
    public function saveMeeting(Request $request){
        $this->validate($request, [
            'title' => 'required',
            /*'content' => 'required|string',*/
            'type_ids' => 'array',
            'tag_ids' => 'array',
        ]);
        $params = $request->only(['id','pub_date','title','description','content','start_date','end_date','image_url','image_alt',
            'main_image',  'main_image_alt',  'sub_image_urls',  'country','address','virtual_view','total_view',
            'download_url','video_path','video_image_url','video_type','video_desc','seo_data','seo_id','status','tag_ids',
            'type_ids', 'is_top','sort','is_recommend','speech_schedule','page_route','meeting_host','expand_content']);
        $ret=$this->service->saveMeeting($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 upMeetingPv(Request $request){
        $this->validate($request, [
            'id' => 'required',
        ], [
            'id.required' => 'id不能为空',
        ]);
        $meetingId=$request->input('id',0);
        $incValue=$request->input('inc_val',1);
        $ret=$this->service->upMeetingPv($meetingId,$incValue);
        return $this->jsonResponse(
            'ok',
            $ret
        );
    }

    /**
     * 产品预览
     * */
    public function previewMeeting(Request $request){
        $this->validate($request, [
            'id' => 'required',
        ], [
            'id.required' => 'id不能为空',
        ]);
        $meetingId=$request->input('id',0);
        $ret=$this->service->getMeetingRenderData(['id'=>$meetingId]);
        return $this->jsonResponse(
            'ok',
            $ret
        );
    }

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

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

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

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

    // 批量删除
    public function batchDeleteMeeting(Request $request)
    {
        $this->validate($request, [
            'ids' => 'required',
        ]);
        $ret= $this->service->batchDelete($request->all());
        return $this->jsonResponse('ok', $ret);
    }
    // 批量修改状态
    public function batchStatusMeeting(Request $request)
    {
        $this->validate($request, [
            'ids' => 'required',
        ]);
        $ret= $this->service->batchStatus($request->all());
        return $this->jsonResponse('ok', $ret);
    }

    // 批量修改分类
    public function typePluralEdit(Request $request)
    {
        $this->validate($request, [
            'ids' => 'required',
        ]);
        $ret= $this->service->typePluralEdit($request->all());
        return $this->jsonResponse('ok', $ret);
    }
    // 批量修改标签
    public function tagPluralEdit(Request $request)
    {
        $this->validate($request, [
            'ids' => 'required',
        ]);
        $ret= $this->service->tagPluralEdit($request->all());
        return $this->jsonResponse('ok', $ret);
    }
    /**
     * 获取全球展会列表筛选项
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @throws \Illuminate\Validation\ValidationException
     */
    public function globalFilterFade(Request $request)
    {
        $this->validate($request, [
            'tag_id' => 'required',
        ]);
        $ret = $this->service->getGlobalFilter($request->input('tag_id'), $request->input('part'));
        return $this->jsonResponse('ok', $ret);
    }
}
