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

$error = '';

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $username = trim($_POST['username']);
    $password = $_POST['password'];

    $stmt = $conn->prepare("SELECT user_id, username, password, role FROM users WHERE username = ? AND role = 'admin'");
    $stmt->bind_param("s", $username);
    $stmt->execute();
    $result = $stmt->get_result();

    if ($result->num_rows === 1) {
        $user = $result->fetch_assoc();
        if (password_verify($password, $user['password'])) {
            $_SESSION['admin_id'] = $user['user_id'];
            $_SESSION['admin_name'] = $user['username'];
            header("Location: dashboard.php");
            exit();
        } else {
            $error = "Invalid password!";
        }
    } else {
        $error = "Admin user not found!";
    }
    $stmt->close();
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
    <title>Admin Login - Unity Healthcare Database</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 0%, #064e3b 100%);
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            min-height: 100vh;
            display: flex;
            flex-direction: column;
        }
        .navbar {
            background: rgba(6, 78, 59, 0.95) !important;
            backdrop-filter: blur(10px);
            box-shadow: 0 2px 10px rgba(0,0,0,0.2);
            padding: 12px 0;
        }
        .navbar-brand img {
            height: 45px;
            margin-right: 12px;
        }
        .brand-title {
            font-size: 1.4rem;
            font-weight: 800;
            line-height: 1.2;
        }
        .brand-sub {
            font-size: 0.7rem;
            font-weight: 400;
            letter-spacing: 1px;
            margin-top: 2px;
        }
        .login-card {
            background: rgba(255,255,255,0.95);
            border-radius: 20px;
            border: none;
            box-shadow: 0 20px 40px rgba(0,0,0,0.2);
            backdrop-filter: blur(5px);
            overflow: hidden;
        }
        .login-card .card-header {
            background: linear-gradient(135deg, #0f6b4c, #064e3b);
            color: white;
            text-align: center;
            padding: 1.5rem;
            border-bottom: none;
        }
        .login-card .card-header i {
            font-size: 3rem;
            margin-bottom: 10px;
        }
        .login-card .card-header h4 {
            font-weight: 700;
            margin-bottom: 0;
        }
        .login-card .card-header p {
            margin-top: 5px;
            font-size: 0.8rem;
            opacity: 0.9;
        }
        .btn-primary {
            background: #0f6b4c;
            border: none;
            border-radius: 30px;
            padding: 10px;
            font-weight: bold;
            transition: 0.2s;
        }
        .btn-primary:hover {
            background: #0a523b;
            transform: translateY(-2px);
        }
        .form-control {
            border-radius: 12px;
            padding: 12px 15px;
        }
        footer {
            background: #064e3b;
            color: white;
            margin-top: auto;
            padding: 20px 0;
            text-align: center;
        }
        footer a {
            color: #ddd;
            text-decoration: none;
        }
        footer a:hover {
            color: white;
            text-decoration: underline;
        }
        @media (max-width: 768px) {
            .brand-title { font-size: 1rem; }
            .brand-sub { font-size: 0.6rem; }
            .login-card .card-header { padding: 1rem; }
        }
    </style>
</head>
<body>

<!-- Professional Header -->
<nav class="navbar navbar-dark">
    <div class="container">
        <a class="navbar-brand d-flex align-items-center" href="../index.php">
            <img src="https://od.lk/s/MzdfODgyMDMwNTZf/IMG-20260414-WA0014%20%281%29.jpg" alt="UHD Logo">
            <div>
                <div class="brand-title">UNITY HEALTHCARE DATABASE</div>
                <div class="brand-sub">NIGERIA</div>
            </div>
        </a>
        <div>
            <a href="../patient/login.php" class="btn btn-outline-light btn-sm me-2"><i class="fas fa-user"></i> Patient</a>
            <a href="../doctor/login.php" class="btn btn-outline-light btn-sm"><i class="fas fa-user-md"></i> Doctor</a>
        </div>
    </div>
</nav>

<div class="container mt-5 mb-5">
    <div class="row justify-content-center">
        <div class="col-11 col-sm-8 col-md-6 col-lg-5">
            <div class="login-card">
                <div class="card-header">
                    <i class="fas fa-user-shield"></i>
                    <h4>Administrator Access</h4>
                    <p>Secure login to the UHD control panel</p>
                </div>
                <div class="card-body p-4">
                    <?php if($error): ?>
                        <div class="alert alert-danger alert-dismissible fade show" role="alert">
                            <i class="fas fa-exclamation-triangle me-2"></i> <?= htmlspecialchars($error) ?>
                            <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
                        </div>
                    <?php endif; ?>
                    <form method="POST">
                        <div class="mb-3">
                            <label class="form-label fw-semibold"><i class="fas fa-user me-2"></i> Username</label>
                            <input type="text" name="username" class="form-control" required autofocus placeholder="Enter your admin username">
                        </div>
                        <div class="mb-4">
                            <label class="form-label fw-semibold"><i class="fas fa-lock me-2"></i> Password</label>
                            <input type="password" name="password" class="form-control" required placeholder="••••••••">
                        </div>
                        <button type="submit" class="btn btn-primary w-100 py-2">
                            <i class="fas fa-sign-in-alt me-2"></i> Login to Dashboard
                        </button>
                    </form>
                </div>
                <div class="card-footer text-center bg-light py-3">
                    <small><i class="fas fa-shield-alt"></i> Authorized personnel only</small>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- Footer -->
<footer>
    <div class="container">
        <div class="row">
            <div class="col-12">
                <p class="mb-0">&copy; <?= date('Y') ?> Unity Healthcare Database. All rights reserved.</p>
                <p class="mb-0 small">Powered by <a href="https://www.fulgentdigit.com.ng" target="_blank">Fulgent Digital Clinic (R)</a></p>
            </div>
        </div>
    </div>
</footer>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>