@extends('layouts.app')

@section('page_title', 'Registration Report')

@section('content')
    <div class="mb-3">
        <h5 class="mb-0 fw-bold text-dark" style="font-size: 1.25rem;">Registration Report</h5>
    </div>
    <div class="row justify-content-center">
        <div class="card-body p-3">
            @if ($errorMessage)
                <div class="alert alert-danger">{{ $errorMessage }}</div>
            @endif

            <div class="d-flex justify-content-between mb-3 flex-wrap gap-2">
                <div class="d-flex align-items-center gap-2">
                    <div class="d-flex flex-column">
                        <label for="startDate" class="form-label mb-1">Start Date</label>
                        <div class="input-group">
                            <span class="input-group-text"><i class="fas fa-calendar-alt"></i></span>
                            <input type="date" class="form-control" id="startDate" value="{{ request('date_start') }}">
                        </div>
                    </div>
                    <div class="d-flex flex-column">
                        <label for="endDate" class="form-label mb-1">End Date</label>
                        <div class="input-group">
                            <input type="date" class="form-control" id="endDate" value="{{ request('date_end') }}">
                        </div>
                    </div>
                </div>
                <div class="d-flex gap-2">
                    <button type="button" class="btn btn-export-csv" id="exportCsv" style="margin:20px">Export CSV</button>
                </div>
            </div>

            @include('components.reports-component', [
                'pageTitle' => 'Registration Report',
                'data' => $registrationData ?? [],
                'columns' => [
                    ['name' => 'Date', 'key' => 'date', 'sortable' => true, 'format' => 'date'],
                    ['name' => 'No. of Activated Cards', 'key' => 'activatedCards', 'sortable' => true],
                    ['name' => 'No. of Registered Members', 'key' => 'registeredMembers', 'sortable' => true]
                ],
                'allFields' => [],
                'actions' => [],
                'showAddButton' => false,
                'showCheckboxes' => false,
                'showBatchDelete' => false,
                'showSearch' => true,
                'currentPage' => $currentPage ?? 1,
                'lastPage' => $lastPage ?? 1,
                'total' => $total ?? 0,
            ])
        </div>
    </div>

    <style>
        .btn-export-csv {
            background-color: #ff6200;
            color: white;
            border: none;
        }
        .btn-export-csv:hover {
            background-color: #e65a00;
        }
    </style>

    <script>
        const startDateInput = document.getElementById('startDate');
        const endDateInput = document.getElementById('endDate');
        const searchInput = document.getElementById('searchInput');
        const exportCsvBtn = document.getElementById('exportCsv');

        function updateQueryParams() {
            const url = new URL(window.location);
            url.searchParams.set('page', '1');
            if (startDateInput.value) url.searchParams.set('date_start', startDateInput.value);
            else url.searchParams.delete('date_start');
            if (endDateInput.value) url.searchParams.set('date_end', endDateInput.value);
            else url.searchParams.delete('date_end');
            if (searchInput?.value) url.searchParams.set('_search', searchInput.value);
            else url.searchParams.delete('_search');
            window.location = url;
        }

        startDateInput.addEventListener('change', updateQueryParams);
        endDateInput.addEventListener('change', updateQueryParams);
        searchInput?.addEventListener('input', () => {
            clearTimeout(window.searchTimeout);
            window.searchTimeout = setTimeout(updateQueryParams, 500);
        });

        exportCsvBtn.addEventListener('click', () => {
            const url = new URL(tableConfig.exportEndpoint, window.location.origin);
            if (startDateInput.value) url.searchParams.set('date_start', startDateInput.value);
            if (endDateInput.value) url.searchParams.set('date_end', endDateInput.value);
            if (searchInput?.value) url.searchParams.set('_search', searchInput.value);
            if (sortColumn && sortDirection) {
                url.searchParams.set('sort', `${sortColumn}|${sortDirection}`);
            }
            console.log('Exporting CSV to:', url.toString());
            window.location.href = url.toString();
        });
    </script>
@endsection