/**
* 2007-2024 PrestaShop
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License (AFL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/afl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to http://www.prestashop.com for more information.
*
*  @author    PrestaShop SA <contact@prestashop.com>
*  @copyright 2007-2024 PrestaShop SA
*  @license   http://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
*  International Registered Trademark & Property of PrestaShop SA
*
* Don't forget to prefix your containers with your own identifier
* to avoid any conflicts with others containers.
*/
$(document).ready(function() {
    initChoiceCollapse();
    initSelect2();

    $('#form_filter_ssellerinfo').on('change', filterCategoryBysellerinfo)
})

function initChoiceCollapse() {
    $('.choice-tree-actions .js-toggle-choice-tree-action').on('click', function() {
        var presentIcon = $(this).find('.material-icons').text();
        var expandedIcon = $(this).data('expanded-icon');

        if (presentIcon === expandedIcon) {
            $(this).find('.material-icons').text($(this).data('collapsed-icon'));
            $(this).find('.js-toggle-text').text($(this).data('collapsed-text'));

            $(this).closest('.js-choice-tree-container')
                .find('.choice-tree .collapsed')
                .removeClass('collapsed')
                .addClass('expanded')
        } else {
            $(this).find('.material-icons').text($(this).data('expanded-icon'));
            $(this).find('.js-toggle-text').text($(this).data('expanded-text'));

            $(this).closest('.js-choice-tree-container')
                .find('.choice-tree .expanded')
                .removeClass('expanded')
                .addClass('collapsed')
        }
    })

    $('.choice-tree .js-input-wrapper').on('click', function() {
        $(this).parent('li').toggleClass('expanded').toggleClass('collapsed');
    })
}

function initSelect2() {
    $('#form_id_order_state_rt_commission').select2({
        templateSelection: disabledStatusesOrderStateRtCommission
    });

    $('#form_id_order_state_ct_commission').select2({
        templateSelection: disabledStatusesOrderStateCtCommission
    });

    if (typeof selectPlaceholder == 'undefined') {
        selectPlaceholder = 'Select an option';
    }

    if ($('select.agile-select2').length > 0) {
        $('select.agile-select2').select2({placeholder: selectPlaceholder});

        var inputLength = $('.select2-search__field').attr('placeholder').length;

        if (inputLength > 10) {
            $('.select2-search__field').attr('size', inputLength)
        }
    }
}

function disabledStatusesOrderStateRtCommission(value) {
    var $el = $('#form_id_order_state_ct_commission');
    var values = $('#form_id_order_state_rt_commission').val();

    $el.find('option').attr('disabled', false);
    if (values) {
        values.forEach(function(id) {
            $el.find('option[value=' + +id + ']').attr('disabled', true);
        });
    }

    return value.text;
}

function disabledStatusesOrderStateCtCommission(value) {
    var $el = $('#form_id_order_state_rt_commission');
    var values = $('#form_id_order_state_ct_commission').val();

    $el.find('option').attr('disabled', false);
    if (values) {
        values.forEach(function(id) {
            $el.find('option[value=' + +id + ']').attr('disabled', true);
        });
    }

    return value.text;
}

function filterCategoryBysellerinfo(e) {
    e.preventDefault();
    var idsSellerinfo = $('#form_filter_ssellerinfo').val();

    if (!idsSellerinfo || idsSellerinfo.length < 1) {
        $('#form_categories .js-input-wrapper').show();
        return;
    }

    $.ajax({
        type: 'POST',
        url: linkAgileAjax,
        data: {
            ajax: true,
            action: 'getSellersCategories',
            idsSellerinfo: idsSellerinfo,
        },
        dataType: 'json',
        async: true,
        success: function (response) {
            if (
                typeof response.idsCategories != 'undefined'
                && response.idsCategories
                && response.idsCategories.length > 0
            ) {
                $('#form_categories .js-input-wrapper').hide();
                response.idsCategories.forEach(function(el) {
                    $('#form_categories input[value=' + +el + ']').closest('.js-input-wrapper').show();
                })
            } else {
                $('#form_categories .js-input-wrapper').show();
            }
        },
    });
}
