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


    public function __construct($viewFile = "", $params = [], $page = 1, $pageSize = 10,$sort=[],$keyword='',$routerPath= '',$dataType='',$typeIds=[])
    {
        if (empty($viewFile)) {
            throw new ApiException("common.page_none", '您访问的地址不存在~');
        }
        $this->page = $params['page'] ?? $page;
        $this->pageSize = $params['page_size'] ?? $pageSize;
        $this->typeIds = $params['type_ids'] ?? $typeIds;
        $this->tagIds = $params['tag_ids'] ?? [];
        $this->keyword = $params['keyword'] ?? $keyword;
        $this->router_path = $params['router_path'] ?? $routerPath;
        $this->data_type = $params['data_type'] ?? $dataType;
        if (empty($params['sort'])) {
            $this->sort = $sort;
        } else {
            $this->sort = $params['sort'];
        }
        $this->viewFile= $viewFile;
    }


    /**
     * 产品列表组件
     */
    public function render()
    {
        $params = [
            "page" => $this->page,
            "page_size" => $this->pageSize,
            "type_ids" => $this->typeIds,
            "tag_ids" => $this->tagIds,
            "sort" => $this->sort,
            "keyword"=> $this->keyword
        ];
        $params['status']=0;
        $params['is_paginate']=1;
        $retData = ProductFacade::getPublishProductList($params,'',$this->data_type);
        $typeParams = [];
        $typeParams['status'] = 0;
        $typeParams['page_size'] = 9999;
        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);
    }

}
