@extends('layouts.app')

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

@section('content')
    @php
        $registrationData = [
            [
                'id' => 1,
                'date' => '2025-04-16',
                'activatedCards' => 2,
                'registeredMembers' => 2
            ],
            [
                'id' => 2,
                'date' => '2025-04-14',
                'activatedCards' => 1,
                'registeredMembers' => 1
            ],
            [
                'id' => 3,
                'date' => '2025-03-20',
                'activatedCards' => 1,
                'registeredMembers' => 1
            ],
            [
                'id' => 4,
                'date' => '2025-03-10',
                'activatedCards' => 1,
                'registeredMembers' => 1
            ],
            [
                'id' => 5,
                'date' => '2025-03-01',
                'activatedCards' => 1,
                'registeredMembers' => 1
],[
                'id' => 6,
                'date' => '2025-03-01',
                'activatedCards' => 1,
                'registeredMembers' => 1
],[
                'id' => 7,
                'date' => '2025-03-01',
                'activatedCards' => 1,
                'registeredMembers' => 1
],
        ];
    @endphp
 <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">
            <!-- Filters -->
            <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">
                        </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">
                        </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.table-component', [
               
                '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,
                'showEditModal' => false,
                'showViewModal' => false,
                'showSearch' => false
            ])
        </div>
    </div>

    <style>
        .card {
            border-radius: 5px;
            border: 1px solid #dee2e6;
        }
        .form-control {
            font-size: 0.9rem;
        }
        .btn-outline-secondary {
            border-color: #6c757d;
            color: #6c757d;
        }
        .btn-outline-secondary:hover {
            background-color: #f8f9fa;
        }
        .btn-export-csv {
            background-color: #ff6200;
            color: white;
            border: none;
            
        }
        .btn-export-csv:hover {
            background-color: #e65a00;
        }
        .input-group-text {
            background-color: #f8f9fa;
        }
    </style>

    <script>
        let originalData = [...tableConfig.data];
        const storedData = JSON.parse(sessionStorage.getItem('registrationReport') || '[]');
        if (storedData.length > 0) {
            tableConfig.data = [...tableConfig.data, ...storedData];
            originalData = [...tableConfig.data];
        }

        const startDateInput = document.getElementById('startDate');
        const endDateInput = document.getElementById('endDate');
        const clearFiltersBtn = document.getElementById('clearFilters');
        const exportCsvBtn = document.getElementById('exportCsv');

        function formatDateForDisplay(dateStr) {
            const date = new Date(dateStr);
            const options = { day: '2-digit', month: 'short', year: 'numeric' };
            return date.toLocaleDateString('en-US', options).replace(/,/, '');
        }

        function filterData() {
            const startDate = startDateInput.value ? new Date(startDateInput.value) : null;
            const endDate = endDateInput.value ? new Date(endDateInput.value) : null;

            tableConfig.data = originalData.filter(item => {
                const itemDate = new Date(item.date);
                if (startDate && itemDate < startDate) return false;
                if (endDate) {
                    const endDateAdjusted = new Date(endDate);
                    endDateAdjusted.setHours(23, 59, 59, 999);
                    if (itemDate > endDateAdjusted) return false;
                }
                return true;
            });

            currentPage = 1;
            renderTable();
            renderPagination();
        }

        startDateInput.addEventListener('change', filterData);
        endDateInput.addEventListener('change', filterData);

        clearFiltersBtn.addEventListener('click', () => {
            startDateInput.value = '';
            endDateInput.value = '';
            tableConfig.data = [...originalData];
            currentPage = 1;
            renderTable();
            renderPagination();
        });

        exportCsvBtn.addEventListener('click', () => {
            const headers = tableConfig.columns.map(col => col.name).join(',');
            const rows = tableConfig.data.map(item => {
                return [
                    formatDateForDisplay(item.date),
                    item.activatedCards,
                    item.registeredMembers
                ].join(',');
            });
            const csvContent = [headers, ...rows].join('\n');
            const blob = new Blob([csvContent], { type: 'text/csv;charset=utf-8;' });
            const link = document.createElement('a');
            const url = URL.createObjectURL(blob);
            link.setAttribute('href', url);
            link.setAttribute('download', 'registration-report.csv');
            document.body.appendChild(link);
            link.click();
            document.body.removeChild(link);
        });

        // Initial render with formatted dates
        tableConfig.data.forEach(item => {
            item.displayDate = formatDateForDisplay(item.date);
        });
        renderTable();
        renderPagination();
    </script>
@endsection