<?php

namespace App\Web\Components\Blog;

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

class RecentNewsList extends Component
{
    protected $viewFile; //视图地址
    protected $plateId;
    protected $router_path;
    protected $pageSize;


    /**
     * @throws ApiException
     */
    public function __construct($viewFile = '',$params = [],$routerPath='',$plateId=BlogPlateModel::NEWS,$pageSize = 3)
    {
        if (empty($viewFile)) {
            throw new ApiException('common.page_none', '您访问的地址不存在~');
        }
        if (!empty($plateId)) {
            $this->plateId = $plateId;
        } else {
            $this->plateId = BlogPlateModel::NEWS;
        }
        $this->pageSize = $params['page_size'] ?? $pageSize;
        $this->viewFile = $viewFile;
        $this->router_path = $params['router_path'] ?? $routerPath;

    }

    /**
     * 最近资讯列表组件
     */
    public function render()
    {
        $retData['recent_news_list'] = BlogRenderFacade::getPublishBlogList($this->plateId, '', 1, $this->pageSize, ['pub_date','is_top']);
        $pageContent = [];
        $retData['menu_data']=WebFacade::getWebMenu();
        if (!empty($this->router_path)) {
            $pageContent = WebFacade::getStaticPageInfo(['route_path' => $this->router_path]);
            //$retData['page_content'] = $pageContent['page_content'];
        }
        $layoutContent = WebFacade::getStaticPageInfo(['route_path' => WebService::LAYOUTS_ROUTE]);
        if (!empty($layoutContent['page_content'])) {
            foreach ($layoutContent['page_content'] as $key => $content_item) {
                if (empty($pageContent['page_content'][$key])) {
                    $pageContent['page_content'][$key] = $content_item;
                }
            }
        }
        if (!empty($pageContent['page_content'])) {
            $retData['page_content'] = $pageContent['page_content'];
        }
//        dd($retData);
        return view($this->viewFile, $retData);
    }

}
