@extends('layouts.admin.app')

@section('title', translate('Courier Settlement Details'))

@section('content')
    <div class="content container-fluid">
        <div class="page-header">
            <h1 class="page-header-title">
                <span class="page-header-icon"><i class="tio-receipt-outlined"></i></span>
                <span>{{ $settlement->voucher_no }}</span>
            </h1>
            <div class="page-header-right">
                <a href="{{ route('admin.courier.settlements.index') }}" class="btn btn--light">{{ translate('Back') }}</a>
            </div>
        </div>

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

        <div class="row g-3 mb-3">
            <div class="col-md-3"><div class="card"><div class="card-body"><div class="text-muted fs-12">{{ translate('Courier') }}</div><h4>{{ $settlement->courier?->name }}</h4></div></div></div>
            <div class="col-md-3"><div class="card"><div class="card-body"><div class="text-muted fs-12">{{ translate('COD') }}</div><h4>{{ \App\CentralLogics\Helpers::format_currency($settlement->total_cod_amount) }}</h4></div></div></div>
            <div class="col-md-3"><div class="card"><div class="card-body"><div class="text-muted fs-12">{{ translate('Deductions') }}</div><h4>{{ \App\CentralLogics\Helpers::format_currency($settlement->delivery_charge_deduction + $settlement->cod_charge_deduction + $settlement->return_charge_deduction) }}</h4></div></div></div>
            <div class="col-md-3"><div class="card"><div class="card-body"><div class="text-muted fs-12">{{ translate('Net Received') }}</div><h4>{{ \App\CentralLogics\Helpers::format_currency($settlement->net_received_amount) }}</h4></div></div></div>
        </div>

        @if($settlement->status !== 'reversed')
            <div class="card mb-3">
                <div class="card-body">
                    <form method="post" action="{{ route('admin.courier.settlements.reverse', $settlement->id) }}" class="row g-3 align-items-end">
                        @csrf
                        <div class="col-md-9">
                            <label class="input-label">{{ translate('Reverse Reason') }}</label>
                            <input type="text" name="reverse_reason" class="form-control" required placeholder="{{ translate('Required for audit trail') }}">
                        </div>
                        <div class="col-md-3">
                            <button class="btn btn-outline-danger btn-block">{{ translate('Reverse Voucher') }}</button>
                        </div>
                    </form>
                </div>
            </div>
        @endif

        <div class="card mb-3">
            <div class="card-header border-0"><h4 class="card-title">{{ translate('Settlement Items') }}</h4></div>
            <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('Parcel') }}</th><th class="text-right">{{ translate('COD') }}</th><th class="text-right">{{ translate('Charges') }}</th><th class="text-right">{{ translate('Net Receivable') }}</th><th class="text-right">{{ translate('Net Received') }}</th><th>{{ translate('Status') }}</th></tr></thead>
                    <tbody>
                    @foreach($settlement->items as $item)
                        <tr>
                            <td><a href="{{ route('admin.order.details', $item->order_id) }}">#{{ $item->order_id }}</a></td>
                            <td class="text-break">{{ $item->parcel_id }}</td>
                            <td class="text-right">{{ \App\CentralLogics\Helpers::format_currency($item->cod_amount) }}</td>
                            <td class="text-right">{{ \App\CentralLogics\Helpers::format_currency($item->delivery_charge + $item->cod_charge + $item->return_charge) }}</td>
                            <td class="text-right">{{ \App\CentralLogics\Helpers::format_currency($item->net_receivable) }}</td>
                            <td class="text-right font-weight-bold">{{ \App\CentralLogics\Helpers::format_currency($item->net_received) }}</td>
                            <td>{{ translate(str_replace('_', ' ', $item->settlement_status)) }}</td>
                        </tr>
                    @endforeach
                    </tbody>
                </table>
            </div>
        </div>

        <div class="card">
            <div class="card-header border-0"><h4 class="card-title">{{ translate('Ledger Entries') }}</h4></div>
            <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('Particulars') }}</th><th class="text-right">{{ translate('Debit') }}</th><th class="text-right">{{ translate('Credit') }}</th><th class="text-right">{{ translate('Balance') }}</th><th>{{ translate('Status') }}</th></tr></thead>
                    <tbody>
                    @foreach($settlement->ledgerEntries as $entry)
                        <tr>
                            <td>{{ $entry->entry_date?->format('d M Y') }}</td>
                            <td>{{ $entry->voucher_no }}</td>
                            <td>{{ $entry->particulars }}</td>
                            <td class="text-right">{{ \App\CentralLogics\Helpers::format_currency($entry->debit) }}</td>
                            <td class="text-right">{{ \App\CentralLogics\Helpers::format_currency($entry->credit) }}</td>
                            <td class="text-right">{{ \App\CentralLogics\Helpers::format_currency($entry->balance) }}</td>
                            <td>{{ translate($entry->status) }}</td>
                        </tr>
                    @endforeach
                    </tbody>
                </table>
            </div>
        </div>
    </div>
@endsection
