@extends('layouts.app')

@section('page_title', 'Station Rating Report')

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

            <!-- 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" 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>

            <!-- Table -->
            @include('components.reports-component', [
                'pageTitle' => 'Station Rating Report',
                'data' => $stationRatingData ?? [],
                'columns' => [
                    ['name' => 'Transaction Date and Time', 'key' => 'transactionDateTime', 'sortable' => true],
                    ['name' => 'Card Number', 'key' => 'cardNumber', 'sortable' => true],
                    ['name' => 'Sales Invoice', 'key' => 'salesInvoice', 'sortable' => true],
                    ['name' => 'Station', 'key' => 'station', 'sortable' => true],
                    ['name' => 'Ratings', 'key' => 'ratings', '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 exportCsvBtn = document.getElementById('exportCsv');
    exportCsvBtn.addEventListener('click', () => {
        const url = new URL(tableConfig.exportEndpoint, window.location.origin);
        const searchTerm = document.getElementById('searchInput')?.value || '';
        const startDate = document.getElementById('startDate')?.value || '';
        const endDate = document.getElementById('endDate')?.value || '';
        if (searchTerm) url.searchParams.set('_search', searchTerm);
        if (startDate) url.searchParams.set('date_start', startDate);
        if (endDate) url.searchParams.set('date_end', endDate);
        if (sortColumn && sortDirection) {
            url.searchParams.set('sort', `${sortColumn}|${sortDirection}`);
        }
        console.log('Exporting CSV to:', url.toString());
        window.location.href = url.toString();
    });
</script>
@endsection