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

<?php 
$canWriteMailing = Auth::user()->canManage('mailing');
$canDeleteMailing = Auth::user()->canDelete('mailing');
?>

<?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"><i class="fas fa-list mr-2"></i>Elenco Mailing Lists</h3>
        <div class="card-tools">
            <?php if($canDeleteMailing): ?>
            <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($canWriteMailing): ?>
            <a href="<?php echo e(route('mailing-liste.create')); ?>" class="btn btn-success btn-sm">
                <i class="fas fa-plus mr-1"></i> Nuova Lista
            </a>
            <?php endif; ?>
        </div>
    </div>
    <div class="card-body p-0">
        <?php if($mailingLists->count() > 0): ?>
        <table class="table table-bordered table-hover table-striped mb-0">
            <thead class="thead-light">
                <tr>
                    <th style="width: 40px;">
                        <?php if($canDeleteMailing): ?>
                        <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>Nome</th>
                    <th>Descrizione</th>
                    <th style="width: 100px;">Contatti</th>
                    <th style="width: 100px;">Stato</th>
                    <th style="width: 130px;">Creata il</th>
                    <th style="width: 100px;">Azioni</th>
                </tr>
            </thead>
            <tbody>
                <?php $__currentLoopData = $mailingLists; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $lista): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                <tr data-id="<?php echo e($lista->id); ?>">
                    <td>
                        <?php if($canDeleteMailing): ?>
                        <div class="custom-control custom-checkbox">
                            <input type="checkbox" class="custom-control-input row-checkbox" id="row-checkbox-<?php echo e($lista->id); ?>" value="<?php echo e($lista->id); ?>">
                            <label class="custom-control-label" for="row-checkbox-<?php echo e($lista->id); ?>"></label>
                        </div>
                        <?php endif; ?>
                    </td>
                    <td>
                        <a href="<?php echo e(route('mailing-liste.show', $lista->id)); ?>"><?php echo e($lista->nome); ?></a>
                    </td>
                    <td><?php echo e($lista->descrizione ?: '-'); ?></td>
                    <td>
                        <span class="badge badge-info"><?php echo e($lista->contatti->count()); ?></span>
                    </td>
                    <td>
                        <?php if($lista->attiva): ?>
                        <span class="badge badge-success">Attiva</span>
                        <?php else: ?>
                        <span class="badge badge-secondary">Disattiva</span>
                        <?php endif; ?>
                    </td>
                    <td><?php echo e($lista->created_at->format('d/m/Y')); ?></td>
                    <td>
                        <a href="<?php echo e(route('mailing-liste.show', $lista->id)); ?>" class="btn btn-xs btn-info" title="Visualizza">
                            <i class="fas fa-eye"></i>
                        </a>
                        <?php if($canWriteMailing): ?>
                        <a href="<?php echo e(route('mailing-liste.edit', $lista->id)); ?>" class="btn btn-xs btn-warning" title="Modifica">
                            <i class="fas fa-edit"></i>
                        </a>
                        <form action="<?php echo e(route('mailing-liste.destroy', $lista->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 questa lista?')" 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-list fa-3x mb-3"></i>
            <p>Nessuna mailing list</p>
            <?php if($canWriteMailing): ?>
            <a href="<?php echo e(route('mailing-liste.create')); ?>" class="btn btn-success">Crea la prima lista</a>
            <?php endif; ?>
        </div>
        <?php endif; ?>
    </div>
</div>

<div class="modal fade" id="deleteModal" tabindex="-1" role="dialog">
    <?php if($canDeleteMailing): ?>
    <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> mailing list selezionate.</p>
                <p class="text-muted small">Verranno eliminati anche tutti i contatti associati.</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 una mailing list');
        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 = '/mailing-liste/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');
}
</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/mailing-liste/index.blade.php ENDPATH**/ ?>