<?php
require_once 'config.php';

if (!empty($_SESSION['patient_id'])) {
    header('Location: ' . SITE_URL . '/patient/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 FROM patients WHERE email = ?");
        $stmt->bind_param('s', $email);
        $stmt->execute();
        $result = $stmt->get_result();

        if ($patient = $result->fetch_assoc()) {
            if (password_verify($password, $patient['password'])) {
                $_SESSION['patient_id'] = $patient['id'];
                $_SESSION['patient_name'] = $patient['full_name'];
                $_SESSION['patient_email'] = $patient['email'];

                setFlash('success', 'Welcome back!');
                header('Location: ' . SITE_URL . '/patient/dashboard.php');
                exit;
            }
        }
        $errors[] = 'Invalid email ya password.';
    }
}
?>
<!DOCTYPE html>
<html lang="ur">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Patient 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-primary text-white text-center">
                    <h4><i class="fas fa-sign-in-alt"></i> Patient 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-primary">
                                <i class="fas fa-sign-in-alt"></i> Login
                            </button>
                        </div>
                    </form>

                    <div class="text-center mt-3">
                        <p><a href="<?= SITE_URL ?>/forgot-password.php">Forgot Password?</a></p>
                        <p>Don't have an account? <a href="<?= SITE_URL ?>/patient-register.php">Register here</a></p>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

</body>
</html>