<?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');
$relatedProducts = [];
if ($product_id) {
    $productColl = $block->getProduct($product_id);
    $relatedProductColl = $productColl->getProductLinks();
    foreach ($relatedProductColl as $key => $value) {
        if ($value['link_type'] == 'related') {
            $productBySku = $block->getProductBySku($value['linked_product_sku']);
            array_push($relatedProducts, $productBySku->getId());
        }
    }
}
?>
<div id="related-product-block">
    <h3 class="title">
        <?= /* @noEscape */ $block->escapeHtml(__('Related Products')); ?>
    </h3>
    <?= /* @noEscape */ $block->escapeHtml(__('Related products are shown to customers in addition to the item the customer is looking at.')); ?>
    <div id="related-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="related-product-block-wrapper" style="display:none;">
        <?= $block->getChildHtml(); ?>
    </div>
    <script id="related-product-template" type="text/x-magento-template">
        <div id="related-product-record<%- data.id %>">
            <input name="links[related][<%- data.index %>][id]" type="hidden" value="<%- data.id %>">
            <input name="links[related][<%- data.index %>][name]" type="hidden" value="<%- data.name %>">
            <input name="links[related][<%- data.index %>][status]" type="hidden" value="<%- data.status %>">
            <input name="links[related][<%- data.index %>][attribute_set]" type="hidden" value="<%- data.attribute_set %>">
            <input name="links[related][<%- data.index %>][sku]" type="hidden" value="<%- data.sku %>">
            <input name="links[related][<%- data.index %>][price]" type="hidden" value="<%- data.price %>">
            <input name="links[related][<%- data.index %>][thumbnail]" type="hidden" value="<%- data.thumbnail %>">
            <input name="links[related][<%- data.index %>][position]" type="hidden" value="<%- data.position %>">
            <input name="links[related][<%- data.index %>][record_id]" type="hidden" value="<%- data.record_id %>">
        </div>
    </script>
</div>
<?php
$formData = [
    'relatedProductId' => '#wk-bodymain',
    'templateId' => '#related-product-template',
    'gridCheckbox' => '.admin__control-checkbox',
    'relatedProducts' => $this->helper(\Magento\Framework\Json\Helper\Data::class)->jsonEncode($relatedProducts)
];
$serializedFormData = $this->helper(\Magento\Framework\Json\Helper\Data::class)->jsonEncode($formData);
?>

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