<?php
session_start();
if (!empty($_SESSION['user_id'])) {
header('Location: ' . ($_SESSION['role'] === 'technician' ? 'tech_dashboard.php' : 'dashboard.php'));
exit;
}
require_once 'config/db.php';
$error = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$username = trim($_POST['username'] ?? '');
$password = $_POST['password'] ?? '';
if ($username === '' || $password === '') {
$error = 'Please enter both username and password.';
} else {
$stmt = $pdo->prepare(
"SELECT id, username, full_name, password, role FROM users WHERE username = ?"
);
$stmt->execute([$username]);
$user = $stmt->fetch();
if ($user && password_verify($password, $user['password'])) {
session_regenerate_id(true);
$_SESSION['user_id'] = $user['id'];
$_SESSION['username'] = $user['username'];
$_SESSION['full_name'] = $user['full_name'] ?: $user['username'];
$_SESSION['role'] = $user['role'];
header('Location: ' . ($user['role'] === 'technician' ? 'tech_dashboard.php' : 'dashboard.php'));
exit;
} else {
$error = 'Invalid username or password.';
}
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Login — ISP Stock Manager</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
<link href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.5/font/bootstrap-icons.css" rel="stylesheet">
<link href="assets/css/style.css" rel="stylesheet">
</head>
<body>
<div class="login-wrapper">
<div class="login-card card shadow-lg">
<div class="text-center mb-4">
<i class="bi bi-box-seam text-primary" style="font-size:3.5rem;"></i>
<h3 class="fw-bold mt-2 mb-1">ISP Stock Manager</h3>
<p class="text-muted small">Sign in to continue</p>
</div>
<?php if ($error !== ''): ?>
<div class="alert alert-danger d-flex align-items-center gap-2">
<i class="bi bi-exclamation-triangle-fill"></i>
<?= htmlspecialchars($error) ?>
</div>
<?php endif; ?>
<form method="POST" action="" novalidate>
<div class="mb-3">
<label for="username" class="form-label fw-semibold">Username</label>
<div class="input-group">
<span class="input-group-text"><i class="bi bi-person"></i></span>
<input type="text" class="form-control" id="username" name="username"
value="<?= htmlspecialchars($_POST['username'] ?? '') ?>"
placeholder="Enter your username" required autofocus>
</div>
</div>
<div class="mb-4">
<label for="password" class="form-label fw-semibold">Password</label>
<div class="input-group">
<span class="input-group-text"><i class="bi bi-lock"></i></span>
<input type="password" class="form-control" id="password" name="password"
placeholder="Enter your password" required>
<button class="btn btn-outline-secondary" type="button" id="togglePwd">
<i class="bi bi-eye" id="eyeIcon"></i>
</button>
</div>
</div>
<button type="submit" class="btn btn-primary w-100 py-2 fw-semibold">
<i class="bi bi-box-arrow-in-right me-1"></i> Login
</button>
</form>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
<script>
document.getElementById('togglePwd').addEventListener('click', function () {
const pwd = document.getElementById('password');
const icon = document.getElementById('eyeIcon');
if (pwd.type === 'password') {
pwd.type = 'text';
icon.classList.replace('bi-eye', 'bi-eye-slash');
} else {
pwd.type = 'password';
icon.classList.replace('bi-eye-slash', 'bi-eye');
}
});
</script>
</body>
</html>