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

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

try {
    // Disable all days first
    $allDays = ['monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday'];
    
    foreach ($allDays as $day) {
        $settingKey = 'tatkal_' . $day . '_enabled';
        $stmt = $pdo->prepare("INSERT INTO settings (setting_key, setting_value) VALUES (?, ?) ON DUPLICATE KEY UPDATE setting_value = ?");
        $stmt->execute([$settingKey, '0', '0']);
    }
    
    // Enable only Saturday and Sunday
    $enabledDays = ['saturday', 'sunday'];
    
    foreach ($enabledDays as $day) {
        $settingKey = 'tatkal_' . $day . '_enabled';
        $stmt = $pdo->prepare("INSERT INTO settings (setting_key, setting_value) VALUES (?, ?) ON DUPLICATE KEY UPDATE setting_value = ?");
        $stmt->execute([$settingKey, '1', '1']);
    }
    
    echo "✅ Tatkal days updated successfully!";
    echo "<br>✅ Enabled: Saturday, Sunday";
    echo "<br>❌ Disabled: Monday, Tuesday, Wednesday, Thursday, Friday";
    
    // Verify the changes
    echo "<br><br>📊 Current Settings:";
    foreach ($allDays as $day) {
        $settingKey = 'tatkal_' . $day . '_enabled';
        $stmt = $pdo->prepare("SELECT setting_value FROM settings WHERE setting_key = ?");
        $stmt->execute([$settingKey]);
        $value = $stmt->fetchColumn() ?: '0';
        $status = ($value === '1') ? '✅ ENABLED' : '❌ DISABLED';
        echo "<br>" . ucfirst($day) . ": $status";
    }
    
} catch (Exception $e) {
    echo "❌ Error: " . $e->getMessage();
}

echo "<br><br><a href='tatkal_management_panel.php'>← Back to Tatkal Management</a>";
echo " | <a href='check_tatkal_days.php'>🔍 Check Day Settings</a>";
?>
