@extends('layouts.admin.app')

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

@section('content')
    <div class="content container-fluid">
        <div class="page-header">
            <h1 class="page-header-title">
                <span class="page-header-icon"><i class="tio-add-circle"></i></span>
                <span>{{ translate('Create Courier Settlement') }}</span>
            </h1>
            <div class="page-header-right">
                <a href="{{ route('admin.courier.settlements.index') }}" class="btn btn--light">{{ translate('Settlement History') }}</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-4">
                            <select name="courier_id" class="form-control" required>
                                <option value="">{{ translate('Select Courier') }}</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-2"><button class="btn btn--primary btn-block">{{ translate('Load Parcels') }}</button></div>
                    </div>
                </form>
            </div>
        </div>

        <form method="post" action="{{ route('admin.courier.settlements.store') }}">
            @csrf
            <input type="hidden" name="courier_id" value="{{ request('courier_id') }}">
            <div class="card mb-3">
                <div class="card-body">
                    <div class="row g-3">
                        <div class="col-md-3">
                            <label class="input-label">{{ translate('Settlement Date') }}</label>
                            <input type="date" name="settlement_date" value="{{ old('settlement_date', now()->toDateString()) }}" class="form-control" required>
                        </div>
                        <div class="col-md-3">
                            <label class="input-label">{{ translate('Reference No') }}</label>
                            <input type="text" name="reference_no" value="{{ old('reference_no') }}" class="form-control">
                        </div>
                        <div class="col-md-3">
                            <label class="input-label">{{ translate('Payment Method') }}</label>
                            <input type="text" name="payment_method" value="{{ old('payment_method', 'Cash/Bank') }}" class="form-control">
                        </div>
                        <div class="col-md-3">
                            <label class="input-label">{{ translate('Cash/Bank Account') }}</label>
                            <input type="text" name="settlement_account" value="{{ old('settlement_account') }}" class="form-control">
                        </div>
                        <div class="col-md-3">
                            <label class="input-label">{{ translate('Net Received Amount') }}</label>
                            <input type="number" step="0.01" min="0" name="net_received_amount" value="{{ old('net_received_amount') }}" class="form-control" placeholder="{{ translate('Auto if blank') }}">
                        </div>
                        <div class="col-md-3">
                            <label class="input-label">{{ translate('Adjustment Amount') }}</label>
                            <input type="number" step="0.01" name="adjustment_amount" value="{{ old('adjustment_amount', 0) }}" class="form-control">
                        </div>
                        <div class="col-md-6">
                            <label class="input-label">{{ translate('Note') }}</label>
                            <input type="text" name="note" value="{{ old('note') }}" class="form-control">
                        </div>
                    </div>
                </div>
            </div>

            <div class="card">
                <div class="card-header border-0">
                    <div class="d-flex justify-content-between align-items-center w-100">
                        <h4 class="card-title mb-0">{{ translate('Unsettled Parcels') }}</h4>
                        <button type="submit" class="btn btn--primary" {{ request('courier_id') ? '' : 'disabled' }}>{{ translate('Post Settlement') }}</button>
                    </div>
                </div>
                <div class="table-responsive">
                    <table class="table table-borderless table-thead-bordered table-nowrap card-table">
                        <thead class="thead-light">
                        <tr>
                            <th><input type="checkbox" onclick="document.querySelectorAll('.settlement-item').forEach(item => item.checked = this.checked)"></th>
                            <th>{{ translate('Order') }}</th>
                            <th>{{ translate('Parcel') }}</th>
                            <th>{{ translate('Status') }}</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('Received') }}</th>
                            <th class="text-right">{{ translate('Balance') }}</th>
                        </tr>
                        </thead>
                        <tbody>
                        @forelse($shipments as $shipment)
                            @php
                                $charges = (float) $shipment->actual_delivery_cost + (float) $shipment->actual_cod_charge + (float) $shipment->actual_return_cost;
                                $netReceivable = $shipment->net_receivable_amount ?? max((float) $shipment->collectable_amount - $charges, 0);
                                $balance = $shipment->balance_amount ?? ($netReceivable - (float) $shipment->settled_amount);
                            @endphp
                            <tr>
                                <td><input type="checkbox" class="settlement-item" name="shipment_ids[]" value="{{ $shipment->id }}"></td>
                                <td><a href="{{ route('admin.order.details', $shipment->order_id) }}">#{{ $shipment->order_id }}</a></td>
                                <td class="text-break">{{ $shipment->courier_consignment_id ?: $shipment->courier_parcel_id ?: $shipment->courier_tracking_id }}</td>
                                <td>{{ translate(str_replace('_', ' ', $shipment->settlement_status ?: 'unsettled')) }}</td>
                                <td class="text-right">{{ \App\CentralLogics\Helpers::format_currency($shipment->collectable_amount) }}</td>
                                <td class="text-right">{{ \App\CentralLogics\Helpers::format_currency($charges) }}</td>
                                <td class="text-right">{{ \App\CentralLogics\Helpers::format_currency($netReceivable) }}</td>
                                <td class="text-right">{{ \App\CentralLogics\Helpers::format_currency($shipment->settled_amount ?? 0) }}</td>
                                <td class="text-right font-weight-bold">{{ \App\CentralLogics\Helpers::format_currency($balance) }}</td>
                            </tr>
                        @empty
                            <tr><td colspan="9" class="text-center py-4">{{ request('courier_id') ? translate('No unsettled parcel found') : translate('Select a courier first') }}</td></tr>
                        @endforelse
                        </tbody>
                    </table>
                </div>
                @if(method_exists($shipments, 'hasPages') && $shipments->hasPages())
                    <div class="card-footer">{!! $shipments->links() !!}</div>
                @endif
            </div>
        </form>
    </div>
@endsection
