<?php

namespace GriffinForms\Admin\Html\Modals;

class UploadForm extends \GriffinForms\Admin\Html\Modals\Format
{
    protected function headerText()
    {
        return $this->lang->getText('upload_form', 'modal_header');
    }
    
    protected function modalContent()
    {
        echo '<div class="d-flex p-4">';
        echo '<div class="mb-3 small">';
        echo '<label for="griffinforms-uploadform-modal-file" class="form-label">Upload File</label>';
        echo '<input class="form-control form-control-sm" id="griffinforms-uploadform-modal-file" type="file">';
        echo '</div>';
        echo '</div>';
    }
        
    protected function modalFooter()
    {
        echo '<div class="modal-footer border-top-0 gap-2">';
        $this->cancelBtn();
        $this->ActionBtn('upload_form', 'dark');
        echo '</div>';  
    }
    
    protected function modalJs() {
        $js = '
        jQuery(document).ready(function($) {
            $("#griffinforms-modalaction-uploadform").click(function(e) {
                e.preventDefault();

                var fileInput = document.getElementById("griffinforms-uploadform-modal-file");
                var file = fileInput.files[0];
    
                if (file) {
                    var reader = new FileReader();
                    reader.onload = function(event) {
                        try {
                            var jsonData = JSON.parse(event.target.result);    
                            $.ajax({
                                url: ajaxurl,
                                type: "POST",
                                data: {
                                    action: "importForm",
                                    json_data: jsonData,
                                    nonce: "' . esc_js(wp_create_nonce("import_form")) . '"
                                },
                                success: function(response) {
                                    if (response.success) {
                                        var importedFormId = response.data.form_id;
                                        sessionStorage.setItem("highlightItemId", importedFormId);
                                        location.reload();
                                    } else {
                                        alert("' . esc_html__('Error:', 'griffinforms-form-builder') . ' " + response.data);
                                    }
                                },
                                error: function(xhr, status, error) {
                                    alert("' . esc_html__('An error occurred:', 'griffinforms-form-builder') . ' " + error);
                                }
                            });
                        } catch (e) {
                            alert("' . esc_html__('Invalid JSON file', 'griffinforms-form-builder') . '");
                        }
                    };
    
                    reader.readAsText(file);
                } else {
                    alert("' . esc_html__('Please select a JSON file.', 'griffinforms-form-builder') . '");
                }
            });
        });
        ';
    
        wp_add_inline_script('griffinforms-global-js', $js);
    }
}