<?php
// phpcs:disable WordPress.DB.DirectDatabaseQuery,WordPress.NamingConventions.PrefixAllGlobals -- Frontend template uses plugin-owned data and local rendering variables.
/**
 * Frontend Basic Newsletter Template
 * Clean, modern design with left/right image positioning support
 */

if (!defined('ABSPATH')) exit; // Exit if accessed directly
$instant_popup_builder_plugin_public = new Instant_Popup_Subscription_Public( 'Instant Popup Builder', INSTANT_POPUP_BUILDER_VERSION );
// Get subscription settings for this popup
global $instant_popup_subscription_public;
$instant_popup_builder_subscription_settings = $instant_popup_builder_plugin_public->get_subscription_settings();

$instant_popup_builder_subscription_subscriber = $instant_popup_builder_plugin_public->get_subscription_count();
// Get privacy policy URL or page
$instant_popup_builder_privacy_url_final = '';
if (isset($instant_popup_builder_subscription_settings['privacy_link_type']) && $instant_popup_builder_subscription_settings['privacy_link_type'] == 'page' && !empty($instant_popup_builder_subscription_settings['privacy_policy_page'])) {
    $instant_popup_builder_privacy_url_final = get_permalink($instant_popup_builder_subscription_settings['privacy_policy_page']);
} elseif (!empty($instant_popup_builder_subscription_settings['privacy_policy_url'])) {
    $instant_popup_builder_privacy_url_final = $instant_popup_builder_subscription_settings['privacy_policy_url'];
}

// Get terms & conditions URL or page
$instant_popup_builder_terms_url_final = '';
if (isset($instant_popup_builder_subscription_settings['terms_link_type']) && $instant_popup_builder_subscription_settings['terms_link_type'] == 'page' && !empty($instant_popup_builder_subscription_settings['terms_conditions_page'])) {
    $instant_popup_builder_terms_url_final = get_permalink($instant_popup_builder_subscription_settings['terms_conditions_page']);
} elseif (!empty($instant_popup_builder_subscription_settings['terms_conditions_url'])) {
    $instant_popup_builder_terms_url_final = $instant_popup_builder_subscription_settings['terms_conditions_url'];
}

$instant_popup_builder_agreement_html = function_exists('ipb_render_subscription_agreement_links') ? ipb_render_subscription_agreement_links($instant_popup_builder_subscription_settings, $instant_popup_builder_privacy_url_final, $instant_popup_builder_terms_url_final) : '';

// Get image position setting from advance data
global $wpdb;
$instant_popup_builder_popup_id = get_transient('instant_popup_builder_popup_id');
$instant_popup_builder_image_position = 'right'; // Default

// Initialize template variables with defaults
$instant_popup_builder_popup_main_title = 'Subscribe to Our Newsletter';
$instant_popup_builder_popup_description = 'Get the latest updates and news delivered to your inbox.';
$instant_popup_builder_first_label = 'Full Name';
$instant_popup_builder_first_placeholder = 'Enter your full name';
$instant_popup_builder_email_label = 'Email Address';
$instant_popup_builder_email_placeholder = 'Enter your email address';
$instant_popup_builder_button_text = 'Subscribe Now';
$instant_popup_builder_button_color = '#3b82f6';

if ($instant_popup_builder_popup_id) {
    $instant_popup_builder_popup_data = $wpdb->get_row($wpdb->prepare("SELECT * FROM {$wpdb->prefix}instant_popup_builder WHERE id = %d", $instant_popup_builder_popup_id));

    if ($instant_popup_builder_popup_data) {
        // Get image position from advance data
        if (isset($instant_popup_builder_popup_data->advance)) {
            $instant_popup_builder_advance_data = json_decode($instant_popup_builder_popup_data->advance, true);
            if ($instant_popup_builder_advance_data && isset($instant_popup_builder_advance_data['image_position'])) {
                $instant_popup_builder_image_position = $instant_popup_builder_advance_data['image_position'];
            }
        }

        // Also get other template data from advance data to ensure consistency
        if (isset($instant_popup_builder_popup_data->advance)) {
            $instant_popup_builder_advance_data = json_decode($instant_popup_builder_popup_data->advance, true);
            if ($instant_popup_builder_advance_data && isset($instant_popup_builder_advance_data['template_style']) && $instant_popup_builder_advance_data['template_style'] === 'basic') {
                // Override variables with saved data if they exist
                $instant_popup_builder_popup_main_title = isset($instant_popup_builder_advance_data['popup_main_title']) ? $instant_popup_builder_advance_data['popup_main_title'] : $instant_popup_builder_popup_main_title;
                $instant_popup_builder_popup_description = isset($instant_popup_builder_advance_data['popup_description']) ? $instant_popup_builder_advance_data['popup_description'] : $instant_popup_builder_popup_description;
                $instant_popup_builder_first_label = isset($instant_popup_builder_advance_data['first_label']) ? $instant_popup_builder_advance_data['first_label'] : $instant_popup_builder_first_label;
                $instant_popup_builder_first_placeholder = isset($instant_popup_builder_advance_data['first_placeholder']) ? $instant_popup_builder_advance_data['first_placeholder'] : $instant_popup_builder_first_placeholder;
                $instant_popup_builder_email_label = isset($instant_popup_builder_advance_data['email_label']) ? $instant_popup_builder_advance_data['email_label'] : $instant_popup_builder_email_label;
                $instant_popup_builder_email_placeholder = isset($instant_popup_builder_advance_data['email_placeholder']) ? $instant_popup_builder_advance_data['email_placeholder'] : $instant_popup_builder_email_placeholder;
                $instant_popup_builder_button_text = isset($instant_popup_builder_advance_data['button_text']) ? $instant_popup_builder_advance_data['button_text'] : $instant_popup_builder_button_text;
                $instant_popup_builder_button_color = isset($instant_popup_builder_advance_data['button_color']) ? $instant_popup_builder_advance_data['button_color'] : $instant_popup_builder_button_color;
            }
        }
    }
}

