<?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 $_htmlId      = $block->getTierPriceCode() ?>
<?php $_htmlClass   = 'input-text magetop__control-text' ?>
<?php $_htmlName    = 'product[tier_price]' ?>
<?php $_readonly    = false ?>
<?php $_showWebsite = $block->checkShowWebsiteColumn(); ?>
<?php $_editWebsite = $block->checkAllowChangeWebsite(); ?>
<?php $_priceValueValidation = ''; ?>
<?php $_showWebsite = $block->checkShowWebsiteColumn(); ?>
<?php $_showWebsite = $block->checkMultiWebsites(); ?>
<div class="field row tierprice" id="attribute-<?php /* @escapeNotVerified */ echo $_htmlId?>-container" data-attribute-code="<?php /* @escapeNotVerified */ echo $_htmlId?>"
    data-apply-to="<?php echo $block->escapeHtml(
        $this->helper('Magento\Framework\Json\Helper\Data')->jsonEncode(
            $block->getTierApplyTo()
        )
    )?>">
    <label class="label col-sm-3"><span><?php /* @escapeNotVerified */ echo __('Tier Price') ?></span></label>
    <div class="control col-sm-9">
        <table class="magetop__control-table tiers_table mv-data-table" id="tiers_table">
            <thead>
                <tr>
                    <th class="col-websites" <?php if (!$_showWebsite): ?>style="display:none"<?php endif; ?>><?php /* @escapeNotVerified */ echo __('Web Site') ?></th>
                    <th class="col-customer-group"><?php /* @escapeNotVerified */ echo __('Customer Group') ?></th>
                    <th class="col-qty required"><?php /* @escapeNotVerified */ echo __('Quantity') ?></th>
                    <th class="col-price required"><?php /* @escapeNotVerified */ echo __('Item Price') ?></th>
                    <th class="col-delete"><?php /* @escapeNotVerified */ echo __('Action') ?></th>
                </tr>
            </thead>
            <tbody id="<?php /* @escapeNotVerified */ echo $_htmlId ?>_container"></tbody>
            <tfoot>
                <tr>
                    <td colspan="<?php if (!$_showWebsite): ?>4<?php else: ?>5<?php endif; ?>" class="col-actions-add">
						<button data-ui-id="widget-button-4" onclick="return tierPriceControl.addItem()" class="action-default scalable add" type="button" title="Add Price">
							<span><?php echo __('Add Price') ?></span>
						</button>					
					</td>
                </tr>
            </tfoot>
        </table>
        <script>
            require([
                'mage/template',
                "prototype",
                "mage/adminhtml/form"
            ], function (mageTemplate) {
                //<![CDATA[
                var tierPriceRowTemplate = '<tr>'
                    + '<td class="col-websites"<?php if (!$_showWebsite): ?> style="display:none"<?php endif; ?>>'
                    + '<select class="<?php /* @escapeNotVerified */ echo $_htmlClass ?> required-entry" name="<?php /* @escapeNotVerified */ echo $_htmlName ?>[<%- data.index %>][website_id]" id="tier_price_row_<%- data.index %>_website">'
                    <?php foreach ($block->getTierWebsites() as $_websiteId => $_info): ?>
                    + '<option value="<?php /* @escapeNotVerified */ echo $_websiteId ?>"><?php /* @escapeNotVerified */ echo $block->escapeJsQuote($block->escapeHtml($_info['name'])) ?><?php if (!empty($_info['currency'])): ?> [<?php echo $block->escapeHtml($_info['currency']) ?>]<?php endif; ?></option>'
                    <?php endforeach ?>
                    + '</select></td>'
                    + '<td class="col-customer-group"><select class="<?php /* @escapeNotVerified */ echo $_htmlClass ?> custgroup required-entry" name="<?php /* @escapeNotVerified */ echo $_htmlName ?>[<%- data.index %>][cust_group]" id="tier_price_row_<%- data.index %>_cust_group">'
                    <?php foreach ($block->getTierCustomerGroups() as $_groupId => $_groupName): ?>
                    + '<option value="<?php /* @escapeNotVerified */ echo $_groupId ?>"><?php /* @escapeNotVerified */ echo $block->escapeJsQuote($block->escapeHtml($_groupName)) ?></option>'
                    <?php endforeach ?>
                    + '</select></td>'
                    + '<td class="col-qty">'
                    + '<input class="<?php /* @escapeNotVerified */ echo $_htmlClass ?> qty required-entry validate-greater-than-zero" type="text" name="<?php /* @escapeNotVerified */ echo $_htmlName ?>[<%- data.index %>][price_qty]" value="<%- data.qty %>" id="tier_price_row_<%- data.index %>_qty" />'
                    + '<span><?php /* @escapeNotVerified */ echo __("and above") ?></span>'
                    + '</td>'
                    + '<td class="col-price"><input class="<?php /* @escapeNotVerified */ echo $_htmlClass ?> required-entry <?php /* @escapeNotVerified */ echo $_priceValueValidation ?>" type="text" name="<?php /* @escapeNotVerified */ echo $_htmlName ?>[<%- data.index %>][price]" value="<%- data.price %>" id="tier_price_row_<%- data.index %>_price" /></td>'
                    + '<td class="col-delete"><input type="hidden" name="<?php /* @escapeNotVerified */ echo $_htmlName ?>[<%- data.index %>][delete]" class="delete" value="" id="tier_price_row_<%- data.index %>_delete" />'
                    + '<button title="<?php /* @escapeNotVerified */ echo $block->escapeHtml(__('Delete Tier')); ?>" type="button" class="action- scalable delete icon-btn delete-product-option delete-select-row" id="tier_price_row_<%- data.index %>_delete_button" onclick="return tierPriceControl.deleteItem(event);">'
                    + '<span><?php /* @escapeNotVerified */ echo __("Delete") ?></span></button></td>'
                    + '</tr>';
                
                var tierPriceControl = {
                    template: mageTemplate(tierPriceRowTemplate),
                    itemsCount: 0,
                    addItem : function () {
                        <?php if ($_readonly): ?>
                            if (arguments.length < 4) {
                                return;
                            }
                        <?php endif; ?>
                        var data = {
                            website_id: '<?php /* @escapeNotVerified */ echo $block->getDefaultWebsite() ?>',
                            group: '<?php /* @escapeNotVerified */ echo $block->getDefaultCustomerGroup() ?>',
                            qty: '',
                            price: '',
                            readOnly: false,
                            index: this.itemsCount++
                        };
                        if(arguments.length >= 4) {
                            data.website_id = arguments[0];
                            data.group      = arguments[1];
                            data.qty        = arguments[2];
                            data.price      = arguments[3];
                        }
                        if (arguments.length == 5) {
                            data.readOnly = arguments[4];
                        }
                        Element.insert($('<?php /* @escapeNotVerified */ echo $_htmlId ?>_container'), {
                            bottom : this.template({
                                data: data
                            })
                        });
                        $('tier_price_row_' + data.index + '_cust_group').value = data.group;
                        $('tier_price_row_' + data.index + '_website').value    = data.website_id;
                        <?php if ($block->checkShowWebsiteColumn() && !$block->checkAllowChangeWebsite()):?>
                            var wss = $('tier_price_row_' + data.index + '_website');
                            var txt = wss.options[wss.selectedIndex].text;
                            wss.insert({after:'<span class="website-name">' + txt + '</span>'});
                            wss.hide();
                        <?php endif;?>
                        if (data.readOnly == '1') {
                            ['website', 'cust_group', 'qty', 'price', 'delete'].each(function(idx){
                                $('tier_price_row_'+data.index+'_'+idx).disabled = true;
                            });
                            $('tier_price_row_'+data.index+'_delete_button').hide();
                        }
                        <?php if ($_readonly): ?>
                            $('<?php /* @escapeNotVerified */ echo $_htmlId ?>_container').select('input', 'select').each(this.disableElement);
                            $('<?php /* @escapeNotVerified */ echo $_htmlId ?>_container').up('table').select('button').each(this.disableElement);
                        <?php else: ?>
                            $('<?php /* @escapeNotVerified */ echo $_htmlId ?>_container').select('input', 'select').each(function(el){ Event.observe(el, 'change', el.setHasChanges.bind(el)); });
                        <?php endif; ?>
                    },
                    disableElement: function(el) {
                        el.disabled = true;
                        el.addClassName('disabled');
                    },
                    deleteItem: function(event) {
                        var tr = Event.findElement(event, 'tr');
                        if (tr) {
                            Element.select(tr, '.delete').each(function(elem){elem.value='1'});
                            Element.select(tr, ['input', 'select']).each(function(elem){elem.hide()});
                            Element.hide(tr);
                            Element.addClassName(tr, 'no-display template');
                        }
                        return false;
                    }
                };
                
                <?php foreach ($block->getTierValues() as $_item): ?>
                    tierPriceControl.addItem('<?php /* @escapeNotVerified */ echo $_item['website_id'] ?>', '<?php /* @escapeNotVerified */ echo $_item['cust_group'] ?>', '<?php /* @escapeNotVerified */ echo $_item['price_qty']*1 ?>', '<?php /* @escapeNotVerified */ echo $_item['price'] ?>', <?php echo (int)!empty($_item['readonly'])?>);
                <?php endforeach; ?>
                <?php if ($_readonly): ?>
                    $('<?php /* @escapeNotVerified */ echo $_htmlId ?>_container').up('table').select('button')
                    .each(tierPriceControl.disableElement);
                <?php endif; ?>
                window.tierPriceControl = tierPriceControl;
                //]]>
            });
        </script>
    </div>
</div>
