@extends('layouts.admin')

@section('title', 'Delivery Reports')

@section('content')
<x-admin.report-page title="Delivery Reports" subtitle="Basic monthly delivery analytics by carrier with export option">
    <x-slot:actions>
        <a href="{{ Route::has('admin.reports.delivery.export-csv') ? route('admin.reports.delivery.export-csv', request()->only(['month', 'year'])) : '#' }}" class="ci-btn ci-btn--primary">
            <i class="bi bi-download"></i> Export CSV
        </a>
    </x-slot:actions>

    <form method="GET" action="{{ Route::has('admin.reports.delivery') ? route('admin.reports.delivery') : '#' }}" class="ci-filters ci-filters--report">
        <div class="ci-filters__field">
            <label for="month">Month</label>
            <select name="month" id="month" required>
                @for($m = 1; $m <= 12; $m++)
                    <option value="{{ $m }}" {{ (int) $month === $m ? 'selected' : '' }}>
                        {{ \Carbon\Carbon::create()->month($m)->format('F') }}
                    </option>
                @endfor
            </select>
        </div>
        <div class="ci-filters__field">
            <label for="year">Year</label>
            <input type="number" min="2020" max="2100" name="year" id="year" value="{{ $year }}" required>
        </div>
        <div class="ci-filters__actions">
            <button type="submit" class="ci-btn ci-btn--primary">
                <i class="bi bi-search"></i> Show
            </button>
            <a href="{{ Route::has('admin.reports.delivery') ? route('admin.reports.delivery') : '#' }}" class="ci-btn ci-btn--ghost ci-btn--icon" title="Reset">
                <i class="bi bi-arrow-clockwise"></i>
            </a>
        </div>
    </form>

    <div class="ci-stats">
        <div class="ci-stat">
            <div>
                <p class="ci-stat__label">Total Orders</p>
                <p class="ci-stat__value">{{ number_format($kpis['total_orders']) }}</p>
            </div>
            <span class="ci-stat__icon"><i class="bi bi-truck"></i></span>
        </div>
        @forelse($carrierKpis as $carrierKpi)
            <div class="ci-stat">
                <div>
                    <p class="ci-stat__label">{{ $carrierKpi['carrier_name'] }}</p>
                    <p class="ci-stat__value">{{ number_format($carrierKpi['total_orders']) }}</p>
                </div>
                <span class="ci-stat__icon"><i class="bi bi-box-seam"></i></span>
            </div>
        @empty
            <div class="ci-stat">
                <div>
                    <p class="ci-stat__label">No Carrier Data</p>
                    <p class="ci-stat__value">0</p>
                </div>
                <span class="ci-stat__icon"><i class="bi bi-inbox"></i></span>
            </div>
        @endforelse
    </div>

    <div class="ci-table-card">
        <div class="ci-section__head">
            <h2>Delivery Records</h2>
            <span class="ci-pill ci-pill--info">{{ $reportRows->total() }} Rows</span>
        </div>
        @if($reportRows->count() > 0)
            <div class="ci-table-wrap">
                <table class="ci-table">
                    <thead>
                        <tr>
                            <th>Order</th>
                            <th>Carrier</th>
                            <th>Order Date</th>
                            <th>Shipment Status</th>
                            <th>Order Status</th>
                            <th class="ci-end">Amount</th>
                            <th>Payment Type</th>
                        </tr>
                    </thead>
                    <tbody>
                        @foreach($reportRows as $row)
                            <tr>
                                <td>
                                    <a href="{{ route('admin.orders.show', $row->order_id) }}" class="ci-link">
                                        {{ $row->order_number ?: ('#' . $row->order_id) }}
                                    </a>
                                </td>
                                <td>{{ $row->carrier_name ?: 'N/A' }}</td>
                                <td>{{ \Carbon\Carbon::parse($row->order_date)->format('Y-m-d H:i') }}</td>
                                <td><span class="ci-pill ci-pill--info">{{ ucfirst($row->shipment_status) }}</span></td>
                                <td><span class="ci-pill ci-pill--chip">{{ ucfirst($row->order_status) }}</span></td>
                                <td class="ci-end"><span class="ci-money">₹{{ number_format((float) $row->order_amount, 2) }}</span></td>
                                <td>{{ $row->payment_method === 'cod' ? 'COD' : 'Prepaid' }}</td>
                            </tr>
                        @endforeach
                    </tbody>
                </table>
            </div>

            <div class="ci-pagination">
                <div class="ci-pagination__info">
                    Showing <strong>{{ $reportRows->firstItem() ?? 0 }}</strong> - <strong>{{ $reportRows->lastItem() ?? 0 }}</strong>
                    of <strong>{{ $reportRows->total() }}</strong> rows
                </div>
                @if($reportRows->hasPages())
                    {{ $reportRows->links('vendor.pagination.admin-categories') }}
                @endif
            </div>
        @else
            <div class="ci-empty">
                <i class="bi bi-inbox"></i>
                <h3>No Delivery Records Found</h3>
                <p>No records matched your selected month/year/carrier filters.</p>
            </div>
        @endif
    </div>
</x-admin.report-page>
@endsection