// Get image URL from parent scope (set in shortcode-subscription.php)
$image_url = isset($instant_popup_builder_image_url) ? $instant_popup_builder_image_url : '';

// Determine layout classes
$instant_popup_builder_has_image = !empty($image_url);
$instant_popup_builder_container_classes = ['basic-newsletter-container'];

if ($instant_popup_builder_has_image) {
    $instant_popup_builder_container_classes[] = 'has-image';
    $instant_popup_builder_container_classes[] = 'image-' . $instant_popup_builder_image_position;
}
?>

<div class="<?php echo esc_attr(implode(' ', is_array($instant_popup_builder_container_classes) ? $instant_popup_builder_container_classes : [])); ?>">

    <?php if ($instant_popup_builder_has_image && $instant_popup_builder_image_position === 'left'): ?>
        <div class="newsletter-image-section">
            <div class="image-wrapper">
                <img src="<?php echo esc_url($image_url); ?>" alt="Newsletter" class="newsletter-image">
            </div>
        </div>
    <?php endif; ?>

    <div class="newsletter-content-section">
        <div class="content-wrapper">

            <div class="newsletter-header">
                <h2 class="newsletter-title"><?php echo esc_html($instant_popup_builder_popup_main_title); ?></h2>
                <?php if ($instant_popup_builder_popup_description): ?>
                    <p class="newsletter-description"><?php echo esc_html($instant_popup_builder_popup_description); ?></p>
                <?php endif; ?>
            </div>

            <form class="newsletter-form" method="post">
                <div class="form-row">
                    <?php if ($instant_popup_builder_first_label): ?>
                        <label class="form-label" for="newsletter_first_name"><?php echo esc_html($instant_popup_builder_first_label); ?></label>
                    <?php endif; ?>
                    <input
                        type="text"
                        id="newsletter_first_name"
                        name="first_name"
                        placeholder="<?php echo esc_attr($instant_popup_builder_first_placeholder); ?>"
                        class="form-input"
                        required
                    >
                </div>

                <div class="form-row">
                    <?php if ($instant_popup_builder_email_label): ?>
                        <label class="form-label" for="newsletter_email"><?php echo esc_html($instant_popup_builder_email_label); ?></label>
                    <?php endif; ?>
                    <input
                        type="email"
                        id="newsletter_email"
                        name="email_address"
                        placeholder="<?php echo esc_attr($instant_popup_builder_email_placeholder); ?>"
                        class="form-input"
                        required
                    >
                </div>

                <?php if ($instant_popup_builder_agreement_html): ?>
                    <div class="form-row privacy-row">
                        <label class="privacy-label">
                            <input type="checkbox" required class="privacy-checkbox" name="privacy_policy" value="yes">
                            <span class="checkbox-custom"></span>
                            <span class="privacy-text">
                                <?php echo wp_kses_post($instant_popup_builder_agreement_html); ?>
                            </span>
                        </label>
                    </div>
                <?php endif; ?>

                <input type="hidden" name="action" value="save_subscribers">
                <input type="hidden" name="campaign" value="">
                <?php wp_nonce_field('instant_popup_subscriber_ajax_action', 'instant_popup_subscriber_ajax'); ?>

                <div class="form-row submit-row">
                    <button
                        type="submit"
                        class="newsletter-submit-btn"
                        id="newsletter_submit_btn"
                        style="<?php echo $instant_popup_builder_button_color ? 'background-color: ' . esc_attr($instant_popup_builder_button_color) . ';' : ''; ?>"
                    >
                        <?php echo esc_html($instant_popup_builder_button_text); ?>
                    </button>
                </div>

                <div class="message" style="display: none;"></div>
            </form>

        </div>
    </div>

    <?php if ($instant_popup_builder_has_image && $instant_popup_builder_image_position === 'right'): ?>
        <div class="newsletter-image-section">
            <div class="image-wrapper">
                <img src="<?php echo esc_url($image_url); ?>" alt="Newsletter" class="newsletter-image">
            </div>
        </div>
    <?php endif; ?>

