@extends('layouts.admin.app')

@section('title', translate('Courier Collection Ledger'))

@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>{{ translate('Courier Collection Ledger') }}</span>
            </h1>
            <div class="page-header-right">
                <a class="btn btn-outline-primary" href="{{ route('admin.courier.export', ['ledger', 'csv'] + request()->query()) }}">
                    <i class="tio-download-to"></i> {{ translate('Export CSV') }}
                </a>
            </div>
        </div>

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

        <div class="row g-3 mb-3">
            <div class="col-md-4">
                <div class="card"><div class="card-body"><div class="text-muted fs-12">{{ translate('Debit') }}</div><h3>{{ \App\CentralLogics\Helpers::format_currency($summary['debit'] ?? 0) }}</h3></div></div>
            </div>
            <div class="col-md-4">
                <div class="card"><div class="card-body"><div class="text-muted fs-12">{{ translate('Credit') }}</div><h3>{{ \App\CentralLogics\Helpers::format_currency($summary['credit'] ?? 0) }}</h3></div></div>
            </div>
            <div class="col-md-4">
                <div class="card"><div class="card-body"><div class="text-muted fs-12">{{ translate('Closing Balance') }}</div><h3>{{ \App\CentralLogics\Helpers::format_currency($summary['balance'] ?? 0) }}</h3></div></div>
            </div>
        </div>

        <div class="card mb-3">
            <div class="card-body">
                <div class="row g-3">
                    <div class="col-md-2">
                        <div class="text-muted fs-12">{{ translate('Opening Balance') }}</div>
                        <strong>{{ \App\CentralLogics\Helpers::format_currency($summary['opening_balance'] ?? 0) }}</strong>
                    </div>
                    <div class="col-md-2">
                        <div class="text-muted fs-12">{{ translate('Delivered COD Collection') }}</div>
                        <strong class="text-success">+ {{ \App\CentralLogics\Helpers::format_currency($summary['cod_collection'] ?? 0) }}</strong>
                    </div>
                    <div class="col-md-2">
                        <div class="text-muted fs-12">{{ translate('Courier Charge') }}</div>
                        <strong class="text-danger">- {{ \App\CentralLogics\Helpers::format_currency($summary['delivery_charge'] ?? 0) }}</strong>
                    </div>
                    <div class="col-md-2">
                        <div class="text-muted fs-12">{{ translate('COD Charge') }}</div>
                        <strong class="text-danger">- {{ \App\CentralLogics\Helpers::format_currency($summary['cod_charge'] ?? 0) }}</strong>
                    </div>
                    <div class="col-md-2">
                        <div class="text-muted fs-12">{{ translate('Return Charge') }}</div>
                        <strong class="text-danger">- {{ \App\CentralLogics\Helpers::format_currency($summary['return_charge'] ?? 0) }}</strong>
                    </div>
                    <div class="col-md-2">
                        <div class="text-muted fs-12">{{ translate('Payment Received') }}</div>
                        <strong class="text-danger">- {{ \App\CentralLogics\Helpers::format_currency($summary['payment_received'] ?? 0) }}</strong>
                    </div>
                </div>
            </div>
        </div>

        <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-2">
                            <select name="event_type" class="form-control">
                                <option value="all">{{ translate('All Events') }}</option>
                                @foreach($eventTypes as $eventType)
                                    <option value="{{ $eventType }}" {{ request('event_type') === $eventType ? 'selected' : '' }}>{{ translate(str_replace('_', ' ', $eventType)) }}</option>
                                @endforeach
                            </select>
                        </div>
                        <div class="col-md-2"><input type="text" name="parcel_id" value="{{ request('parcel_id') }}" class="form-control" placeholder="{{ translate('Parcel ID') }}"></div>
                        <div class="col-md-2"><input type="text" name="invoice_no" value="{{ request('invoice_no') }}" class="form-control" placeholder="{{ translate('Invoice No') }}"></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-2"><button class="btn btn--primary btn-block">{{ translate('Filter') }}</button></div>
                        <div class="col-md-2"><a class="btn btn--reset btn-block" href="{{ route('admin.courier.ledger') }}">{{ translate('Reset') }}</a></div>
                    </div>
                </form>
            </div>
        </div>

        <div class="card mb-3">
            <div class="card-body d-flex justify-content-between align-items-center flex-wrap">
                <div>
                    <strong>{{ translate('Legacy Backfill') }}</strong>
                    <div class="fs-12 text-muted">{{ translate('Creates missing courier ledger entries from existing shipments and syncs opening balance. Duplicate-safe.') }}</div>
                </div>
                <form method="post" action="{{ route('admin.courier.ledger.backfill') }}">
                    @csrf
                    <input type="hidden" name="courier_id" value="{{ request('courier_id') }}">
                    <button class="btn btn-outline-primary"><i class="tio-sync"></i> {{ translate('Backfill Ledger') }}</button>
                </form>
            </div>
        </div>

        <div class="card mb-3">
            <div class="card-header border-0"><h4 class="card-title">{{ translate('Manual Adjustment') }}</h4></div>
            <div class="card-body">
                <form method="post" action="{{ route('admin.courier.ledger.adjustment') }}">
                    @csrf
                    <div class="row g-3">
                        <div class="col-md-3">
                            <select name="courier_id" class="form-control" required>
                                <option value="">{{ translate('Select Courier') }}</option>
                                @foreach($couriers as $courier)
                                    <option value="{{ $courier->id }}">{{ $courier->name }}</option>
                                @endforeach
                            </select>
                        </div>
                        <div class="col-md-2">
                            <select name="entry_side" class="form-control" required>
                                <option value="debit">{{ translate('Debit') }}</option>
                                <option value="credit">{{ translate('Credit') }}</option>
                            </select>
                        </div>
                        <div class="col-md-2">
                            <input type="number" step="0.01" min="0.01" name="amount" class="form-control" placeholder="{{ translate('Amount') }}" required>
                        </div>
                        <div class="col-md-4">
                            <input type="text" name="particulars" class="form-control" placeholder="{{ translate('Particulars / note') }}" required>
                        </div>
                        <div class="col-md-1">
                            <button class="btn btn--primary btn-block">{{ translate('Post') }}</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('Order / Parcel') }}</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>
                    @forelse($entries as $entry)
                        <tr>
                            <td>{{ $entry->entry_date?->format('d M Y') }}</td>
                            <td>{{ $entry->voucher_no }}</td>
                            <td>{{ $entry->courier?->name }}</td>
                            <td>
                                @if($entry->order_id)
                                    <a href="{{ route('admin.order.details', $entry->order_id) }}">#{{ $entry->order_id }}</a>
                                @else
                                    {{ translate('N/A') }}
                                @endif
                                <div class="fs-12 text-muted text-break">{{ $entry->parcel_id ?? translate('N/A') }}</div>
                            </td>
                            <td>
                                <div>{{ $entry->particulars }}</div>
                                <div class="fs-12 text-muted">{{ translate(str_replace('_', ' ', $entry->event_type)) }}</div>
                            </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 font-weight-bold">{{ \App\CentralLogics\Helpers::format_currency($entry->balance) }}</td>
                            <td><span class="badge {{ $entry->reversed_at ? 'badge-soft-danger' : 'badge-soft-success' }}">{{ $entry->reversed_at ? translate('Reversed') : translate($entry->status) }}</span></td>
                        </tr>
                    @empty
                        <tr><td colspan="9" class="text-center py-4">{{ translate('No courier ledger entry found') }}</td></tr>
                    @endforelse
                    </tbody>
                </table>
            </div>
            @if($entries->hasPages())
                <div class="card-footer">{!! $entries->links() !!}</div>
            @endif
        </div>
    </div>
@endsection
