@extends('layouts.app')
@section('title','Payment History')
@section('content')
<main class="nxl-container"><div class="nxl-content"><div class="page-header d-flex justify-content-between align-items-center">
 <div><h5 class="mb-1">Payment History</h5><div class="text-muted">{{ $document->document_number }} · {{ $document->currency }} {{ number_format($document->total,2) }}</div></div>
 <a href="{{ route('workflow.index','trade-documents') }}" class="btn btn-outline-secondary">Back to Trade Documents</a>
</div><div class="main-content">
@if(session('success'))<div class="alert alert-success">{{ session('success') }}</div>@endif
@if($errors->any())<div class="alert alert-danger">{{ $errors->first() }}</div>@endif
<div class="row g-3 mb-4">
 <div class="col-md-4"><div class="card"><div class="card-body"><small>Total</small><h4>{{ $document->currency }} {{ number_format($document->total,2) }}</h4></div></div></div>
 <div class="col-md-4"><div class="card"><div class="card-body"><small>Net Paid</small><h4>{{ $document->currency }} {{ number_format($document->paid_amount,2) }}</h4></div></div></div>
 <div class="col-md-4"><div class="card"><div class="card-body"><small>Outstanding</small><h4>{{ $document->currency }} {{ number_format(max(0,$document->total-$document->paid_amount),2) }}</h4></div></div></div>
</div>
<div class="card"><div class="card-body table-responsive"><table class="table align-middle"><thead><tr><th>Date</th><th>Type</th><th>Status</th><th>Reference</th><th>Method</th><th>Reconciliation</th><th>Amount</th><th>Employee</th><th>Audit/Actions</th></tr></thead><tbody>
@forelse($document->payments as $payment)<tr>
 <td>{{ $payment->paid_at?->format('Y-m-d H:i') }}</td><td>{{ ucfirst($payment->payment_type) }}</td><td>{{ ucfirst($payment->status) }}</td>
  <td>{{ $payment->reference }}</td><td>{{ $payment->method }}</td><td>{{ ucfirst($payment->reconciliation_status ?? 'unreconciled') }} @if($payment->bank_reference)<small class="d-block">{{ $payment->bank_reference }}</small>@endif</td>
 <td class="{{ in_array($payment->payment_type,['refund','reversal'])?'text-danger':'text-success' }}">{{ in_array($payment->payment_type,['refund','reversal'])?'-':'+' }}{{ number_format($payment->amount,2) }}</td>
 <td>{{ $payment->recorder?->name }}</td><td>
  @if($payment->corrected_at)<small class="d-block">Corrected {{ $payment->corrected_at }} by {{ $payment->corrector?->name }}: {{ $payment->correction_reason }}</small>@endif
  @if($payment->voided_at)<small class="d-block">Voided {{ $payment->voided_at }} by {{ $payment->voider?->name }}: {{ $payment->void_reason }}</small>@endif
  @if($payment->parentPayment)<small class="d-block">Related payment: #{{ $payment->parent_payment_id }}</small>@endif
  @if($payment->receipt_number)<a class="btn btn-sm btn-outline-success mb-1" target="_blank" href="{{ route('trade-documents.payments.receipt',[$document,$payment]) }}">Receipt</a>@endif
  @if($payment->payment_type==='payment'&&$payment->status==='posted')
   @if(($payment->reconciliation_status ?? 'unreconciled')==='unreconciled')<form method="POST" action="{{ route('trade-documents.payments.reconcile',[$document,$payment]) }}" class="d-flex gap-1 mb-2">@csrf<select name="reconciliation_status" class="form-select form-select-sm"><option value="reconciled">Reconciled</option><option value="rejected">Rejected</option></select><input name="bank_reference" class="form-control form-control-sm" placeholder="Bank reference"><input name="note" class="form-control form-control-sm" placeholder="Reconciliation note"><button class="btn btn-sm btn-outline-success">Confirm</button></form>@endif
   <details><summary class="btn btn-sm btn-outline-primary">Manage</summary><div class="border rounded p-2 mt-2" style="min-width:360px">
    <form method="POST" action="{{ route('trade-documents.payments.correct',[$document,$payment]) }}" class="row g-1 mb-2">@csrf @method('PUT')
     <div class="col"><input name="amount" type="number" step=".01" value="{{ $payment->amount }}" class="form-control form-control-sm" required></div><div class="col"><input name="reference" value="{{ $payment->reference }}" class="form-control form-control-sm" required></div><div class="col"><input name="method" value="{{ $payment->method }}" class="form-control form-control-sm"></div><input type="hidden" name="paid_at" value="{{ $payment->paid_at?->format('Y-m-d H:i:s') }}"><div class="col-12"><input name="reason" class="form-control form-control-sm" placeholder="Correction reason" required></div><div class="col-12"><button class="btn btn-sm btn-primary">Correct</button></div>
    </form>
    <form method="POST" action="{{ route('trade-documents.payments.refund',[$document,$payment]) }}" class="d-flex gap-1 mb-2">@csrf<input name="amount" type="number" step=".01" class="form-control form-control-sm" placeholder="Refund" required><input name="reference" class="form-control form-control-sm" placeholder="Refund reference" required><input name="method" value="{{ $payment->method }}" type="hidden"><input name="paid_at" value="{{ now()->format('Y-m-d H:i:s') }}" type="hidden"><button class="btn btn-sm btn-warning">Refund</button></form>
    <div class="d-flex gap-1"><form method="POST" action="{{ route('trade-documents.payments.reverse',[$document,$payment]) }}" class="d-flex gap-1">@csrf<input name="reason" class="form-control form-control-sm" placeholder="Reversal reason" required><button class="btn btn-sm btn-danger">Reverse</button></form><form method="POST" action="{{ route('trade-documents.payments.void',[$document,$payment]) }}" class="d-flex gap-1">@csrf<input name="reason" class="form-control form-control-sm" placeholder="Void reason" required><button class="btn btn-sm btn-dark">Void</button></form></div>
   </div></details>
  @endif
 </td>
</tr>@empty<tr><td colspan="9" class="text-center text-muted">No payment history.</td></tr>@endforelse
</tbody></table></div></div>
</div></div></main>
@endsection
