<?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 NewsList extends Component
{
    protected $page; //这是参数
    protected $pageSize;
    protected $viewFile; //视图地址
    protected $typeId;
    protected $tagId;
    protected $keyword;
    protected $router_path;
    protected $echo_content;
    protected $plate_id;
    protected $sort;


    /**
     * @throws ApiException
     */
    public function __construct($viewFile = '', $params = [], $keyword = '',$routerPath='', $sort=[], $typeId = 0, $tagId = 0, $page = 1, $pageSize = 10,$plateId=BlogPlateModel::NEWS,$echoContent = false)
    {
        if (empty($viewFile)) {
            throw new ApiException('common.page_none', '您访问的地址不存在~');
        }
        $this->keyword = $params['keyword'] ?? $keyword;
        $this->page = $params['page'] ?? $page;
        $this->pageSize = $params['page_size'] ?? $pageSize;
        $this->typeId = $params['type_id'] ?? $typeId;
        $this->tagId = $params['tag_id'] ?? $tagId;
        $this->router_path = $params['router_path'] ?? $routerPath;
        $this->plate_id = $params['plate_id'] ?? $plateId;
        if (empty($this->plate_id)) {
            $this->plate_id = BlogPlateModel::NEWS;
        }
        if (empty($params['sort'])) {
            $this->sort = ['pub_date'];
        } else {
            $this->sort = $params['sort'];
        }
        $this->echo_content = $params['echo_content'] ?? $echoContent;
        $this->viewFile = $viewFile;
    }

    /**
     * 资讯列表组件
     */
    public function render()
    {
        $retData = [
            'recent_news_list' => []
        ];
        $retData['news_list'] = BlogRenderFacade::getPublishBlogList(
            $this->plate_id,
            $this->keyword,
            $this->page,
            $this->pageSize,
            $this->sort,
            $this->typeId,
            $this->tagId,
            null,
            $this->echo_content
        );
        if (!empty($this->typeId)) {
            $retData['blog_type_info'] = BlogRenderFacade::getBlogTypeInfo($this->typeId);
//            dd($retData);
        }
        $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'];
        }
        $retData['menu_data']=WebFacade::getWebMenu();
        return view($this->viewFile, $retData);
    }

}
