<h2 class="page-title">Gestión de Grupos y Subgrupos</h2>
<p class="text-muted">Administre los tipos de grupos, grupos y subgrupos desde esta sección.</p>

<div class="d-flex justify-content-end mb-3">
    <a href="<?= BASE_HREF ?>areas" class="btn btn-secondary">Volver</a>
</div>

<div class="row">
    <div class="col-md-12">
        <div class="card shadow mb-4">
            <div class="card-header">
                <strong class="card-title">Añadir Tipo de Grupo Existente</strong>
            </div>
            <div class="card-body">
                <form action="<?= BASE_ACTION ?>grouptypes/assign" method="POST">
                    <input type="hidden" name="area_id" value="<?= $area->id ?>">
                    <div class="form-group">
                        <label for="group_type_id">Seleccionar Tipo de Grupo</label>
                        <select name="group_type_id" class="form-control" required>
                            <option value="" disabled selected>Seleccionar</option>
                            <?php foreach ($availableGroupTypes as $groupType) : ?>
                                <option value="<?= $groupType->id ?>"><?= $groupType->description ?></option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                    <button type="submit" class="btn btn-primary">Añadir</button>
                </form>
            </div>
        </div>
    </div>
</div>

<div class="row">
    <div class="col-md-12">
        <div class="card shadow mb-4">
            <div class="card-header">
                <strong class="card-title">Tipos de Grupo Asignados</strong>
            </div>
            <div class="card-body">
                <ul class="list-group">
                    <?php foreach ($area->groupTypes as $groupType) : ?>
                        <li class="list-group-item bg-light-blue">
                            <div class="d-flex justify-content-between align-items-center">
                                <span class="font-weight-bold text-primary"><i class="fas fa-layer-group"></i> <?= $groupType->description ?></span>
                                <form action="<?= BASE_ACTION ?>grouptypes/unassign" method="POST" onsubmit="return confirm('¿Eliminar este tipo de grupo?');">
                                    <input type="hidden" name="_method" value="DELETE">
                                    <input type="hidden" name="area_id" value="<?= $area->id ?>">
                                    <input type="hidden" name="group_type_id" value="<?= $groupType->id ?>">
                                    <button type="submit" class="btn btn-danger btn-sm">
                                        <i class="fas fa-trash-alt"></i> Eliminar
                                    </button>
                                </form>
                            </div>

                            <ul class="mt-3 list-group">
                                <?php foreach ($groupType->groups as $group) : ?>
                                    <li class="list-group-item bg-light">
                                        <div class="d-flex justify-content-between align-items-center">
                                            <span><i class="fas fa-folder"></i> <?= $group->description ?></span>
                                            <div class="btn-group">
                                                <a href="<?= BASE_HREF ?>groups/<?= $group->id ?>/edit" class="btn btn-secondary btn-sm">Editar Grupo</a>
                                                <form action="<?= BASE_ACTION ?>groups/<?= $group->id ?>/delete" method="POST" onsubmit="return confirm('¿Eliminar este grupo?');">
                                                    <input type="hidden" name="_method" value="DELETE">    
                                                    <input type="hidden" name="group_id" value="<?= $group->id ?>">
                                                    <button type="submit" class="btn btn-danger btn-sm">Eliminar Grupo</button>
                                                </form>
                                            </div>
                                        </div>

                                        <ul class="mt-3 list-group">
                                            <?php foreach ($group->subgroups as $subgroup) : ?>
                                                <li class="list-group-item border">
                                                    <div class="d-flex justify-content-between align-items-center">
                                                        <span><i class="fas fa-tag"></i> <?= $subgroup->description ?></span>
                                                        <div class="btn-group">
                                                            <a href="<?= BASE_HREF ?>subgroups/<?= $subgroup->id ?>/edit" class="btn btn-secondary btn-sm">Editar Subgrupo</a>
                                                            <form action="<?= BASE_ACTION ?>subgroups/<?= $subgroup->id ?>/delete" method="POST" onsubmit="return confirm('¿Eliminar este subgrupo?');">
                                                                <input type="hidden" name="_method" value="DELETE">
                                                                <input type="hidden" name="subgroup_id" value="<?= $subgroup->id ?>">
                                                                <button type="submit" class="btn btn-danger btn-sm">Eliminar Subgrupo</button>
                                                            </form>
                                                        </div>
                                                    </div>
                                                </li>
                                            <?php endforeach; ?>
                                        </ul>

                                        <form action="<?= BASE_ACTION ?>subgroups" method="POST" class="mt-3 d-flex gap-2">
                                            <input type="hidden" name="group_id" value="<?= $group->id ?>">
                                            <input type="text" name="subgroup_name" class="form-control w-50" placeholder="Nuevo Subgrupo" required>
                                            <input type="text" name="subgroup_abbreviation" class="form-control w-25" placeholder="Abreviatura" required>
                                            <button type="submit" class="btn btn-success btn-sm">Añadir Subgrupo</button>
                                        </form>
                                    </li>
                                <?php endforeach; ?>
                            </ul>

                            <form action="<?= BASE_ACTION ?>groups" method="POST" class="mt-3 d-flex gap-2">
                                <input type="hidden" name="area_id" value="<?= $area->id ?>">
                                <input type="hidden" name="group_type_id" value="<?= $groupType->id ?>">
                                <input type="text" name="group_name" class="form-control w-50" placeholder="Nuevo Grupo" required>
                                <input type="text" name="abbreviation" class="form-control w-25" placeholder="Abreviatura" required>
                                <button type="submit" class="btn btn-primary btn-sm">Añadir Grupo</button>
                            </form>
                        </li>
                    <?php endforeach; ?>
                </ul>
            </div>
        </div>
    </div>
</div>

<script>
    document.querySelectorAll('input[name="abbreviation"], input[name="group_name"], input[name="subgroup_name"], input[name="subgroup_abbreviation"]').forEach(input => {
        input.addEventListener('input', function (event) {
            event.target.value = event.target.value.toUpperCase();
        });
    });
</script>
