37497-vm/view_request.php
Flatlogic Bot 65bff23a0b 1
2026-01-16 09:31:10 +00:00

168 lines
7.2 KiB
PHP

<?php
session_start();
if (!isset($_SESSION['user_id'])) {
header('Location: login.php');
exit;
}
require_once 'db/config.php';
if (!isset($_GET['id']) || empty($_GET['id'])) {
header('Location: request_dashboard.php');
exit;
}
$request_id = $_GET['id'];
try {
$pdo = db();
$stmt = $pdo->prepare('SELECT * FROM ChangeRequests WHERE id = ?');
$stmt->execute([$request_id]);
$request = $stmt->fetch(PDO::FETCH_ASSOC);
if (!$request) {
die('Request not found.');
}
} catch (PDOException $e) {
die("Database error: " . $e->getMessage());
}
function getStatusColor($status) {
switch ($status) {
case 'Draft': return 'grey';
case 'Pending Approval': return 'orange';
case 'Approved': return 'green';
case 'Rejected': return 'red';
case 'In Progress': return 'blue';
case 'Completed': return 'purple';
default: return 'black';
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>View Change Request</title>
<style>
body { font-family: Inter, system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif; background-color: #F7F9FC; color: #333; margin: 0; padding: 20px; }
.container { max-width: 800px; margin: 0 auto; background-color: #FFFFFF; border-radius: 8px; box-shadow: 0 2px 4px rgba(0,0,0,0.1); padding: 20px; }
h1 { color: #4A90E2; }
.request-details { margin-top: 20px; }
.request-details p { margin: 10px 0; }
.request-details strong { display: inline-block; width: 150px; }
.status { padding: 5px 10px; border-radius: 15px; color: white; font-weight: bold; }
.actions { margin-top: 30px; }
.actions form { display: inline-block; margin-right: 5px; }
.actions button {
padding: 10px 20px;
border: none;
border-radius: 4px;
cursor: pointer;
color: white;
font-weight: bold;
}
.actions button[value="approve"] { background-color: #50E3C2; }
.actions button[value="reject"] { background-color: #E35050; }
.rejection-reason { margin-top: 15px; }
.rejection-reason textarea { width: 100%; padding: 10px; border-radius: 4px; border: 1px solid #ddd; }
</style>
</head>
<body>
<div class="container">
<h1>Request Details</h1>
<div class="request-details">
<p><strong>Request No.:</strong> <?php echo htmlspecialchars($request['request_number'] ?? 'N/A'); ?></p>
<p><strong>Title:</strong> <?php echo htmlspecialchars($request['request_title']); ?></p>
<p><strong>Background & Purpose:</strong></p>
<div style="padding: 10px; border: 1px solid #eee; border-radius: 5px; background: #fafafa;">
<?php echo nl2br(htmlspecialchars($request['background_purpose'])); ?>
</div>
<p><strong>Implementation Details:</strong></p>
<div style="padding: 10px; border: 1px solid #eee; border-radius: 5px; background: #fafafa;">
<?php echo nl2br(htmlspecialchars($request['implementation_details'])); ?>
</div>
<p><strong>Requester:</strong> <?php echo htmlspecialchars($request['requester_name']); ?></p>
<p><strong>Department:</strong> <?php echo htmlspecialchars($request['department_name']); ?></p>
<p><strong>Issued Date:</strong> <?php echo htmlspecialchars($request['issued_date']); ?></p>
<p><strong>Status:</strong>
<span class="status" style="background-color: <?php echo getStatusColor($request['status']); ?>;">
<?php echo htmlspecialchars(str_replace('_', ' ', $request['status'])); ?>
</span>
</p>
<p><strong>Pending Approval:</strong> <?php echo htmlspecialchars($request['approval_level_pending']); ?></p>
<?php if ($request['status'] === 'Rejected' && !empty($request['rejection_reason'])): ?>
<p><strong>Rejection Reason:</strong> <?php echo htmlspecialchars($request['rejection_reason']); ?></p>
<?php endif; ?>
</div>
<div class="actions">
<?php
$is_authorized = false;
if (isset($_SESSION['role'])) {
$user_role = $_SESSION['role'];
$user_department = $_SESSION['department'] ?? null;
$request_status = $request['status'];
$pending_level = $request['approval_level_pending'];
$request_department = $request['department_name'];
if ($request_status === 'Pending Approval' && $user_role === $pending_level) {
if ($user_role === 'Admin' || $user_department === $request_department) {
$is_authorized = true;
}
}
}
if ($is_authorized): ?>
<form action="approve_request.php" method="POST" id="approvalForm">
<input type="hidden" name="request_id" value="<?php echo $request['id']; ?>">
<button type="submit" name="action" value="approve">Approve</button>
<button type="button" id="rejectBtn" name="action" value="reject">Reject</button>
<div class="rejection-reason" id="rejectionReasonContainer" style="display:none;">
<label for="rejection_reason"><strong>Reason for Rejection:</strong></label>
<textarea id="rejection_reason" name="rejection_reason" rows="4"></textarea>
<button type="submit" id="submitRejectionBtn">Submit Rejection</button>
</div>
</form>
<?php endif; ?>
</div>
<br>
<a href="request_dashboard.php">Back to Dashboard</a>
</div>
<script>
const rejectBtn = document.getElementById('rejectBtn');
const rejectionReasonContainer = document.getElementById('rejectionReasonContainer');
const approvalForm = document.getElementById('approvalForm');
const submitRejectionBtn = document.getElementById('submitRejectionBtn');
const rejectionReasonTextarea = document.getElementById('rejection_reason');
if (rejectBtn) {
rejectBtn.addEventListener('click', () => {
rejectionReasonContainer.style.display = rejectionReasonContainer.style.display === 'none' ? 'block' : 'none';
});
}
if (submitRejectionBtn) {
submitRejectionBtn.addEventListener('click', (e) => {
e.preventDefault();
if (rejectionReasonTextarea.value.trim() === '') {
alert('Rejection reason is required.');
return;
}
const actionInput = document.createElement('input');
actionInput.setAttribute('type', 'hidden');
actionInput.setAttribute('name', 'action');
actionInput.setAttribute('value', 'reject');
approvalForm.appendChild(actionInput);
approvalForm.submit();
});
}
</script>
</body>
</html>