<?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 BlogPlateTypeTag extends Component
{
    protected $viewFile; //视图地址
    protected $router_path;
    protected $plate_id;


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

    /**
     * 资讯列表组件
     */
    public function render()
    {
        $retData = [
            'type_items' => [],
            'tag_items' => []
        ];
        $retData['type_items'] = BlogRenderFacade::getPublishedTypeList($this->plate_id);
        $retData['tag_items'] = BlogRenderFacade::getPublishedTagList($this->plate_id);
        if (!empty($this->router_path)) {
            $pageContent = WebFacade::getStaticPageInfo(['route_path' => $this->router_path]);
            $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;
                    }
                }
            }
            $retData['page_content'] = $pageContent['page_content'];
        }
//        dd($retData);
        $retData['menu_data'] = WebFacade::getWebMenu();
        return view($this->viewFile, $retData);
    }

}
