<?php
// 1. ENABLE ERROR REPORTING (This will show you the specific error)
error_reporting(E_ALL);
ini_set('display_errors', 1);

session_start();

// 2. Check if Database Connection file exists
if (!file_exists('../db_connect.php')) {
    die("<h2 style='color:red; text-align:center; margin-top:50px;'>Error: Cannot find db_connect.php! <br>Make sure it is in public_html folder.</h2>");
}

include '../db_connect.php';

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $username = $conn->real_escape_string($_POST['username']);
    $password = $conn->real_escape_string($_POST['password']);

    // 3. Check if Table Exists (Common Error)
    $checkTable = $conn->query("SHOW TABLES LIKE 'staff_users'");
    if($checkTable->num_rows == 0) {
        die("<h2 style='color:red;'>Error: Table 'staff_users' does not exist. Please run the SQL command from the previous step.</h2>");
    }

    $sql = "SELECT * FROM staff_users WHERE username = '$username' AND password = '$password'";
    $result = $conn->query($sql);

    if ($result && $result->num_rows > 0) {
        $row = $result->fetch_assoc();
        
        // Identify which doctor is logging in
        if ($username == 'doctor') {
            $_SESSION['doctor_logged_in'] = true;
            $_SESSION['doctor_name'] = "Dr. Gajanan Padghan";
            $_SESSION['theme_color'] = "teal"; 
            header("Location: dashboard.php");
            exit();
        } 
        elseif ($username == 'anita') {
            $_SESSION['doctor_logged_in'] = true;
            $_SESSION['doctor_name'] = "Dr. Anita Padghan";
            $_SESSION['theme_color'] = "pink"; 
            header("Location: dashboard.php");
            exit();
        } 
        else {
            $error = "Access Denied. Doctor account not recognized.";
        }
    } else {
        $error = "Invalid Username or Password";
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Doctor Login | Aastha Hospital</title>
    <script src="https://cdn.tailwindcss.com"></script>
</head>
<body class="bg-blue-900 h-screen flex items-center justify-center">
    <div class="bg-white p-8 rounded-lg shadow-2xl w-96 text-center">
        <h2 class="text-2xl font-bold mb-2 text-blue-800">Doctor's Desk</h2>
        <p class="text-gray-500 mb-6 text-sm">Authorized Medical Personnel Only</p>
        
        <?php if(isset($error)) echo "<p class='bg-red-100 text-red-600 p-2 rounded mb-4 text-sm'>$error</p>"; ?>
        
        <form method="POST" class="space-y-4">
            <input type="text" name="username" placeholder="Username" class="w-full p-3 border rounded focus:outline-none focus:border-blue-500" required>
            <input type="password" name="password" placeholder="Password" class="w-full p-3 border rounded focus:outline-none focus:border-blue-500" required>
            <button type="submit" class="w-full bg-blue-600 text-white font-bold py-3 rounded hover:bg-blue-700 transition">Login</button>
        </form>
    </div>
</body>
</html>