<?php
/**
 * Copyright © 2016 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */

// @codingStandardsIgnoreFile

/** @var $block \Magento\Tax\Block\Adminhtml\Rule\Edit\Form */
?>
<script>
require([
    "jquery",
    'Magento_Ui/js/modal/alert',
    "jquery/ui",
    "jquery/editableMultiselect/js/jquery.multiselect",
    "mage/mage",
    'Magento_Ui/js/modal/modal'
], function($, alert){

    $.widget("adminhtml.dialogRates", $.mage.modal, {
        options: {
            itemRate: {},
            itemRateDefault: {}
        },
        _create: function() {
            this._super();
            this._getFormData(this.options.itemRateDefault);
        },
        openModal: function() {
            var zipIsRange = this.modal.find('#zip_is_range');

            this._applyItem(this.options.itemRateDefault);
            if (this.options.itemRate && !$.isEmptyObject(this.options.itemRate)) {
                this._applyItem(this.options.itemRate);
            }
            zipIsRange.attr('checked', zipIsRange.val() == 1);
            zipIsRange.trigger('change');
            updater.update();
            this._super();
        },
        closeModal: function() {
            this._super();
        },
        _applyItem: function(rate) {
            var dialogElement = this.modal;

            $.each(rate, function(key, value) {
                if (!value) {
                    value = '';
                }
                dialogElement.find('[name="' + key + '"]').attr('value', value);
            });
        },
        updateItemRate: function() {
            this._getFormData(this.options.itemRate);
        },
        _getFormData: function(data) {
            $.each(this.modal.find(':input'), function() {
                if (this.name) {
                    data[this.name] = this.value
                }
            });
        }
    });

    TaxRateEditableMultiselect = function (settings) {
        this.settings = settings || {};
        this.isEntityEditable = this.settings.is_entity_editable || false;

        this.edit = function() {
            var listItem = $(this).closest('.mselect-list-item');
            var that = listItem.children('.mselect-edit');
            var index = listItem.index(),
                select = that.closest('.mselect-list').prevAll('.multiselect:first'),
                id = select.find('option').eq(index).attr('value'),
                item;

            $('body').trigger('processStart');
            $.ajax({
                type: "POST",
                data: {id:id},
                url: '<?php /* @escapeNotVerified */ echo $block->getTaxRateLoadUrl()?>',
                success: function(result, status) {
                    $('body').trigger('processStop');
                    if (result.success) {
                        item=result.result;
                        item.itemElement = that.prev();
                        $('#tax-rate-form')
                            .dialogRates({itemRate: item})
                            .dialogRates('openModal');

                    } else {
                        if (result.error_message)
                            alert({
                                content: result.error_message
                            });
                        else
                            alert({
                                content: '<?php /* @escapeNotVerified */ echo __('An error occurred'); ?>'
                            });
                    }
                },
                error: function () {
                    $('body').trigger('processStop');
                    alert({
                        content: '<?php /* @escapeNotVerified */ echo __('An error occurred'); ?>'
                    });
                },
                dataType: "json"
            });
        };

        TaxRateEditableMultiselect.prototype.init = function () {
            var options = {
                toggleAddButton:false,
                addText: '<?php /* @escapeNotVerified */ echo __('Add New Tax Rate'); ?>',
                parse: null,
                mselectInputSubmitCallback: function (value, options) {
                    var select = $('#tax_rate');
                    select.append('<option value="" selected="selected">' + value + '</option>');
                    var mselectItemHtml = $(options.item.replace(/%value%|%label%/gi, value)
                        .replace(/%mselectDisabledClass%|%iseditable%|%isremovable%/gi, '')
                        .replace(/%mselectListItemClass%/gi, options.mselectListItemClass))
                        .find('[type=checkbox]')
                        .attr('checked', true)
                        .addClass(options.mselectCheckedClass)
                        .end();
                    var itemsWrapper = select.nextAll('section.block:first')
                        .find('.' + options.mselectItemsWrapperClass + '');
                    itemsWrapper.children('.' + options.mselectListItemClass + '').length
                        ? itemsWrapper.children('.' + options.mselectListItemClass + ':last').after(mselectItemHtml)
                        : itemsWrapper.prepend(mselectItemHtml);
                }
            };
            var taxRate = $('#tax_rate'),
                taxRateField = taxRate.parent(),
                taxRateForm = $('#tax-rate-form'),
                taxRateFormElement = $('#<?php /* @escapeNotVerified */ echo \Magento\Tax\Block\Adminhtml\Rate\Form::FORM_ELEMENT_ID ?>');

            if (!this.isEntityEditable) {
                // Override default layout of editable multiselect
                options['layout'] = '<section class="block %mselectListClass%">'
                        + '<div class="block-content"><div class="%mselectItemsWrapperClass%">'
                        + '%items%'
                        + '</div></div>'
                        + '<div class="%mselectInputContainerClass%">'
                        + '<input type="text" class="%mselectInputClass%" title="%inputTitle%"/>'
                        + '<span class="%mselectButtonCancelClass%" title="%cancelText%"></span>'
                        + '<span class="%mselectButtonSaveClass%" title="Add"></span>'
                        + '</div>'
                        + '</section>';
                options['mselectInputSubmitCallback'] = null;
            }

            taxRate.multiselect(options);

            taxRateField.find('.mselect-button-add').off('click');

            taxRateField.find('.mselect-list')
                    .on('click.mselect-edit', '.mselect-edit', this.edit)
                    .on("click.mselect-delete", ".mselect-delete", function () {
                        if (!confirm('<?php /* @escapeNotVerified */ echo __('Do you really want to delete this tax rate?'); ?>')) {
                            return;
                        }

                        var that = $(this),
                                index = that.parent().index(),
                                select = that.closest('.mselect-list').prev();

                        $('body').trigger('processStart');
                        var ajaxOptions = {
                            type: 'POST',
                            data: {
                                tax_calculation_rate_id: select.find('option').eq(index).val(),
                                form_key: $('input[name="form_key"]').val()
                            },
                            dataType: 'json',
                            url: '<?php /* @escapeNotVerified */ echo $block->getTaxRateDeleteUrl()?>',
                            success: function(result, status) {
                                $('body').trigger('processStop');
                                if (result.success) {
                                    that.parent().remove();
                                    select.find('option').eq(index).remove();
                                } else {
                                    if (result.error_message)
                                        alert({
                                            content: result.error_message
                                        });
                                    else
                                        alert({
                                            content: '<?php /* @escapeNotVerified */ echo __('An error occurred'); ?>'
                                        });
                                }
                            },
                            error: function () {
                                $('body').trigger('processStop');
                                alert({
                                    content: '<?php /* @escapeNotVerified */ echo __('An error occurred'); ?>'
                                });
                            }
                        };
                        $.ajax(ajaxOptions);

                    })
                    .on('click.mselectAdd', '.mselect-button-add', function () {
                        taxRateForm
                                .dialogRates({itemRate: {}})
                                .dialogRates('openModal');
                    })
                    .on('click.mselect-checked', '.mselect-list-item input', function (event) {
                        var el = $(this),
                                checkedClassName = 'mselect-checked';
                        el[el.is(':checked') ? 'addClass' : 'removeClass'](checkedClassName);
                    })
                    .on('dblclick.mselect-list-item', '.mselect-list-item label span', this.edit);
            taxRateFormElement.mage('form').mage('validation');

            taxRateForm.dialogRates({
                title: '<?php /* @escapeNotVerified */ echo __('Tax Rate'); ?>',
                type: 'slide',
                id: '<?php /* @escapeNotVerified */ echo $block->getJsId() ?>',
                modalClass: 'tax-rate-popup',
                closed: function () {
                    taxRateFormElement.data('validation').clearError();
                },
                buttons: [{
                    text: '<?php /* @escapeNotVerified */ echo __('Save'); ?>',
                    'class': 'action-save action-primary',
                    click: function() {
                        this.updateItemRate();
                        var itemRate = this.option('itemRate'),
                            itemRateData = $.extend({}, itemRate);

                        if (itemRateData.itemElement) {
                            delete itemRateData.itemElement;
                        }

                        if (!taxRateFormElement.validation().valid()) {
                            return;
                        }
                        $('.tax-rate-popup').trigger('processStart');
                        $('.loading-mask').css('z-index','1004');
                        var ajaxOptions = {
                            type: 'POST',
                            data: itemRateData,
                            dataType: 'json',
                            url: '<?php /* @escapeNotVerified */ echo $block->getTaxRateSaveUrl()?>',
                            success: function(result, status) {
                                $('body').trigger('processStop');
                                if (result.success) {
                                    itemRate.code = result.code;
                                    if (itemRate.tax_calculation_rate_id) {
                                        itemRate.itemElement.find('span').html(itemRate.code);
                                        var index = itemRate.itemElement.parent().index();
                                        itemRate.itemElement.closest('.mselect-list').prev().find('option').eq(index)
                                            .html(itemRate.code)
                                            .val(itemRate.tax_calculation_rate_id);
                                    } else {
                                        itemRate.tax_calculation_rate_id = result.tax_calculation_rate_id;
                                        taxRateField.find('.mselect-input').val(itemRate.code);
                                        taxRateField.find('.mselect-save').trigger('mousedown');
                                        taxRateField.find('option[value=""]:last')
                                            .val(itemRate.tax_calculation_rate_id);
                                    }
                                    taxRateForm.dialogRates('closeModal');
                                } else {
                                    if (result.error_message)
                                        alert({
                                            content: result.error_message
                                        });
                                    else
                                        alert({
                                            content: '<?php /* @escapeNotVerified */ echo __('An error occurred'); ?>'
                                        });
                                }
                            },
                            error: function () {
                                $('body').trigger('processStop');
                                alert({
                                    content: '<?php /* @escapeNotVerified */ echo __('An error occurred'); ?>'
                                });
                            }
                        };
                        $.ajax(ajaxOptions);
                    }
                }]
            });
            $('.grid-loading-mask').hide();
        }
    };

    window.TaxRateEditableMultiselect = TaxRateEditableMultiselect;
});
</script>
