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

<?php 
$canWriteEventi = Auth::user()->canManage('eventi');
$canDeleteEventi = Auth::user()->canDelete('eventi');
?>

<?php $__env->startSection('content'); ?>

<?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; ?>

<div class="card">
    <div class="card-header">
        <h3 class="card-title">Tutti gli Eventi</h3>
        <div class="card-tools">
            <a href="/eventi/calendar" class="btn btn-sm btn-outline-primary mr-2">
                <i class="fas fa-calendar-alt mr-1"></i> Calendario
            </a>
            <button type="button" class="btn btn-sm btn-success mr-2" onclick="exportSelectedIcs()">
                <i class="fas fa-calendar-plus mr-1"></i> Esporta ICS
            </button>
            <?php if($canDeleteEventi): ?>
            <div class="btn-group mr-2">
                <button type="button" class="btn btn-sm btn-danger" onclick="deleteSelected()">
                    <i class="fas fa-trash mr-1"></i> Elimina Selezionati
                </button>
            </div>
            <?php endif; ?>
            <?php if($canWriteEventi): ?>
            <a href="/eventi/create" class="btn btn-xs btn-success">
                <i class="fas fa-plus mr-1"></i> Nuovo Evento
            </a>
            <?php endif; ?>
        </div>
    </div>
    <div class="card-body">
        <form method="GET" class="mb-3">
            <div class="row">
                <div class="col-md-3">
                    <input type="text" name="search" class="form-control" placeholder="Cerca evento..." value="<?php echo e(request('search')); ?>">
                </div>
                <div class="col-md-3">
                    <select name="gruppo_id" class="form-control" onchange="this.form.submit()">
                        <option value="">Tutti i gruppi</option>
                        <?php $__currentLoopData = $gruppi; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $g): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                            <option value="<?php echo e($g->id); ?>" <?php echo e(request('gruppo_id') == $g->id ? 'selected' : ''); ?>><?php echo e($g->full_path); ?></option>
                        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                    </select>
                </div>
                <div class="col-md-2">
                    <select name="tipo" class="form-control" onchange="this.form.submit()">
                        <option value="">Tutte le ricorrenze</option>
                        <option value="singolo" <?php echo e(request('tipo') == 'singolo' ? 'selected' : ''); ?>>Singolo</option>
                        <option value="settimanale" <?php echo e(request('tipo') == 'settimanale' ? 'selected' : ''); ?>>Settimanale</option>
                        <option value="mensile" <?php echo e(request('tipo') == 'mensile' ? 'selected' : ''); ?>>Mensile</option>
                        <option value="annuale" <?php echo e(request('tipo') == 'annuale' ? 'selected' : ''); ?>>Annuale</option>
                    </select>
                </div>
                <div class="col-md-2">
                    <button type="submit" class="btn btn-primary"><i class="fas fa-search mr-1"></i> Cerca</button>
                    <a href="/eventi" class="btn btn-secondary"><i class="fas fa-times"></i></a>
                </div>
            </div>
        </form>

        <table class="table table-bordered table-hover">
            <thead class="thead-light">
                <tr>
                    <th style="width: 40px;">
                        <?php if($canDeleteEventi): ?>
                        <div class="custom-control custom-checkbox">
                            <input type="checkbox" class="custom-control-input" id="select-all" onchange="toggleSelectAll(this)">
                            <label class="custom-control-label" for="select-all"></label>
                        </div>
                        <?php endif; ?>
                    </th>
                    <th>
                        <a href="<?php echo e(request()->fullUrlWithQuery(['sort' => 'nome_evento', 'direction' => request('sort') === 'nome_evento' && request('direction') === 'asc' ? 'desc' : 'asc'])); ?>" class="text-dark">
                            Nome Evento
                            <?php if(request('sort') === 'nome_evento'): ?>
                                <i class="fas fa-sort-<?php echo e(request('direction') === 'asc' ? 'up' : 'down'); ?> ml-1"></i>
                            <?php else: ?>
                                <i class="fas fa-sort ml-1 text-muted"></i>
                            <?php endif; ?>
                        </a>
                    </th>
                    <th>
                        <a href="<?php echo e(request()->fullUrlWithQuery(['sort' => 'descrizione_evento', 'direction' => request('sort') === 'descrizione_evento' && request('direction') === 'asc' ? 'desc' : 'asc'])); ?>" class="text-dark">
                            Descrizione
                            <?php if(request('sort') === 'descrizione_evento'): ?>
                                <i class="fas fa-sort-<?php echo e(request('direction') === 'asc' ? 'up' : 'down'); ?> ml-1"></i>
                            <?php else: ?>
                                <i class="fas fa-sort ml-1 text-muted"></i>
                            <?php endif; ?>
                        </a>
                    </th>
                    <th>
                        <a href="<?php echo e(request()->fullUrlWithQuery(['sort' => 'tipo_evento', 'direction' => request('sort') === 'tipo_evento' && request('direction') === 'asc' ? 'desc' : 'asc'])); ?>" class="text-dark">
                            Tipologia
                            <?php if(request('sort') === 'tipo_evento'): ?>
                                <i class="fas fa-sort-<?php echo e(request('direction') === 'asc' ? 'up' : 'down'); ?> ml-1"></i>
                            <?php else: ?>
                                <i class="fas fa-sort ml-1 text-muted"></i>
                            <?php endif; ?>
                        </a>
                    </th>
                    <th>
                        <a href="<?php echo e(request()->fullUrlWithQuery(['sort' => 'tipo_recorrenza', 'direction' => request('sort') === 'tipo_recorrenza' && request('direction') === 'asc' ? 'desc' : 'asc'])); ?>" class="text-dark">
                            Ricorrenza
                            <?php if(request('sort') === 'tipo_recorrenza'): ?>
                                <i class="fas fa-sort-<?php echo e(request('direction') === 'asc' ? 'up' : 'down'); ?> ml-1"></i>
                            <?php else: ?>
                                <i class="fas fa-sort ml-1 text-muted"></i>
                            <?php endif; ?>
                        </a>
                    </th>
                    <th>
                        <a href="<?php echo e(request()->fullUrlWithQuery(['sort' => 'gruppi', 'direction' => request('sort') === 'gruppi' && request('direction') === 'asc' ? 'desc' : 'asc'])); ?>" class="text-dark">
                            Gruppi
                            <?php if(request('sort') === 'gruppi'): ?>
                                <i class="fas fa-sort-<?php echo e(request('direction') === 'asc' ? 'up' : 'down'); ?> ml-1"></i>
                            <?php else: ?>
                                <i class="fas fa-sort ml-1 text-muted"></i>
                            <?php endif; ?>
                        </a>
                    </th>
                    <th>
                        <a href="<?php echo e(request()->fullUrlWithQuery(['sort' => 'responsabili', 'direction' => request('sort') === 'responsabili' && request('direction') === 'asc' ? 'desc' : 'asc'])); ?>" class="text-dark">
                            Responsabili
                            <?php if(request('sort') === 'responsabili'): ?>
                                <i class="fas fa-sort-<?php echo e(request('direction') === 'asc' ? 'up' : 'down'); ?> ml-1"></i>
                            <?php else: ?>
                                <i class="fas fa-sort ml-1 text-muted"></i>
                            <?php endif; ?>
                        </a>
                    </th>
                    <th style="width: 120px;">
                        <a href="<?php echo e(request()->fullUrlWithQuery(['sort' => 'created_at', 'direction' => request('sort') === 'created_at' && request('direction') === 'asc' ? 'desc' : 'asc'])); ?>" class="text-dark">
                            Creato
                            <?php if(request('sort') === 'created_at'): ?>
                                <i class="fas fa-sort-<?php echo e(request('direction') === 'asc' ? 'up' : 'down'); ?> ml-1"></i>
                            <?php else: ?>
                                <i class="fas fa-sort ml-1 text-muted"></i>
                            <?php endif; ?>
                        </a>
                    </th>
                    <th style="width: 120px;">Azioni</th>
                </tr>
            </thead>
            <tbody>
                <?php $__empty_1 = true; $__currentLoopData = $eventi; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $evento): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); $__empty_1 = false; ?>
                <tr data-id="<?php echo e($evento->id); ?>">
                    <td>
                        <?php if($canDeleteEventi): ?>
                        <div class="custom-control custom-checkbox">
                            <input type="checkbox" class="custom-control-input row-checkbox" id="row-checkbox-<?php echo e($evento->id); ?>" value="<?php echo e($evento->id); ?>">
                            <label class="custom-control-label" for="row-checkbox-<?php echo e($evento->id); ?>"></label>
                        </div>
                        <?php endif; ?>
                    </td>
                    <td>
                        <a href="/eventi/<?php echo e($evento->id); ?>">
                            <i class="fas fa-calendar mr-1 text-primary"></i>
                            <?php echo e($evento->nome_evento); ?>

                        </a>
                        <?php if($evento->is_incontro_gruppo): ?>
                            <span class="badge badge-success ml-1">Incluso</span>
                        <?php endif; ?>
                    </td>
                    <td><?php echo e(Str::limit($evento->descrizione_evento, 50) ?: '-'); ?></td>
                    <td>
                        <?php if($evento->tipo_evento): ?>
                            <?php
                                $tipologiaEvento = \App\Models\TipologiaEvento::where('nome', $evento->tipo_evento)->first();
                            ?>
                            <?php if($tipologiaEvento): ?>
                                <span class="badge badge-primary"><?php echo e($tipologiaEvento->descrizione ?: $tipologiaEvento->nome); ?></span>
                            <?php else: ?>
                                <span class="badge badge-secondary"><?php echo e($evento->tipo_evento); ?></span>
                            <?php endif; ?>
                        <?php else: ?>
                            <span class="text-muted">-</span>
                        <?php endif; ?>
                    </td>
                    <td>
                        <?php if($evento->tipo_recorrenza && $evento->tipo_recorrenza !== 'singolo'): ?>
                            <span class="badge badge-info"><?php echo e($evento->periodicita_label); ?></span>
                        <?php else: ?>
                            <span class="badge badge-secondary">Singolo</span>
                        <?php endif; ?>
                    </td>
                    <td>
                        <?php $__empty_2 = true; $__currentLoopData = $evento->gruppi->take(2); $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $gruppo): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); $__empty_2 = false; ?>
                            <a href="/gruppi/<?php echo e($gruppo->id); ?>">
                                <span class="badge badge-warning"><?php echo e($gruppo->nome); ?></span>
                            </a>
                        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); if ($__empty_2): ?>
                            <span class="text-muted">-</span>
                        <?php endif; ?>
                        <?php if($evento->gruppi->count() > 2): ?>
                            <small class="text-muted">+<?php echo e($evento->gruppi->count() - 2); ?></small>
                        <?php endif; ?>
                    </td>
                    <td>
                        <?php $__empty_2 = true; $__currentLoopData = $evento->responsabili->take(2); $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $resp): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); $__empty_2 = false; ?>
                            <small><?php echo e($resp->nome_completo); ?></small><br>
                        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); if ($__empty_2): ?>
                            <span class="text-muted">-</span>
                        <?php endif; ?>
                        <?php if($evento->responsabili->count() > 2): ?>
                            <small class="text-muted">+<?php echo e($evento->responsabili->count() - 2); ?> altri</small>
                        <?php endif; ?>
                    </td>
                    <td><small class="text-muted"><?php echo e($evento->created_at->format('d/m/Y')); ?></small></td>
                    <td>
                        <a href="<?php echo e(url('/eventi/' . $evento->id)); ?>" class="btn btn-xs btn-info" title="Visualizza">
                            <i class="fas fa-eye"></i>
                        </a>
                        <?php if($canWriteEventi): ?>
                        <a href="<?php echo e(url('/eventi/' . $evento->id . '/edit')); ?>" class="btn btn-xs btn-warning" title="Modifica">
                            <i class="fas fa-edit"></i>
                        </a>
                        <form action="<?php echo e(url('/eventi/' . $evento->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('Confermi l\'eliminazione?')" title="Elimina">
                                <i class="fas fa-trash"></i>
                            </button>
                        </form>
                        <?php endif; ?>
                    </td>
                </tr>
                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); if ($__empty_1): ?>
                <tr>
                    <td colspan="9" class="text-center text-muted py-4">
                        <i class="fas fa-calendar fa-2x mb-2"></i>
                        <p class="mb-0">Nessun evento trovato</p>
                    </td>
                </tr>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
    <div class="card-footer">
        <?php echo e($eventi->withQueryString()->links()); ?>

    </div>
