<?php
$base=array_filter(['token'=>$token]);
$editing=!empty($editGroup);
$selected=array_fill_keys(array_map('intval',$editMemberIds??[]),true);
?>
<main class="container py-4 py-md-5">
    <div class="d-flex flex-column flex-md-row justify-content-between gap-3 align-items-md-center mb-4">
        <div><span class="badge text-bg-warning text-dark rounded-pill px-3 py-2 mb-2">Gestão teórica</span><h1 class="h3 fw-bold text-cfc-dark mb-1">Minhas turmas teóricas</h1><p class="text-muted mb-0">Crie e edite somente as turmas sob sua responsabilidade.</p></div>
        <a class="btn btn-outline-primary rounded-pill" href="<?= e(url('professional/panel',$base)) ?>"><i class="bi bi-arrow-left"></i> Voltar ao painel</a>
    </div>
    <div class="card border-0 shadow-soft rounded-4 mb-4"><div class="card-body p-4">
        <div class="d-flex justify-content-between align-items-center gap-2 mb-3"><h2 class="h5 fw-bold mb-0"><?= $editing?'Editar turma':'Criar nova turma' ?></h2><?php if($editing): ?><a class="btn btn-sm btn-outline-secondary rounded-pill" href="<?= e(url('professional/theory-groups',$base)) ?>">Cancelar</a><?php endif; ?></div>
        <form method="post" action="<?= e(url('professional/theory-groups/save')) ?>" enctype="multipart/form-data" class="row g-3" data-image-compression="auto">
            <?= CSRF::input() ?><input type="hidden" name="token" value="<?= e($token) ?>"><?php if($editing): ?><input type="hidden" name="group_id" value="<?= (int)$editGroup['id'] ?>"><?php endif; ?>
            <div class="col-md-6"><label class="form-label">Nome da turma</label><input name="name" class="form-control" value="<?= e($editGroup['name']??'') ?>" required></div>
            <div class="col-md-6"><label class="form-label">Imagem da turma (opcional)</label><input type="file" name="group_image" class="form-control" accept="image/jpeg,image/png,image/webp"></div>
            <div class="col-12"><label class="form-label">Descrição</label><textarea name="description" rows="2" class="form-control"><?= e($editGroup['description']??'') ?></textarea></div>
            <div class="col-12"><label class="form-label">Alunos da turma</label><div class="student-check-grid"><?php foreach($students as $student): ?><label class="student-check"><input type="checkbox" name="student_ids[]" value="<?= (int)$student['id'] ?>" <?= isset($selected[(int)$student['id']])?'checked':'' ?>> <span><?= e(short_person_name($student['name'])) ?></span></label><?php endforeach; ?></div></div>
            <div class="col-12"><button class="btn btn-primary rounded-pill px-4"><i class="bi bi-save"></i> <?= $editing?'Atualizar turma':'Criar turma' ?></button></div>
        </form>
    </div></div>
    <div class="row g-4">
        <?php foreach($groups as $group): $members=TheoryGroup::members((int)$group['id']); ?>
            <div class="col-lg-6"><div class="card border-0 shadow-soft rounded-4 h-100 overflow-hidden">
                <?php if(!empty($group['image_path'])): ?><img src="<?= e(asset($group['image_path'])) ?>" class="group-admin-image" alt="Imagem da turma"><?php endif; ?>
                <div class="card-body p-4"><div class="d-flex justify-content-between gap-2"><h2 class="h5 fw-bold"><?= e($group['name']) ?></h2><span class="badge <?= !empty($group['is_active'])?'text-bg-success':'text-bg-secondary' ?>"><?= !empty($group['is_active'])?'Ativa':'Inativa' ?></span></div><p class="text-muted"><?= e($group['description']??'') ?></p><small class="d-block mb-3">Código <?= e($group['group_code']) ?> · <?= count($members) ?> aluno(s)</small><div class="d-flex flex-wrap gap-2"><a class="btn btn-sm btn-primary rounded-pill" href="<?= e(url('portal/messages/group',array_merge($base,['id'=>(int)$group['id']]))) ?>">Mensagens</a><a class="btn btn-sm btn-outline-secondary rounded-pill" href="<?= e(url('professional/theory-groups',array_merge($base,['edit'=>(int)$group['id']]))) ?>">Editar</a></div></div>
            </div></div>
        <?php endforeach; ?>
        <?php if(empty($groups)): ?><div class="col-12"><div class="alert alert-info rounded-4">Você ainda não criou nenhuma turma.</div></div><?php endif; ?>
    </div>
</main>
