<?php
session_start();
require 'config.php';

$error = '';

// If user already logged in, send to correct dashboard
if (isset($_SESSION['username']) && isset($_SESSION['role'])) {
    if ($_SESSION['role'] === 'admin') {
        header("Location: admin_dashboard.php");
    } else {
        header("Location: member_dashboard.php");
    }
    exit;
}

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

    if ($username === '' || $password === '') {
        $error = "Please fill both username and password.";
    } else {
        // Check user in DB
        $stmt = $pdo->prepare("SELECT * FROM users WHERE username = ?");
        $stmt->execute([$username]);
        $user = $stmt->fetch();

        if ($user && password_verify($password, $user['password'])) {
            // Save user info in session
            $_SESSION['username'] = $user['username'];
            $_SESSION['user_id'] = $user['id'];
            $_SESSION['role'] = $user['role'];

            // Redirect depending on role
            if ($user['role'] === 'admin') {
                header("Location: admin_dashboard.php");
            } else {
                header("Location: member_dashboard.php");
            }
            exit;
        } else {
            $error = "Invalid username or password.";
        }
    }
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8" />
    <title>Library LMS - Login</title>
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet" />
    <!-- Optional Google Font -->
    <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@400;600&display=swap" rel="stylesheet" />
    <style>
        body, html {
            height: 100%;
            background: linear-gradient(135deg, #667eea, #764ba2);
            font-family: 'Poppins', sans-serif;
            color: #fff;
            margin: 0;
            padding: 20px;
        }

        /* Wrapper for image + form */
        .login-wrapper {
            min-height: 100vh;
            max-width: 900px;
            margin: 0 auto;
            display: flex;
            flex-direction: column;
            gap: 30px;
            justify-content: center;
            align-items: center;
        }

        @media(min-width: 768px) {
            .login-wrapper {
                flex-direction: row;
                gap: 60px;
            }
        }

        /* Image wrapper */
        .login-image-wrapper {
            flex: 1;
            max-width: 450px;
            display: flex;
            flex-direction: column;
            justify-content: center;
            align-items: center;
            text-align: center;
        }

        .login-image-wrapper h1 {
            margin-bottom: 24px;
            font-weight: 800;
            font-size: 2.8rem;
            text-align: center;
            color: #fff;
            letter-spacing: 2px;
            text-shadow:
                0 0 8px rgba(255, 255, 255, 0.8),
                0 0 20px rgba(255, 255, 255, 0.6),
                0 0 30px rgba(255, 255, 255, 0.5);
            background-color: rgba(0, 0, 0, 0.25);
            padding: 10px 20px;
            border-radius: 12px;
            box-shadow: 0 4px 15px rgba(0,0,0,0.5);
        }

        .login-image {
            width: 100%;
            height: auto;
            border-radius: 15px;
            box-shadow: 0 15px 40px rgba(0,0,0,0.3);
            object-fit: cover;
        }

        /* Form container */
        .login-container {
            flex: 1;
            max-width: 420px;
            background: rgba(255, 255, 255, 0.1);
            padding: 40px 50px;
            border-radius: 15px;
            box-shadow: 0 15px 40px rgba(0,0,0,0.3);
            backdrop-filter: blur(10px);
            animation: fadeIn 1s ease forwards;
            width: 100%;
        }

        h2 {
            font-weight: 600;
            margin-bottom: 30px;
            text-align: center;
            letter-spacing: 1.5px;
            text-shadow: 0 2px 8px rgba(0,0,0,0.3);
        }

        label {
            font-weight: 600;
            letter-spacing: 0.8px;
            color: #e0e0e0;
        }

        .form-control {
            background: rgba(255,255,255,0.2);
            border: none;
            border-radius: 10px;
            color: #fff;
            padding: 15px 20px;
            font-size: 1rem;
            box-shadow: inset 2px 2px 4px rgba(255,255,255,0.3);
            transition: background-color 0.3s ease;
        }
        .form-control:focus {
            background: rgba(255,255,255,0.4);
            color: #222;
            box-shadow: 0 0 10px #9155fd;
            outline: none;
        }

        .btn-primary {
            background: #9155fd;
            border: none;
            padding: 15px 0;
            font-weight: 600;
            font-size: 1.1rem;
            border-radius: 10px;
            width: 100%;
            transition: background-color 0.3s ease;
            box-shadow: 0 5px 15px rgba(145,85,253,0.6);
        }
        .btn-primary:hover {
            background: #682bdf;
            box-shadow: 0 8px 25px rgba(104,43,223,0.7);
        }

        .alert-danger {
            background-color: rgba(255, 72, 66, 0.85);
            border-radius: 8px;
            padding: 12px 20px;
            font-weight: 600;
            margin-bottom: 20px;
            text-align: center;
            box-shadow: 0 3px 10px rgba(255,72,66,0.5);
        }

        .register-link {
            margin-top: 15px;
            font-weight: 500;
            text-align: center;
        }

        .register-link a {
            color: #fff;
            text-decoration: underline;
        }

        .register-link a:hover {
            color: #dcdcff;
        }

        @keyframes fadeIn {
            0% {opacity: 0; transform: translateY(20px);}
            100% {opacity: 1; transform: translateY(0);}
        }

        ::placeholder {
            color: rgba(255,255,255,0.7);
            font-weight: 400;
        }
    </style>
</head>
<body>
<div class="login-wrapper">

    <!-- Image Section -->
    <div class="login-image-wrapper">
        <h1>Library Management System</h1>
        <img src="https://images.unsplash.com/photo-1524995997946-a1c2e315a42f?auto=format&fit=crop&w=800&q=80" alt="Library" class="login-image" />
    </div>

    <!-- Login Form Section -->
    <div class="login-container shadow-lg">
        <h2>User Login</h2>
        <?php if ($error): ?>
            <div class="alert alert-danger"><?=htmlspecialchars($error)?></div>
        <?php endif; ?>
        <form method="post" novalidate autocomplete="off">
            <div class="mb-4">
                <label for="username" class="form-label">Username</label>
                <input id="username" type="text" name="username" class="form-control" placeholder="Enter your username" required value="<?=htmlspecialchars($_POST['username'] ?? '')?>" />
            </div>
            <div class="mb-5">
                <label for="password" class="form-label">Password</label>
                <input id="password" type="password" name="password" class="form-control" placeholder="Enter your password" required />
            </div>
            <button type="submit" class="btn btn-primary">Log In</button>
        </form>

        <div class="register-link">
            <p>Don't have an account? <a href="register.php">Register here</a></p>
        </div>
    </div>

</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>  make this short like only logic beacuse i want to write this is in blackbook