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

// Generate unique ID for scoped styles
$leadmachine_form_uid = 'lm-' . wp_rand(1000, 9999);

// 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;';
}
?>
<style>
#<?php echo esc_attr($leadmachine_form_uid); ?> input[type="text"],
#<?php echo esc_attr($leadmachine_form_uid); ?> input[type="email"],
#<?php echo esc_attr($leadmachine_form_uid); ?> input[type="tel"],
#<?php echo esc_attr($leadmachine_form_uid); ?> textarea {
    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,
#<?php echo esc_attr($leadmachine_form_uid); ?> textarea::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" 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" id="leadmachine-form" method="post">
        <div class="leadmachine-form-messages"></div>

        <div class="leadmachine-form-row leadmachine-form-row-split">
            <div class="leadmachine-form-group">
                <label for="leadmachine-first-name" style="color: <?php echo esc_attr($atts['label_color']); ?>;">
                    First Name <span class="leadmachine-required">*</span>
                </label>
                <input
                    type="text"
                    id="leadmachine-first-name"
                    name="first_name"
                    required
                    placeholder="John"
                />
            </div>
            <div class="leadmachine-form-group">
                <label for="leadmachine-last-name" style="color: <?php echo esc_attr($atts['label_color']); ?>;">
                    Last Name <span class="leadmachine-required">*</span>
                </label>
                <input
                    type="text"
                    id="leadmachine-last-name"
                    name="last_name"
                    required
                    placeholder="Smith"
                />
            </div>
        </div>

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

        <div class="leadmachine-form-row">
            <div class="leadmachine-form-group">
                <label for="leadmachine-phone" style="color: <?php echo esc_attr($atts['label_color']); ?>;">Phone</label>
                <input
                    type="tel"
                    id="leadmachine-phone"
                    name="phone"
                    placeholder="(555) 123-4567"
                />
            </div>
        </div>

        <div class="leadmachine-form-row">
            <div class="leadmachine-form-group">
                <label for="leadmachine-company" style="color: <?php echo esc_attr($atts['label_color']); ?>;">Company</label>
                <input
                    type="text"
                    id="leadmachine-company"
                    name="company"
                    placeholder="Your company name"
                />
            </div>
        </div>

        <div class="leadmachine-form-row">
            <div class="leadmachine-form-group">
                <label for="leadmachine-message" style="color: <?php echo esc_attr($atts['label_color']); ?>;">Message</label>
                <textarea
                    id="leadmachine-message"
                    name="message"
                    rows="4"
                    placeholder="How can we help you?"
                ></textarea>
            </div>
        </div>

        <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' ) ); ?>" />

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

        <!-- Spam Protection: Honeypot field (hidden from humans, bots fill it) -->
        <div class="leadmachine-hp-field" aria-hidden="true">
            <label for="leadmachine-website">Website</label>
            <input type="text" name="website_url" id="leadmachine-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))); ?>" />

        <div class="leadmachine-form-row">
            <div class="leadmachine-form-group">
                <label class="leadmachine-checkbox-label">
                    <input
                        type="checkbox"
                        name="agree_terms"
                        id="leadmachine-agree-terms"
                        required
                    />
                    <span class="leadmachine-checkbox-text">
                        I agree to the <a href="https://app.leadmachine.fyi/pages/terms.php" target="_blank" rel="noopener noreferrer">Terms of Service</a> and <a href="https://app.leadmachine.fyi/pages/privacy.php" target="_blank" rel="noopener noreferrer">Privacy Policy</a> <span class="leadmachine-required">*</span>
                    </span>
                </label>
            </div>
        </div>

        <div class="leadmachine-form-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>
    </form>
</div>
