Your IP : 216.73.216.84


Current Path : /home/helpink/www/components/com_jbusinessdirectory/assets/js/src/
Upload File :
Current File : /home/helpink/www/components/com_jbusinessdirectory/assets/js/src/jbdAI.js

const jbdAI = {
    targetField: null,
    type: null,
    field: null,
    language: null,

    showTextGenerationPopup: function (type, field, targetField, promptText, language) {
        // Store parameters
        this.type = type;
        this.field = field;
        this.targetField = targetField;
        this.language = language;

        // Remove existing modal if it exists
        jQuery('#text-generation-dialog').remove();

        // Create modal HTML
        const modalHtml = `
            <div id="text-generation-dialog" class="jbd-container message-modal">
                <div class="jmodal-md">
                    <div class="jmodal-header">
                        <p class="jmodal-header-title">${Joomla.JText._('COM_JBUSINESSDIRECTORY_AI_GENERATE_TEXT')}</p>
                        <a href="#close-modal" rel="modal:close" class="close-btn"><i class="la la-close "></i></a>
                    </div>
                    <div class="jmodal-body">
                        <div class="form-group">
                            <label for="ai-prompt">${Joomla.JText._('COM_JBUSINESSDIRECTORY_AI_PROMPT')}</label>
                            <textarea id="ai-prompt" class="form-control" rows="3">${promptText}</textarea>
                        </div>
                        <div class="form-group">
                            <button type="button" class="btn btn-primary" onclick="jbdAI.generateText();">
                                <i class="la la-magic"></i> ${Joomla.JText._('COM_JBUSINESSDIRECTORY_AI_GENERATE')}
                            </button>
                        </div>
                        <div id="ai-result" class="form-group">
                            <label for="ai-preview">${Joomla.JText._('COM_JBUSINESSDIRECTORY_AI_GENERATED_TEXT')}</label>
                            <div id="ai-preview" style="display:none;" class="border p-3 mb-3"></div>
                            <textarea id="ai-generated-text" class="form-control" rows="5"></textarea>
                        </div>
                        <div class="button-row">
                            <button type="button" class="btn btn-dark" onclick="jQuery.jbdModal.close();">
                                ${Joomla.JText._('COM_JBUSINESSDIRECTORY_CANCEL')}
                            </button>
                            <button type="button" class="btn btn-success" onclick="jbdAI.useGeneratedText();">
                                ${Joomla.JText._('COM_JBUSINESSDIRECTORY_USE_TEXT')}
                            </button>
                        </div>
                    </div>
                </div>
            </div>
        `;

        // Add modal to body and show it
        jQuery('body').append(modalHtml);
        jQuery('#text-generation-dialog').jbdModal();
    },

    generateText: function () {
        const prompt = jQuery('#ai-prompt').val();
        const loadingHtml = '<div class="text-center"><i class="la la-spinner la-spin la-2x"></i><br>' +
            Joomla.JText._('COM_JBUSINESSDIRECTORY_AI_GENERATING') + '</div>';

        jQuery('#ai-preview').show();
        jQuery('#ai-preview').html(loadingHtml);

        let url = jbdUtils.getAjaxUrl('ai', 'frontai');
        
        jQuery.ajax({
            url: url,
            type: 'POST',
            cache: false,
            data: {
                prompt: prompt,
                type: this.type,
                field: this.field,
                language: this.language,
                task: 'ai.generateText'
            },
            success: function (response) {
                try {
                    jQuery('#ai-preview').hide();
                    jQuery('#ai-preview').html("");

                    const result = typeof response === 'string' ? JSON.parse(response) : response;

                    if (result.status === 'success') {
                        jQuery('#ai-generated-text').val(result.text);
                    } else {
                        jQuery('#ai-preview').html('<div class="alert alert-danger">' +
                            (result.message || Joomla.JText._('COM_JBUSINESSDIRECTORY_AI_ERROR')) + '</div>');
                    }
                } catch (e) {
                    jQuery('#ai-preview').html('<div class="alert alert-danger">' +
                        Joomla.JText._('COM_JBUSINESSDIRECTORY_AI_ERROR') + '</div>');
                }
            },
            error: function () {
                jQuery('#ai-preview').html('<div class="alert alert-danger">' +
                    Joomla.JText._('COM_JBUSINESSDIRECTORY_AI_ERROR') + '</div>');
            }
        });
    },

    useGeneratedText: function () {
        const generatedText = jQuery('#ai-generated-text').val();
        // Replace newlines with HTML line breaks
        const formattedText = generatedText.replace(/\n/g, '<br>');

        if (jQuery(this.targetField).length > 0) {
            jQuery(this.targetField).val(generatedText);
        }
        // Check if the target field is a TinyMCE editor
        if (typeof tinyMCE !== 'undefined') {
            const textField = this.targetField.replace('#', '').replace("-", "_");
            if (tinyMCE.get(textField)) {
                tinyMCE.get(textField).setContent(formattedText, { format: 'html' });
            }
        }
        jQuery.jbdModal.close();
    }
};