@extends('layouts.admin.app')

@section('title', translate('Courier Charges Report'))

@section('content')
    <div class="content container-fluid">
        <div class="page-header">
            <h1 class="page-header-title">
                <span class="page-header-icon"><i class="tio-money-vs"></i></span>
                <span>{{ translate('Courier Charges Report') }}</span>
            </h1>
        </div>

        @include('admin-views.courier.partials._nav')

        <div class="card mb-3">
            <div class="card-body">
                <form method="get">
                    <div class="row g-3">
                        <div class="col-md-3">
                            <select name="courier_id" class="form-control">
                                <option value="">{{ translate('All Couriers') }}</option>
                                @foreach($couriers as $courier)
                                    <option value="{{ $courier->id }}" {{ (string) request('courier_id') === (string) $courier->id ? 'selected' : '' }}>{{ $courier->name }}</option>
                                @endforeach
                            </select>
                        </div>
                        <div class="col-md-3"><input type="text" name="parcel_id" value="{{ request('parcel_id') }}" class="form-control" placeholder="{{ translate('Parcel ID') }}"></div>
                        <div class="col-md-3"><input type="text" name="invoice_no" value="{{ request('invoice_no') }}" class="form-control" placeholder="{{ translate('Invoice No') }}"></div>
                        <div class="col-md-3"><button class="btn btn--primary">{{ translate('Filter') }}</button></div>
                    </div>
                </form>
            </div>
        </div>

        <div class="card">
            <div class="table-responsive">
                <table class="table table-borderless table-thead-bordered table-nowrap card-table">
                    <thead class="thead-light">
                    <tr>
                        <th>{{ translate('Order') }}</th>
                        <th>{{ translate('Courier / Parcel') }}</th>
                        <th>{{ translate('Courier Status') }}</th>
                        <th class="text-right">{{ translate('Delivery Charge') }}</th>
                        <th class="text-right">{{ translate('COD Charge') }}</th>
                        <th class="text-right">{{ translate('Return Charge') }}</th>
                        <th class="text-right">{{ translate('Total') }}</th>
                    </tr>
                    </thead>
                    <tbody>
                    @forelse($shipments as $shipment)
                        @php
                            $total = (float) $shipment->actual_delivery_cost + (float) $shipment->actual_cod_charge + (float) $shipment->actual_return_cost;
                        @endphp
                        <tr>
                            <td><a href="{{ route('admin.order.details', $shipment->order_id) }}">#{{ $shipment->order_id }}</a></td>
                            <td>
                                <strong>{{ $shipment->courier?->name ?: ucfirst($shipment->provider) }}</strong>
                                <div class="fs-12 text-muted text-break">{{ $shipment->courier_consignment_id ?: $shipment->courier_parcel_id ?: $shipment->courier_tracking_id }}</div>
                            </td>
                            <td>{{ $shipment->courier_status ?: translate('N/A') }}</td>
                            <td class="text-right">{{ \App\CentralLogics\Helpers::format_currency($shipment->actual_delivery_cost ?? 0) }}</td>
                            <td class="text-right">{{ \App\CentralLogics\Helpers::format_currency($shipment->actual_cod_charge ?? 0) }}</td>
                            <td class="text-right">{{ \App\CentralLogics\Helpers::format_currency($shipment->actual_return_cost ?? 0) }}</td>
                            <td class="text-right font-weight-bold">{{ \App\CentralLogics\Helpers::format_currency($total) }}</td>
                        </tr>
                    @empty
                        <tr><td colspan="7" class="text-center py-4">{{ translate('No courier charge found') }}</td></tr>
                    @endforelse
                    </tbody>
                </table>
            </div>
            @if($shipments->hasPages())
                <div class="card-footer">{!! $shipments->links() !!}</div>
            @endif
        </div>
    </div>
@endsection
