<?php $u=current_user();if(is_admin())$tickets=db()->query('SELECT t.*,u.name user_name,u.email user_email FROM tickets t JOIN users u ON u.id=t.user_id ORDER BY t.id DESC')->fetchAll();else{$q=db()->prepare('SELECT t.*,u.name user_name,u.email user_email FROM tickets t JOIN users u ON u.id=t.user_id WHERE t.user_id=? ORDER BY t.id DESC');$q->execute([$u['id']]);$tickets=$q->fetchAll();}?>
<section class="panel"><div class="panel-header"><div><h2>Suporte LinkExpress</h2><p>Abra chamados e acompanhe as respostas da equipe.</p></div><button class="btn btn-primary" data-modal-open="#ticketModal">+ Novo chamado</button></div><div class="table-wrap"><table><thead><tr><th>Assunto</th><th>Usuário</th><th>Prioridade</th><th>Atualização</th><th>Status</th><th>Ação</th></tr></thead><tbody><?php foreach($tickets as $t):?><tr><td><strong><?= e($t['subject']) ?></strong><small style="display:block"><?= e(short_text($t['message'],70)) ?></small></td><td><?= e($t['user_name']) ?></td><td><?= status_badge($t['priority']) ?></td><td><?= e(date('d/m/Y H:i',strtotime($t['updated_at']?:$t['created_at']))) ?></td><td><?= status_badge($t['status']) ?></td><td><button class="btn btn-secondary btn-sm" data-modal-open="#ticketView<?= (int)$t['id'] ?>">Acessar</button></td></tr><div class="modal" id="ticketView<?= (int)$t['id'] ?>"><div class="modal-card"><div class="modal-head"><h2><?= e($t['subject']) ?></h2><button class="close-btn" data-modal-close>×</button></div><div class="notice"><strong>Mensagem:</strong><br><?= nl2br(e($t['message'])) ?></div><?php if($t['admin_reply']):?><div class="panel" style="margin-top:15px"><strong>Resposta do suporte</strong><p class="muted" style="margin-top:8px"><?= nl2br(e($t['admin_reply'])) ?></p></div><?php endif;?><?php if(is_admin()):?><form method="post" action="actions.php"><?= csrf_field() ?><input type="hidden" name="action" value="update_ticket"><input type="hidden" name="return_page" value="support"><input type="hidden" name="id" value="<?= (int)$t['id'] ?>"><div class="form-grid"><div class="form-group"><label>Status</label><select name="status"><option value="open"<?= selected($t['status'],'open') ?>>Aberto</option><option value="in_progress"<?= selected($t['status'],'in_progress') ?>>Em atendimento</option><option value="closed"<?= selected($t['status'],'closed') ?>>Finalizado</option></select></div><div class="form-group"><label>Prioridade</label><select name="priority"><option value="low"<?= selected($t['priority'],'low') ?>>Baixa</option><option value="normal"<?= selected($t['priority'],'normal') ?>>Normal</option><option value="high"<?= selected($t['priority'],'high') ?>>Alta</option></select></div><div class="form-group full"><label>Resposta</label><textarea name="admin_reply"><?= e($t['admin_reply']) ?></textarea></div></div><div class="form-actions"><button class="btn btn-primary">Responder</button></div></form><?php if(is_admin()):?><?php $delState=moderation_deletion_approval_state('ticket',(int)$t['id'],(int)$u['id']);?><form method="post" action="actions.php" style="margin-top:10px"><?= csrf_field() ?><input type="hidden" name="action" value="delete_ticket"><input type="hidden" name="return_page" value="support"><input type="hidden" name="id" value="<?= (int)$t['id'] ?>"><button class="btn btn-danger btn-sm" type="submit" data-confirm="<?= is_super_admin()?'Apagar definitivamente esta mensagem de suporte?':'Registrar sua confirmação para apagar esta mensagem? São necessárias duas confirmações.' ?>"><?= is_super_admin()?'Apagar mensagem':'Confirmar exclusão ('.(int)$delState['count'].'/2)' ?></button></form><?php endif;?><?php endif;?></div></div><?php endforeach;?></tbody></table></div></section>
<div class="modal" id="ticketModal"><div class="modal-card"><div class="modal-head"><h2>Novo chamado</h2><button class="close-btn" data-modal-close>×</button></div><form method="post" action="actions.php"><?= csrf_field() ?><input type="hidden" name="action" value="save_ticket"><input type="hidden" name="return_page" value="support"><div class="form-grid"><div class="form-group"><label>Assunto</label><input class="input" name="subject" required></div><div class="form-group"><label>Prioridade</label><select name="priority"><option value="low">Baixa</option><option value="normal">Normal</option><option value="high">Alta</option></select></div><div class="form-group full"><label>Mensagem</label><textarea name="message" required></textarea></div></div><div class="form-actions"><button class="btn btn-primary">Acessar Chamado</button></div></form></div></div>
