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

<?php $__env->startSection('content'); ?>
<div class="sinagem-main">
    <div class="s-card">
        <div class="s-card-header">
            <h4 class="s-card-title">
                <i class="fas fa-store"></i>
                Comerciantes
            </h4>
            <a href="<?php echo e(route('mercado.comerciantes.create')); ?>" class="btn btn-primary btn-sm">
                <i class="fas fa-plus-circle"></i>
                Novo Comerciante
            </a>
        </div>

        <div class="s-card-body">
            <!-- Filtros -->
            <form method="GET" class="row g-3 mb-4" id="filter-form">
                <div class="col-md-2">
                    <label for="market_id" class="form-label">Mercado</label>
                    <select name="market_id" id="market_id" class="form-control">
                        <option value="">Todos</option>
                        <?php $__currentLoopData = $markets ?? []; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $market): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                            <option value="<?php echo e($market->id); ?>"><?php echo e($market->name); ?></option>
                        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                    </select>
                </div>
                <div class="col-md-2">
                    <label for="business_type_id" class="form-label">Tipo de Negócio</label>
                    <select name="business_type_id" id="business_type_id" class="form-control">
                        <option value="">Todos</option>
                        <?php $__currentLoopData = $businessTypes ?? []; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $type): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                            <option value="<?php echo e($type->id); ?>"><?php echo e($type->name); ?></option>
                        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                    </select>
                </div>
                <div class="col-md-2">
                    <label for="province_id" class="form-label">Província</label>
                    <select name="province_id" id="province_id" class="form-control">
                        <option value="">Todas</option>
                        <?php $__currentLoopData = $provinces ?? []; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $province): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                            <option value="<?php echo e($province->id); ?>"><?php echo e($province->name); ?></option>
                        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                    </select>
                </div>
                <div class="col-md-2">
                    <label for="municipality_id" class="form-label">Município</label>
                    <select name="municipality_id" id="municipality_id" class="form-control">
                        <option value="">Todos</option>
                        <?php $__currentLoopData = $municipalities ?? []; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $municipality): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                            <option value="<?php echo e($municipality->id); ?>"><?php echo e($municipality->name); ?></option>
                        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                    </select>
                </div>
                <div class="col-md-2">
                    <label for="status" class="form-label">Status</label>
                    <select name="status" id="status" class="form-control">
                        <option value="">Todos</option>
                        <option value="pending">Pendente</option>
                        <option value="approved">Aprovado</option>
                        <option value="active">Ativo</option>
                        <option value="suspended">Bloqueado</option>
                    </select>
                </div>
                <div class="col-md-2 d-flex align-items-end">
                    <button type="submit" class="btn btn-primary w-100" id="apply-filters">
                        <i class="fas fa-search"></i> Filtrar
                    </button>
                    <button type="button" class="btn btn-outline-secondary w-100 ms-2" id="clear-filters">
                        <i class="fas fa-eraser"></i> Limpar
                    </button>
                </div>
            </form>

            <!-- DataTable Server-side -->
            <div class="sinagem-datatable-wrapper">
                <div class="table-responsive">
                    <table id="comerciantes-table" class="s-table">
                        <thead>
                            <tr>
                                <th>Nome</th>
                                <th>Identidade</th>
                                <th>Telefone</th>
                                <th>Status</th>
                                <th class="text-center">Ações</th>
                            </tr>
                        </thead>
                        <tbody>
                            <!-- DataTables preenche via AJAX -->
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>
<?php $__env->stopSection(); ?>

<?php $__env->startPush('scripts'); ?>
<script>
$(document).ready(function() {
    // Inicializar DataTable Server-side
    const table = $('#comerciantes-table').DataTable({
        processing: true,
        serverSide: true,
        pageLength: 10,
        ajax: {
            url: '<?php echo e(route("mercado.comerciantes.table-data")); ?>',
            type: 'GET',
            data: function(d) {
                // Adicionar filtros à requisição
                d.market_id = $('#market_id').val();
                d.business_type_id = $('#business_type_id').val();
                d.province_id = $('#province_id').val();
                d.municipality_id = $('#municipality_id').val();
                d.status = $('#status').val();
            }
        },
        columns: [
            { data: 'name', name: 'full_name' },
            { data: 'identity_number', name: 'identity_number' },
            { data: 'phone', name: 'phone' },
            { data: 'status', name: 'status' },
            { data: 'actions', name: 'actions', orderable: false, searchable: false }
        ],
        pageLength: 15,
        language: {
            processing: '<i class="fas fa-spinner fa-spin"></i> Processando...',
            search: '<i class="fas fa-search"></i> Pesquisar:',
            lengthMenu: 'Mostrar _MENU_ registros por página',
            info: 'Mostrando _START_ a _END_ de _TOTAL_ registros',
            infoEmpty: 'Mostrando 0 a 0 de 0 registros',
            infoFiltered: '(filtrado de _MAX_ registros no total)',
            loadingRecords: '<i class="fas fa-spinner fa-spin"></i> Carregando...',
            zeroRecords: '<i class="fas fa-database"></i> Nenhum registro encontrado',
            emptyTable: '<i class="fas fa-table"></i> Nenhum dado disponível na tabela',
            paginate: {
                first: '<i class="fas fa-angle-double-left"></i>',
                previous: '<i class="fas fa-angle-left"></i>',
                next: '<i class="fas fa-angle-right"></i>',
                last: '<i class="fas fa-angle-double-right"></i>'
            }
        },
        responsive: true,
        order: [[0, 'asc']],
        drawCallback: function() {
            // Reinicializar tooltips após cada redraw
            initTooltips();
        }
    });

    // Aplicar filtros
    $('#apply-filters').on('click', function(e) {
        e.preventDefault();
        table.ajax.reload();
    });

    // Limpar filtros
    $('#clear-filters').on('click', function() {
        $('#market_id').val('');
        $('#business_type_id').val('');
        $('#province_id').val('');
        $('#municipality_id').val('');
        $('#status').val('');
        table.ajax.reload();
    });

    // Função para inicializar tooltips
    function initTooltips() {
        if (typeof bootstrap !== 'undefined' && bootstrap.Tooltip) {
            $('[title]').each(function() {
                const tooltip = bootstrap.Tooltip.getInstance(this);
                if (tooltip) tooltip.dispose();
                new bootstrap.Tooltip(this, { placement: 'top', trigger: 'hover' });
            });
        }
    }
});
</script>
<?php $__env->stopPush(); ?>

<?php echo $__env->make('layouts.app', array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?><?php /**PATH /var/www/html/resources/views/admin/mercado/comerciantes/index.blade.php ENDPATH**/ ?>