<?php
require_once 'auth.php';
requireAdminLogin();

$page_title = 'Tatkal Debug';
$current_page = 'debug_tatkal.php';

require_once '../config/database.php';
$pdo = getDBConnection();

// Get all tatkal related settings
$stmt = $pdo->prepare("SELECT setting_key, setting_value FROM settings WHERE setting_key LIKE 'tatkal_%' ORDER BY setting_key");
$stmt->execute();
$tatkalSettings = $stmt->fetchAll(PDO::FETCH_ASSOC);

// Get consultation types
$stmt = $pdo->prepare("SELECT * FROM consultation_types WHERE type_code = 'tatkal_booking'");
$stmt->execute();
$tatkalConsultationType = $stmt->fetch(PDO::FETCH_ASSOC);

ob_start();
?>

<style>
    .debug-container {
        max-width: 1200px;
        margin: 0 auto;
        padding: 20px;
    }
    
    .debug-card {
        background: white;
        border-radius: 12px;
        padding: 20px;
        margin-bottom: 20px;
        box-shadow: 0 4px 15px rgba(0,0,0,0.1);
    }
    
    .debug-title {
        color: #333;
        font-size: 18px;
        font-weight: bold;
        margin-bottom: 15px;
        border-bottom: 2px solid #4f46e5;
        padding-bottom: 10px;
    }
    
    .debug-table {
        width: 100%;
        border-collapse: collapse;
    }
    
    .debug-table th,
    .debug-table td {
        padding: 12px;
        text-align: left;
        border-bottom: 1px solid #ddd;
    }
    
    .debug-table th {
        background: #f8f9fa;
        font-weight: 600;
    }
    
    .status-enabled {
        color: #10b981;
        font-weight: bold;
    }
    
    .status-disabled {
        color: #ef4444;
        font-weight: bold;
    }
    
    .api-test {
        background: #f0f9ff;
        border: 1px solid #0ea5e9;
        border-radius: 8px;
        padding: 15px;
        margin-top: 15px;
    }
    
    .api-response {
        background: #1f2937;
        color: #10b981;
        padding: 15px;
        border-radius: 8px;
        font-family: monospace;
        white-space: pre-wrap;
        max-height: 400px;
        overflow-y: auto;
    }
</style>

