<?php
require_once 'config.php';

if (!empty($_SESSION['doctor_id'])) {
    header('Location: ' . SITE_URL . '/doctor/dashboard.php');
    exit;
}

// Login is now handled via modal on landing page
header('Location: ' . SITE_URL . '/index.php');
exit;

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $email = clean($_POST['email'] ?? '');
    $password = $_POST['password'] ?? '';

    if (empty($email)) $errors[] = 'Email dalo.';
    if (empty($password)) $errors[] = 'Password dalo.';

    if (empty($errors)) {
        $stmt = $conn->prepare("SELECT id, full_name, email, password, verification_status FROM doctors WHERE email = ?");
        $stmt->bind_param('s', $email);
        $stmt->execute();
        $result = $stmt->get_result();

        if ($doctor = $result->fetch_assoc()) {
            if ($doctor['verification_status'] !== 'approved') {
                $errors[] = 'Aapka account abhi verify nahi hua. Admin approval ka wait karo.';
            } elseif (password_verify($password, $doctor['password'])) {
                $_SESSION['doctor_id'] = $doctor['id'];
                $_SESSION['doctor_name'] = $doctor['full_name'];
                $_SESSION['doctor_email'] = $doctor['email'];

                setFlash('success', 'Welcome back, Dr. ' . $doctor['full_name']);
                header('Location: ' . SITE_URL . '/doctor/dashboard.php');
                exit;
            } else {
                $errors[] = 'Invalid password.';
            }
        } else {
            $errors[] = 'Doctor account nahi mila.';
        }
    }
}
?>
<!DOCTYPE html>
<html lang="ur">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Doctor Login - HealthLink</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" rel="stylesheet">
</head>
<body class="bg-light">

<div class="container mt-5">
    <div class="row justify-content-center">
        <div class="col-md-6 col-lg-4">
            <div class="card shadow">
                <div class="card-header bg-success text-white text-center">
                    <h4><i class="fas fa-user-md"></i> Doctor Login</h4>
                </div>
                <div class="card-body">
                    
                    <?php if (!empty($errors)): ?>
                        <div class="alert alert-danger">
                            <?php foreach ($errors as $error): ?>
                                <?= htmlspecialchars($error) ?><br>
                            <?php endforeach; ?>
                        </div>
                    <?php endif; ?>

                    <?php displayFlash(); ?>

                    <form method="POST">
                        <div class="mb-3">
                            <label class="form-label">Email</label>
                            <input type="email" name="email" class="form-control" value="<?= htmlspecialchars($_POST['email'] ?? '') ?>" required>
                        </div>

                        <div class="mb-3">
                            <label class="form-label">Password</label>
                            <input type="password" name="password" class="form-control" required>
                        </div>

                        <div class="d-grid">
                            <button type="submit" class="btn btn-success">
                                <i class="fas fa-sign-in-alt"></i> Login
                            </button>
                        </div>
                    </form>

                    <div class="text-center mt-3">
                        <p>Don't have an account? <a href="<?= SITE_URL ?>/register-doctor.php">Register here</a></p>
                        <a href="<?= SITE_URL ?>/index.php" class="text-muted">Back to Home</a>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

</body>
</html>