<?php

namespace App\Web\Controllers;

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


class BlogController extends BaseController
{

    protected $service = null;

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

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

    /**
     * 文章类型保存
     * */
    public function saveBlogType(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', 'plate_id']);
        $ret = $this->service->saveBlogType($params);
        return $this->jsonResponse(
            'ok',
            $ret
        );
    }

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

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

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

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


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

    /**
     * 文章标签保存
     * */
    public function saveBlogTag(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->saveBlogTag($params);
        return $this->jsonResponse(
            'ok',
            $ret
        );
    }

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

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

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


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

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

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

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


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

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

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

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

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

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

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

    /**
     * 保存文章数据
     * */
    public function saveBlog(Request $request)
    {
        $this->validate($request, [
            'plate_id' => 'required',
            'title' => 'required',
            'content' => 'required|string',
            'type_ids' => 'array',
            'tag_ids' => 'array',
        ]);
        $params = $request->only(['id', 'plate_id', 'pub_date', 'title', 'author', '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', 'expand_content']);
        $ret = $this->service->saveBlog($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 upBlogPv(Request $request)
    {
        $this->validate($request, [
            'id' => 'required',
        ], [
            'id.required' => 'id不能为空',
        ]);
        $blogId = $request->input('id', 0);
        $incValue = $request->input('inc_val', 1);
        $ret = $this->service->upBlogPv($blogId, $incValue);
        return $this->jsonResponse(
            'ok',
            $ret
        );
    }

    /**
     * 文章预览
     * */
    public function previewBlog(Request $request)
    {
        $this->validate($request, [
            'id' => 'required',
        ], [
            'id.required' => 'id不能为空',
        ]);
        $blogId = $request->input('id', 0);
        $ret = $this->service->getBlogRenderData(['id' => $blogId]);
        return $this->jsonResponse(
            'ok',
            $ret
        );
    }

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

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

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

    /**
     * 新闻排序上下移
     * @url /api/blog/change-sort
     */
    public function changeBlogSort(Request $request)
    {
        $this->validate($request, [
            'id' => 'required',
            'sort' => 'required',
        ]);
        $ret = $this->service->changeBlogSort($request->input('id'), $request->input('sort'));
        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);
    }

    // 批量删除
    public function batchDeleteNews(Request $request)
    {
        $this->validate($request, [
            'ids' => 'required',
        ]);
        $ret = $this->service->batchDelete($request->all(), 2);
        return $this->jsonResponse('ok', $ret);
    }

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