36360-vm/index.php
2025-11-27 10:28:21 +00:00

170 lines
7.8 KiB
PHP

<?php
require_once 'db/config.php';
$pdo = db();
$errors = [];
$success_message = '';
// Handle POST request to add a new employee
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$first_name = trim($_POST['first_name'] ?? '');
$last_name = trim($_POST['last_name'] ?? '');
$email = trim($_POST['email'] ?? '');
$designation = trim($_POST['designation'] ?? '');
$base_salary = trim($_POST['base_salary'] ?? '');
// Validation
if (empty($first_name)) $errors[] = 'First name is required.';
if (empty($last_name)) $errors[] = 'Last name is required.';
if (empty($email)) {
$errors[] = 'Email is required.';
} elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$errors[] = 'Invalid email format.';
}
if (empty($designation)) $errors[] = 'Designation is required.';
if (empty($base_salary)) {
$errors[] = 'Base salary is required.';
} elseif (!is_numeric($base_salary) || $base_salary < 0) {
$errors[] = 'Base salary must be a positive number.';
}
if (empty($errors)) {
try {
$stmt = $pdo->prepare("INSERT INTO employees (first_name, last_name, email, designation, base_salary) VALUES (?, ?, ?, ?, ?)");
$stmt->execute([$first_name, $last_name, $email, $designation, $base_salary]);
$success_message = "Employee ''' . htmlspecialchars($first_name . ' ' . $last_name) . ''' added successfully!";
} catch (PDOException $e) {
if ($e->errorInfo[1] == 1062) { // Duplicate entry
$errors[] = "An employee with this email already exists.";
} else {
$errors[] = "Database error: " . $e->getMessage();
}
}
}
}
// Fetch all employees
$stmt = $pdo->query("SELECT id, first_name, last_name, email, designation, base_salary FROM employees ORDER BY created_at DESC");
$employees = $stmt->fetchAll();
$project_name = htmlspecialchars($_SERVER['PROJECT_NAME'] ?? 'SalaryBook');
$project_description = htmlspecialchars($_SERVER['PROJECT_DESCRIPTION'] ?? 'Smart Payroll Management System');
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title><?= $project_name ?> - Employee Management</title>
<meta name="description" content="<?= $project_description ?>">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap" rel="stylesheet">
<link rel="stylesheet" href="assets/css/custom.css?v=<?php echo time(); ?>">
</head>
<body>
<nav class="navbar navbar-expand-lg navbar-dark">
<div class="container">
<a class="navbar-brand" href="/"><?= $project_name ?></a>
</div>
</nav>
<main class="container mt-4">
<?php if (!empty($success_message)): ?>
<div class="alert alert-success" role="alert">
<?= $success_message ?>
</div>
<?php endif; ?>
<?php if (!empty($errors)): ?>
<div class="alert alert-danger" role="alert">
<ul class="mb-0">
<?php foreach ($errors as $error): ?>
<li><?= $error ?></li>
<?php endforeach; ?>
</ul>
</div>
<?php endif; ?>
<div class="row">
<div class="col-lg-4 mb-4">
<div class="card shadow-sm">
<div class="card-body">
<h1 class="card-title h5 mb-3">Add New Employee</h1>
<form action="index.php" method="POST">
<div class="mb-3">
<label for="first_name" class="form-label">First Name</label>
<input type="text" class="form-control" id="first_name" name="first_name" required>
</div>
<div class="mb-3">
<label for="last_name" class="form-label">Last Name</label>
<input type="text" class="form-control" id="last_name" name="last_name" required>
</div>
<div class="mb-3">
<label for="email" class="form-label">Email Address</label>
<input type="email" class="form-control" id="email" name="email" required>
</div>
<div class="mb-3">
<label for="designation" class="form-label">Designation</label>
<input type="text" class="form-control" id="designation" name="designation" required>
</div>
<div class="mb-3">
<label for="base_salary" class="form-label">Base Salary ($)</label>
<input type="number" step="0.01" class="form-control" id="base_salary" name="base_salary" required>
</div>
<button type="submit" class="btn btn-primary w-100">Add Employee</button>
</form>
</div>
</div>
</div>
<div class="col-lg-8">
<div class="card shadow-sm">
<div class="card-body">
<h2 class="card-title h5 mb-3">Current Employees</h2>
<div class="table-responsive">
<table class="table table-hover align-middle">
<thead class="table-light">
<tr>
<th>Name</th>
<th>Email</th>
<th>Designation</th>
<th class="text-end">Salary</th>
</tr>
</thead>
<tbody>
<?php if (empty($employees)): ?>
<tr>
<td colspan="4" class="text-center text-muted">No employees found. Add one to get started!</td>
</tr>
<?php else: ?>
<?php foreach ($employees as $employee): ?>
<tr>
<td><?= htmlspecialchars($employee['first_name'] . ' ' . $employee['last_name']) ?></td>
<td><?= htmlspecialchars($employee['email']) ?></td>
<td><?= htmlspecialchars($employee['designation']) ?></td>
<td class="text-end">$<?= number_format($employee['base_salary'], 2) ?></td>
</tr>
<?php endforeach; ?>
<?php endif; ?>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</main>
<footer class="text-center text-muted py-4 mt-auto">
<p>&copy; <?= date('Y') ?> <?= $project_name ?>. All rights reserved.</p>
</footer>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
<script src="assets/js/main.js?v=<?php echo time(); ?>"></script>
</body>
</html>