import { fetchTicket, fetchComments, fetchHistory, createComment, formatDateTime, formatMinutes, getHistoryIcon } from './ticket-shared.js'; document.addEventListener('DOMContentLoaded', () => { document.body.classList.add('user-view'); const ticketId = new URLSearchParams(window.location.search).get('id'); if (!ticketId) return window.location.href = '/'; // DOM elements const ticketTitle = document.getElementById('ticket-title-text'); const ticketIdElem = document.getElementById('ticket-id-val'); const ticketStatus = document.getElementById('ticket-status-badge'); const ticketPriority = document.getElementById('priority-text'); const ticketDescription = document.getElementById('ticket-description-text'); const ticketRequester = document.getElementById('ticket-requester'); const ticketCreated = document.getElementById('ticket-created'); const ticketUpdated = document.getElementById('ticket-updated'); const ticketCategory = document.getElementById('ticket-category'); const commentsContainer = document.getElementById('comments-container'); const historyContainer = document.getElementById('history-container'); const commentForm = document.getElementById('comment-form'); const tabBtns = document.querySelectorAll('.tab-btn'); const totalTimeDisplay = document.getElementById('total-time'); let webhookConfig = null; // Fetch Webhook Configuration async function loadWebhookConfig() { try { const response = await fetch('/api/webhooks'); if (response.ok) { webhookConfig = await response.json(); console.log('Webhook configuration loaded:', webhookConfig.configured); } else { console.warn('Failed to load webhook configuration'); webhookConfig = { admin: null, user: null, configured: { admin: false, user: false } }; } } catch (error) { console.error('Error loading webhook configuration:', error); webhookConfig = { admin: null, user: null, configured: { admin: false, user: false } }; } } // Send Webhook Notification async function sendAdminWebhook(message) { if (!webhookConfig?.admin) { console.log('Admin webhook not configured, skipping notification'); return; } try { const res = await fetch(webhookConfig.admin, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ content: message }) }); if (!res.ok) { console.error('Admin webhook failed:', res.statusText); } } catch (err) { console.error('Error sending admin webhook:', err); } } // Links automatically convert to clickable function linkify(text) { const urlPattern = /(\bhttps?:\/\/[^\s<]+)/g; return text.replace(urlPattern, '$1'); } let originalStatus = ''; let originalPriority = ''; async function loadTicketDetails() { const ticket = await fetchTicket(ticketId); if (!ticket) return alert('Ticket not found'); ticketTitle.textContent = ticket.title; ticketIdElem.textContent = ticket.id; ticketStatus.textContent = ticket.status; ticketDescription.innerHTML = linkify(ticket.description || ''); ticketRequester.textContent = ticket.requester; ticketCreated.textContent = formatDateTime(ticket.created_at); ticketUpdated.textContent = formatDateTime(ticket.updated_at); ticketPriority.textContent = ticket.priority; if (ticketCategory) { ticketCategory.textContent = ticket.category || '—'; } ticketStatus.className = `ticket-status status-${ticket.status.replace(/ /g, '-')}`; const totalTimeDisplay = document.getElementById('total-time'); if (totalTimeDisplay) { totalTimeDisplay.textContent = formatMinutes(ticket.total_time || 0); } originalStatus = ticket.status; originalPriority = ticket.priority; } async function loadComments() { const comments = await fetchComments(ticketId); commentsContainer.innerHTML = comments.map(comment => { const role = comment.role ? comment.role.toLowerCase() : 'user'; let avatarUrl = '/assets/user.jpg'; if (role === 'admin') avatarUrl = '/assets/admin.jpg'; else if (role === 'moderator') avatarUrl = '/assets/moderator.jpg'; // Comment content is linkified const linkedContent = linkify(comment.content || ''); return `