<?php
namespace App\Web\Components\Help;

use App\Exceptions\ApiException;
use App\Web\Facades\HelpFacade;
use App\Web\Facades\WebFacade;
use App\Web\Services\WebService;
use Illuminate\View\Component;

class HelpList extends Component
{
    protected $page; //这是参数
    protected $pageSize;
    protected $viewFile; //视图地址
    protected $typeId;
    protected $tagId;


    public function __construct($viewFile = "", $params = [], $page = 1, $pageSize = 10)
    {

        if (empty($viewFile)) {
            throw new ApiException("common.page_none", '您访问的地址不存在~');
        }
        $this->page = isset($params['page']) ? $params['page'] : $page;
        $this->pageSize = isset($params['page_size']) ? $params['page_size'] : $pageSize;
        $this->typeId = isset($params['type_id']) ? $params['type_id'] : 0;
        $this->tagId = isset($params['tag_id']) ? $params['tag_id'] : 0;
        $this->viewFile= $viewFile;
    }


    /**
     * 资讯列表组件
     */
    public function render()
    {
        $params = [
            "page" => $this->page,
            "page_size" => $this->pageSize,
            "type_id" => $this->typeId,
            "tag_id" => $this->tagId,
        ];
        $params['status']=0;
        $retData=[];
        if (!empty($params['type_id'])) {
            $retData = HelpFacade::getPublishHelpListByTypeId($params);;
        } else if (!empty($params['tag_id'])) {
            $retData = HelpFacade::getPublishHelpListByTagId($params);
        } else {
            $params['sort']['pub_date']=1;
            $retData = HelpFacade::getPublishHelpList($params);
        }
        $pageContent = WebFacade::getStaticPageInfo(['route_path' => WebService::LAYOUTS_ROUTE]);
        $retData['page_content'] = $pageContent['page_content'];
        $retData['menu_data']=WebFacade::getWebMenu();
        return view($this->viewFile, $retData);
    }

}