</div>

<style>
/* Basic Newsletter Template - Fresh Modern Design */
.basic-newsletter-container {
    display: flex;
    background: #ffffff;
    border-radius: 12px;
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.12);
    overflow: hidden;
    max-width: 850px;
    width: 100%;
    margin: 0 auto;
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
    min-height: 480px;
    position: relative;
}

/* Content Only Layout (No Image) */
.basic-newsletter-container:not(.has-image) {
    max-width: 500px;
    justify-content: center;
}

.basic-newsletter-container:not(.has-image) .newsletter-content-section {
    width: 100%;
    max-width: 100%;
}

/* With Image Layouts */
.basic-newsletter-container.has-image {
    flex-direction: row;
}

.basic-newsletter-container.has-image.image-left {
    flex-direction: row;
}

.basic-newsletter-container.has-image.image-right {
    flex-direction: row-reverse;
}

/* Image Section */
.newsletter-image-section {
    flex: 0 0 320px;
    background: linear-gradient(135deg, #f8f9fa 0%, #e9ecef 100%);
    display: flex;
    align-items: center;
    justify-content: center;
    position: relative;
    overflow: hidden;
}

.image-wrapper {
    width: 100%;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
}

.newsletter-image {
    width: 100%;
    height: 100%;
    object-fit: cover;
    object-position: center;
    transition: transform 0.3s ease;
}

.newsletter-image:hover {
    transform: scale(1.02);
}

/* Content Section */
.newsletter-content-section {
    flex: 1;
    display: flex;
    align-items: center;
    min-height: 480px;
    padding: 0;
}

.content-wrapper {
    width: 100%;
    padding: 50px 45px;
    max-width: 100%;
}

/* Header */
.newsletter-header {
    margin-bottom: 35px;
    text-align: center;
}

.newsletter-title {
    font-size: 32px;
    font-weight: 700;
    color: #1a1a1a;
    margin: 0 0 16px 0;
    line-height: 1.2;
    letter-spacing: -0.5px;
}

.newsletter-description {
    font-size: 16px;
    color: #6c757d;
    margin: 0;
    line-height: 1.5;
    font-weight: 400;
}

/* Form Styles */
.newsletter-form {
    width: 100%;
}

.form-row {
    margin-bottom: 20px;
}

.form-row.submit-row {
    margin-bottom: 10px;
    margin-top: 30px;
}

.form-row.privacy-row {
    margin-bottom: 25px;
}

.form-label {
    display: block;
    font-size: 14px;
    font-weight: 600;
    color: #374151;
    margin-bottom: 8px;
    letter-spacing: 0.025em;
}

.form-input {
    width: 100%;
    padding: 16px 18px;
    border: 2px solid #e5e7eb;
    border-radius: 8px;
    font-size: 16px;
    font-weight: 400;
    color: #374151;
    background: #ffffff;
    transition: all 0.2s ease;
    box-sizing: border-box;
}

.form-input:focus {
    outline: none;
    border-color: #3b82f6;
    box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.1);
    transform: translateY(-1px);
}

.form-input::placeholder {
    color: #9ca3af;
    font-weight: 400;
}

/* Privacy Checkbox */
.privacy-label {
    display: flex;
    align-items: flex-start;
    gap: 12px;
    cursor: pointer;
    font-size: 14px;
    line-height: 1.5;
    color: #6b7280;
}

.privacy-checkbox {
    appearance: none;
    width: 18px;
    height: 18px;
    border: 2px solid #d1d5db;
    border-radius: 4px;
    background: #ffffff;
    cursor: pointer;
    position: relative;
    margin: 0;
    flex-shrink: 0;
    margin-top: 2px;
}

.privacy-checkbox:checked {
    background: #3b82f6;
    border-color: #3b82f6;
}

.privacy-checkbox:checked::after {
    content: '';
    position: absolute;
    top: 1px;
    left: 5px;
    width: 6px;
    height: 10px;
    border: solid white;
    border-width: 0 2px 2px 0;
    transform: rotate(45deg);
}

