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

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

echo "<h2>🔍 Current Tatkal Day Settings</h2>";
echo "<style>
    body { font-family: Arial, sans-serif; margin: 20px; }
    table { border-collapse: collapse; width: 100%; }
    th, td { border: 1px solid #ddd; padding: 12px; text-align: left; }
    th { background-color: #f2f2f2; }
    .enabled { color: green; font-weight: bold; }
    .disabled { color: red; }
</style>";

try {
    // Get all tatkal day settings
    $stmt = $pdo->prepare("SELECT setting_key, setting_value FROM settings WHERE setting_key LIKE 'tatkal_%_enabled' ORDER BY setting_key");
    $stmt->execute();
    $daySettings = $stmt->fetchAll(PDO::FETCH_ASSOC);
    
    echo "<table>";
    echo "<tr><th>Day</th><th>Setting Key</th><th>Value</th><th>Status</th></tr>";
    
    $days = ['monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday'];
    
    foreach ($days as $day) {
        $settingKey = 'tatkal_' . $day . '_enabled';
        $found = false;
        $value = '0';
        
        foreach ($daySettings as $setting) {
            if ($setting['setting_key'] === $settingKey) {
                $value = $setting['setting_value'];
                $found = true;
                break;
            }
        }
        
        $status = ($value === '1') ? 'ENABLED' : 'DISABLED';
        $statusClass = ($value === '1') ? 'enabled' : 'disabled';
        
        echo "<tr>";
        echo "<td>" . ucfirst($day) . "</td>";
        echo "<td>$settingKey</td>";
        echo "<td>$value</td>";
        echo "<td class='$statusClass'>$status</td>";
        echo "</tr>";
    }
    
    echo "</table>";
    
    // Quick enable buttons
    echo "<h3>Quick Actions:</h3>";
    echo "<div style='margin: 20px 0;'>";
    
    foreach ($days as $day) {
        $settingKey = 'tatkal_' . $day . '_enabled';
        echo "<form method='POST' style='display: inline; margin-right: 10px;'>";
        echo "<input type='hidden' name='day' value='$day'>";
        echo "<input type='hidden' name='action' value='toggle'>";
        echo "<button type='submit' style='padding: 8px 15px; margin: 2px;'>";
        echo "Toggle " . ucfirst($day);
        echo "</button>";
        echo "</form>";
    }
    
    echo "</div>";
    
    // Handle toggle action
    if ($_POST['action'] === 'toggle' && $_POST['day']) {
        $day = $_POST['day'];
        $settingKey = 'tatkal_' . $day . '_enabled';
        
        // Get current value
        $stmt = $pdo->prepare("SELECT setting_value FROM settings WHERE setting_key = ?");
        $stmt->execute([$settingKey]);
        $currentValue = $stmt->fetchColumn() ?: '0';
        
        // Toggle value
        $newValue = ($currentValue === '1') ? '0' : '1';
        
        // Update setting
        $stmt = $pdo->prepare("INSERT INTO settings (setting_key, setting_value) VALUES (?, ?) ON DUPLICATE KEY UPDATE setting_value = ?");
        $stmt->execute([$settingKey, $newValue, $newValue]);
        
        echo "<div style='background: #d4edda; padding: 15px; border-radius: 5px; margin: 10px 0;'>";
        echo "✅ " . ucfirst($day) . " tatkal setting updated: " . ($newValue === '1' ? 'ENABLED' : 'DISABLED');
        echo "</div>";
        
        // Refresh page to show updated values
        echo "<script>setTimeout(() => window.location.reload(), 1000);</script>";
    }
    
} catch (Exception $e) {
    echo "<p style='color: red;'>Error: " . $e->getMessage() . "</p>";
}

echo "<br><a href='tatkal_management_panel.php'>← Back to Tatkal Management</a>";
?>
