<?php

namespace App\Web\Components\Blog;

use App\Exceptions\ApiException;
use App\Web\Facades\BlogRenderFacade;
use App\Web\Facades\WebFacade;
use App\Web\Services\WebService;
use Illuminate\View\Component;

class NewsInfo extends Component
{
    protected $viewFile; //视图地址
    protected $blogId;
    protected $router_path;

    /**
     * @throws ApiException
     */
    public function __construct($viewFile = '', $params = [], $blogId = 0, $routerPath= '')
    {
        if (empty($viewFile)) {
            throw new ApiException('common.page_none', '您访问的地址不存在~');
        }
        $this->blogId = $params['blog_id'] ?? $blogId;
        $this->router_path = $params['router_path'] ?? $routerPath;

        $this->viewFile = $viewFile;
    }

    /**
     * 资讯详情页组件
     */
    public function render()
    {
        $retData = [
            'recent_news_list' => []
        ];
        $retData['news_info'] = BlogRenderFacade::getPublishedBlogInfo($this->blogId);
        $retData['pre_news_info'] = BlogRenderFacade::getPreBlog($retData['news_info']['plate_id'], $this->blogId);
        $retData['next_news_info'] = BlogRenderFacade::getNextBlog($retData['news_info']['plate_id'], $this->blogId);
        $retData['plate_info'] = BlogRenderFacade::getPlateInfo($retData['news_info']['plate_id']);
        $recentNews = BlogRenderFacade::getPublishBlogList($retData['news_info']['plate_id'], '', 1, 3, ['is_hot']);
        if(!empty($recentNews['data'])) {
            $retData['recent_news_list'] = $recentNews['data'];
        }
        $retData['type_items'] = BlogRenderFacade::getPublishedTypeList($retData['news_info']['plate_id']);
        $retData['tag_items'] = BlogRenderFacade::getPublishedTagList($retData['news_info']['plate_id']);
        if (!empty($this->router_path)) {
            $pageContent = WebFacade::getStaticPageInfo(['route_path' => $this->router_path]);
            $retData['page_content'] = $pageContent['page_content'];
        }
        $retData['menu_data']=WebFacade::getWebMenu();
        //dd($retData);
        return view($this->viewFile, $retData);
    }

}
