var jQuery = jQuery || $.noConflict();

(function ($) {
    "use strict";

    function replaceLogoBox(boxSelector, fieldName, accept) {
        $(boxSelector).html('<input type="file" name="' + fieldName + '" class="form-control" accept="' + accept + '" />');
    }

    function deleteLogo(url, boxSelector, fieldName, accept) {
        var message = myLabel.confirm_delete_msg || 'Are you sure?';
        if (!window.confirm(message)) {
            return;
        }

        $.ajax({
            url: url,
            type: 'POST',
            dataType: 'json',
            success: function (response) {
                if (response && response.status === 'OK') {
                    replaceLogoBox(boxSelector, fieldName, accept);
                } else {
                    alert(response && response.text ? response.text : 'Unable to remove logo.');
                }
            }
        });
    }

    $(function () {
        if ($.fn.validate) {
            $('#frmProfile').validate({
                submitHandler: function (form) {
                    var laddaButtons = $(form).find('.ladda-button');
                    if (laddaButtons.length && $.fn.ladda) {
                        var l = laddaButtons.ladda();
                        l.ladda('start');
                    }
                    form.submit();
                }
            });
        }

        if ($.fn.multilang && typeof pjLocale !== 'undefined') {
            $('.multilang').multilang({
                langs: pjLocale.langs,
                flagPath: pjLocale.flagPath || '',
                select: function (e, data) {
                    var defaultId = $('#scProfileCopyLang').data('default-id');
                    $('#scProfileCopyLang').toggle(String(data.index) !== String(defaultId));
                }
            });
        }

        $(document).on('click', '#scProfileCopyLang', function (e) {
            e.preventDefault();
            var defaultId = $(this).data('default-id');
            var targetIndex = $('.pj-multilang-wrap:visible').first().data('index');
            if (!targetIndex || String(defaultId) === String(targetIndex)) {
                return;
            }

            $('input[name^="i18n[' + defaultId + ']"]').each(function () {
                var name = $(this).attr('name');
                var targetName = name.replace('i18n[' + defaultId + ']', 'i18n[' + targetIndex + ']');
                var target = $('input[name="' + targetName + '"]');
                if (target.length && $(this).val()) {
                    target.val($(this).val());
                }
            });
        });

        $(document).on('click', '.profile_delete_logo', function (e) {
            e.preventDefault();
            deleteLogo('index.php?controller=pjAdminProfile&action=pjActionDeleteLogo', '#profile_box_logo', 'y_logo', '.png,.jpg,.jpeg,.gif,.svg,.webp');
        });

        $(document).on('click', '.profile_delete_email_logo', function (e) {
            e.preventDefault();
            deleteLogo('index.php?controller=pjAdminProfile&action=pjActionDeleteEmailLogo', '#profile_box_email_logo', 'y_email_logo', '.png,.jpg,.jpeg');
        });
    });
})(jQuery);
