<?php $__env->startSection('title', 'Dettaglio Gruppo'); ?>
<?php $__env->startSection('page_title', 'Dettaglio Gruppo'); ?>

<?php
$canWriteGruppi = Auth::user()->canManage('gruppi');
$canDeleteGruppi = Auth::user()->canDelete('gruppi');
?>

<?php $__env->startSection('breadcrumbs'); ?>
<li class="breadcrumb-item"><a href="<?php echo e(route('gruppi.index')); ?>">Gruppi</a></li>
<li class="breadcrumb-item active"><?php echo e($gruppo->nome); ?></li>
<?php $__env->stopSection(); ?>

<?php if(session('success')): ?>
<div class="alert alert-success alert-dismissible">
    <button type="button" class="close" data-dismiss="alert">&times;</button>
    <?php echo e(session('success')); ?>

</div>
<?php endif; ?>
<?php if(session('error')): ?>
<div class="alert alert-danger alert-dismissible">
    <button type="button" class="close" data-dismiss="alert">&times;</button>
    <?php echo e(session('error')); ?>

</div>
<?php endif; ?>

<?php $__env->startSection('content'); ?>
<div class="row">
    <div class="col-md-4">
        <div class="card card-success">
            <div class="card-header">
                <h3 class="card-title">
                    <i class="fas fa-folder text-warning mr-2"></i>
                    <?php echo e($gruppo->nome); ?>

                </h3>
            </div>
            <div class="card-body">
                <table class="table table-sm table-borderless">
                    <tr>
                        <td style="width: 130px;"><strong>Path:</strong></td>
                        <td class="text-muted small"><?php echo e($gruppo->full_path); ?></td>
                    </tr>
                    <tr>
                        <td><strong>Gruppo Padre:</strong></td>
                        <td>
                            <?php if($gruppo->parent): ?>
                                <a href="<?php echo e(route('gruppi.show', $gruppo->parent->id)); ?>"><?php echo e($gruppo->parent->nome); ?></a>
                            <?php else: ?>
                                <span class="badge badge-success">Gruppo radice</span>
                            <?php endif; ?>
                        </td>
                    </tr>
                    <tr>
                        <td><strong>Diocesi:</strong></td>
                        <td><?php echo e($gruppo->diocesi?->nome ?? '-'); ?></td>
                    </tr>
                    <tr>
                        <td><strong>Responsabili:</strong></td>
                        <td>
                            <?php $responsabili = $gruppo->getResponsabili() ?>
                            <?php if($responsabili->count() > 0): ?>
                                <?php $__currentLoopData = $responsabili; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $resp): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                    <a href="<?php echo e(route('individui.show', $resp->id)); ?>">
                                        <i class="fas fa-user text-info mr-1"></i>
                                        <?php echo e($resp->nome_completo); ?>

                                    </a><br>
                                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                            <?php else: ?>
                                <span class="text-muted">-</span>
                            <?php endif; ?>
                        </td>
                    </tr>
                </table>
            </div>
        </div>
    </div>
    <div class="col-md-4">
        <div class="card">
            <div class="card-header"><h3 class="card-title"><i class="fas fa-map-marker-alt mr-2"></i>Luogo Incontro</h3></div>
            <div class="card-body">
                <table class="table table-sm table-borderless mb-0">
                    <tr>
                        <td style="width: 100px;"><strong>Indirizzo:</strong></td>
                        <td><?php echo e($gruppo->indirizzo_incontro ?: '-'); ?></td>
                    </tr>
                    <tr>
                        <td><strong>CAP:</strong></td>
                        <td><?php echo e($gruppo->cap_incontro ?: '-'); ?></td>
                    </tr>
                    <tr>
                        <td><strong>Città:</strong></td>
                        <td><?php echo e($gruppo->città_incontro ?: '-'); ?> <?php echo e($gruppo->sigla_provincia_incontro ? "({$gruppo->sigla_provincia_incontro})" : ''); ?></td>
                    </tr>
                </table>
                <?php if($gruppo->mappa_posizione): ?>
                    <a href="<?php echo e($gruppo->mappa_posizione); ?>" target="_blank" class="btn btn-sm btn-outline-primary mt-2">
                        <i class="fas fa-external-link-alt mr-1"></i> Visualizza su mappa
                    </a>
                <?php endif; ?>
            </div>
        </div>
    </div>
    <div class="col-md-4">
        <div class="card">
            <div class="card-header"><h3 class="card-title"><i class="fas fa-chart-pie mr-2"></i>Statistiche</h3></div>
            <div class="card-body">
                <div class="row text-center">
                    <div class="col-6">
                        <div class="border-right">
                            <h2 class="mb-0 text-primary"><?php echo e($gruppo->individui->count()); ?></h2>
                            <small class="text-muted">Membri</small>
                        </div>
                    </div>
                    <div class="col-6">
                        <h2 class="mb-0 text-warning"><?php echo e($gruppo->children->count()); ?></h2>
                        <small class="text-muted">Sottogruppi</small>
                    </div>
