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

if (isset($_POST['login'])) {
    $username = $_POST['username'];
    $password = $_POST['password'];

    $query = "SELECT * FROM admin WHERE username='$username'";
    $result = mysqli_query($conn, $query);

    if (mysqli_num_rows($result) == 1) {
        $admin = mysqli_fetch_assoc($result);

        if (password_verify($password, $admin['password'])) {
            $_SESSION['admin_id'] = $admin['id'];
            $_SESSION['admin_user'] = $admin['username'];
            $_SESSION['admin_role'] = $admin['role'];

            header("Location: dashboard.php");
            exit();
        } else {
            $error = "Wrong password!";
        }
    } else {
        $error = "Admin not found!";
    }
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Admin Login - ShopNest</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <link href="../assets/css/style.css" rel="stylesheet">
    <style>
        body {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
        }
        .admin-login-card {
            background: rgba(255, 255, 255, 0.95);
            border-radius: 20px;
            padding: 40px;
            box-shadow: 0 20px 60px rgba(0,0,0,0.3);
            width: 100%;
            max-width: 400px;
            backdrop-filter: blur(10px);
        }
        .login-header {
            text-align: center;
            margin-bottom: 30px;
        }
        .login-header i {
            font-size: 4rem;
            background: linear-gradient(45deg, var(--primary-orange), var(--secondary-orange));
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            margin-bottom: 15px;
        }
        .form-control {
            background: rgba(255,255,255,0.9);
            border: 2px solid #e0e0e0;
        }
        .security-notice {
            background: rgba(52, 152, 219, 0.1);
            border-left: 4px solid var(--primary-blue);
            padding: 10px 15px;
            border-radius: 5px;
            font-size: 0.85rem;
            margin-top: 20px;
        }
    </style>
</head>
<body>
    <div class="admin-login-card fade-in">
        <div class="login-header">
            <i class="fas fa-lock"></i>
            <h2 style="color: var(--dark-blue);">Admin Portal</h2>
            <p class="text-muted">ShopNest Administration</p>
        </div>

        <?php if(isset($error)): ?>
            <div class="alert alert-danger alert-dismissible fade show" role="alert">
                <i class="fas fa-exclamation-triangle me-2"></i>
                <?php echo $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-bold">
                    <i class="fas fa-user-shield me-2"></i>Username
                </label>
                <input type="text" 
                       name="username" 
                       class="form-control form-control-lg"
                       placeholder="Enter admin username"
                       required
                       autocomplete="off">
            </div>

            <div class="mb-3">
                <label class="form-label fw-bold">
                    <i class="fas fa-key me-2"></i>Password
                </label>
                <input type="password" 
                       name="password" 
                       class="form-control form-control-lg"
                       placeholder="Enter admin password"
                       required
                       autocomplete="off">
            </div>

            <div class="form-check mb-4">
                <input class="form-check-input" type="checkbox" id="remember">
                <label class="form-check-label" for="remember">Remember this device</label>
            </div>

            <button type="submit" name="login" class="btn btn-orange btn-lg w-100">
                <i class="fas fa-sign-in-alt me-2"></i>Login to Dashboard
            </button>

            <div class="security-notice mt-4">
                <i class="fas fa-info-circle me-2"></i>
                <small>This is a secure admin area. Unauthorized access is prohibited.</small>
            </div>

            <div class="text-center mt-3">
                <a href="../index.php" class="text-decoration-none" style="color: var(--primary-blue);">
                    <i class="fas fa-arrow-left me-1"></i>Back to Shop
                </a>
            </div>
        </form>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>
    <script>
        // Prevent form resubmission on refresh
        if (window.history.replaceState) {
            window.history.replaceState(null, null, window.location.href);
        }
    </script>
</body>
</html>