<?php $base=array_filter(['token'=>$token]); ?>
<main class="container py-4 py-md-5">
<div class="d-flex flex-column flex-md-row justify-content-between gap-3 mb-4"><div><span class="badge text-bg-dark rounded-pill px-3 py-2 mb-2">Privado entre instrutores</span><h1 class="h3 fw-bold mb-1">Grupos de mensagens</h1><p class="text-muted mb-0">Mensagens e áudios desta área não aparecem para o administrador.</p></div><a class="btn btn-primary rounded-pill align-self-start" href="<?= e(url('professional/panel',$base)) ?>"><i class="bi bi-arrow-left"></i> Painel</a></div>
<div class="row g-4"><div class="col-lg-4">
<div class="card border-0 shadow-soft rounded-4 mb-4"><div class="card-body p-4"><h2 class="h5 fw-bold"><i class="bi bi-people-fill"></i> Criar grupo</h2><form method="post" action="<?= e(url('professional/chat-groups/save')) ?>" class="d-grid gap-3"><?= CSRF::input() ?><input type="hidden" name="token" value="<?= e($token) ?>"><div><label class="form-label">Nome do grupo</label><input name="group_name" class="form-control" maxlength="180" required></div><div><label class="form-label">Instrutores participantes</label><div class="border rounded-4 p-3" style="max-height:260px;overflow:auto"><?php foreach($professionals as $p): ?><label class="form-check mb-2"><input class="form-check-input" type="checkbox" name="member_ids[]" value="<?= (int)$p['id'] ?>"><span class="form-check-label"><?= e($p['name']) ?> <small class="text-muted">· <?= e($p['type']) ?></small></span></label><?php endforeach; ?></div><small class="text-muted">Você será incluído automaticamente.</small></div><button class="btn btn-dark rounded-pill"><i class="bi bi-plus-circle"></i> Criar grupo privado</button></form></div></div>
<div class="card border-0 shadow-soft rounded-4"><div class="card-body p-4"><h2 class="h5 fw-bold">Meus grupos</h2><?php if(empty($groups)): ?><div class="alert alert-light border mb-0">Nenhum grupo criado.</div><?php else: ?><div class="d-grid gap-2"><?php foreach($groups as $g): ?><a class="btn <?= !empty($group)&&((int)$group['id']===(int)$g['id'])?'btn-dark':'btn-outline-dark' ?> text-start rounded-4 p-3" href="<?= e(url('professional/chat-groups',array_merge($base,['id'=>(int)$g['id']]))) ?>"><strong class="d-block"><?= e($g['name']) ?></strong><small><?= (int)$g['member_count'] ?> participante(s)</small></a><?php endforeach; ?></div><?php endif; ?></div></div>
</div><div class="col-lg-8">
<?php if(empty($group)): ?><div class="card border-0 shadow-soft rounded-4"><div class="card-body p-5 text-center"><i class="bi bi-chat-square-heart fs-1"></i><h2 class="h5 mt-3">Selecione ou crie um grupo</h2><p class="text-muted mb-0">Converse em particular com outros instrutores.</p></div></div><?php else: ?>
<div class="card border-0 shadow-soft rounded-4"><div class="card-body p-4"><div class="d-flex justify-content-between gap-3 align-items-start mb-3"><div><h2 class="h4 fw-bold mb-1"><?= e($group['name']) ?></h2><div class="d-flex flex-wrap gap-1"><?php foreach($members as $m): ?><span class="badge text-bg-light border text-dark"><?= e(short_person_name($m['name'])) ?></span><?php endforeach; ?></div></div><span class="badge text-bg-success"><i class="bi bi-shield-lock-fill"></i> Privado</span></div>
<div class="message-action-hint"><i class="bi bi-hand-index-thumb"></i> Toque em uma mensagem para mostrar as opções de apagar.</div>
<div class="chat-scroll-panel mb-4" data-chat-scroll><?php if(empty($messages)): ?><div class="alert alert-light border">Ainda não há mensagens neste grupo.</div><?php endif; ?><?php foreach($messages as $message): ?><?php $fileUrl=!empty($message['attachment_path'])?url('portal/messages/file',array_merge($base,['message_id'=>(int)$message['id']])):''; ?><div class="internal-message <?= !empty($message['is_mine'])?'mine':'received' ?>" data-message-item tabindex="0"><div class="small text-muted mb-1"><strong><?= e($message['sender_name']) ?></strong> · <?= e(date('d/m/Y H:i',strtotime($message['created_at']))) ?></div><div class="message-bubble"><?php if(trim((string)$message['message_body'])!==''): ?><?= nl2br(e($message['message_body'])) ?><?php endif; ?><?php if($fileUrl!==''): ?><div class="audio-message-wrap mt-2"><audio controls preload="metadata" src="<?= e($fileUrl) ?>"></audio><small class="d-block opacity-75 mt-1"><i class="bi bi-mic-fill"></i> Mensagem de áudio</small></div><?php endif; ?></div><div class="message-actions mt-2" data-message-actions><form method="post" action="<?= e(url('portal/messages/delete')) ?>"><?= CSRF::input() ?><input type="hidden" name="token" value="<?= e($token) ?>"><input type="hidden" name="message_id" value="<?= (int)$message['id'] ?>"><input type="hidden" name="professional_group_id" value="<?= (int)$group['id'] ?>"><input type="hidden" name="delete_mode" value="<?= !empty($message['is_mine'])?'all':'self' ?>"><button class="btn btn-sm btn-outline-danger rounded-pill"><?= !empty($message['is_mine'])?'Apagar para todos':'Apagar para mim' ?></button></form></div></div><?php endforeach; ?></div>
<form method="post" action="<?= e(url('professional/chat-groups/send')) ?>" enctype="multipart/form-data" class="row g-2" data-audio-message-form data-audio-chunk-url="<?= e(url('portal/messages/audio-chunk')) ?>" data-audio-finalize-url="<?= e(url('portal/messages/audio-finalize')) ?>"><?= CSRF::input() ?><input type="hidden" name="token" value="<?= e($token) ?>"><input type="hidden" name="group_id" value="<?= (int)$group['id'] ?>"><div class="col-12"><textarea name="message_body" rows="2" class="form-control" placeholder="Mensagem para os instrutores..."></textarea></div><div class="col-12" data-audio-recorder><input type="file" name="message_audio" class="d-none" accept="audio/*,.webm,.ogg,.mp3,.m4a,.wav,.aac" data-audio-input><div class="audio-recorder-controls d-flex flex-wrap gap-2"><button type="button" class="btn btn-outline-danger rounded-pill" data-audio-record><i class="bi bi-mic-fill"></i> Gravar</button><button type="button" class="btn btn-danger rounded-pill d-none" data-audio-stop><i class="bi bi-stop-fill"></i> Parar</button><button type="button" class="btn btn-outline-secondary rounded-pill" data-audio-choose><i class="bi bi-paperclip"></i> Escolher áudio</button><button type="button" class="btn btn-outline-dark rounded-pill d-none" data-audio-clear>Remover</button></div><div class="small text-muted mt-2" data-audio-status>Nenhum áudio selecionado.</div><audio class="w-100 mt-2 d-none" controls data-audio-preview></audio></div><div class="col-12"><button class="btn btn-success rounded-pill px-4"><i class="bi bi-send"></i> Enviar</button></div></form>
</div></div><?php endif; ?>
</div></div></main>