</div>
                </div>
            </div>
        </div>
    </div>
    <div class="col-md-4">
        <div class="card">
            <div class="card-header">
                <h3 class="card-title"><i class="fas fa-camera mr-2"></i>Avatar</h3>
            </div>
            <div class="card-body text-center">
                <?php if($gruppo->avatar_url): ?>
                    <img src="<?php echo e($gruppo->avatar_url); ?>"
                         alt="<?php echo e($gruppo->nome); ?>"
                         class="img-thumbnail mb-2"
                         style="max-width: 120px; max-height: 120px; object-fit: cover; border-radius: 50%;">
                    <br>
                    <form id="remove-avatar-form" class="d-inline">
                        <button type="button" class="btn btn-danger btn-sm" onclick="removeAvatar()">
                            <i class="fas fa-trash mr-1"></i>Rimuovi
                        </button>
                    </form>
                <?php else: ?>
                    <div class="bg-secondary d-flex align-items-center justify-content-center mb-3"
                         style="width: 120px; height: 120px; margin: 0 auto; border-radius: 50%;">
                        <i class="fas fa-folder fa-4x text-white"></i>
                    </div>
                <?php endif; ?>
                <form id="avatar-upload-form" enctype="multipart/form-data">
                    <?php echo csrf_field(); ?>
                    <div class="custom-file mb-2">
                        <input type="file" class="custom-file-input" id="avatar-input" name="avatar" accept="image/jpeg,image/png,image/gif,image/webp">
                        <label class="custom-file-label" for="avatar-input">Scegli immagine...</label>
                    </div>
                    <button type="button" class="btn btn-primary btn-sm" onclick="uploadAvatar()">
                        <i class="fas fa-upload mr-1"></i>Carica
                    </button>
                </form>
                <div id="avatar-message" class="mt-2"></div>
            </div>
        </div>
    </div>

    <?php
    $incontroEvento = $gruppo->eventi->where('is_incontro_gruppo', true)->first();
    ?>
    <?php if($incontroEvento): ?>
    <div class="row">
        <div class="col-md-4">
            <div class="card card-success">
                <div class="card-header">
                    <h3 class="card-title">
                        <i class="fas fa-calendar-check mr-2"></i>
                        Giorno di Incontro
                    </h3>
                </div>
                <div class="card-body">
                    <table class="table table-sm table-borderless mb-0">
                        <tr>
                            <td style="width: 100px;"><strong>Evento:</strong></td>
                            <td><?php echo e($incontroEvento->nome_evento); ?></td>
                        </tr>
                        <tr>
                            <td><strong>Quando:</strong></td>
                            <td>
                                <?php if($incontroEvento->tipo_recorrenza === 'settimanale'): ?>
                                    <?php echo e($incontroEvento->giorno_settimana_label); ?>

                                    <span class="badge badge-info ml-1">Settimanale</span>
                                <?php elseif($incontroEvento->tipo_recorrenza === 'mensile'): ?>
                                    <?php echo e($incontroEvento->occorrenza_mensile_label); ?>

                                    <?php if($incontroEvento->mesi_recorrenza): ?>
                                        <small class="text-muted">(<?php echo e($incontroEvento->mesi_recorrenza_label); ?>)</small>
                                    <?php endif; ?>
                                    <span class="badge badge-info ml-1">Mensile</span>
                                <?php elseif($incontroEvento->tipo_recorrenza === 'annuale'): ?>
                                    <?php echo e($incontroEvento->mese_annuale_label); ?>

                                    <?php if($incontroEvento->giorno_mese): ?>
                                        , giorno <?php echo e($incontroEvento->giorno_mese); ?>

                                    <?php endif; ?>
                                    <span class="badge badge-info ml-1">Annuale</span>
                                <?php elseif($incontroEvento->tipo_recorrenza === 'altro'): ?>
                                    <?php echo e($incontroEvento->giorno_settimana_label); ?>

                                    <span class="badge badge-info ml-1">Altro</span>
                                <?php else: ?>
                                    <?php echo e($incontroEvento->data_specifica?->format('d/m/Y') ?: '-'); ?>

                                <?php endif; ?>
                            </td>
                        </tr>
                        <?php if($incontroEvento->ora_inizio): ?>
                        <tr>
                            <td><strong>Ora:</strong></td>
                            <td>ore <?php echo e($incontroEvento->ora_inizio->format('H:i')); ?></td>
                        </tr>
                        <?php endif; ?>
                    </table>
                    <?php if($incontroEvento->responsabili->count() > 0): ?>
                    <hr>
                    <small class="text-muted"><strong>Responsabili:</strong></small>
                    <?php $__currentLoopData = $incontroEvento->responsabili; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $resp): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                        <div class="mt-1">
                            <i class="fas fa-user text-info mr-1"></i>
                            <a href="/individui/<?php echo e($resp->id); ?>"><?php echo e($resp->cognome); ?> <?php echo e($resp->nome); ?></a>
                            <?php if($resp->telefono_primario): ?>
                                <br><small class="text-success ml-3"><?php echo e($resp->telefono_primario); ?></small>
                            <?php endif; ?>
                        </div>
                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                    <?php endif; ?>
                    <div class="mt-2">
                        <a href="/eventi/<?php echo e($incontroEvento->id); ?>" class="btn btn-xs btn-outline-primary">
                            <i class="fas fa-eye mr-1"></i> Vedi evento
                        </a>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <?php endif; ?>

    <?php if($gruppo->descrizione): ?>
