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

228 lines
11 KiB
PHP

<?php
require_once 'includes/header.php';
require_once 'db/config.php';
if (!isset($_SESSION['user_id'])) {
header('Location: login.php');
exit;
}
$dealer_id = $_SESSION['dealer_id'];
$success_message = '';
$error_message = '';
try {
$pdo = db();
// Fetch products for the dropdown
$stmt_products = $pdo->query("SELECT id, name, model_number FROM products ORDER BY name ASC");
$products = $stmt_products->fetchAll();
// Handle form submission
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$submitted_products = $_POST['products'] ?? [];
$file_path = null;
if (isset($_FILES['file_upload']) && $_FILES['file_upload']['error'] == UPLOAD_ERR_OK) {
$upload_dir = __DIR__ . '/uploads/';
if (!is_dir($upload_dir)) {
mkdir($upload_dir, 0777, true);
}
$file_name = uniqid('file_') . '_' . basename($_FILES['file_upload']['name']);
$target_file = $upload_dir . $file_name;
if (move_uploaded_file($_FILES['file_upload']['tmp_name'], $target_file)) {
$file_path = 'uploads/' . $file_name;
} else {
$error_message = "Sorry, there was an error uploading your file.";
}
}
if (empty($submitted_products)) {
$error_message = "Please add at least one product and provide an issue description.";
} else {
try {
$pdo->beginTransaction();
// Create the main service request
$sql_request = "INSERT INTO service_requests (dealer_id, user_id, file_path) VALUES (?, ?, ?)";
$stmt_request = $pdo->prepare($sql_request);
$stmt_request->execute([$dealer_id, $_SESSION['user_id'], $file_path]);
$service_request_id = $pdo->lastInsertId();
// Add each product to the service_request_items table
$sql_item = "INSERT INTO service_request_items (service_request_id, product_id, serial_number, issue_description) VALUES (?, ?, ?, ?)";
$stmt_item = $pdo->prepare($sql_item);
foreach ($submitted_products as $product_data) {
$product_id = trim($product_data['product_id']);
$serial_number = trim($product_data['serial_number']);
$issue_description = trim($product_data['issue_description']);
if (!empty($product_id) && !empty($serial_number) && !empty($issue_description)) {
$stmt_item->execute([$service_request_id, $product_id, $serial_number, $issue_description]);
}
}
$pdo->commit();
$success_message = "Service request submitted successfully!";
} catch (PDOException $e) {
$pdo->rollBack();
$error_message = "Failed to submit service request: " . $e->getMessage();
}
}
}
// Fetch existing service requests for the dealer
$stmt_requests = $pdo->prepare(
"SELECT sr.id, sr.status, sr.created_at,
GROUP_CONCAT(p.name SEPARATOR ', ') as product_names,
GROUP_CONCAT(sri.serial_number SEPARATOR ', ') as serial_numbers
FROM service_requests sr
LEFT JOIN service_request_items sri ON sr.id = sri.service_request_id
LEFT JOIN products p ON sri.product_id = p.id
WHERE sr.dealer_id = ?
GROUP BY sr.id
ORDER BY sr.created_at DESC"
);
$stmt_requests->execute([$dealer_id]);
$service_requests = $stmt_requests->fetchAll();
} catch (PDOException $e) {
$error_message = "Database error: " . $e->getMessage();
}
?>
<div class="row">
<div class="col-md-5">
<div class="card">
<div class="card-header">
<h1 class="h3 mb-0">Submit a Service Request</h1>
</div>
<div class="card-body">
<?php if ($success_message): ?>
<div class="alert alert-success"><?php echo $success_message; ?></div>
<?php endif; ?>
<?php if ($error_message): ?>
<div class="alert alert-danger"><?php echo $error_message; ?></div>
<?php endif; ?>
<form action="service_requests.php" method="POST" enctype="multipart/form-data">
<div id="product-entries">
<div class="product-entry mb-3 border p-3">
<div class="row">
<div class="col-md-6">
<label for="product_id_1" class="form-label">Product</label>
<select class="form-select" id="product_id_1" name="products[1][product_id]" required>
<option value="">-- Select a Product --</option>
<?php foreach ($products as $product): ?>
<option value="<?php echo htmlspecialchars($product['id']); ?>">
<?php echo htmlspecialchars($product['name'] . ' (' . $product['model_number'] . ')'); ?>
</option>
<?php endforeach; ?>
</select>
</div>
<div class="col-md-6">
<label for="serial_number_1" class="form-label">Serial Number</label>
<input type="text" class="form-control" id="serial_number_1" name="products[1][serial_number]" required>
</div>
</div>
<div class="mt-2">
<label for="issue_description_1" class="form-label">Issue Description</label>
<textarea class="form-control" id="issue_description_1" name="products[1][issue_description]" rows="2" required></textarea>
</div>
</div>
</div>
<button type="button" class="btn btn-secondary mb-3" id="add-product-btn">Add Another Product</button>
<div class="mb-3">
<label for="file_upload" class="form-label">Attach a File (optional)</label>
<input type="file" class="form-control" id="file_upload" name="file_upload">
</div>
<button type="submit" class="btn btn-primary"><i class="bi bi-send"></i> Submit Request</button>
</form>
</div>
</div>
</div>
<div class="col-md-7">
<div class="card">
<div class="card-header">
<h1 class="h3 mb-0">Your Service Requests</h1>
</div>
<div class="card-body">
<?php if (empty($service_requests)): ?>
<p>You have not submitted any service requests yet.</p>
<?php else: ?>
<table class="table table-striped">
<thead>
<tr>
<th>Request ID</th>
<th>Products</th>
<th>Serials</th>
<th>Status</th>
<th>Date</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php foreach ($service_requests as $request): ?>
<tr>
<td><?php echo htmlspecialchars($request['id']); ?></td>
<td><?php echo htmlspecialchars($request['product_names']); ?></td>
<td><?php echo htmlspecialchars($request['serial_numbers']); ?></td>
<td><span class="badge bg-warning text-dark"><?php echo htmlspecialchars(ucfirst($request['status'])); ?></span></td>
<td><?php echo date('M j, Y', strtotime($request['created_at'])); ?></td>
<td>
<a href="service_request_details.php?id=<?php echo $request['id']; ?>" class="btn btn-primary btn-sm">View Details</a>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php endif; ?>
</div>
</div>
</div>
</div>
<script>
document.addEventListener('DOMContentLoaded', function() {
let productEntryCount = 1;
const addProductBtn = document.getElementById('add-product-btn');
const productEntries = document.getElementById('product-entries');
const productOptions = `<?php foreach ($products as $product) { echo '<option value="' . htmlspecialchars($product['id']) . '">' . htmlspecialchars($product['name'] . ' (' . $product['model_number'] . ')') . '</option>'; } ?>`;
addProductBtn.addEventListener('click', function() {
productEntryCount++;
const newEntry = document.createElement('div');
newEntry.classList.add('product-entry', 'mb-3', 'border', 'p-3');
newEntry.innerHTML = `
<div class="row">
<div class="col-md-6">
<label for="product_id_${productEntryCount}" class="form-label">Product</label>
<select class="form-select" id="product_id_${productEntryCount}" name="products[${productEntryCount}][product_id]" required>
<option value="">-- Select a Product --</option>
${productOptions}
</select>
</div>
<div class="col-md-6">
<label for="serial_number_${productEntryCount}" class="form-label">Serial Number</label>
<input type="text" class="form-control" id="serial_number_${productEntryCount}" name="products[${productEntryCount}][serial_number]" required>
</div>
</div>
<div class="mt-2">
<label for="issue_description_${productEntryCount}" class="form-label">Issue Description</label>
<textarea class="form-control" id="issue_description_${productEntryCount}" name="products[${productEntryCount}][issue_description]" rows="2" required></textarea>
</div>
`;
productEntries.appendChild(newEntry);
});
});
</script>
<?php
require_once 'includes/footer.php';
?>