36716-vm/view-ticket.php
2025-12-07 05:00:42 +00:00

124 lines
4.4 KiB
PHP

<?php
require_once 'includes/header.php';
require_once 'db/config.php';
if (!isset($_SESSION['user_id']) || $_SESSION['user_type'] !== 'client') {
header('Location: login.php');
exit;
}
if (!isset($_GET['id'])) {
header('Location: support.php');
exit;
}
$ticket_id = $_GET['id'];
$client_id = $_SESSION['user_id'];
$pdo = db();
// Verify the ticket belongs to the client
$stmt = $pdo->prepare('SELECT * FROM support_tickets WHERE id = ? AND client_id = ?');
$stmt->execute([$ticket_id, $client_id]);
$ticket = $stmt->fetch();
if (!$ticket) {
// Ticket not found or doesn't belong to the client
header('Location: support.php');
exit;
}
// Handle new message submission
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['reply_to_ticket'])) {
$message = trim($_POST['message']);
if (!empty($message)) {
try {
$pdo->beginTransaction();
// Insert the new message
$stmt = $pdo->prepare('INSERT INTO support_ticket_messages (ticket_id, user_id, is_admin, message) VALUES (?, ?, ?, ?)');
$stmt->execute([$ticket_id, $client_id, false, $message]);
// Update the ticket's updated_at timestamp
$stmt = $pdo->prepare('UPDATE support_tickets SET updated_at = CURRENT_TIMESTAMP WHERE id = ?');
$stmt->execute([$ticket_id]);
$pdo->commit();
$_SESSION['success_message'] = 'Your reply has been sent.';
header('Location: view-ticket.php?id=' . $ticket_id);
exit;
} catch (Exception $e) {
$pdo->rollBack();
$error_message = 'Failed to send reply. Please try again.';
}
}
}
// Fetch all messages for the ticket
$stmt = $pdo->prepare("SELECT m.*, c.name as client_name, co.name as coach_name FROM support_ticket_messages m LEFT JOIN clients c ON m.user_id = c.id AND m.is_admin = 0 LEFT JOIN coaches co ON m.user_id = co.id AND m.is_admin = 1 WHERE m.ticket_id = ? ORDER BY m.created_at ASC");
$stmt->execute([$ticket_id]);
$messages = $stmt->fetchAll();
?>
<div class="container mt-5">
<a href="support.php" class="btn btn-secondary mb-3">Back to Tickets</a>
<h3><?php echo htmlspecialchars($ticket['subject']); ?></h3>
<p>Status: <span class="badge bg-<?php echo $ticket['status'] === 'Closed' ? 'secondary' : 'success'; ?>"><?php echo htmlspecialchars($ticket['status']); ?></span></p>
<?php if (isset($_SESSION['success_message'])): ?>
<div class="alert alert-success">
<?php echo $_SESSION['success_message']; unset($_SESSION['success_message']); ?>
</div>
<?php endif; ?>
<?php if (isset($error_message)): ?>
<div class="alert alert-danger">
<?php echo $error_message; ?>
</div>
<?php endif; ?>
<div class="card mb-4">
<div class="card-header">Conversation</div>
<div class="card-body">
<?php foreach ($messages as $message): ?>
<div class="mb-3">
<strong>
<?php
if ($message['is_admin']) {
// Here you might want to display a generic "Support Team" or the actual admin/coach name
echo 'Support Team';
} else {
echo htmlspecialchars($message['client_name']);
}
?>
</strong> <small class="text-muted"><?php echo date('M j, Y, g:i a', strtotime($message['created_at'])); ?></small>
<p><?php echo nl2br(htmlspecialchars($message['message'])); ?></p>
</div>
<hr>
<?php endforeach; ?>
</div>
</div>
<?php if ($ticket['status'] !== 'Closed'): ?>
<div class="card">
<div class="card-header">Reply</div>
<div class="card-body">
<form action="view-ticket.php?id=<?php echo $ticket_id; ?>" method="POST">
<div class="form-group">
<textarea class="form-control" name="message" rows="4" required></textarea>
</div>
<button type="submit" name="reply_to_ticket" class="btn btn-primary mt-3">Send Reply</button>
</form>
</div>
</div>
<?php else: ?>
<div class="alert alert-info">This ticket is closed. You cannot add new replies.</div>
<?php endif; ?>
</div>
<?php require_once 'includes/footer.php'; ?>