<div class="row mt-3">
    <div class="col-md-8">
        <div class="card">
            <div class="card-header"><h3 class="card-title"><i class="fas fa-align-left mr-2"></i>Descrizione</h3></div>
            <div class="card-body">
                <p class="mb-0"><?php echo nl2br(e($gruppo->descrizione)); ?></p>
            </div>
        </div>
    </div>
</div>
<?php endif; ?>

<div class="card mt-3">
    <div class="card-header">
        <h3 class="card-title">
            <i class="fas fa-users mr-2"></i>Membri (<?php echo e($gruppo->individui->count()); ?>)
        </h3>
    </div>
    <div class="card-body p-0">
        <?php if($gruppo->individui->count() > 0): ?>
            <table class="table table-bordered table-hover table-striped mb-0">
                <thead class="thead-light">
                    <tr>
                        <th style="width: 100px;">Codice</th>
                        <th>Nome Completo</th>
                        <th>Email</th>
                        <th>Telefono</th>
                        <th style="width: 150px;">Ruolo</th>
                        <th style="width: 110px;">Data Adesione</th>
                        <th style="width: 80px;">Azioni</th>
                    </tr>
                </thead>
                <tbody>
                    <?php $__currentLoopData = $gruppo->individui; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $individuo): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                    <tr>
                        <td><span class="badge badge-secondary"><?php echo e($individuo->codice_id); ?></span></td>
                        <td>
                            <a href="<?php echo e(route('individui.show', $individuo->id)); ?>">
                                <i class="fas fa-user text-info mr-1"></i>
                                <?php echo e($individuo->cognome); ?> <?php echo e($individuo->nome); ?>

                            </a>
                        </td>
                        <td><?php echo e($individuo->email_primaria ?? '-'); ?></td>
                        <td><?php echo e($individuo->telefono_primario ?? '-'); ?></td>
                        <td>
                            <?php
                                $ruoloIds = $individuo->pivot->ruolo_ids ? json_decode($individuo->pivot->ruolo_ids, true) : [];
                                $ruoli = \App\Models\Ruolo::findByIds($ruoloIds);
                            ?>
                            <?php if($ruoli->count() > 0): ?>
                                <?php $__currentLoopData = $ruoli; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $ruolo): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                    <span class="badge badge-info mr-1"><?php echo e($ruolo->nome); ?></span>
                                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                            <?php else: ?>
                                <span class="text-muted">-</span>
                            <?php endif; ?>
                        </td>
                        <td>
                            <?php if($individuo->pivot->data_adesione): ?>
                                <?php echo e(\Carbon\Carbon::parse($individuo->pivot->data_adesione)->format('d/m/Y')); ?>

                            <?php else: ?>
                                <span class="text-muted">-</span>
                            <?php endif; ?>
                        </td>
                        <td>
                            <a href="<?php echo e(route('individui.show', $individuo->id)); ?>" class="btn btn-xs btn-info" title="Visualizza">
                                <i class="fas fa-eye"></i>
                            </a>
                        </td>
                    </tr>
                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                </tbody>
            </table>
        <?php else: ?>
            <div class="text-center text-muted py-4">
                <i class="fas fa-users fa-2x mb-2"></i>
                <p class="mb-0">Nessun membro</p>
            </div>
        <?php endif; ?>
    </div>
