<div class="container-fluid fade-in">
    <div class="d-flex justify-content-between align-items-center mb-3">
        <h4><?= lang('applications.waiting_list') ?></h4>
        <?php if (Gate::can('waitlist.manage')): ?>
        <a href="/waiting-list/create-entry" class="btn btn-primary"><i class="fas fa-plus me-1"></i><?= lang('applications.add_to_waitlist') ?></a>
        <?php endif; ?>
    </div>

    <!-- Summary Cards -->
    <div class="card mb-4">
        <div class="card-header"><h6 class="mb-0">Waiting Lists by Type</h6></div>
        <div class="card-body">
            <table id="waitlists-table" class="table table-sm w-100">
                <thead><tr><th>#</th><th>Type</th><th>Max</th><th>Current</th><th>Waiting</th><th>Offered</th><th>Status</th></tr></thead>
            </table>
        </div>
    </div>

    <!-- Entries -->
    <div class="card">
        <div class="card-header d-flex justify-content-between align-items-center">
            <h6 class="mb-0">All Entries</h6>
            <div class="d-flex gap-2">
                <select id="filter-list" class="form-select form-select-sm" style="width:auto">
                    <option value="">All Lists</option>
                    <?php foreach ($membershipTypes as $t): ?>
                    <option value=""><?= e($t['name_en']) ?></option>
                    <?php endforeach; ?>
                </select>
                <select id="filter-entry-status" class="form-select form-select-sm" style="width:auto">
                    <option value="">All Statuses</option>
                    <option value="waiting">Waiting</option>
                    <option value="offered">Offered</option>
                    <option value="accepted">Accepted</option>
                    <option value="declined">Declined</option>
                    <option value="expired">Expired</option>
                </select>
            </div>
        </div>
        <div class="card-body">
            <table id="entries-table" class="table table-striped w-100">
                <thead><tr><th>Queue #</th><th>Name</th><th>Phone</th><th>National ID</th><th>Type</th><th>Date</th><th>Priority</th><th>Status</th><th>Actions</th></tr></thead>
            </table>
        </div>
    </div>
</div>

<?php $__view->push('scripts') ?>
<script>
let entriesTable;

$(document).ready(function() {
    $('#waitlists-table').DataTable({
        processing: true, serverSide: true,
        ajax: { url: '/api/waiting-list/datatable', type: 'GET' },
        columns: [
            { data: 'id' }, { data: 'type' }, { data: 'max' }, { data: 'current' },
            { data: 'waiting' }, { data: 'offered' }, { data: 'status' }
        ],
        paging: false, searching: false, info: false, ordering: false,
    });

    entriesTable = $('#entries-table').DataTable({
        processing: true, serverSide: true,
        ajax: {
            url: '/api/waiting-list/entries/datatable', type: 'GET',
            data: function(d) {
                d.waiting_list_id = $('#filter-list').val();
                d.status = $('#filter-entry-status').val();
            }
        },
        columns: [
            { data: 'queue_number' }, { data: 'name' }, { data: 'phone' }, { data: 'national_id' },
            { data: 'type' }, { data: 'date' }, { data: 'priority' }, { data: 'status' },
            { data: 'actions', orderable: false }
        ],
        order: [[0, 'asc']], pageLength: 25,
    });

    $('#filter-list, #filter-entry-status').on('change', function() { entriesTable.ajax.reload(); });
});

function offerEntry(id) {
    Swal.fire({ title: 'Send offer to this applicant?', icon: 'question', showCancelButton: true }).then(r => {
        if (r.isConfirmed) {
            $.post('/api/waiting-list/entries/' + id + '/offer', { _token: '<?= csrf_token() ?>' }, function(res) {
                if (res.success) { FormHelper.showToast('success', res.message); entriesTable.ajax.reload(); }
                else FormHelper.showToast('error', res.message);
            });
        }
    });
}

function promoteEntry(id) {
    $.post('/api/waiting-list/entries/' + id + '/promote', { _token: '<?= csrf_token() ?>' }, function(res) {
        if (res.success) { FormHelper.showToast('success', res.message); entriesTable.ajax.reload(); }
        else FormHelper.showToast('error', res.message);
    });
}

function removeEntry(id) {
    Swal.fire({ title: 'Remove this entry?', icon: 'warning', showCancelButton: true, confirmButtonColor: '#e53e3e' }).then(r => {
        if (r.isConfirmed) {
            $.post('/api/waiting-list/entries/' + id + '/remove', { _token: '<?= csrf_token() ?>' }, function(res) {
                if (res.success) { FormHelper.showToast('success', res.message); entriesTable.ajax.reload(); }
                else FormHelper.showToast('error', res.message);
            });
        }
    });
}
</script>
<?php $__view->endPush() ?>