@extends('iqec.layouts.app')

@section('content')
    <div class="container-fluid">
        <div class="row page-titles">
            <ol class="breadcrumb">
                <li class="breadcrumb-item"><a href="{{ route('dashboard') }}">Dashboard</a></li>
                <li class="breadcrumb-item active"><a href="javascript:void(0)">Promoters</a></li>
            </ol>
        </div>
        @include('iqec.partials.flash')
        <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="{{ 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>
                            @forelse ($promoters as $promoter)
                                <tr>
                                    <td>{{ $promoter->applicant_name }}</td>
                                    <td>{{ $promoter->email }}</td>
                                    <td>{{ $promoter->institute_name }}</td>
                                    <td>
                                        <span class="badge badge-primary light me-2">{{ $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="{{ route('promoters.authorised-partners', $promoter) }}"
                                            data-name="{{ $promoter->applicant_name }}"
                                            data-count="{{ $promoter->authorised_partners_count }}"
                                        >View</button>
                                    </td>
                                    <td><span class="badge {{ $promoter->user->status->badgeClass() }}">{{ $promoter->user->status->label() }}</span></td>
                                    <td class="text-end">
                                        @include('iqec.partials.table-actions', [
                                            'resource' => 'promoters',
                                            'record' => $promoter,
                                            'label' => 'promoter',
                                        ])
                                    </td>
                                </tr>
                            @empty
                                <tr><td colspan="6">No promoters yet.</td></tr>
                            @endforelse
                        </tbody>
                    </table>
                </div>
                {{ $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>
@endsection

@section('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>
@endsection