</div>

<div class="modal fade" id="deleteModal" tabindex="-1" role="dialog">
    <?php if($canDeleteEventi): ?>
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header bg-danger">
                <h5 class="modal-title text-white"><i class="fas fa-trash-alt mr-2"></i>Conferma Eliminazione</h5>
                <button type="button" class="close text-white" data-dismiss="modal">
                    <span>&times;</span>
                </button>
            </div>
            <div class="modal-body">
                <p>Stai per eliminare <strong id="delete-count">0</strong> eventi selezionati.</p>
                <p class="text-muted small">Verranno eliminati anche i collegamenti con gruppi e responsabili.</p>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-dismiss="modal">Annulla</button>
                <button type="button" class="btn btn-danger" onclick="confirmDelete()">Elimina</button>
            </div>
        </div>
    </div>
    <?php endif; ?>
</div>
<?php $__env->stopSection(); ?>

<?php $__env->startSection('scripts'); ?>
<script>
function toggleSelectAll(source) {
    document.querySelectorAll('.row-checkbox').forEach(cb => cb.checked = source.checked);
}

function getSelectedIds() {
    return Array.from(document.querySelectorAll('.row-checkbox:checked')).map(cb => cb.value);
}

function deleteSelected() {
    const selectedIds = getSelectedIds();
    if (selectedIds.length === 0) {
        alert('Seleziona almeno un evento');
        return;
    }

    document.getElementById('delete-count').textContent = selectedIds.length;

    document.querySelector('#deleteModal .btn-danger').onclick = function() {
        const form = document.createElement('form');
        form.method = 'POST';
        form.action = '/eventi/mass-elimina';
        
        const csrfToken = document.querySelector('meta[name="csrf-token"]')?.content || '<?php echo e(csrf_token()); ?>';
        
        let html = `<input type="hidden" name="_token" value="${csrfToken}">`;
        selectedIds.forEach(function(id) {
            html += `<input type="hidden" name="ids[]" value="${id}">`;
        });
        
        form.innerHTML = html;
        document.body.appendChild(form);
        form.submit();
    };

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

function exportSelectedIcs() {
    const selectedIds = getSelectedIds();
    const form = document.createElement('form');
    form.method = 'POST';
    form.action = '/eventi/export-ics';
    
    const csrfToken = document.querySelector('meta[name="csrf-token"]')?.content || '<?php echo e(csrf_token()); ?>';
    
    let html = `<input type="hidden" name="_token" value="${csrfToken}">`;
    
    if (selectedIds.length > 0) {
        selectedIds.forEach(function(id) {
            html += `<input type="hidden" name="ids[]" value="${id}">`;
        });
    } else {
        const params = new URLSearchParams(window.location.search);
        params.forEach(function(value, key) {
            if (key === 'search') html += `<input type="hidden" name="search" value="${value}">`;
            if (key === 'gruppo_id') html += `<input type="hidden" name="gruppo_id" value="${value}">`;
            if (key === 'tipo') html += `<input type="hidden" name="tipo" value="${value}">`;
        });
    }
    
    form.innerHTML = html;
    document.body.appendChild(form);
    form.submit();
}
</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/eventi/index.blade.php ENDPATH**/ ?>