@extends('layouts.admin.app')

@section('title', translate('Courier Settlements'))

@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"></i></span>
                <span>{{ translate('Courier Settlement') }}</span>
            </h1>
            <div class="page-header-right">
                <a href="{{ route('admin.courier.settlements.create') }}" class="btn btn--primary">
                    <i class="tio-add-circle"></i> {{ translate('New Settlement') }}
                </a>
            </div>
        </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="search" value="{{ request('search') }}" class="form-control" placeholder="{{ translate('Voucher, reference or note') }}"></div>
                        <div class="col-md-2">
                            <select name="status" class="form-control">
                                <option value="all">{{ translate('All Status') }}</option>
                                <option value="posted" {{ request('status') === 'posted' ? 'selected' : '' }}>{{ translate('Posted') }}</option>
                                <option value="reversed" {{ request('status') === 'reversed' ? 'selected' : '' }}>{{ translate('Reversed') }}</option>
                            </select>
                        </div>
                        <div class="col-md-3"><div class="input-group"><input type="date" name="from" value="{{ request('from') }}" class="form-control"><input type="date" name="to" value="{{ request('to') }}" class="form-control"></div></div>
                        <div class="col-md-1"><button class="btn btn--primary btn-block">{{ translate('Go') }}</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('Date') }}</th>
                        <th>{{ translate('Voucher') }}</th>
                        <th>{{ translate('Courier') }}</th>
                        <th>{{ translate('Reference') }}</th>
                        <th class="text-right">{{ translate('COD') }}</th>
                        <th class="text-right">{{ translate('Deductions') }}</th>
                        <th class="text-right">{{ translate('Net Received') }}</th>
                        <th>{{ translate('Status') }}</th>
                        <th class="text-center">{{ translate('Action') }}</th>
                    </tr>
                    </thead>
                    <tbody>
                    @forelse($settlements as $settlement)
                        <tr>
                            <td>{{ $settlement->settlement_date?->format('d M Y') }}</td>
                            <td>{{ $settlement->voucher_no }}</td>
                            <td>{{ $settlement->courier?->name }}</td>
                            <td>{{ $settlement->reference_no ?? translate('N/A') }}</td>
                            <td class="text-right">{{ \App\CentralLogics\Helpers::format_currency($settlement->total_cod_amount) }}</td>
                            <td class="text-right">{{ \App\CentralLogics\Helpers::format_currency($settlement->delivery_charge_deduction + $settlement->cod_charge_deduction + $settlement->return_charge_deduction) }}</td>
                            <td class="text-right font-weight-bold">{{ \App\CentralLogics\Helpers::format_currency($settlement->net_received_amount) }}</td>
                            <td><span class="badge {{ $settlement->status === 'reversed' ? 'badge-soft-danger' : 'badge-soft-success' }}">{{ translate($settlement->status) }}</span></td>
                            <td class="text-center">
                                <a class="btn btn-sm btn-outline-primary" href="{{ route('admin.courier.settlements.show', $settlement->id) }}"><i class="tio-visible"></i></a>
                            </td>
                        </tr>
                    @empty
                        <tr><td colspan="9" class="text-center py-4">{{ translate('No courier settlement found') }}</td></tr>
                    @endforelse
                    </tbody>
                </table>
            </div>
            @if($settlements->hasPages())
                <div class="card-footer">{!! $settlements->links() !!}</div>
            @endif
        </div>
    </div>
@endsection
