<?php
if ( ! defined( 'ABSPATH' ) ) exit;
function easy_gpt_support_page() {
    ?>
    <div class="wrap">
        <h1 style="font-size: 28px; color: #333;">Support</h1>
        <p style="font-size: 16px; color: #555; line-height: 1.6;">If you need any help or have any questions about EasyGPT for WP, please feel free to reach out to us through the form below or directly via email.</p>
        
        <div style="margin-top: 30px;">
            <h2 style="font-size: 24px; color: #0073aa;">Contact Form</h2>
            <form method="post" action="<?php echo esc_url(admin_url('admin-post.php')); ?>" style="background-color: #f9f9f9; padding: 20px; border-radius: 8px; border: 1px solid #ddd; max-width: 600px;">
                <input type="hidden" name="action" value="submit_support_form">
                <?php wp_nonce_field('submit_support_form_nonce', 'submit_support_nonce'); ?>
                <p>
                    <label for="name" style="font-weight: bold;">Name:</label><br>
                    <input type="text" id="name" name="name" required style="width: 100%; padding: 10px; border: 1px solid #ddd; border-radius: 4px;">
                </p>
                <p>
                    <label for="email" style="font-weight: bold;">Email:</label><br>
                    <input type="email" id="email" name="email" required style="width: 100%; padding: 10px; border: 1px solid #ddd; border-radius: 4px;">
                </p>
                <p>
                    <label for="message" style="font-weight: bold;">Message:</label><br>
                    <textarea id="message" name="message" rows="6" required style="width: 100%; padding: 10px; border: 1px solid #ddd; border-radius: 4px;"></textarea>
                </p>
                <p>
                    <button type="submit" class="button button-primary" style="padding: 10px 20px; font-size: 16px;">Send Message</button>
                </p>
            </form>
        </div>

        <div style="margin-top: 40px;">
            <h2 style="font-size: 24px; color: #0073aa;">Email Directly</h2>
            <p style="font-size: 16px; color: #555; line-height: 1.6;">You can also email us directly at <a href="mailto:contact@easygptforwp.com" style="color: #0073aa; text-decoration: underline;">contact@easygptforwp.com</a>.</p>
        </div>
    </div>
    <?php
}

// Handle form submission
function easy_gpt_handle_submit_support_form() {
    // Verificar nonce para seguridad
    if (
        ! isset( $_POST['submit_support_nonce'] ) ||
        ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['submit_support_nonce'] ) ), 'submit_support_form_nonce' )
    ) {
        wp_die( esc_html__( 'Security check failed', 'easy-gpt-for-wp' ) );
    }

    // Sanitizar datos del formulario
    $name = isset( $_POST['name'] ) ? sanitize_text_field( wp_unslash( $_POST['name'] ) ) : '';
    $email = isset( $_POST['email'] ) ? sanitize_email( wp_unslash( $_POST['email'] ) ) : '';
    $message = isset( $_POST['message'] ) ? sanitize_textarea_field( wp_unslash( $_POST['message'] ) ) : '';

    // Validar campos requeridos
    if ( empty( $name ) || empty( $email ) || empty( $message ) ) {
        wp_die( esc_html__( 'Please fill in all required fields.', 'easy-gpt-for-wp' ) );
    }

    // Enviar el correo electrónico al administrador
    $admin_email = 'contact@easygptforwp.com';
    $subject = esc_html__( 'New Support Request', 'easy-gpt-for-wp' );
    $headers = array(
        'Content-Type: text/html; charset=UTF-8',
        'Reply-To: ' . sanitize_email( $email )
    );

    $body = '
        <h2 style="color: #333;">' . esc_html__( 'New Support Request', 'easy-gpt-for-wp' ) . '</h2>
        <p><strong>' . esc_html__( 'Name:', 'easy-gpt-for-wp' ) . '</strong> ' . esc_html( $name ) . '</p>
        <p><strong>' . esc_html__( 'Email:', 'easy-gpt-for-wp' ) . '</strong> ' . esc_html( $email ) . '</p>
        <p><strong>' . esc_html__( 'Message:', 'easy-gpt-for-wp' ) . '</strong><br>' . nl2br( esc_html( $message ) ) . '</p>
    ';

    $sent = wp_mail( $admin_email, $subject, $body, $headers );

    // Redirigir de vuelta a la página de soporte con mensaje de éxito o error
    if ( $sent ) {
        wp_redirect( esc_url_raw( add_query_arg( 'sent', 'success', admin_url( 'admin.php?page=easy-gpt-support' ) ) ) );
    } else {
        wp_redirect( esc_url_raw( add_query_arg( 'sent', 'error', admin_url( 'admin.php?page=easy-gpt-support' ) ) ) );
    }
    exit;
}
add_action( 'admin_post_submit_support_form', 'easy_gpt_handle_submit_support_form' );


// Show success or error message after form submission
function easy_gpt_show_support_form_feedback() {
    if ( isset( $_GET['sent'] ) ) {
        $sent_status = sanitize_text_field( wp_unslash( $_GET['sent'] ) );
        if ( $sent_status === 'success' ) {
            echo '<div class="notice notice-success is-dismissible"><p>' . esc_html__( 'Thank you! Your message has been sent successfully.', 'easy-gpt-for-wp' ) . '</p></div>';
        } elseif ( $sent_status === 'error' ) {
            echo '<div class="notice notice-error is-dismissible"><p>' . esc_html__( 'Sorry, there was an issue sending your message. Please try again later.', 'easy-gpt-for-wp' ) . '</p></div>';
        }
    }
}
add_action( 'admin_notices', 'easy_gpt_show_support_form_feedback' );
?>
