<?php
/**
 * @author      Magetop Developer (Hau)
 * @package     Magento Multi Vendor Marketplace
 * @copyright   Copyright (c) Magetop (https://www.magetop.com)
 * @terms       https://www.magetop.com/terms
 * @license     https://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
 **/

?>
<?php echo $block->getOptionsTemplatesHtml() ?>
<script id="custom-option-base-template" type="text/x-magento-template">
    <div class="fieldset-wrapper magetop__collapsible-block-wrapper opened" id="option_<%- data.id %>">
        <div class="fieldset-wrapper-title">
            <strong
                class="magetop__collapsible-title"
                data-toggle="collapse"
                data-target="#<%- data.id %>-content">
                <span id="option_<%- data.id %>_header_title"><%- data.title %></span>
            </strong>
            <div class="actions">
                <button type="button" title="<?php /* @escapeNotVerified */ echo __('Delete Custom Option'); ?>" class="action-delete" id="<?php /* @escapeNotVerified */ echo $block->getOptFieldId() ?>_<%- data.id %>_delete">
                    <span><?php /* @escapeNotVerified */ echo __('Delete Custom Option'); ?></span>
                </button>
            </div>
            <div id="<?php /* @escapeNotVerified */ echo $block->getOptFieldId() ?>_<%- data.id %>_move" data-role="draggable-handle" class="draggable-handle"
                 title="<?php /* @escapeNotVerified */ echo __('Sort Custom Options'); ?>"></div>
        </div>
        <div class="fieldset-wrapper-content in collapse" id="<%- data.id %>-content">
            <fieldset class="fieldset">
                <fieldset class="fieldset-alt" id="<?php /* @escapeNotVerified */ echo $block->getOptFieldId() ?>_<%- data.id %>">
                    <input id="<?php /* @escapeNotVerified */ echo $block->getOptFieldId() ?>_<%- data.id %>_is_delete" name="<?php /* @escapeNotVerified */ echo $block->getOptFieldName() ?>[<%- data.id %>][is_delete]" type="hidden" value=""/>
                    <input id="<?php /* @escapeNotVerified */ echo $block->getOptFieldId() ?>_<%- data.id %>_previous_type" name="<?php /* @escapeNotVerified */ echo $block->getOptFieldName() ?>[<%- data.id %>][previous_type]" type="hidden" value="<%- data.type %>"/>
                    <input id="<?php /* @escapeNotVerified */ echo $block->getOptFieldId() ?>_<%- data.id %>_previous_group" name="<?php /* @escapeNotVerified */ echo $block->getOptFieldName() ?>[<%- data.id %>][previous_group]" type="hidden" value=""/>
                    <input id="<?php /* @escapeNotVerified */ echo $block->getOptFieldId() ?>_<%- data.id %>_id" name="<?php /* @escapeNotVerified */ echo $block->getOptFieldName() ?>[<%- data.id %>][id]" type="hidden" value="<%- data.id %>"/>
                    <input id="<?php /* @escapeNotVerified */ echo $block->getOptFieldId() ?>_<%- data.id %>_option_id" name="<?php /* @escapeNotVerified */ echo $block->getOptFieldName() ?>[<%- data.id %>][option_id]" type="hidden" value="<%- data.option_id %>"/>
                    <input name="<?php /* @escapeNotVerified */ echo $block->getOptFieldName() ?>[<%- data.id %>][sort_order]" type="hidden" value="<%- data.sort_order %>"/>

                    <div class="field field-option-title required">
                        <label class="label" for="<?php /* @escapeNotVerified */ echo $block->getOptFieldId() ?>_<%- data.id %>_title">
                            <?php /* @escapeNotVerified */ echo __('Option Title') ?>
                        </label>
                        <div class="control">
                            <input id="<?php /* @escapeNotVerified */ echo $block->getOptFieldId() ?>_<%- data.id %>_title"
                                   name="<?php /* @escapeNotVerified */ echo $block->getOptFieldName() ?>[<%- data.id %>][title]"
                                   class="required-entry input-text"
                                   type="text"
                                   value="<%- data.title %>"
                                   data-store-label="<%- data.title %>"
                                   <% if (typeof data.scopeTitleDisabled != 'undefined' && data.scopeTitleDisabled != null) { %> disabled="disabled" <% } %>
                                   >
                            <%- data.checkboxScopeTitle %>
                        </div>
                    </div>

                    <div class="field field-option-input-type required">
                        <label class="label" for="<?php /* @escapeNotVerified */ echo $block->getOptFieldId() ?>_<%- data.id %>_title">
                            <?php /* @escapeNotVerified */ echo __('Input Type') ?>
                        </label>
                        <div class="control opt-type">
                            <?php echo $block->getTypeSelectHtml() ?>
                        </div>
                    </div>

                    <div class="field field-option-req">
                        <div class="control">
                            <input id="<?php /* @escapeNotVerified */ echo $block->getOptFieldId() ?>_<%- data.id %>_required" class="is-required" type="checkbox" checked="checked"/>
                            <label for="field-option-req">
                                <?php /* @escapeNotVerified */ echo __('Required')?>
                            </label>
                            <span style="display:none"><?php echo $block->getOptRequireSelectHtml() ?></span>
                        </div>
                    </div>
                </fieldset>
            </fieldset>
        </div>
    </div>
</script>

<div id="import-container" style="display: none;"></div>
<?php if (!$block->checkReadonly()): ?>
<div><input type="hidden" name="affect_product_custom_options" value="1"/></div>
<?php endif; ?>
<script>
require([
    "jquery",
    "Magetop_Marketplace/js/custom-options"
], function(jQuery){

jQuery(function ($) {
    var fieldSet = $('[data-block=product-custom-options]');
    fieldSet.customOptions(<?php /* @escapeNotVerified */ echo $this->helper('Magento\Framework\Json\Helper\Data')->jsonEncode(
        [
            'fieldId' => $block->getOptFieldId(),
            'productGridUrl' => $block->getUrl('product/optionsImportGrid'),
            'formKey' => $block->getFormKey(),
            'customOptionsUrl' => $block->getUrl('product/customOptions'),
            'isReadonly' => $block->checkReadonly(),
            'itemCount' => $block->getItemCount(),
            'currentProductId' => $block->getProduct()->getId(),
        ]
    )?>);
    //adding data to templates
    <?php /** @var $_value \Magento\Framework\DataObject */ ?>
    <?php foreach ($block->getProOptionValues() as $_value): ?>
        fieldSet.customOptions('addOption', <?php /* @escapeNotVerified */ echo $_value->toJson() ?>);
    <?php endforeach; ?>
});

});
</script>
