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

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

$action = $_GET['action'] ?? '';
$message = '';
$success = false;

try {
    if ($action === 'enable') {
        // Enable tatkal
        $stmt = $pdo->prepare("INSERT INTO settings (setting_key, setting_value) VALUES (?, ?) ON DUPLICATE KEY UPDATE setting_value = ?");
        $stmt->execute(['tatkal_enabled', '1', '1']);
        
        // Ensure consultation type is active
        $stmt = $pdo->prepare("UPDATE consultation_types SET is_active = 1, base_price = 1500.00 WHERE type_code = 'tatkal_booking'");
        $stmt->execute();
        
        $message = "✅ Tatkal ENABLED successfully!";
        $success = true;
        
    } elseif ($action === 'disable') {
        // Disable tatkal
        $stmt = $pdo->prepare("INSERT INTO settings (setting_key, setting_value) VALUES (?, ?) ON DUPLICATE KEY UPDATE setting_value = ?");
        $stmt->execute(['tatkal_enabled', '0', '0']);
        
        $message = "❌ Tatkal DISABLED successfully!";
        $success = true;
    }
    
    // Get current status
    $stmt = $pdo->prepare("SELECT setting_value FROM settings WHERE setting_key = 'tatkal_enabled'");
    $stmt->execute();
    $currentStatus = $stmt->fetchColumn() ?? '0';
    
} catch (Exception $e) {
    $message = "❌ Error: " . $e->getMessage();
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Quick Tatkal Toggle</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 600px;
            margin: 50px auto;
            padding: 20px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
        }
        .container {
            background: rgba(255, 255, 255, 0.1);
            padding: 30px;
            border-radius: 20px;
            backdrop-filter: blur(10px);
            border: 1px solid rgba(255, 255, 255, 0.2);
        }
        .status {
            text-align: center;
            font-size: 24px;
            margin-bottom: 30px;
            padding: 20px;
            border-radius: 15px;
            font-weight: bold;
        }
        .enabled {
            background: rgba(16, 185, 129, 0.2);
            border: 2px solid #10b981;
        }
        .disabled {
            background: rgba(239, 68, 68, 0.2);
            border: 2px solid #ef4444;
        }
        .buttons {
            display: flex;
            gap: 20px;
            justify-content: center;
            margin: 30px 0;
        }
        .btn {
            padding: 15px 30px;
            border: none;
            border-radius: 12px;
            font-size: 16px;
            font-weight: bold;
            cursor: pointer;
            text-decoration: none;
            display: inline-block;
            transition: all 0.3s ease;
        }
        .btn-enable {
            background: #10b981;
            color: white;
        }
        .btn-disable {
            background: #ef4444;
            color: white;
        }
        .btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 8px 25px rgba(0, 0, 0, 0.3);
        }
        .message {
            text-align: center;
            font-size: 18px;
            margin: 20px 0;
            padding: 15px;
            border-radius: 10px;
            background: rgba(255, 255, 255, 0.1);
        }
        .back-link {
            text-align: center;
            margin-top: 30px;
        }
        .back-link a {
            color: #fbbf24;
            text-decoration: none;
            font-weight: bold;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1 style="text-align: center; margin-bottom: 30px;">🚀 Quick Tatkal Toggle</h1>
        
        <?php if ($message): ?>
            <div class="message"><?php echo $message; ?></div>
        <?php endif; ?>
        
        <div class="status <?php echo ($currentStatus == '1') ? 'enabled' : 'disabled'; ?>">
            Current Status: <?php echo ($currentStatus == '1') ? '✅ ENABLED' : '❌ DISABLED'; ?>
        </div>
        
        <div class="buttons">
            <a href="?action=enable" class="btn btn-enable">
                ✅ ENABLE Tatkal
            </a>
            <a href="?action=disable" class="btn btn-disable">
                ❌ DISABLE Tatkal
            </a>
        </div>
        
        <div class="back-link">
            <a href="tatkal_management_panel.php">← Back to Tatkal Management</a> |
            <a href="debug_tatkal.php">🔍 Debug Tatkal</a>
        </div>
    </div>
    
    <?php if ($success): ?>
    <script>
        // Auto refresh after 2 seconds
        setTimeout(() => {
            window.location.href = 'quick_tatkal_toggle.php';
        }, 2000);
    </script>
    <?php endif; ?>
</body>
</html>
