@extends('admin.layouts.app')
@section('title','Reports')
@section('breadcrumb','Reports')
@section('content')
<div class="admin-page pn-reports-page">
    <div class="page-title-row">
        <div><div class="eyebrow">OPERATIONS REPORTING</div><h1>Reports</h1><p>Operational and customer activity for {{ $periodLabel }}.</p></div>
        <a class="btn btn-secondary" href="{{ request()->fullUrlWithQuery(['export' => 'csv']) }}">Export CSV ↓</a>
    </div>

    <form class="report-filter panel" method="GET" action="{{ url('/admin/reports') }}">
        <div class="report-filter-title"><strong>Reporting period</strong><span>All figures update to match the selected period.</span></div>
        <div class="report-filter-controls">
            <label><span>Period</span><select name="period" id="report-period"><option value="today" @selected($period === 'today')>Today</option><option value="last_7_days" @selected($period === 'last_7_days')>Last 7 days</option><option value="this_month" @selected($period === 'this_month')>This month</option><option value="last_month" @selected($period === 'last_month')>Last month</option><option value="custom" @selected($period === 'custom')>Custom range</option></select></label>
            <label class="report-custom-date"><span>Start date</span><input type="date" name="start_date" value="{{ request('start_date', $period === 'custom' ? $from->toDateString() : '') }}"></label>
            <label class="report-custom-date"><span>End date</span><input type="date" name="end_date" value="{{ request('end_date', $period === 'custom' ? $to->toDateString() : '') }}"></label>
            <button class="btn btn-primary" type="submit">Apply filters</button>
            <a class="report-reset" href="{{ url('/admin/reports') }}">Reset</a>
        </div>
    </form>

    @php
        $reportKpis = [
            ['Total Shipments', $stats['orders'], 'package', 'navy'],
            ['Delivered', $stats['delivered'], 'check-circle', 'green'],
            ['In Transit', $stats['in_transit'], 'truck', 'blue'],
            ['Cancelled', $stats['cancelled'], 'x-circle', 'red'],
            ['New Customers', $stats['customers'], 'users', 'purple'],
            ['Quotes', $stats['quotes'], 'file-text', 'amber'],
            ['Invoices', $stats['invoices'], 'receipt', 'orange'],
        ];
    @endphp
    <div class="report-kpi-grid">
        @foreach($reportKpis as $item)
        <div class="report-kpi {{ $item[3] }}"><div class="report-kpi-top"><span>{{ $item[0] }}</span><i>{{ $item[2] === 'package' ? '□' : ($item[2] === 'check-circle' ? '✓' : ($item[2] === 'truck' ? '↗' : ($item[2] === 'x-circle' ? '×' : ($item[2] === 'users' ? '♙' : ($item[2] === 'file-text' ? '▤' : '▧')))) }}</i></div><strong>{{ number_format($item[1]) }}</strong><small>{{ $periodLabel }}</small></div>
        @endforeach
    </div>

    <div class="report-analysis-grid">
        <section class="panel report-panel">
            <div class="panel-heading"><div><h2>Shipment Status</h2><p>How shipments are progressing in this period.</p></div><span class="report-total">{{ $stats['orders'] }} total</span></div>
            <div class="report-bars report-bars-spaced">
                @forelse($statusMix as $status => $count)
                    <div><div><strong>{{ $status ?: 'Unspecified' }}</strong><span>{{ number_format($count) }} <em>{{ number_format(($count / max(1, $totalStatusShipments)) * 100, 0) }}%</em></span></div><div class="report-bar"><i style="width:{{ min(100, ($count / max(1, $totalStatusShipments)) * 100) }}%"></i></div></div>
                @empty <div class="empty">No shipment status data for this period.</div> @endforelse
            </div>
        </section>
        <section class="panel report-panel">
            <div class="panel-heading"><div><h2>Shipment Service Mix</h2><p>Shipment records grouped by courier service.</p></div><span class="report-total">{{ $serviceMix->count() }} services</span></div>
            <div class="report-bars report-bars-spaced">
                @forelse($serviceMix as $service => $count)
                    <div><div><strong>{{ $service ?: 'Other' }}</strong><span>{{ number_format($count) }} <em>{{ number_format(($count / max(1, $totalServiceShipments)) * 100, 0) }}%</em></span></div><div class="report-bar"><i style="width:{{ min(100, ($count / max(1, $totalServiceShipments)) * 100) }}%"></i></div></div>
                @empty <div class="empty">No shipment service data for this period.</div> @endforelse
            </div>
        </section>
    </div>

    <section class="panel report-table-panel">
        <div class="panel-heading"><div><h2>Shipment Details</h2><p>Latest shipments within {{ $periodLabel }}.</p></div><span class="report-total">Up to 10 records</span></div>
        <div class="table-scroll"><table class="admin-table modern"><thead><tr><th>Tracking ID</th><th>Customer</th><th>Destination</th><th>Service</th><th>Status</th><th>Date</th></tr></thead><tbody>
        @forelse($recentShipments as $shipment)
            <tr><td><strong>{{ $shipment->tracking_id }}</strong></td><td>{{ optional($shipment->user)->name ?? '—' }}</td><td>{{ trim(($shipment->destination_city ?? '') . ', ' . ($shipment->destination_country ?? ''), ', ') ?: '—' }}</td><td>{{ $shipment->service_type ?: '—' }}</td><td><span class="report-status">{{ $shipment->status ?: 'Unknown' }}</span></td><td>{{ optional($shipment->created_at)->format('d M Y') ?? '—' }}</td></tr>
        @empty <tr><td colspan="6"><div class="empty">No shipments found for the selected period.</div></td></tr> @endforelse
        </tbody></table></div>
    </section>
    <p class="report-footnote">Counts are based on record creation dates in the selected period. Financial totals are not shown here because a created invoice or displayed payment QR does not by itself confirm that money was received.</p>
</div>
<script>
(function(){const period=document.getElementById('report-period');const form=period&&period.form;const fields=document.querySelectorAll('.report-custom-date');function update(){const custom=period&&period.value==='custom';fields.forEach(function(el){el.style.display=custom?'grid':'none';el.querySelector('input').required=!!custom;});}if(period){period.addEventListener('change',update);update();}})();
</script>
@endsection
