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

use App\Exceptions\ApiException;
use App\Web\Facades\ProductFacade;
use App\Web\Facades\WebFacade;
use Illuminate\View\Component;
use App\Web\Services\WebService;
class ProductListByIds extends Component
{
    protected $ids;
    protected $router_path;
    protected $viewFile;
    /**
     * @var mixed|string
     */


    public function __construct($viewFile = "", $params = [],$routerPath='', $ids = [])
    {
        if (empty($viewFile)) {
            throw new ApiException("common.page_none", '您访问的地址不存在~');
        }
        $this->ids = $params['ids'] ?? $ids;
        $this->router_path = $params['router_path'] ?? $routerPath;
        $this->viewFile= $viewFile;
    }


    /**
     * 产品列表组件
     */
    public function render()
    {
        $params = [
            "ids" => $this->ids
        ];
        $retData = [];
        //dd($params);
        $retData['data'] = ProductFacade::getProductByIds($params["ids"]);
        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();
//        dd($retData);
        return view($this->viewFile, $retData);
    }

}
