<?php
/**@var \Magetop\Advancepreview\Block\Adminhtml\Product\Edit\PreviewAdditionalTab $block */
//echo get_class($block);
$productId = $block->getRequest()->getParam('id',0);
$urls = $block->getAjaxUrl();
$saveUrl = $urls['save_url'];
$itemsUrl = $urls['item_url'];
$editUrl = $urls['edit_url'];
$dellUrl = $urls['dell_url'];

$togalvalue = 0;
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$colPreviewAdditionalFactory = $objectManager->get('\Magetop\Advancepreview\Model\ResourceModel\PreviewAdditional\CollectionFactory');
$storeManager = $objectManager->get('\Magento\Store\Model\StoreManagerInterface');
$ajexurl = $storeManager->getStore()->getBaseUrl()."customadvancepreview/index/index";
$collactionoption = $colPreviewAdditionalFactory->create()->addFieldToFilter('product_id', $productId)->addFieldToFilter('title', '5C transparent');
if(key_exists(0,$collactionoption->getData()))
{
    if($collactionoption->getData()[0]['fivec_enabale'] == null)
    {
        $togalvalue = 1 ;
    }
    else
    {
        $togalvalue = $collactionoption->getData()[0]['fivec_enabale'] ;
    }
}
?>
<?php if($productId > 0) : ?>
    <input type="hidden" id="txt-preview-additional-save-url" value="<?php echo $saveUrl; ?>">
    <input type="hidden" id="txt-preview-additional-items-url" value="<?php echo $itemsUrl; ?>">
    <input type="hidden" id="txt-preview-additional-edit-url" value="<?php echo $editUrl; ?>">
    <input type="hidden" id="txt-preview-additional-dell-url" value="<?php echo $dellUrl; ?>">
    <input type="hidden" id="txt-product-additional-id" value="<?php echo $productId; ?>">

<button type="button" id="btn-add-product-additional-preview"><?php echo __('Add custom service') ?></button>
    <div id="product-additional-review-message"></div>
    <div class="new-custome-button-for-toggal" style="display: flex;">
        <div class="admin__field-label" style="padding-right: 10px;">
            <label>
                <span style="font-weight: bold;">5C Transparent</span>
            </label>
        </div>
        <div class="admin__actions-switch" data-role="switcher">
            <input type="checkbox" class="admin__actions-switch-checkbox" data-bind="attr: {id: uid, name: inputName}, value: value, disable: disabled, hasFocus: focused, simpleChecked: checked" id="option-5c" name="product[5c]" value="0">
            <label class="admin__actions-switch-label" data-bind="attr: {for: uid}" for="option-5c">
                <span class="admin__actions-switch-text" data-bind="attr: {'data-text-on': toggleLabels.on, 'data-text-off': toggleLabels.off}" data-text-on="Yes" data-text-off="No"></span>
            </label>
        </div>
    </div>
<div style="text-align: center; margin-top: 10px; font-size: 1.8em;"><?php echo __('Custom service items') ?></div>
<div id="preview-product-additional-lists-content">

</div>
<div id="preview-product-additional-form-content" style="display: none">

</div>

<script>
    require([
        'jquery',
        'preview_additional_form'
    ],function ($, previewAdditionalForm) {
        previewAdditionalForm.init();
    });
    require([
        'jquery'
    ],function ($) {
        $("#option-5c").click(function(e){
            $("#option-5c").val($("#option-5c").val() == 1? 0 : 1);
            var new5c = $('#option-5c').val();
            var ajexurl  = "<?php echo $ajexurl; ?>";
            $.ajax({
                url: ajexurl,
                type: 'POST',
                dataType: 'json',
                data: {
                    fivec: new5c,
                    productid: '<?php echo $productId; ?>',
                },
            complete: function(response) {             
                console.log(response.responseJSON.message)
                },
                error: function (xhr, status, errorThrown) {
                    console.log('Error happens. Try again.');
                }
            });
        });
        $(document).ready( function() {
            var togalvalue = <?php echo $togalvalue;?>;
            if(togalvalue == 1)
            {
                $('#option-5c').trigger('click');
            }
        });
    });
</script>
<?php else: ?>
<p><?php echo __('Please create product before configuring Preview Discount') ?></p>
<?php endif; ?>
