@extends('layouts.app')
@section('title',$settlement->settlement_number)
@section('page_title','Driver Settlement')
@section('content')
@php($status=$settlement->status instanceof \App\Enums\DocumentStatus?$settlement->status->value:$settlement->status)
<div class="page-head"><div><h1>{{ $settlement->settlement_number }}</h1><p>{{ $advance->advance_number }} · {{ $advance->driver?->name }} · <span class="badge status-{{ $status }}">{{ ucwords(str_replace('_',' ',$status)) }}</span></p></div><div class="actions"><a class="btn btn-secondary" href="{{ route('driver-advances.show',$advance) }}">Back to Advance</a>@if(in_array($status,['draft','rejected']))<a class="btn btn-primary" href="{{ route('driver-advances.settlements.edit',[$advance,$settlement]) }}">Edit</a>@endif</div></div>
<div class="grid grid-3"><div class="card metric"><div class="metric-label">Expenses</div><div class="metric-value">AED {{ number_format((float)$settlement->expense_amount,2) }}</div></div><div class="card metric"><div class="metric-label">Cash Refund</div><div class="metric-value">AED {{ number_format((float)$settlement->cash_refund_amount,2) }}</div></div><div class="card metric"><div class="metric-label">Total Settled</div><div class="metric-value">AED {{ number_format((float)$settlement->total_settled,2) }}</div></div></div>
<div class="card" style="margin-top:18px"><div class="detail-grid" style="border:0"><div class="detail"><div class="detail-label">Date</div><div class="detail-value">{{ $settlement->settlement_date->format('d M Y') }}</div></div><div class="detail"><div class="detail-label">Expense Account</div><div class="detail-value">{{ $settlement->expenseAccount?->code }} - {{ $settlement->expenseAccount?->name }}</div></div><div class="detail"><div class="detail-label">Refund Account</div><div class="detail-value">{{ $settlement->cashBankAccount?->code }} - {{ $settlement->cashBankAccount?->name }}</div></div><div class="detail full"><div class="detail-label">Description</div><div class="detail-value">{{ $settlement->description ?: '—' }}</div></div></div></div>
<div class="card" style="margin-top:18px"><div class="card-head"><strong>Workflow</strong></div><div class="card-body"><div class="actions">
@if(in_array($status,['draft','rejected'])&&auth()->user()->hasPermission('driver_advances.submit'))<form method="post" action="{{ route('driver-advances.settlements.submit',[$advance,$settlement]) }}">@csrf<button class="btn btn-warning">Submit for Approval</button></form>@endif
@if($status==='pending_approval'&&auth()->user()->hasPermission('driver_advances.approve'))<form method="post" action="{{ route('driver-advances.settlements.approve',[$advance,$settlement]) }}">@csrf<button class="btn btn-success">Approve</button></form>@endif
@if($status==='pending_approval'&&auth()->user()->hasPermission('driver_advances.reject'))<form method="post" action="{{ route('driver-advances.settlements.reject',[$advance,$settlement]) }}" style="display:flex;gap:8px"><input class="control" name="reason" placeholder="Rejection reason" required><button class="btn btn-danger">Reject</button></form>@endif
@if($status==='approved'&&auth()->user()->hasPermission('driver_advances.post'))<form method="post" action="{{ route('driver-advances.settlements.post',[$advance,$settlement]) }}">@csrf<button class="btn btn-primary" data-confirm="Post this driver settlement to the ledger?">Post Settlement</button></form>@endif
@if(in_array($status,['draft','rejected'])&&auth()->user()->hasPermission('driver_advances.delete_draft'))<form method="post" action="{{ route('driver-advances.settlements.destroy',[$advance,$settlement]) }}">@csrf @method('delete')<button class="btn btn-danger" data-confirm="Delete this draft settlement?">Delete Draft</button></form>@endif
</div>@if($settlement->journal)<div class="divider"></div><strong>Posted Journal: {{ $settlement->journal->journal_number }}</strong><div class="table-wrap"><table class="table"><thead><tr><th>Account</th><th>Description</th><th class="text-right">Debit</th><th class="text-right">Credit</th></tr></thead><tbody>@foreach($settlement->journal->lines as $line)<tr><td>{{ $line->account->code }} - {{ $line->account->name }}</td><td>{{ $line->description }}</td><td class="text-right">{{ number_format((float)$line->debit,2) }}</td><td class="text-right">{{ number_format((float)$line->credit,2) }}</td></tr>@endforeach</tbody></table></div>@endif</div></div>
@endsection
