<?php
// Prevent direct access
if (!defined('ABSPATH')) {
    exit;
}

// Prepare inline styles based on shortcode attributes
$leadmachine_container_style = 'background: ' . esc_attr($atts['background']) . ';';
if ($atts['background'] === 'transparent') {
    $leadmachine_container_style .= ' box-shadow: none;';
}

// Generate unique form ID for multiple forms on same page
$form_id = 'leadmachine-newsletter-' . wp_rand(1000, 9999);
$leadmachine_form_uid = 'lm-' . wp_rand(1000, 9999);
?>
<style>
#<?php echo esc_attr($leadmachine_form_uid); ?> input[type="text"],
#<?php echo esc_attr($leadmachine_form_uid); ?> input[type="email"] {
    background-color: <?php echo esc_attr($atts['input_background']); ?>;
    border-color: <?php echo esc_attr($atts['input_border_color']); ?>;
    color: <?php echo esc_attr($atts['input_text_color']); ?>;
}
#<?php echo esc_attr($leadmachine_form_uid); ?> input::placeholder {
    color: <?php echo esc_attr($atts['input_text_color']); ?>;
    opacity: 0.6;
}
</style>
<div id="<?php echo esc_attr($leadmachine_form_uid); ?>" class="leadmachine-form-container leadmachine-newsletter-container" style="<?php echo esc_attr($leadmachine_container_style); ?>">
    <div class="leadmachine-form-header">
        <?php if (!empty($atts['title'])): ?>
            <h3 class="leadmachine-form-title" style="color: <?php echo esc_attr($atts['title_color']); ?>;">
                <?php echo esc_html($atts['title']); ?>
            </h3>
        <?php endif; ?>

        <?php if (!empty($atts['description'])): ?>
            <p class="leadmachine-form-description" style="color: <?php echo esc_attr($atts['description_color']); ?>;">
                <?php echo esc_html($atts['description']); ?>
            </p>
        <?php endif; ?>
    </div>

    <form class="leadmachine-form leadmachine-newsletter-form" id="<?php echo esc_attr($form_id); ?>" method="post" data-form-type="newsletter">
        <div class="leadmachine-form-messages"></div>

        <div class="leadmachine-form-row">
            <div class="leadmachine-form-group">
                <label for="<?php echo esc_attr($form_id); ?>-name" style="color: <?php echo esc_attr($atts['label_color']); ?>;">
                    <?php echo esc_html($atts['name_label']); ?> <span class="leadmachine-required">*</span>
                </label>
                <input
                    type="text"
                    id="<?php echo esc_attr($form_id); ?>-name"
                    name="full_name"
                    required
                    placeholder="<?php echo esc_attr($atts['name_placeholder']); ?>"
                />
            </div>
        </div>

        <div class="leadmachine-form-row">
            <div class="leadmachine-form-group">
                <label for="<?php echo esc_attr($form_id); ?>-email" style="color: <?php echo esc_attr($atts['label_color']); ?>;">
                    <?php echo esc_html($atts['email_label']); ?> <span class="leadmachine-required">*</span>
                </label>
                <input
                    type="email"
                    id="<?php echo esc_attr($form_id); ?>-email"
                    name="email"
                    required
                    placeholder="<?php echo esc_attr($atts['email_placeholder']); ?>"
                />
            </div>
        </div>

        <div class="leadmachine-form-row leadmachine-button-row">
            <button
                type="submit"
                class="leadmachine-submit-button"
                style="background-color: <?php echo esc_attr($atts['button_color']); ?>;"
            >
                <span class="leadmachine-button-text"><?php echo esc_html($atts['button_text']); ?></span>
                <span class="leadmachine-button-spinner" style="display: none;">
                    <svg width="20" height="20" viewBox="0 0 50 50">
                        <circle cx="25" cy="25" r="20" fill="none" stroke="currentColor" stroke-width="4" stroke-dasharray="31.4 31.4" transform="rotate(-90 25 25)">
                            <animateTransform attributeName="transform" type="rotate" from="0 25 25" to="360 25 25" dur="1s" repeatCount="indefinite"/>
                        </circle>
                    </svg>
                </span>
            </button>
        </div>

        <div class="leadmachine-form-row leadmachine-optin-row">
            <div class="leadmachine-form-group">
                <label class="leadmachine-checkbox-label">
                    <input
                        type="checkbox"
                        name="optin"
                        id="<?php echo esc_attr($form_id); ?>-optin"
                        required
                    />
                    <span class="leadmachine-checkbox-text leadmachine-text-muted" style="color: <?php echo esc_attr($atts['label_color']); ?>;">
                        <?php echo wp_kses_post($atts['optin_text']); ?> <span class="leadmachine-required">*</span>
                    </span>
                </label>
            </div>
        </div>

        <!-- Hidden fields -->
        <input type="hidden" name="form_type" value="newsletter" />
        <input type="hidden" name="source_url" value="<?php echo esc_url(get_permalink() ?: home_url($_SERVER['REQUEST_URI'])); ?>" />
        <input type="hidden" name="tags" value="<?php echo esc_attr($atts['tags']); ?>" />
        <input type="hidden" name="nonce" value="<?php echo esc_attr(wp_create_nonce('leadmachine_nonce')); ?>" />

        <!-- Spam Protection: Honeypot field (hidden from humans, bots fill it) -->
        <div class="leadmachine-hp-field" aria-hidden="true">
            <label for="<?php echo esc_attr($form_id); ?>-website">Website</label>
            <input type="text" name="website_url" id="<?php echo esc_attr($form_id); ?>-website" tabindex="-1" autocomplete="off" />
        </div>

        <!-- Spam Protection: Timestamp for time-based check -->
        <input type="hidden" name="form_token" value="<?php echo esc_attr(base64_encode(time() . ':' . wp_generate_password(12, false))); ?>" />

        <!-- Analytics/Tracking data container (populated by JS) -->
        <input type="hidden" name="analytics_data" value="" />
    </form>
</div>
