<?php
session_start();
error_reporting(E_ALL);
ini_set('display_errors', 1);
include '../include/config.php';

$error = '';

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $username = $_POST['username'];
    $password = $_POST['password'];
    
    $result = $conn->query("SELECT * FROM users WHERE username = '$username' AND role = 'nurse'");
    
    if ($result && $result->num_rows > 0) {
        $user = $result->fetch_assoc();
        
        if (password_verify($password, $user['password'])) {
            // Check approval status
            if ($user['approval_status'] == 'pending') {
                $error = "Your account is pending admin approval. Please wait for confirmation.";
            } elseif ($user['approval_status'] == 'rejected') {
                $error = "Your registration was rejected. Please contact administrator.";
            } elseif ($user['status'] != 'active') {
                $error = "Your account is inactive. Please contact administrator.";
            } else {
                // Get nurse details
                $nurse = $conn->query("SELECT * FROM nurses WHERE user_id = '{$user['user_id']}'")->fetch_assoc();
                
                $_SESSION['nurse_id'] = $user['user_id'];
                $_SESSION['nurse_name'] = $user['full_name'];
                $_SESSION['nurse_credentials'] = $nurse['license_number'] ?? 'Registered Nurse';
                $_SESSION['nurse_uhd_id'] = $nurse['nurse_number'] ?? '';
                
                header("Location: dashboard.php");
                exit();
            }
        } else {
            $error = "Invalid password!";
        }
    } else {
        $error = "Username not found!";
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Nurse Login - Unity Healthcare</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, #3d2b1f 0%, #2c1a0e 100%);
            font-family: 'Segoe UI', sans-serif;
            min-height: 100vh;
            display: flex;
            align-items: center;
        }
        .login-card {
            background: #2c1a0e;
            border: 1px solid #d4a017;
            border-radius: 20px;
            box-shadow: 0 15px 35px rgba(0,0,0,0.3);
            overflow: hidden;
        }
        .login-header {
            background: linear-gradient(135deg, #1a0f08, #2c1a0e);
            color: #d4a017;
            padding: 30px;
            text-align: center;
            border-bottom: 2px solid #d4a017;
        }
        .login-header i { font-size: 60px; margin-bottom: 15px; }
        .login-header h2 { margin: 0; font-weight: 700; }
        .login-body { padding: 30px; }
        .form-control {
            background: #3d2b1f;
            border: 1px solid #d4a017;
            color: white;
            padding: 12px;
            border-radius: 10px;
        }
        .form-control:focus {
            background: #4a3828;
            color: white;
            border-color: #d4a017;
            box-shadow: 0 0 8px #d4a017;
        }
        .form-label { color: #d4a017; font-weight: 500; }
        .btn-login {
            background: #d4a017;
            color: #1a0f08;
            border: none;
            padding: 12px;
            font-weight: bold;
            border-radius: 10px;
            width: 100%;
        }
        .btn-login:hover { background: #b8860b; color: white; }
        .alert { border-radius: 10px; margin-bottom: 20px; }
    </style>
</head>
<body>
<div class="container">
    <div class="row justify-content-center">
        <div class="col-md-5">
            <div class="login-card">
                <div class="login-header">
                    <i class="fas fa-user-nurse"></i>
                    <h2>Nurse Login</h2>
                    <p>Unity Healthcare Database</p>
                </div>
                <div class="login-body">
                    <?php if($error): ?>
                        <div class="alert alert-danger"><?= htmlspecialchars($error) ?></div>
                    <?php endif; ?>
                    <form method="POST">
                        <div class="mb-3">
                            <label class="form-label"><i class="fas fa-user"></i> Username</label>
                            <input type="text" name="username" class="form-control" required>
                        </div>
                        <div class="mb-3">
                            <label class="form-label"><i class="fas fa-lock"></i> Password</label>
                            <input type="password" name="password" class="form-control" required>
                        </div>
                        <button type="submit" class="btn-login">Login</button>
                    </form>
                    <div class="text-center mt-3">
                        <a href="register.php" class="text-warning">Don't have an account? Register here</a>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
</body>
</html>