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


    /**
     * @throws ApiException
     */
    public function __construct($viewFile = '', $params = [], $page = 1, $pageSize = 6,$routerPath='')
    {

        if (empty($viewFile)) {
            throw new ApiException('common.page_none', '您访问的地址不存在~');
        }
        $this->page = $params['page'] ?? $page;
        $this->pageSize = $params['page_size'] ?? $pageSize;
        $this->plateId = BlogPlateModel::SUCCESSCASES;
        $this->viewFile = $viewFile;
        $this->router_path = $params['router_path'] ?? $routerPath;
    }

    /**
     * 成功案例列表组件
     */
    public function render()
    {
        $retData['recommend_case_list'] = [];
        $recommend = BlogRenderFacade::getPublishBlogList(
            $this->plateId,
            '',
            1,
            3,
            ['is_recommend','pub_date'],
            0,
            0,
            'a.id,a.title,a.image_url,a.image_alt,a.total_view,b.urla'
        );
        if(!empty($recommend['data'])) {
            $retData['recommend_case_list'] = $recommend['data'];
        }
        $retData['case_list'] = BlogRenderFacade::getPublishBlogList(
            $this->plateId,
            '',
            $this->page,
            $this->pageSize,
            [],
            0,
            0,
            'a.id,a.title,a.image_url,a.image_alt,a.description,b.urla'
        );
        if (!empty($this->router_path)) {
            $pageContent = WebFacade::getStaticPageInfo(['route_path' => $this->router_path]);
            $retData['page_content'] = $pageContent['page_content'];
        }
        $retData['menu_data']=WebFacade::getWebMenu();
        //dd($retData);
        return view($this->viewFile, $retData);
    }

}
