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


    /**
     * @throws ApiException
     */
    public function __construct($viewFile = '', $params = [], $keyword = '', $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->keyword = $params['keyword'] ?? $keyword;
        $this->plateId = BlogPlateModel::Q_AND_A;
        $this->router_path = $params['router_path'] ?? $routerPath;
        $this->viewFile = $viewFile;
    }

    /**
     * 问答列表组件
     */
    public function render()
    {
        $retData = [
            'recent_news_list' => []
        ];
        $retData['q_and_a_list'] = BlogRenderFacade::getPublishBlogList(
            $this->plateId,
            $this->keyword,
            $this->page,
            $this->pageSize,
            [],
           0,
            0,
            'a.id,a.title,a.description,a.total_view'
        );
        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);
    }

}
