<?php
/**
 * Webkul Software.
 *
 * @category  Webkul
 * @package   Webkul_Marketplace
 * @author    Webkul
 * @copyright Copyright (c) Webkul Software Private Limited (https://webkul.com)
 * @license   https://store.webkul.com/license.html
 */
/** @var $block \Webkul\Marketplace\Block\Product\Create */

$product_id=$this->getRequest()->getParam('id');
$upsellProducts = [];
if ($product_id) {
    $productColl = $block->getProduct($product_id);
    $upsellProductColl = $productColl->getProductLinks();
    foreach ($upsellProductColl as $key => $value) {
        if ($value['link_type'] == 'upsell') {
            $productBySku = $block->getProductBySku($value['linked_product_sku']);
            array_push($upsellProducts, $productBySku->getId());
        }
    }
}
?>
<div id="upsell-product-block">
    <h3 class="title">
        <?= /* @noEscape */ $block->escapeHtml(__('Up-Sell Products')); ?>
    </h3>
    <?= /* @noEscape */ $block->escapeHtml(__('An up-sell item is offered to the customer as a pricier or higher-quality alternative to the product the customer is looking at.')); ?>
    <div id="upsell-product-block-loader" class="admin__data-grid-outer-wrap" style="display:block;">
        <div class="admin__data-grid-loading-mask">
            <div class="spinner">
                <span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span>
            </div>
        </div>
    </div>
    <div id="upsell-product-block-wrapper" style="display:none;">
        <?= $block->getChildHtml(); ?>
    </div>
    <script id="upsell-product-template" type="text/x-magento-template">
        <div id="upsell-product-record<%- data.id %>">
            <input name="links[upsell][<%- data.index %>][id]" type="hidden" value="<%- data.id %>">
            <input name="links[upsell][<%- data.index %>][name]" type="hidden" value="<%- data.name %>">
            <input name="links[upsell][<%- data.index %>][status]" type="hidden" value="<%- data.status %>">
            <input name="links[upsell][<%- data.index %>][attribute_set]" type="hidden" value="<%- data.attribute_set %>">
            <input name="links[upsell][<%- data.index %>][sku]" type="hidden" value="<%- data.sku %>">
            <input name="links[upsell][<%- data.index %>][price]" type="hidden" value="<%- data.price %>">
            <input name="links[upsell][<%- data.index %>][thumbnail]" type="hidden" value="<%- data.thumbnail %>">
            <input name="links[upsell][<%- data.index %>][position]" type="hidden" value="<%- data.position %>">
            <input name="links[upsell][<%- data.index %>][record_id]" type="hidden" value="<%- data.record_id %>">
        </div>
    </script>
</div>
<?php
$formData = [
    'upsellProductId' => '#wk-bodymain',
    'templateId' => '#upsell-product-template',
    'gridCheckbox' => '.admin__control-checkbox',
    'upsellProducts' => $this->helper(\Magento\Framework\Json\Helper\Data::class)->jsonEncode($upsellProducts)
];
$serializedFormData = $this->helper(\Magento\Framework\Json\Helper\Data::class)->jsonEncode($formData);
?>

<script type="text/x-magento-init">
    {
        "#upsell-product-block": {
            "upsellProduct": <?= /* @noEscape */ $serializedFormData; ?>
        }
    }
</script>
