HEX
Server: Apache
System: Linux srv13.cpanelhost.cl 3.10.0-962.3.2.lve1.5.38.el7.x86_64 #1 SMP Thu Jun 18 05:28:41 EDT 2020 x86_64
User: cca63905 (4205)
PHP: 7.3.20
Disabled: NONE
Upload Files
File: //proc/self/cwd/nueva/modules/btecommercecopilot/views/js/catalog-generator.js
/**
 * Ecommerce copilot
 *
 * @author    businesstech.fr <modules@businesstech.fr> - https://www.businesstech.fr/
 * @copyright https://www.businesstech.fr/
 * @license   see file: LICENSE.txt
 *
 *           ____    _______
 *          |  _ \  |__   __|
 *          | |_) |    | |
 *          |  _ <     | |
 *          | |_) |    | |
 *          |____/     |_|
 */

function updateProductDescriptionContent(value, contentElementId) {
    var contentElement = document.getElementById(contentElementId);
    if (contentElement) {
        contentElement.innerHTML = value;
    }
}

document.addEventListener('DOMContentLoaded', function () {

    function attachOnInputEvent() {
        $('#ecopProductDescription').on('input', function() {
            updateProductDescriptionContent(this.value, 'ecopProductDescriptionOverview');
        });
    }
    attachOnInputEvent();

    var toggleBtn = document.getElementById('toggleBtecop');
    var content = document.getElementById('btecopContent');

    toggleBtn.addEventListener('click', function() {
        if (content.style.display === 'none') {
            content.style.display = 'block';
            toggleBtn.innerHTML = '<i class="material-icons">visibility_off</i> <span>' + ecop.hide + '</span>';
        } else {
            content.style.display = 'none';
            toggleBtn.innerHTML = '<i class="material-icons">visibility</i> <span>' + ecop.show + '</span>';
        }
    });

    // Common function to handle AJAX requests
    function handleAjaxRequest(url, data, successCallback) {
        $.ajax({
            type: "POST",
            url: url,
            dataType: "json",
            data: { ajax: "1", ...data },
            beforeSend: function () {
                $("#btCopGeneratedProgress").removeClass('d-none');
                $("#btCopilotBlock").addClass('d-none');
                $('#invalidData').addClass('d-none');
            },
            success: function (res) {
                attachOnInputEvent();
                $("#btCopGeneratedProgress").addClass('d-none');
                $("#btCopilotBlock").removeClass('d-none');
                successCallback(res);
            }
        });
    }

    // Function to update form values
    function updateFormValues(description, dataType, action) {
        document.getElementById("ecopProductDescription").value = description;
        document.getElementById("ecopProductDescriptionOverview").innerHTML = description;
        document.getElementById("generatedDataType").value = dataType;
        document.getElementById("generateAction").value = action;
    }

    // Handle generation buttons
    ["Long", "Short", "Meta"].forEach(function(type) {
        $("#BtCopGenerate" + type + "Desc").click(function() {
            handleAjaxRequest(
                $(this).attr('data-action'),
                { langIso: $('#form_switch_language').val() },
                function(res) {
                    $("#btCopGeneratedData").removeClass('d-none');
                    updateFormValues(res.description, type.toLowerCase() + '-description', 'generate');
                }
            );
        });
    });

    // Handle translation buttons
    ["Long", "Short", "Meta"].forEach(function(type) {
        $("#BtCopTranslate" + type + "Desc").click(function() {
            handleAjaxRequest(
                $(this).attr('data-action'),
                {
                    langIsoFrom: $('#form_switch_language').val(),
                    langIdTo: $('#idLangTo').val()
                },
                function(res) {
                    $("#btCopGeneratedData").removeClass('d-none');
                    updateFormValues(res.description, type.toLowerCase() + '-description', 'translate');
                    if (res.noDescriptionToTranslate) {
                        $('#invalidData').removeClass('d-none');
                    }
                    location.reload();
                }
            );
        });
    });

    // Handle save button
    $("#BtCopSave").click(function() {
        handleAjaxRequest(
            $(this).attr('data-action'),
            {
                description_generated: $('#ecopProductDescription').val(),
                generateType: $('#generatedDataType').val(),
                generateAction: $('#generateAction').val(),
                langIso: $('#form_switch_language').val(),
                langIdTo: $('#idLangTo').val()
            },
            function(res) {
                if (res.saved) {
                    location.reload();
                } else {
                    alert('Error during product save');
                }
            }
        );
    });
});