.privacy-text a {
    color: #3b82f6;
    text-decoration: none;
    font-weight: 500;
}

.privacy-text a:hover {
    text-decoration: underline;
}

/* Submit Button */
.newsletter-submit-btn {
    width: 100%;
    background: #3b82f6;
    color: #ffffff;
    border: none;
    padding: 18px 24px;
    border-radius: 8px;
    font-size: 16px;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.2s ease;
    letter-spacing: 0.025em;
    text-transform: none;
}

.newsletter-submit-btn:hover {
    background: #2563eb;
    transform: translateY(-2px);
    box-shadow: 0 8px 25px rgba(59, 130, 246, 0.3);
}

.newsletter-submit-btn:active {
    transform: translateY(0);
}

.newsletter-submit-btn:disabled {
    background: #9ca3af;
    cursor: not-allowed;
    transform: none;
    box-shadow: none;
}

/* Form Message */
.message {
    text-align: center;
    font-size: 14px;
    margin-top: 15px;
    padding: 8px;
    border-radius: 6px;
    display: none;
}

.message.success {
    background: #d1fae5;
    color: #065f46;
    border: 1px solid #a7f3d0;
}

.message.error {
    background: #fee2e2;
    color: #dc2626;
    border: 1px solid #fca5a5;
}

/* Responsive Design */
@media (max-width: 1024px) {
    .basic-newsletter-container {
        max-width: 95%;
        margin: 20px auto;
    }

    .newsletter-image-section {
        flex: 0 0 280px;
    }

    .content-wrapper {
        padding: 40px 35px;
    }
}

@media (max-width: 768px) {
    .basic-newsletter-container.has-image {
        flex-direction: column !important;
        max-width: 95%;
        min-height: auto;
    }

    .basic-newsletter-container.has-image.image-left,
    .basic-newsletter-container.has-image.image-right {
        flex-direction: column !important;
    }

    .newsletter-image-section {
        flex: none;
        width: 100%;
        height: 240px;
        order: 1;
    }

    .newsletter-content-section {
        order: 2;
        min-height: auto;
    }

    .content-wrapper {
        padding: 35px 30px;
    }

    .newsletter-title {
        font-size: 28px;
    }

    .newsletter-description {
        font-size: 15px;
    }

    .form-input {
        padding: 14px 16px;
        font-size: 15px;
    }

    .newsletter-submit-btn {
        padding: 16px 20px;
        font-size: 15px;
    }
}

@media (max-width: 480px) {
    .basic-newsletter-container {
        margin: 10px;
        border-radius: 8px;
    }

    .content-wrapper {
        padding: 30px 25px;
    }

    .newsletter-title {
        font-size: 24px;
    }

    .newsletter-description {
        font-size: 14px;
    }

    .form-input {
        padding: 12px 14px;
        font-size: 14px;
    }

    .newsletter-submit-btn {
        padding: 14px 18px;
        font-size: 14px;
    }

    .newsletter-image-section {
        height: 200px;
    }
}

/* Loading States */
.newsletter-submit-btn.loading {
    position: relative;
    color: transparent;
}

.newsletter-submit-btn.loading::after {
    content: '';
    position: absolute;
    top: 50%;
    left: 50%;
    width: 20px;
    height: 20px;
    margin: -10px 0 0 -10px;
    border: 2px solid transparent;
    border-top: 2px solid #ffffff;
    border-radius: 50%;
    animation: spin 1s linear infinite;
}

@keyframes spin {
    0% { transform: rotate(0deg); }
    100% { transform: rotate(360deg); }
}

/* Focus Styles for Accessibility */
.form-input:focus,
.newsletter-submit-btn:focus,
.privacy-checkbox:focus {
    outline: 2px solid #3b82f6;
    outline-offset: 2px;
}

/* High Contrast Mode Support */
@media (prefers-contrast: high) {
    .basic-newsletter-container {
        border: 2px solid #000000;
    }

    .form-input {
        border-color: #000000;
    }

    .newsletter-submit-btn {
        border: 2px solid #000000;
    }
}

/* Reduced Motion Support */
@media (prefers-reduced-motion: reduce) {
    .newsletter-image,
    .newsletter-submit-btn,
    .form-input {
        transition: none;
    }

    .newsletter-submit-btn.loading::after {
        animation: none;
    }
}
</style>
</style>
<?php if($instant_popup_builder_subscription_subscriber == 1000){ ?>
<script>
    jQuery(document).ready(function($) {
        $('[data-template="basic"]').remove();
    });
</script>
<?php } ?>
