36766-vm/service_request_details.php
Flatlogic Bot 6c14b2436f 2.0
2025-12-18 09:40:37 +00:00

250 lines
10 KiB
PHP

<?php
require_once 'includes/header.php';
require_once 'db/config.php';
if (!isset($_SESSION['user_id'])) {
header('Location: login.php');
exit;
}
if (!isset($_GET['id']) || !is_numeric($_GET['id'])) {
header('Location: service_requests.php');
exit;
}
$request_id = $_GET['id'];
$dealer_id = $_SESSION['dealer_id'];
try {
$pdo = db();
$stmt = $pdo->prepare(
"SELECT sr.*
FROM service_requests sr
WHERE sr.id = ? AND (sr.dealer_id = ? OR ?)"
);
$stmt->execute([$request_id, $dealer_id, $_SESSION['is_admin']]);
$request = $stmt->fetch();
if (!$request) {
// Request not found or doesn't belong to the dealer
header('Location: service_requests.php');
exit;
}
// Fetch service request items
$stmt_items = $pdo->prepare(
"SELECT sri.serial_number, sri.issue_description, p.name as product_name
FROM service_request_items sri
JOIN products p ON sri.product_id = p.id
WHERE sri.service_request_id = ?"
);
$stmt_items->execute([$request_id]);
$request_items = $stmt_items->fetchAll();
// Handle comment submission
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['comment'])) {
$comment = trim($_POST['comment']);
if (!empty($comment)) {
$stmt_insert_comment = $pdo->prepare(
"INSERT INTO service_request_comments (service_request_id, user_id, comment) VALUES (?, ?, ?)"
);
$stmt_insert_comment->execute([$request_id, $_SESSION['user_id'], $comment]);
// Create a notification
$current_user_id = $_SESSION['user_id'];
$request_owner_id = $request['user_id'];
$message = "A new comment has been added to your service request #{$request_id}.";
if ($current_user_id != $request_owner_id) {
// Notify the request owner
$stmt_notify = $pdo->prepare("INSERT INTO notifications (user_id, service_request_id, message) VALUES (?, ?, ?)");
$stmt_notify->execute([$request_owner_id, $request_id, $message]);
} else {
// Notify all admins
$stmt_admins = $pdo->query("SELECT id FROM users WHERE is_admin = 1");
$admins = $stmt_admins->fetchAll(PDO::FETCH_COLUMN);
$stmt_notify = $pdo->prepare("INSERT INTO notifications (user_id, service_request_id, message) VALUES (?, ?, ?)");
foreach ($admins as $admin_id) {
if($admin_id != $current_user_id) {
$stmt_notify->execute([$admin_id, $request_id, $message]);
}
}
}
// Redirect to the same page to prevent form resubmission
header("Location: service_request_details.php?id=$request_id");
exit;
}
}
// Fetch comments for the service request
$stmt_comments = $pdo->prepare(
"SELECT c.*, u.username
FROM service_request_comments c
JOIN users u ON c.user_id = u.id
WHERE c.service_request_id = ?
ORDER BY c.created_at ASC"
);
$stmt_comments->execute([$request_id]);
$comments = $stmt_comments->fetchAll();
} catch (PDOException $e) {
die("Database error: " . $e->getMessage());
}
?>
<nav aria-label="breadcrumb">
<ol class="breadcrumb">
<li class="breadcrumb-item"><a href="service_requests.php">Service Requests</a></li>
<li class="breadcrumb-item active" aria-current="page">Request #<?php echo htmlspecialchars($request['id']); ?></li>
</ol>
</nav>
<h1 class="mb-4">Service Request Details</h1>
<div class="card">
<div class="card-header d-flex justify-content-between align-items-center">
<h5 class="mb-0">Request #<?php echo htmlspecialchars($request['id']); ?></h5>
<span class="badge bg-warning text-dark"><?php echo htmlspecialchars(ucfirst($request['status'])); ?></span>
</div>
<div class="card-body">
<div class="row">
<div class="col-md-6">
<p><strong>Submitted At:</strong> <?php echo date('F j, Y, g:i a', strtotime($request['created_at'])); ?></p>
<p><strong>Last Updated:</strong> <?php echo date('F j, Y, g:i a', strtotime($request['updated_at'])); ?></p>
</div>
</div>
<hr>
<h5>Products</h5>
<table class="table table-bordered">
<thead>
<tr>
<th>Product Name</th>
<th>Serial Number</th>
<th>Issue Description</th>
</tr>
</thead>
<tbody>
<?php foreach ($request_items as $item): ?>
<tr>
<td><?php echo htmlspecialchars($item['product_name']); ?></td>
<td><?php echo htmlspecialchars($item['serial_number']); ?></td>
<td><?php echo nl2br(htmlspecialchars($item['issue_description'])); ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php if (!empty($request['file_path'])) : ?>
<hr>
<h5>Attached File</h5>
<p><a href="<?php echo htmlspecialchars($request['file_path']); ?>" target="_blank">View Attached File</a></p>
<?php endif; ?>
<?php if (isset($_SESSION['is_admin']) && $_SESSION['is_admin'] || !empty($_SESSION['dealer_id'])) : ?>
<hr>
<h5>Update Status</h5>
<form action="update_service_request_status.php" method="POST">
<input type="hidden" name="request_id" value="<?php echo $request_id; ?>">
<div class="input-group">
<select name="status" class="form-select">
<option value="pending" <?php if ($request['status'] == 'pending') echo 'selected'; ?>>Pending</option>
<option value="in_progress" <?php if ($request['status'] == 'in_progress') echo 'selected'; ?>>In Progress</option>
<option value="resolved" <?php if ($request['status'] == 'resolved') echo 'selected'; ?>>Resolved</option>
<option value="closed" <?php if ($request['status'] == 'closed') echo 'selected'; ?>>Closed</option>
</select>
<button type="submit" class="btn btn-primary">Update</button>
</div>
</form>
<?php endif; ?>
</div>
</div>
<div class="card mt-4">
<div class="card-header">
<h5 class="mb-0">Shipment Details</h5>
</div>
<div class="card-body">
<?php
$stmt_shipment = $pdo->prepare("SELECT * FROM shipment_details WHERE service_request_id = ?");
$stmt_shipment->execute([$request_id]);
$shipments = $stmt_shipment->fetchAll();
?>
<?php if (empty($shipments)): ?>
<p>No shipment details available.</p>
<?php if (!isset($_SESSION['is_admin']) || !$_SESSION['is_admin'] && empty($_SESSION['dealer_id'])) : ?>
<p class="text-muted">Only administrators and dealers can add shipment details.</p>
<?php endif; ?>
<?php else: ?>
<table class="table">
<thead>
<tr>
<th>Carrier</th>
<th>Tracking Number</th>
<th>Shipment Date</th>
<?php if (isset($_SESSION['is_admin']) && $_SESSION['is_admin'] || !empty($_SESSION['dealer_id'])) : ?>
<th>Actions</th>
<?php endif; ?>
</tr>
</thead>
<tbody>
<?php foreach ($shipments as $shipment): ?>
<tr>
<td><?php echo htmlspecialchars($shipment['carrier']); ?></td>
<td><?php echo htmlspecialchars($shipment['tracking_number']); ?></td>
<td><?php echo htmlspecialchars($shipment['shipment_date']); ?></td>
<?php if (isset($_SESSION['is_admin']) && $_SESSION['is_admin'] || !empty($_SESSION['dealer_id'])) : ?>
<td>
<a href="edit_shipment_details.php?id=<?php echo $shipment['id']; ?>" class="btn btn-sm btn-primary">Edit</a>
<a href="delete_shipment_details.php?id=<?php echo $shipment['id']; ?>" class="btn btn-sm btn-danger" onclick="return confirm('Are you sure you want to delete this shipment?')">Delete</a>
</td>
<?php endif; ?>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php endif; ?>
<div class="d-flex justify-content-end">
<?php if ((isset($_SESSION['is_admin']) && $_SESSION['is_admin']) || !empty($_SESSION['dealer_id'])) : ?>
<a href="add_shipment_details.php?request_id=<?php echo $request_id; ?>" class="btn btn-primary">Add Shipment</a>
<?php endif; ?>
</div>
</div>
</div>
<div class="card mt-4">
<div class="card-header">
<h5 class="mb-0">Comments</h5>
</div>
<div class="card-body">
<?php foreach ($comments as $comment): ?>
<div class="d-flex mb-3">
<div class="flex-shrink-0">
<i class="bi bi-person-circle fs-3"></i>
</div>
<div class="ms-3">
<div class="fw-bold"><?php echo htmlspecialchars($comment['username']); ?></div>
<?php echo nl2br(htmlspecialchars($comment['comment'])); ?>
<div class="text-muted fs-sm"><?php echo date('F j, Y, g:i a', strtotime($comment['created_at'])); ?></div>
</div>
</div>
<?php endforeach; ?>
<?php if (empty($comments)): ?>
<p>No comments yet.</p>
<?php endif; ?>
</div>
<div class="card-footer">
<form method="POST" action="service_request_details.php?id=<?php echo $request_id; ?>">
<div class="input-group">
<textarea class="form-control" name="comment" placeholder="Add a comment..." rows="3" required></textarea>
<button class="btn btn-primary" type="submit">Post Comment</button>
</div>
</form>
</div>
</div>
<?php require_once 'includes/footer.php'; ?>