<?php if ( ! defined( 'ABSPATH' ) ) exit; ?>
<?php
global $wpdb;
// Direct database query used below – Safely gets data from the database using $wpdb->prepare()
$dialbunny_keys = $wpdb->get_results($wpdb->prepare("SELECT license_key FROM {$wpdb->prefix}dialbunny_api_keys WHERE id = %s LIMIT 1", 'ACWJB59')); // phpcs:ignore WordPress.DB.DirectDatabaseQuery
?>

<div class="dialbunny-admin-wrap">
    <?php
    // Display notice based on license key validation
    if (isset($dialbunny_keys[0]->license_key) && strlen($dialbunny_keys[0]->license_key) == 27 && stripos($dialbunny_keys[0]->license_key, 'd') !== false) {
        echo '<div class="dialbunny-notice dialbunny-notice-success"><p><strong>License Key is Active!</strong> Your license code is active!</p><button class="dialbunny-notice-dismiss">Dismiss</button></div>';
    } else {
        echo '<div class="dialbunny-notice dialbunny-notice-error"><p><strong>Incorrect License Code!</strong> Incorrect license code was applied.</p><button class="dialbunny-notice-dismiss">Dismiss</button></div>';
    }
    ?>

    <div class="dialbunny-card">
        <h2 class="dialbunny-title">DialBunny License Key</h2>
        <p>Please enter your DialBunny license key to activate and use the plugin.</p>
        <p>You can purchase a license key at <a href="https://dialbunny.com/pricing/" target="_blank" class="dialbunny-link">DialBunny.com</a></p>
    </div>

    <div class="dialbunny-card">
        <h2 class="dialbunny-title">Enter License Key</h2>
        <form action="<?php echo esc_url(admin_url('admin-post.php')); ?>" method="post">
            <?php wp_nonce_field('DialBunny', 'user_nonce'); ?>
            <input type="hidden" name="action" value="dialbunny_update_license_key">
            <div class="dialbunny-form-group">
                <label for="license_key" class="dialbunny-label">License Key</label>
                <input required type="text" name="license_key" id="license_key" placeholder="Enter your license key" 
                       value="<?php if (isset($dialbunny_keys[0]->license_key)) { echo esc_html($dialbunny_keys[0]->license_key); } ?>" 
                       class="dialbunny-input">
            </div>
            <div class="dialbunny-form-group">
                <button type="submit" class="dialbunny-button dialbunny-button-primary">Save License</button>
            </div>
        </form>
    </div>
</div>