36398-vm/leave_requests.php
2025-11-28 03:31:58 +00:00

231 lines
7.0 KiB
PHP

<?php
session_start();
require_once 'db/config.php';
if (!isset($_SESSION['loggedin']) || $_SESSION['loggedin'] !== true) {
header('location: login.php');
exit;
}
if ($_SESSION['role'] !== 'Admin' && $_SESSION['role'] !== 'HR' && $_SESSION['role'] !== 'Employee') {
header('location: index.php');
exit;
}
$requests = [];
try {
$pdo = db();
$sql = "SELECT lr.*, u.username FROM leave_requests lr JOIN users u ON lr.employee_id = u.id";
if ($_SESSION['role'] == 'Employee') {
$sql .= " WHERE lr.employee_id = :employee_id";
}
$sql .= " ORDER BY lr.created_at DESC";
$stmt = $pdo->prepare($sql);
if ($_SESSION['role'] == 'Employee') {
$stmt->bindParam(':employee_id', $_SESSION['id'], PDO::PARAM_INT);
}
$stmt->execute();
$requests = $stmt->fetchAll(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
die("Could not fetch leave requests.");
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Leave Requests - Employee Attendance System</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.min.css">
<link rel="stylesheet" href="assets/css/custom.css">
<link href='https://cdn.jsdelivr.net/npm/fullcalendar@5.11.3/main.min.css' rel='stylesheet' />
</head>
<body>
<?php include 'sidebar.php'; ?>
<div class="main-content">
<div class="container-fluid">
<div class="d-flex justify-content-between align-items-center">
<h1 class="mt-4">Leave Requests</h1>
<div>
<a href="submit_leave_request.php" class="btn btn-primary">Submit New Request</a>
<button id="toggle-view" class="btn btn-secondary">Calendar View</button>
</div>
</div>
<p class="lead">Manage and view employee leave requests.</p>
<?php if ($_SESSION['role'] === 'Admin' || $_SESSION['role'] === 'HR'): ?>
<p class="text-muted">You are viewing all employee leave requests.</p>
<?php else: ?>
<p class="text-muted">You are viewing your own leave requests.</p>
<?php endif; ?> <div id="calendar-view" style="display: none;">
<div id="calendar"></div>
</div>
<div id="list-view">
<div class="card shadow-sm">
<div class="card-body">
<table class="table table-hover">
<thead class="table-light">
<tr>
<th scope="col">Employee</th>
<th scope="col">Start Date</th>
<th scope="col">End Date</th>
<th scope="col">Reason</th>
<th scope="col">Status</th>
<th scope="col">Actions</th>
</tr>
</thead>
<tbody>
<?php if (empty($requests)):
?>
<tr>
<td colspan="6" class="text-center">No leave requests found.</td>
</tr>
<?php else:
?>
<?php foreach ($requests as $request):
?>
<tr>
<td><?php echo htmlspecialchars($request['username']); ?></td>
<td><?php echo htmlspecialchars($request['start_date']); ?></td>
<td><?php echo htmlspecialchars($request['end_date']); ?></td>
<td><?php echo htmlspecialchars($request['reason']); ?></td>
<td>
<span class="badge bg-<?php echo $request['status'] == 'approved' ? 'success' : ($request['status'] == 'rejected' ? 'danger' : 'warning'); ?>">
<?php echo ucfirst($request['status']); ?>
</span>
</td>
<td>
<?php if (($_SESSION['role'] === 'Admin' || $_SESSION['role'] === 'HR') && $request['status'] === 'pending'): ?>
<a href="update_leave_status.php?id=<?php echo $request['id']; ?>&status=approved" class="btn btn-sm btn-outline-success">Approve</a>
<a href="update_leave_status.php?id=<?php echo $request['id']; ?>&status=rejected" class="btn btn-sm btn-outline-danger">Reject</a>
<?php endif; ?>
</td>
</tr>
<?php endforeach; ?>
<?php endif; ?>
</tbody>
</table>
</div>
</div>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
<script src='https://cdn.jsdelivr.net/npm/fullcalendar@5.11.3/main.min.js'></script>
<script>
document.addEventListener('DOMContentLoaded', function() {
var calendarEl = document.getElementById('calendar');
var calendar = new FullCalendar.Calendar(calendarEl, {
initialView: 'dayGridMonth',
events: 'get_leave_events.php',
height: 'auto'
});
var listView = document.getElementById('list-view');
var calendarView = document.getElementById('calendar-view');
var toggleButton = document.getElementById('toggle-view');
toggleButton.addEventListener('click', function() {
if (listView.style.display === 'none') {
listView.style.display = 'block';
calendarView.style.display = 'none';
toggleButton.textContent = 'Calendar View';
} else {
listView.style.display = 'none';
calendarView.style.display = 'block';
toggleButton.textContent = 'List View';
calendar.render();
}
});
});
</script>
</body>
</html>