<div class="wk-loading-mask wk-display-none">
    <div class="wk-loader"></div>
</div>
<?php
    $itemWidth = 95.6;
    $productId = $block->getProduct()->getId();
    $helper = $this->helper("Magetop\Agency\Helper\Data");
    $cartHelper = $this->helper('Magento\Checkout\Helper\Cart');
    $product = $helper->getProduct($productId);
    $url = $cartHelper->getAddUrl($product);
    $formKey = $helper->getFormKey();
    $allProducts = $block->getAssignedProducts();
    $totalSellers = count($allProducts);
    $dir = $block->getDirection();
    $sortOrder = $block->getSortOrder();
    $defaultUrl = $block->getDefaultUrl();
    $sortOrders = ["price" => "Price",  "rating" => "Rating"];
    $productType = $product->getTypeId();
    $singleMinPriceConfigProduct = [];
    $mainConfigChilds = '';
    if ($productType == "configurable") {
        $isConfig = 1;
        $singleMinPriceConfigProduct = $helper->getMinimumPriceConfigProductDetails($productId);
        $mainConfigChilds = $helper->getConfigProductChildrens($productId);
    } else {
        $isConfig = 0;
    }
    $customOptions = $helper->getCustomOptions($product)->getData();
    $customOptionsAvail = 0;
    if (count($customOptions) > 0) {
        $customOptionsAvail = 1;
    }
    $jsonResult = [0];
    $btnHtml = "";
    $minPriceToShow = "";
    $symbol = $helper->getCurrentCurrencySymbol(); 
    $minPriceProducts = $helper->getMinimumPriceProductDetails($productId, $productType);
    if($product->getPrice() > 0) {
        $minPriceToShow = $this->helper('Magento\Framework\Pricing\Helper\Data')
                            ->currency($product->getPrice(), true, false);
    }
    $remainingConfigProducts = [];
    if ($isConfig && $singleMinPriceConfigProduct) {
        $remainingConfigProducts = array_slice($minPriceProducts, count($singleMinPriceConfigProduct));
    }
?>
<?php if (count($allProducts) > 0):
        $minPrice = $helper->getMinimumPriceHtml($productId, $productType);
        if ($minPriceProducts) {
            $minPriceToShow = $this->helper('Magento\Framework\Pricing\Helper\Data')
                                    ->currency($minPriceProducts[0]['price'], true, false);
        }
        if (isset($minPriceProducts[1])) {
            $minPrice = $this->helper('Magento\Framework\Pricing\Helper\Data')
                            ->currency($minPriceProducts[1]['price'], true, false);
        }
        if (count($remainingConfigProducts) >0) {
            $minPrice = $this->helper('Magento\Framework\Pricing\Helper\Data')
            ->currency($remainingConfigProducts[0]['price'], true, false);
        }
    $totalSellersHtml = "<span>$totalSellers</span>";
    $minPriceHtml = "<span>$minPrice</span>";
    ?>
    <div id="product_list" class="wk-ap-product-list wk-ap-fieldset">
        <table class="data table wk-table-product-list">
            <?php
				$mainProduct = $helper->getProduct($productId);
				$mainConfigClass = '';
                if ($mainProduct->getTypeId() == "configurable") {
                    $mainConfigClass = 'main-is-config';
               }
			?>
			<?php foreach ($allProducts as $product): ?>
                <?php
                    $assignId = $product->getId();

                    $price = $this->helper('Magento\Framework\Pricing\Helper\Data')
                                ->currency($product->getPrice(), true, false);
                ?>
                    <tr data-id="<?php echo $product->getId();?>">
                        <td class="col">
                            <span class="wk-ap-product-price"><?php echo $price; ?></span>
							<input type="number" class="wk-ap-qty input-text qty" title="Qty" value="1" maxlength="12" id="qty" name="qty">
                            <button data-id="<?php echo $assignId; ?>" class="wk-ap-add-to-cart action primary tocart <?= $mainConfigClass ?>" title="Add to Cart" type="button">
                                 <span>Add to Cart</span>
                            </button>
                        </td>
                    </tr>
            <?php endforeach; ?>
        </table>
    </div>
    <?php 
    $min = [];
    $qty = [];
    if ($product->getType() == "configurable") {
        foreach($associatedOptions as $key => $value) {
            foreach ($value as $id => $options) {
                if (isset($min[$id])) {
                    if ($min[$id]>$associatedOptions[$key][$id]['price']) {
                        $min[$id] = $associatedOptions[$key][$id]['price'];
                        $qty[$id] = $associatedOptions[$key][$id]['qty'];
                    }
                } else { 
                    $min[$id] = $associatedOptions[$key][$id]['price'];
                    $qty[$id] = $associatedOptions[$key][$id]['qty'];
                }
            }
        }
    }
    ?>
    <?php
        $data = [];
        $data["isConfig"] = $isConfig;
        $data["productId"] = $productId;
        $data["formKey"] = $formKey;
        $data["url"] = $url;
        $data["dir"] = $dir;
        $data["defaultUrl"] = $defaultUrl;
        $data["sortOrder"] = $sortOrder;
        $data["btnHtml"] = $btnHtml;
        $data["url"] = $url;
        $data["jsonResult"] = $jsonResult;
        $data["symbol"] = $symbol;
        $data["itemWidth"] = $itemWidth;
        $data['min'] = $min;
        $data['qty'] = $qty;
        $data['minPriceToShow'] = $minPriceToShow;
        $data['mainConfigChilds'] = $mainConfigChilds;
        $data['singleMinPriceConfigProduct'] = $singleMinPriceConfigProduct;
        $data['customOptionsAvail'] = $customOptionsAvail;
        $data = json_encode($data);
    ?>
    <script type="text/x-magento-init">
        {
            "body": {
                "Magetop_Agency/js/assignproduct": <?php echo $data ?>
            }
        }
    </script>
<?php endif; ?>