<div class="debug-container">
    <h1>🔥 Tatkal Debug Information</h1>
    
    <!-- Database Settings -->
    <div class="debug-card">
        <div class="debug-title">📊 Database Settings (settings table)</div>
        <table class="debug-table">
            <thead>
                <tr>
                    <th>Setting Key</th>
                    <th>Setting Value</th>
                    <th>Status</th>
                </tr>
            </thead>
            <tbody>
                <?php if (empty($tatkalSettings)): ?>
                    <tr>
                        <td colspan="3" style="text-align: center; color: #ef4444;">
                            ❌ No tatkal settings found in database!
                        </td>
                    </tr>
                <?php else: ?>
                    <?php foreach ($tatkalSettings as $setting): ?>
                        <tr>
                            <td><strong><?php echo htmlspecialchars($setting['setting_key']); ?></strong></td>
                            <td><?php echo htmlspecialchars($setting['setting_value']); ?></td>
                            <td>
                                <?php if ($setting['setting_key'] === 'tatkal_enabled'): ?>
                                    <span class="<?php echo $setting['setting_value'] === '1' ? 'status-enabled' : 'status-disabled'; ?>">
                                        <?php echo $setting['setting_value'] === '1' ? '✅ ENABLED' : '❌ DISABLED'; ?>
                                    </span>
                                <?php else: ?>
                                    -
                                <?php endif; ?>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
    
    <!-- Consultation Types -->
    <div class="debug-card">
        <div class="debug-title">🏥 Consultation Types (tatkal_booking)</div>
        <?php if ($tatkalConsultationType): ?>
            <table class="debug-table">
                <tr>
                    <th>Type Name</th>
                    <td><?php echo htmlspecialchars($tatkalConsultationType['type_name']); ?></td>
                </tr>
                <tr>
                    <th>Type Code</th>
                    <td><?php echo htmlspecialchars($tatkalConsultationType['type_code']); ?></td>
                </tr>
                <tr>
                    <th>Base Price</th>
                    <td>₹<?php echo number_format($tatkalConsultationType['base_price']); ?></td>
                </tr>
                <tr>
                    <th>Is Active</th>
                    <td>
                        <span class="<?php echo $tatkalConsultationType['is_active'] ? 'status-enabled' : 'status-disabled'; ?>">
                            <?php echo $tatkalConsultationType['is_active'] ? '✅ ACTIVE' : '❌ INACTIVE'; ?>
                        </span>
                    </td>
                </tr>
                <tr>
                    <th>Description</th>
                    <td><?php echo htmlspecialchars($tatkalConsultationType['description'] ?? 'N/A'); ?></td>
                </tr>
            </table>
        <?php else: ?>
            <p style="color: #ef4444; text-align: center;">
                ❌ Tatkal consultation type not found in consultation_types table!
            </p>
        <?php endif; ?>
    </div>
    
    <!-- API Test -->
    <div class="debug-card">
        <div class="debug-title">🔌 API Response Test</div>
        <div class="api-test">
            <p><strong>Testing:</strong> <code>/api/get_tatkal_settings.php</code></p>
            <button onclick="testTatkalAPI()" style="background: #4f46e5; color: white; border: none; padding: 10px 20px; border-radius: 6px; cursor: pointer;">
                🧪 Test API
            </button>
        </div>
        <div id="api-response" class="api-response" style="display: none;"></div>
    </div>
    
    <!-- Quick Actions -->
    <div class="debug-card">
        <div class="debug-title">⚡ Quick Actions</div>
        <div style="display: flex; gap: 15px; flex-wrap: wrap;">
            <form method="POST" action="tatkal_management_panel.php" style="display: inline;">
                <input type="hidden" name="action" value="toggle_tatkal">
                <input type="hidden" name="tatkal_enabled" value="1">
                <button type="submit" style="background: #10b981; color: white; border: none; padding: 12px 20px; border-radius: 6px; cursor: pointer;">
                    ✅ Force Enable Tatkal
                </button>
            </form>
            
            <form method="POST" action="tatkal_management_panel.php" style="display: inline;">
                <input type="hidden" name="action" value="add_tatkal_consultation">
                <button type="submit" style="background: #0ea5e9; color: white; border: none; padding: 12px 20px; border-radius: 6px; cursor: pointer;">
                    🏥 Fix Consultation Type
                </button>
            </form>
            
            <form method="POST" action="tatkal_management_panel.php" style="display: inline;">
                <input type="hidden" name="action" value="update_price">
                <input type="hidden" name="tatkal_price" value="1500">
                <button type="submit" style="background: #f59e0b; color: white; border: none; padding: 12px 20px; border-radius: 6px; cursor: pointer;">
                    💰 Fix Price (₹1500)
                </button>
            </form>
            
            <a href="fix_tatkal.php" style="background: #dc2626; color: white; text-decoration: none; padding: 12px 20px; border-radius: 6px; display: inline-block; font-weight: bold;">
                🚀 AUTO FIX ALL ISSUES
            </a>
            
            <a href="tatkal_management_panel.php" style="background: #6366f1; color: white; text-decoration: none; padding: 12px 20px; border-radius: 6px; display: inline-block;">
                ⚙️ Go to Tatkal Management
            </a>
        </div>
    </div>
</div>

<script>
async function testTatkalAPI() {
    const responseDiv = document.getElementById('api-response');
    responseDiv.style.display = 'block';
    responseDiv.textContent = 'Loading...';
    
    try {
        const response = await fetch('/astro_admin/api/get_tatkal_settings.php');
        const data = await response.json();
        
        responseDiv.textContent = JSON.stringify(data, null, 2);
    } catch (error) {
        responseDiv.textContent = 'Error: ' + error.message;
        responseDiv.style.color = '#ef4444';
    }
}
</script>

<?php
$content = ob_get_clean();
include 'layout.php';
?>
