<?php
require_once 'config.php';

$error = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $username = $_POST['username'] ?? '';
    $password = $_POST['password'] ?? '';
    
    if ($username && $password) {
        try {
            $pdo = getDBConnection();
            $stmt = $pdo->prepare("SELECT * FROM users WHERE username = ? AND active = 1");
            $stmt->execute([$username]);
            $user = $stmt->fetch();
            
            if ($user && password_verify($password, $user['password'])) {
                $_SESSION['user_id'] = $user['id'];
                $_SESSION['username'] = $user['username'];
                $_SESSION['role'] = $user['role'];
                $_SESSION['full_name'] = $user['full_name'];
                
                $updateStmt = $pdo->prepare("UPDATE users SET last_login = NOW() WHERE id = ?");
                $updateStmt->execute([$user['id']]);
                
                if ($_SESSION['role'] === 'admin') {
                    header('Location: admin/index.php');
                } else {
                    header('Location: admin/index.php');
                }
                exit;
            } else {
                $error = 'Invalid username or password';
            }
        } catch (Exception $e) {
            $error = 'Login failed. Please try again.';
        }
    } else {
        $error = 'Please enter username and password';
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Login - InvAnalytics</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.0/font/bootstrap-icons.css" rel="stylesheet">
    <link href="assets/css/style.css" rel="stylesheet">
    <style>
        .alert-error { background: var(--danger-light); color: var(--danger); border: 1px solid #e4b4b4; padding: 14px 18px; border-radius: 4px; margin-bottom: 14px; display: flex; align-items: center; justify-content: space-between; }
        .alert-close { background: none; border: none; font-size: 1.3rem; cursor: pointer; color: inherit; opacity: 0.7; }
        .alert-close:hover { opacity: 1; }
    </style>
</head>
<body class="auth-body">
    <div class="auth-container">
        <div class="auth-card">
            <div class="auth-header">
                <div class="brand">
                    <i class="bi bi-shield-check" style="color: var(--primary); font-size: 2rem; display: block; margin-bottom: 12px;"></i>
                    InvAnalytics
                </div>
                <p>Inventory Management System</p>
            </div>
            <?php if ($error): ?>
                <div class="alert alert-error">
                    <span><?php echo sanitize($error); ?></span>
                    <button class="alert-close" onclick="this.parentElement.remove()">&times;</button>
                </div>
            <?php endif; ?>
            <form method="POST">
                <div class="form-group">
                    <label for="username" class="form-label">Username</label>
                    <input type="text" class="form-control" id="username" name="username" required autofocus placeholder="Enter your username">
                </div>
                <div class="form-group">
                    <label for="password" class="form-label">Password</label>
                    <input type="password" class="form-control" id="password" name="password" required placeholder="Enter your password">
                </div>
                <button type="submit" class="btn btn-primary btn-block">
                    <i class="bi bi-box-arrow-in-right"></i> Login
                </button>
            </form>
            <div class="auth-footer">
                <p><small>Default: admin / password123</small></p>
            </div>
        </div>
    </div>
</body>
</html>