@extends('layouts.app')
@section('title',$advance->advance_number)
@section('page_title','Driver Advance')
@section('content')
@php($status=$advance->status instanceof \App\Enums\DocumentStatus?$advance->status->value:$advance->status)
@php($open=\App\Support\Decimal::subtract((string)$advance->amount,(string)$advance->settled_amount))
<div class="page-head"><div><h1>{{ $advance->advance_number }}</h1><p>{{ $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.index') }}">Back</a>@if(in_array($status,['draft','rejected']))<a class="btn btn-primary" href="{{ route('driver-advances.edit',$advance) }}">Edit</a>@endif @if($status==='posted' && \App\Support\Decimal::compare($open,'0')>0 && auth()->user()->hasPermission('driver_advances.create'))<a class="btn btn-success" href="{{ route('driver-advances.settlements.create',$advance) }}">New Settlement</a>@endif</div></div>
<div class="grid grid-4"><div class="card metric"><div class="metric-label">Advance Amount</div><div class="metric-value">AED {{ number_format((float)$advance->amount,2) }}</div></div><div class="card metric"><div class="metric-label">Settled</div><div class="metric-value">AED {{ number_format((float)$advance->settled_amount,2) }}</div></div><div class="card metric"><div class="metric-label">Open Balance</div><div class="metric-value">AED {{ number_format((float)$open,2) }}</div></div><div class="card metric"><div class="metric-label">Settlement Progress</div><div class="metric-value">{{ \App\Support\Decimal::compare((string)$advance->amount,'0')>0?number_format(((float)$advance->settled_amount/(float)$advance->amount)*100,1):'0.0' }}%</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">{{ $advance->advance_date->format('d M Y') }}</div></div><div class="detail"><div class="detail-label">Type</div><div class="detail-value">{{ ucwords(str_replace('_',' ',$advance->advance_type)) }}</div></div><div class="detail"><div class="detail-label">Branch</div><div class="detail-value">{{ $advance->branch?->name }}</div></div><div class="detail"><div class="detail-label">Trip</div><div class="detail-value">{{ $advance->trip?->trip_number ?? '—' }}</div></div><div class="detail"><div class="detail-label">Advance Account</div><div class="detail-value">{{ $advance->advanceAccount?->code }} - {{ $advance->advanceAccount?->name }}</div></div><div class="detail"><div class="detail-label">Payment Account</div><div class="detail-value">{{ $advance->cashBankAccount?->code }} - {{ $advance->cashBankAccount?->name }}</div></div><div class="detail full"><div class="detail-label">Description</div><div class="detail-value">{{ $advance->description ?: '—' }}</div></div></div></div>
@if($advance->settlements->isNotEmpty())<div class="card" style="margin-top:18px"><div class="card-head"><strong>Settlements</strong></div><div class="table-wrap"><table class="table"><thead><tr><th>Settlement</th><th>Date</th><th class="text-right">Expenses</th><th class="text-right">Cash Refund</th><th class="text-right">Total</th><th>Status</th></tr></thead><tbody>@foreach($advance->settlements as $settlement)@php($ss=$settlement->status instanceof \App\Enums\DocumentStatus?$settlement->status->value:$settlement->status)<tr><td><a href="{{ route('driver-advances.settlements.show',[$advance,$settlement]) }}">{{ $settlement->settlement_number }}</a></td><td>{{ $settlement->settlement_date->format('d M Y') }}</td><td class="text-right">{{ number_format((float)$settlement->expense_amount,2) }}</td><td class="text-right">{{ number_format((float)$settlement->cash_refund_amount,2) }}</td><td class="text-right"><strong>{{ number_format((float)$settlement->total_settled,2) }}</strong></td><td><span class="badge status-{{ $ss }}">{{ ucwords(str_replace('_',' ',$ss)) }}</span></td></tr>@endforeach</tbody></table></div></div>@endif
@include('components.workflow-actions',['document'=>$advance,'routePrefix'=>'driver-advances','permissionPrefix'=>'driver_advances','status'=>$status,'postLabel'=>'Post Advance'])
@endsection
