<?php $__env->startSection('content'); ?>
    <div class="container-fluid">
        <div class="row page-titles">
            <ol class="breadcrumb">
                <li class="breadcrumb-item"><a href="<?php echo e(route('dashboard')); ?>">Dashboard</a></li>
                <li class="breadcrumb-item active"><a href="javascript:void(0)">Promoters</a></li>
            </ol>
        </div>
        <?php echo $__env->make('iqec.partials.flash', array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?>
        <div class="card">
            <div class="card-header d-flex justify-content-between align-items-center">
                <h4 class="card-title mb-0">Promoters</h4>
                <a href="<?php echo e(route('promoters.create')); ?>" class="btn btn-primary btn-sm">Add Promoter</a>
            </div>
            <div class="card-body">
                <div class="table-responsive">
                    <table class="table table-responsive-md">
                        <thead>
                            <tr>
                                <th>Name</th>
                                <th>Email</th>
                                <th>Institute</th>
                                <th>Authorised Partners</th>
                                <th>Status</th>
                                <th class="text-end">Actions</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php $__empty_1 = true; $__currentLoopData = $promoters; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $promoter): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); $__empty_1 = false; ?>
                                <tr>
                                    <td><?php echo e($promoter->applicant_name); ?></td>
                                    <td><?php echo e($promoter->email); ?></td>
                                    <td><?php echo e($promoter->institute_name); ?></td>
                                    <td>
                                        <span class="badge badge-primary light me-2"><?php echo e($promoter->authorised_partners_count); ?></span>
                                        <button
                                            type="button"
                                            class="btn btn-info btn-xs js-view-partners"
                                            data-bs-toggle="modal"
                                            data-bs-target="#authorised-partners-modal"
                                            data-url="<?php echo e(route('promoters.authorised-partners', $promoter)); ?>"
                                            data-name="<?php echo e($promoter->applicant_name); ?>"
                                            data-count="<?php echo e($promoter->authorised_partners_count); ?>"
                                        >View</button>
                                    </td>
                                    <td><span class="badge <?php echo e($promoter->user->status->badgeClass()); ?>"><?php echo e($promoter->user->status->label()); ?></span></td>
                                    <td class="text-end">
                                        <?php echo $__env->make('iqec.partials.table-actions', [
                                            'resource' => 'promoters',
                                            'record' => $promoter,
                                            'label' => 'promoter',
                                        ], array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?>
                                    </td>
                                </tr>
                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); if ($__empty_1): ?>
                                <tr><td colspan="6">No promoters yet.</td></tr>
                            <?php endif; ?>
                        </tbody>
                    </table>
                </div>
                <?php echo e($promoters->links()); ?>

            </div>
        </div>
        <div class="modal fade" id="authorised-partners-modal">
            <div class="modal-dialog modal-dialog-centered modal-lg" role="document">
                <div class="modal-content">
                    <div class="modal-header">
                        <h5 class="modal-title" id="authorised-partners-modal-title">Authorised Partners</h5>
                        <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                    </div>
                    <div class="modal-body" id="authorised-partners-modal-body">
                        <p class="text-center mb-0">Loading...</p>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-danger light" data-bs-dismiss="modal">Close</button>
                    </div>
                </div>
            </div>
        </div>
    </div>
<?php $__env->stopSection(); ?>

<?php $__env->startSection('scripts'); ?>
<script>
    (function () {
        var modalEl = document.getElementById('authorised-partners-modal');
        if (!modalEl) {
            return;
        }

        var titleEl = document.getElementById('authorised-partners-modal-title');
        var bodyEl = document.getElementById('authorised-partners-modal-body');

        function loadPartners(url) {
            bodyEl.innerHTML = '<p class="text-center mb-0">Loading...</p>';

            fetch(url, {
                headers: {
                    'X-Requested-With': 'XMLHttpRequest',
                    'Accept': 'text/html',
                },
            }).then(function (response) {
                if (!response.ok) {
                    throw new Error('Request failed');
                }
                return response.text();
            }).then(function (html) {
                bodyEl.innerHTML = html;
            }).catch(function () {
                bodyEl.innerHTML = '<p class="text-danger mb-0">Unable to load authorised partners.</p>';
            });
        }

        modalEl.addEventListener('show.bs.modal', function (event) {
            var button = event.relatedTarget;
            if (!button || !button.getAttribute('data-url')) {
                return;
            }

            var name = button.getAttribute('data-name') || '';
            var count = button.getAttribute('data-count') || '0';
            titleEl.textContent = 'Authorised Partners — ' + name + ' (' + count + ')';
            loadPartners(button.getAttribute('data-url'));
        });

        bodyEl.addEventListener('click', function (event) {
            var link = event.target.closest('.js-partners-pagination a');
            if (!link || !link.getAttribute('href')) {
                return;
            }

            event.preventDefault();
            loadPartners(link.getAttribute('href'));
        });
    })();
</script>
<?php $__env->stopSection(); ?>

<?php echo $__env->make('iqec.layouts.app', array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?><?php /**PATH /Users/fasil/Sites/iqec/admin/resources/views/iqec/promoters/index.blade.php ENDPATH**/ ?>