<?php
session_start();
error_reporting(E_ALL);
ini_set('display_errors', 1);

include '../include/config.php';

$error = '';

// If already logged in, go to dashboard
if (isset($_SESSION['patient_id'])) {
    header("Location: dashboard.php");
    exit();
}

// Handle Login
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $username = trim($_POST['username']);
    $password = $_POST['password'];
    
    if (empty($username) || empty($password)) {
        $error = "Please enter both email/ID and password.";
    } else {
        $query = "SELECT * FROM patients WHERE email = '$username' OR patient_id = '$username'";
        $result = $conn->query($query);
        
        if ($result && $result->num_rows > 0) {
            $patient = $result->fetch_assoc();
            
            if (password_verify($password, $patient['password'])) {
                if ($patient['status'] == 'active') {
                    $_SESSION['patient_id'] = $patient['patient_id'];
                    $_SESSION['patient_name'] = $patient['first_name'] . ' ' . $patient['last_name'];
                    $_SESSION['patient_email'] = $patient['email'];
                    
                    header("Location: dashboard.php");
                    exit();
                } else {
                    $error = "Your account is pending activation. Please contact support.";
                }
            } else {
                $error = "Invalid password. Please try again.";
            }
        } else {
            $error = "No account found with this email/ID. Please register first.";
        }
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Patient Login - UHD</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.6.0/css/all.min.css">
    <style>
        body {
            background: linear-gradient(135deg, #0a3d2f, #064e3b);
            min-height: 100vh;
            font-family: 'Segoe UI', sans-serif;
        }
        .login-container {
            max-width: 450px;
            margin: 0 auto;
            padding: 100px 20px;
        }
        .login-card {
            background: white;
            border-radius: 24px;
            padding: 40px;
            box-shadow: 0 20px 40px rgba(0,0,0,0.2);
        }
        .form-control {
            border-radius: 12px;
            padding: 12px 15px;
            margin-bottom: 15px;
            border: 1px solid #ddd;
        }
        .form-control:focus {
            border-color: #d4a017;
            outline: none;
            box-shadow: 0 0 0 2px rgba(212,160,23,0.2);
        }
        .btn-login {
            background: linear-gradient(135deg, #0a3d2f, #064e3b);
            color: white;
            border-radius: 40px;
            padding: 12px;
            width: 100%;
            font-weight: 600;
            border: none;
            transition: 0.3s;
        }
        .btn-login:hover {
            background: #0f6b4c;
            transform: translateY(-2px);
        }
        .register-link {
            text-align: center;
            margin-top: 20px;
            padding-top: 20px;
            border-top: 1px solid #e0e0e0;
        }
        .register-link a {
            color: #d4a017;
            text-decoration: none;
        }
        .register-link a:hover {
            text-decoration: underline;
        }
        .alert {
            padding: 12px;
            border-radius: 12px;
            margin-bottom: 20px;
        }
        .alert-danger {
            background: #fee;
            color: #c00;
            border: 1px solid #fcc;
        }
        .logo {
            text-align: center;
            margin-bottom: 20px;
        }
        .logo i {
            font-size: 3rem;
            color: #d4a017;
        }
    </style>
</head>
<body>
<div class="container">
    <div class="login-container">
        <div class="login-card">
            <div class="logo">
                <i class="fas fa-hospital-user"></i>
            </div>
            <h2 class="text-center mb-2">Patient Login</h2>
            <p class="text-center text-muted mb-4">Access your UHD Patient Portal</p>
            
            <?php if ($error): ?>
                <div class="alert alert-danger">
                    <i class="fas fa-exclamation-triangle me-2"></i> <?= htmlspecialchars($error) ?>
                </div>
            <?php endif; ?>
            
            <form method="POST">
                <input type="text" name="username" class="form-control" placeholder="Email Address or Patient ID" required>
                <input type="password" name="password" class="form-control" placeholder="Password" required>
                <button type="submit" class="btn btn-login">
                    <i class="fas fa-sign-in-alt me-2"></i> Login
                </button>
            </form>
            
            <div class="register-link">
                <i class="fas fa-user-plus me-1"></i> Don't have an account? 
                <a href="register.php">Register here</a> - <strong class="text-success">FREE</strong>
            </div>
        </div>
    </div>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>