</div>

<div class="card mt-3">
    <div class="card-header">
        <h3 class="card-title">
            <i class="fas fa-file mr-2"></i>Documenti (<?php echo e($gruppo->documenti->count()); ?>)
        </h3>
        <?php if($canWriteGruppi): ?>
        <button type="button" class="btn btn-xs btn-primary float-right" data-toggle="modal" data-target="#uploadDocumentoModal">
            <i class="fas fa-upload mr-1"></i> Carica
        </button>
        <?php endif; ?>
    </div>
    <div class="card-body p-0">
        <?php if($gruppo->documenti->count() > 0): ?>
            <table class="table table-bordered table-hover table-striped mb-0">
                <thead class="thead-light">
                    <tr>
                        <th>Nome</th>
                        <th style="width: 120px;">Tipologia</th>
                        <th style="width: 80px;">Dimensione</th>
                        <th style="width: 130px;">Data Upload</th>
                        <th style="width: 100px;">Azioni</th>
                    </tr>
                </thead>
                <tbody>
                    <?php $__currentLoopData = $gruppo->documenti; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $documento): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                    <tr>
                        <td>
                            <a href="#" onclick="event.preventDefault(); previewDocumento(<?php echo e($documento->id); ?>, '<?php echo e($documento->mime_type); ?>');">
                                <i class="fas fa-file text-secondary mr-1"></i>
                                <?php echo e($documento->nome_file); ?>

                            </a>
                        </td>
                        <td><?php echo e(ucfirst(str_replace('_', ' ', $documento->tipologia))); ?></td>
                        <td><?php echo e(number_format($documento->dimensione / 1024, 1)); ?> KB</td>
                        <td><?php echo e($documento->created_at->format('d/m/Y')); ?></td>
                        <td>
                            <?php if($documento->file_path): ?>
                                <a href="<?php echo e(url('/documenti/' . $documento->id . '/download')); ?>" class="btn btn-xs btn-primary" title="Scarica">
                                    <i class="fas fa-download"></i>
                                </a>
                            <?php endif; ?>
                        </td>
                    </tr>
                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                </tbody>
            </table>
        <?php else: ?>
            <div class="text-center text-muted py-4">
                <i class="fas fa-file fa-2x mb-2"></i>
                <p class="mb-0">Nessun documento</p>
            </div>
        <?php endif; ?>
    </div>
</div>

