<?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 KnowledgeCentre extends Component
{
    protected $page; //这是参数
    protected $pageSize;
    protected $viewFile; //视图地址
    protected $typeId;
    protected $plateId;
    protected $router_path;


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

    /**
     * 知识中心列表组件
     */
    public function render()
    {
        $retData['train_list'] = [];
        $recommend = BlogRenderFacade::getPublishBlogList(
            $this->plateId,
            '',
            $this->page,
            $this->pageSize,
            ['pub_date'],
            (int)$this->typeId,
            0,
            'a.id,a.title,a.image_url,a.image_alt,a.pub_date,a.author,a.total_view,b.urla'
        );
        if(!empty($recommend['data'])) {
            $retData = $recommend;
        }
        if (!empty($this->router_path)) {
            $pageContent = WebFacade::getStaticPageInfo(['route_path' => $this->router_path]);
            $retData['page_content'] = $pageContent['page_content'];
        }
        $retData['menu_data']=WebFacade::getWebMenu();
        return view($this->viewFile, $retData);
    }

}
