<?php
session_start();
require_once '../config.php';

$error = '';

// Handle logout message
if (isset($_GET['logout'])) {
    $error = '<div class="alert alert-success">✅ আপনি সফলভাবে লগআউট করেছেন।</div>';
}

// Handle login
if (isset($_POST['login'])) {
    $email = $conn->real_escape_string($_POST['email']);
    $password = $_POST['password'];
    
    $result = $conn->query("SELECT * FROM doctors WHERE email='$email' AND is_active=1");
    
    if ($result && $result->num_rows > 0) {
        $doctor = $result->fetch_assoc();
        
        if (password_verify($password, $doctor['password'])) {
            $_SESSION['doctor'] = $doctor['id'];
            $_SESSION['doctor_name'] = $doctor['name'];
            header('Location: dashboard.php');
            exit;
        } else {
            $error = '<div class="alert alert-danger">❌ পাসওয়ার্ড ভুল!</div>';
        }
    } else {
        $error = '<div class="alert alert-danger">❌ ডাক্তার পাওয়া যায়নি!</div>';
    }
}

// Get doctors list
$doctors = $conn->query("SELECT * FROM doctors WHERE is_active=1 ORDER BY name");
?>
<!DOCTYPE html>
<html lang="bn">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Doctor Login | স্বাস্থ্য কানন</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <style>
        body {
            background: #f0f7f4;
            display: flex;
            align-items: center;
            justify-content: center;
            min-height: 100vh;
            font-family: 'Hind Siliguri', sans-serif;
        }
        .login-box {
            background: white;
            padding: 35px;
            border-radius: 15px;
            box-shadow: 0 5px 25px rgba(0,0,0,0.1);
            max-width: 420px;
            width: 100%;
        }
        .btn-login {
            background: #2d6a4f;
            color: white;
            width: 100%;
            padding: 12px;
            border: none;
            border-radius: 8px;
            font-size: 16px;
            font-weight: 600;
        }
        .btn-login:hover { background: #1b4332; }
        select, input {
            border-radius: 8px !important;
            padding: 10px 15px !important;
            border: 1px solid #ddd !important;
            width: 100%;
            margin-bottom: 12px;
            font-size: 15px;
        }
    </style>
</head>
<body>

<div class="login-box">
    <div class="text-center mb-4">
        <div style="font-size:3rem">👨‍⚕️</div>
        <h4>Doctor Login</h4>
        <p class="text-muted">স্বাস্থ্য কানন</p>
    </div>
    
    <?php echo $error; ?>
    
    <form method="POST">
        <label class="fw-bold">ডাক্তার সিলেক্ট করুন</label>
        <select name="email" required>
            <option value="">-- সিলেক্ট করুন --</option>
            <?php while($doc = $doctors->fetch_assoc()): ?>
            <option value="<?=$doc['email']?>"><?=$doc['name']?></option>
            <?php endwhile; ?>
        </select>
        
        <label class="fw-bold">পাসওয়ার্ড</label>
        <input type="password" name="password" placeholder="পাসওয়ার্ড" required>
        
        <button type="submit" name="login" class="btn-login mt-2">লগইন</button>
    </form>
    
    <p class="text-center mt-3 text-muted small">Default: doctor123</p>
    <p class="text-center"><a href="../index.php" class="text-muted small">← হোম</a></p>
</div>

</body>
</html>