<div class="card mt-3">
    <div class="card-header">
        <h3 class="card-title">
            <i class="fas fa-sitemap mr-2"></i>Sottogruppi (<?php echo e($gruppo->children->count()); ?>)
        </h3>
        <a href="<?php echo e(route('gruppi.create', ['parent_id' => $gruppo->id])); ?>" class="btn btn-xs btn-success float-right">
            <i class="fas fa-plus mr-1"></i> Aggiungi Sottogruppo
        </a>
    </div>
    <div class="card-body p-0">
        <?php if($gruppo->children->count() > 0): ?>
            <table class="table table-bordered table-hover table-striped mb-0">
                <thead class="thead-light">
                    <tr>
                        <th>Nome</th>
                        <th>Diocesi</th>
                        <th>Responsabile</th>
                        <th style="text-align: center;">Membri</th>
                        <th style="width: 100px;">Azioni</th>
                    </tr>
                </thead>
                <tbody>
                    <?php $__currentLoopData = $gruppo->children->sortBy('nome'); $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $child): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                    <tr>
                        <td>
                            <a href="<?php echo e(route('gruppi.show', $child->id)); ?>">
                                <i class="fas fa-folder-open text-warning mr-1"></i>
                                <?php echo e($child->nome); ?>

                            </a>
                        </td>
                        <td><?php echo e($child->diocesi?->nome ?? '-'); ?></td>
                        <td><?php echo e($child->getResponsabili()->isNotEmpty() ? $child->getResponsabili()->first()->nome_completo : '-'); ?></td>
                        <td class="text-center">
                            <?php if($child->individui()->count() > 0): ?>
                                <span class="badge badge-info"><?php echo e($child->individui()->count()); ?></span>
                            <?php else: ?>
                                <span class="text-muted">-</span>
                            <?php endif; ?>
                        </td>
                        <td>
                            <a href="<?php echo e(route('gruppi.show', $child->id)); ?>" class="btn btn-xs btn-info" title="Visualizza">
                                <i class="fas fa-eye"></i>
                            </a>
                            <?php if($canWriteGruppi): ?>
                            <a href="<?php echo e(route('gruppi.edit', $child->id)); ?>" class="btn btn-xs btn-warning" title="Modifica">
                                <i class="fas fa-edit"></i>
                            </a>
                            <?php endif; ?>
                            <?php if($canDeleteGruppi): ?>
                            <form action="<?php echo e(route('gruppi.destroy', $child->id)); ?>" method="POST" class="d-inline">
                                <?php echo csrf_field(); ?> <?php echo method_field('DELETE'); ?>
                                <button type="submit" class="btn btn-xs btn-danger" onclick="return confirm('Eliminare <?php echo e($child->nome); ?>?')" title="Elimina">
                                    <i class="fas fa-trash"></i>
                                </button>
                            </form>
                            <?php endif; ?>
                        </td>
                    </tr>
                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                </tbody>
            </table>
        <?php else: ?>
            <div class="text-center text-muted py-4">
                <i class="fas fa-folder-open fa-2x mb-2"></i>
                <p class="mb-0">Nessun sottogruppo</p>
            </div>
        <?php endif; ?>
    </div>
</div>

<div class="mt-3">
    <?php if($canWriteGruppi): ?>
    <a href="<?php echo e(url('/gruppi/' . $gruppo->id . '/edit')); ?>" class="btn btn-warning">
        <i class="fas fa-edit mr-1"></i> Modifica
    </a>
    <?php endif; ?>
    <?php if($canDeleteGruppi): ?>
    <form action="<?php echo e(url('/gruppi/' . $gruppo->id)); ?>" method="POST" class="d-inline">
        <?php echo csrf_field(); ?> <?php echo method_field('DELETE'); ?>
        <button type="submit" class="btn btn-danger" onclick="return confirm('Confermi l\'eliminazione di <?php echo e($gruppo->nome); ?>? Verranno eliminati anche tutti i sottogruppi.')">
            <i class="fas fa-trash mr-1"></i> Elimina
        </button>
    </form>
    <?php endif; ?>
    <a href="<?php echo e(route('gruppi.index')); ?>" class="btn btn-secondary">
        <i class="fas fa-arrow-left mr-1"></i> Torna all'elenco
    </a>
</div>

<div class="modal fade" id="uploadDocumentoModal" tabindex="-1" role="dialog">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header bg-primary text-white">
                <h5 class="modal-title"><i class="fas fa-upload mr-2"></i>Carica Documento</h5>
                <button type="button" class="close text-white" data-dismiss="modal">
                    <span>&times;</span>
                </button>
            </div>
            <form action="/documenti" method="POST" enctype="multipart/form-data">
                <?php echo csrf_field(); ?>
                <input type="hidden" name="visibilita" value="gruppo">
                <input type="hidden" name="visibilita_target_id" value="<?php echo e($gruppo->id); ?>">
                <input type="hidden" name="visibilita_target_type" value="App\Models\Gruppo">
                <input type="hidden" name="_redirect" value="<?php echo e(url()->current()); ?>">
                <div class="modal-body">
                    <div class="form-group">
                        <label>Nome *</label>
                        <input type="text" name="nome_file" class="form-control" placeholder="Nome del documento" required>
                    </div>
                    <div class="form-group">
                        <label>Tipologia *</label>
                        <select name="tipologia" class="form-control" required>
                            <option value="">Seleziona...</option>
                            <option value="documento">Documento</option>
                            <option value="statuto">Statuto</option>
                            <option value="avatar">Avatar</option>
                            <option value="galleria">Galleria</option>
                            <option value="altro">Altro</option>
                        </select>
                    </div>
                    <div class="form-group">
                        <label>File *</label>
                        <input type="file" name="file" class="form-control" required>
                        <small class="text-muted">Max 10MB</small>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-dismiss="modal">Annulla</button>
                    <button type="submit" class="btn btn-primary">
                        <i class="fas fa-upload mr-1"></i> Carica
                    </button>
                </div>
            </form>
        </div>
    </div>
