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

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

echo "<h2>💰 Current Consultation Type Prices</h2>";
echo "<style>
    body { font-family: Arial, sans-serif; margin: 20px; }
    table { border-collapse: collapse; width: 100%; margin: 20px 0; }
    th, td { border: 1px solid #ddd; padding: 12px; text-align: left; }
    th { background-color: #f2f2f2; font-weight: bold; }
    .active { color: green; font-weight: bold; }
    .inactive { color: red; font-weight: bold; }
    .price { font-size: 18px; font-weight: bold; color: #333; }
</style>";

try {
    // Get all consultation types
    $stmt = $pdo->prepare("SELECT * FROM consultation_types ORDER BY id ASC");
    $stmt->execute();
    $consultationTypes = $stmt->fetchAll(PDO::FETCH_ASSOC);
    
    if (empty($consultationTypes)) {
        echo "<p style='color: red;'>❌ No consultation types found in database!</p>";
    } else {
        echo "<table>";
        echo "<tr>";
        echo "<th>ID</th>";
        echo "<th>Type Name</th>";
        echo "<th>Type Code</th>";
        echo "<th>Base Price</th>";
        echo "<th>Status</th>";
        echo "<th>Description</th>";
        echo "</tr>";
        
        foreach ($consultationTypes as $type) {
            $statusClass = $type['is_active'] ? 'active' : 'inactive';
            $statusText = $type['is_active'] ? 'ACTIVE' : 'INACTIVE';
            
            echo "<tr>";
            echo "<td>{$type['id']}</td>";
            echo "<td>{$type['type_name']}</td>";
            echo "<td>{$type['type_code']}</td>";
            echo "<td class='price'>₹{$type['base_price']}</td>";
            echo "<td class='$statusClass'>$statusText</td>";
            echo "<td>{$type['description']}</td>";
            echo "</tr>";
        }
        
        echo "</table>";
    }
    
    // Check if there's a hardcoded 300 price
    echo "<h3>🔍 Checking for ₹300 Prices:</h3>";
    $stmt = $pdo->prepare("SELECT * FROM consultation_types WHERE base_price = 300");
    $stmt->execute();
    $price300Types = $stmt->fetchAll(PDO::FETCH_ASSOC);
    
    if (!empty($price300Types)) {
        echo "<p style='color: orange;'>⚠️ Found consultation types with ₹300 price:</p>";
        foreach ($price300Types as $type) {
            echo "<p>• {$type['type_name']} ({$type['type_code']}) - ₹{$type['base_price']}</p>";
        }
    } else {
        echo "<p style='color: green;'>✅ No consultation types found with ₹300 price</p>";
    }
    
    // Quick fix buttons
    echo "<h3>🛠️ Quick Actions:</h3>";
    echo "<div style='margin: 20px 0;'>";
    
    foreach ($consultationTypes as $type) {
        if ($type['base_price'] == 300) {
            echo "<form method='POST' style='display: inline; margin-right: 10px;'>";
            echo "<input type='hidden' name='action' value='update_price'>";
            echo "<input type='hidden' name='type_id' value='{$type['id']}'>";
            echo "<input type='number' name='new_price' value='500' min='100' step='50' style='width: 80px; margin-right: 5px;'>";
            echo "<button type='submit' style='padding: 8px 15px; background: #4CAF50; color: white; border: none; border-radius: 4px;'>";
            echo "Update {$type['type_name']} Price";
            echo "</button>";
            echo "</form>";
        }
    }
    
    echo "</div>";
    
    // Handle price update
    if ($_POST['action'] === 'update_price' && $_POST['type_id'] && $_POST['new_price']) {
        $typeId = (int)$_POST['type_id'];
        $newPrice = (float)$_POST['new_price'];
        
        $stmt = $pdo->prepare("UPDATE consultation_types SET base_price = ? WHERE id = ?");
        $stmt->execute([$newPrice, $typeId]);
        
        echo "<div style='background: #d4edda; padding: 15px; border-radius: 5px; margin: 10px 0;'>";
        echo "✅ Price updated successfully to ₹$newPrice";
        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><br><a href='tatkal_management_panel.php'>← Back to Tatkal Management</a>";
echo " | <a href='manage_booking_settings.php'>⚙️ Booking Settings</a>";
?>
