<?php
session_start();
include("includes/db.php");

$err = "";

if ($_SERVER['REQUEST_METHOD'] == "POST") {
    $u = $_POST['username'] ?? '';
    $p = $_POST['password'] ?? '';

    // Secure Prepared Statement
    $stmt = $conn->prepare("SELECT id, password FROM admins WHERE username = ?");
    $stmt->bind_param("s", $u);
    $stmt->execute();
    $result = $stmt->get_result();

    if ($user = $result->fetch_assoc()) {
        // password_verify is the modern replacement for md5
        if (password_verify($p, $user['password'])) {
            $_SESSION['admin_id'] = $user['id'];
            header("Location: dashboard.php");
            exit();
        } else {
            $err = "Invalid credentials. Please try again.";
        }
    } else {
        $err = "Invalid credentials. Please try again.";
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Admin Portal | Login</title>
    <link href="https://fonts.googleapis.com/css2?family=Plus+Jakarta+Sans:wght@400;600;700&display=swap" rel="stylesheet">
    <style>
        :root {
            --primary: #6366f1;
            --primary-hover: #4f46e5;
            --bg-gradient: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            --text-main: #1e293b;
            --text-muted: #64748b;
        }

        * { box-sizing: border-box; margin: 0; padding: 0; }

        body {
            font-family: 'Plus Jakarta Sans', sans-serif;
            background: var(--bg-gradient);
            height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
            color: var(--text-main);
        }

        .login-container {
            background: rgba(255, 255, 255, 0.9);
            backdrop-filter: blur(10px);
            padding: 40px;
            border-radius: 24px;
            box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 10px -5px rgba(0, 0, 0, 0.04);
            width: 100%;
            max-width: 420px;
            text-align: center;
        }

        .header-section { margin-bottom: 32px; }
        .header-section h1 { font-size: 1.75rem; font-weight: 700; margin-bottom: 8px; }
        .header-section p { color: var(--text-muted); font-size: 0.95rem; }

        .error-box {
            background: #fef2f2;
            color: #b91c1c;
            padding: 12px;
            border-radius: 12px;
            font-size: 0.85rem;
            margin-bottom: 20px;
            border: 1px solid #fee2e2;
        }

        .input-group { text-align: left; margin-bottom: 20px; }
        .input-group label { 
            display: block; 
            font-size: 0.85rem; 
            font-weight: 600; 
            margin-bottom: 8px; 
            margin-left: 4px;
        }

        input {
            width: 100%;
            padding: 14px 16px;
            border: 1.5px solid #e2e8f0;
            border-radius: 12px;
            font-size: 1rem;
            transition: all 0.2s ease;
        }

        input:focus {
            outline: none;
            border-color: var(--primary);
            background: white;
            box-shadow: 0 0 0 4px rgba(99, 102, 241, 0.1);
        }

        button {
            width: 100%;
            padding: 14px;
            background: var(--primary);
            color: white;
            border: none;
            border-radius: 12px;
            font-size: 1rem;
            font-weight: 600;
            cursor: pointer;
            transition: transform 0.1s ease, background 0.2s ease;
            margin-top: 10px;
        }

        button:hover { background: var(--primary-hover); }
        button:active { transform: scale(0.98); }

        .footer-text {
            margin-top: 24px;
            font-size: 0.85rem;
            color: var(--text-muted);
        }
    </style>
</head>
<body>

<div class="login-container">
    <div class="header-section">
        <h1>Welcome Back</h1>
        <p>Please enter your admin credentials</p>
    </div>

    <?php if ($err): ?>
        <div class="error-box"><?= htmlspecialchars($err) ?></div>
    <?php endif; ?>

    <form method="POST" action="">
        <div class="input-group">
            <label>Username</label>
            <input type="text" name="username" placeholder="e.g. admin_jane" required>
        </div>

        <div class="input-group">
            <label>Password</label>
            <input type="password" name="password" placeholder="••••••••" required>
        </div>

        <button type="submit">Sign In to Dashboard</button>
    </form>

    <p class="footer-text">&copy; <?= date('Y') ?> Admin Control Panel</p>
</div>

</body>
</html>