</div>

<div class="modal fade" id="previewModal" tabindex="-1" role="dialog">
    <div class="modal-dialog modal-lg" role="document" style="max-width: 90vw;">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title"><i class="fas fa-file mr-2"></i><span id="previewModalTitle">Anteprima</span></h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Chiudi">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <div class="modal-body text-center p-0" style="min-height: 400px;">
                <iframe id="previewFrame" src="" style="width: 100%; height: 70vh; border: none;"></iframe>
            </div>
            <div class="modal-footer">
                <a id="previewDownloadBtn" href="#" class="btn btn-primary" download>
                    <i class="fas fa-download mr-1"></i> Scarica
                </a>
                <button type="button" class="btn btn-secondary" data-dismiss="modal">Chiudi</button>
            </div>
        </div>
    </div>
</div>
<?php $__env->stopSection(); ?>

<?php $__env->startSection('scripts'); ?>
<script>
function previewDocumento(id, mimeType) {
    var previewUrl = '/documenti/' + id + '/preview';
    var downloadUrl = '/documenti/' + id + '/download';

    document.getElementById('previewFrame').src = previewUrl;
    document.getElementById('previewDownloadBtn').href = downloadUrl;
    document.getElementById('previewModalTitle').textContent = 'Anteprima';

    document.getElementById('previewDownloadBtn').style.display = 'inline-block';

    $('#previewModal').modal('show');
}

document.getElementById('avatar-input').addEventListener('change', function(e) {
    if (e.target.files.length > 0) {
        var label = e.target.files[0].name;
        var next = e.target.nextElementSibling;
        if (next && next.classList.contains('custom-file-label')) {
            next.textContent = label;
        }
    }
});

function uploadAvatar() {
    var input = document.getElementById('avatar-input');
    if (!input.files.length) {
        document.getElementById('avatar-message').innerHTML = '<span class="text-danger">Seleziona un\'immagine</span>';
        return;
    }

    var formData = new FormData();
    formData.append('avatar', input.files[0]);
    formData.append('_token', '<?php echo e(csrf_token()); ?>');

    document.getElementById('avatar-message').innerHTML = '<span class="text-info"><i class="fas fa-spinner fa-spin"></i> Caricamento...</span>';

    fetch('<?php echo e(url('/gruppi/' . $gruppo->id . '/avatar')); ?>', {
        method: 'POST',
        body: formData,
        headers: { 'X-Requested-With': 'XMLHttpRequest' },
    })
    .then(response => response.json())
    .then(data => {
        if (data.success) {
            document.getElementById('avatar-message').innerHTML = '<span class="text-success">Avatar caricato!</span>';
            setTimeout(function() { window.location.reload(); }, 1000);
        } else {
            document.getElementById('avatar-message').innerHTML = '<span class="text-danger">' + (data.message || 'Errore') + '</span>';
        }
    })
    .catch(error => {
        document.getElementById('avatar-message').innerHTML = '<span class="text-danger">Errore: ' + error + '</span>';
    });
}

function removeAvatar() {
    if (!confirm('Rimuovere l\'avatar?')) return;

    fetch('<?php echo e(url('/gruppi/' . $gruppo->id . '/avatar')); ?>', {
        method: 'DELETE',
        headers: {
            'X-CSRF-TOKEN': '<?php echo e(csrf_token()); ?>',
            'X-Requested-With': 'XMLHttpRequest',
            'Accept': 'application/json',
        },
    })
    .then(response => response.json())
    .then(data => {
        if (data.success) {
            document.getElementById('avatar-message').innerHTML = '<span class="text-success">Avatar rimosso!</span>';
            setTimeout(function() { window.location.reload(); }, 1000);
        } else {
            document.getElementById('avatar-message').innerHTML = '<span class="text-danger">' + (data.message || 'Errore') + '</span>';
        }
    })
    .catch(error => {
        document.getElementById('avatar-message').innerHTML = '<span class="text-danger">Errore: ' + error + '</span>';
    });
}
</script>
<?php $__env->stopSection(); ?>

<?php echo $__env->make('layouts.adminlte', array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?><?php /**PATH /var/www/html/glastree/resources/views/gruppi/show.blade.php ENDPATH**/ ?>