@extends('layouts.app')
@section('title',$job->job_number)
@section('page_title','Transport Job '.$job->job_number)
@section('content')
<div class="page-head"><div><h1>{{ $job->job_number }}</h1><p>{{ $job->customer?->legal_name }} · {{ $job->pickup_location }} → {{ $job->delivery_location }}</p></div><div class="actions"><a class="btn btn-primary" href="{{ route('trips.create',['job_id'=>$job->id]) }}">+ Add Trip</a>@if(auth()->user()->hasPermission('transport_jobs.edit_draft'))<a class="btn btn-secondary" href="{{ route('jobs.edit',$job) }}">Edit Job</a>@endif<a class="btn btn-secondary" href="{{ route('jobs.index') }}">Back</a></div></div>
<div class="grid grid-4"><div class="card metric"><div class="metric-label">Actual Revenue</div><div class="metric-value">AED {{ number_format((float)$job->actual_revenue,2) }}</div></div><div class="card metric"><div class="metric-label">Actual Cost</div><div class="metric-value">AED {{ number_format((float)$job->actual_cost,2) }}</div></div><div class="card metric"><div class="metric-label">Gross Profit</div><div class="metric-value">AED {{ number_format((float)$job->profit,2) }}</div></div><div class="card metric"><div class="metric-label">Margin</div><div class="metric-value">{{ number_format((float)$job->profit_percentage,2) }}%</div></div></div>
<div class="card" style="margin-top:18px"><div class="card-body"><div class="detail-grid"><div class="detail"><div class="detail-label">Customer</div><div class="detail-value">{{ $job->customer?->legal_name }}</div></div><div class="detail"><div class="detail-label">Date / PO</div><div class="detail-value">{{ $job->job_date->format('d M Y') }} · {{ $job->customer_po ?: 'No PO' }}</div></div><div class="detail"><div class="detail-label">Status</div><div class="detail-value"><span class="badge status-{{ $job->status }}">{{ ucwords(str_replace('_',' ',$job->status)) }}</span></div></div><div class="detail"><div class="detail-label">Service</div><div class="detail-value">{{ $job->serviceType?->name ?? '—' }}</div></div><div class="detail"><div class="detail-label">Route</div><div class="detail-value">{{ $job->route?->name ?? 'Custom' }}</div></div><div class="detail"><div class="detail-label">Cargo</div><div class="detail-value">{{ $job->cargo_description ?: '—' }}</div></div></div>
<div class="divider"></div><form method="post" action="{{ route('jobs.status',$job) }}" class="actions">@csrf<select class="control" name="status" style="max-width:220px">@foreach(['enquiry','confirmed','scheduled','vehicle_assigned','driver_assigned','in_transit','reached_destination','delivered','pod_received','ready_to_invoice','invoiced','closed','cancelled'] as $s)<option value="{{ $s }}" @selected($job->status===$s)>{{ ucwords(str_replace('_',' ',$s)) }}</option>@endforeach</select><input type="hidden" name="delivery_status" value="{{ $job->delivery_status }}"><button class="btn btn-secondary">Update Status</button></form></div></div>
<div class="card" style="margin-top:18px"><div class="card-head"><strong>Trips</strong><a class="btn btn-primary btn-sm" href="{{ route('trips.create',['job_id'=>$job->id]) }}">+ Trip</a></div><div class="table-wrap"><table class="table"><thead><tr><th>Trip</th><th>Vehicle / Driver</th><th>Vendor</th><th>Pickup</th><th>Revenue</th><th>Cost</th><th>Status</th><th></th></tr></thead><tbody>@forelse($job->trips as $trip)<tr><td>{{ $trip->trip_number }}</td><td>{{ $trip->vehicle?->plate_number ?? '—' }}<div class="muted">{{ $trip->driver?->name }}</div></td><td>{{ $trip->vendor?->legal_name ?? 'Own vehicle' }}</td><td>{{ optional($trip->pickup_at)->format('d M Y H:i') }}</td><td>{{ number_format((float)$trip->customer_rate+(float)$trip->additional_revenue,2) }}</td><td>{{ number_format((float)$trip->vendor_rate+(float)$trip->additional_cost,2) }}</td><td><span class="badge status-{{ $trip->status }}">{{ ucwords(str_replace('_',' ',$trip->status)) }}</span></td><td><a class="btn btn-secondary btn-sm" href="{{ route('trips.show',$trip) }}">Open</a></td></tr>@empty<tr><td colspan="8" class="empty">No trips created.</td></tr>@endforelse</tbody></table></div></div>
<div class="grid grid-2" style="margin-top:18px"><div class="card"><div class="card-head"><strong>Additional Customer Charges</strong></div><div class="card-body"><form method="post" action="{{ route('jobs.charges.store',$job) }}" class="form-grid">@csrf<div class="field"><label>Type</label><input class="control" name="charge_type" placeholder="Salik, waiting, loading" required></div><div class="field"><label>Amount</label><input class="control" type="number" step="0.01" name="amount" required></div><div class="field full"><label>Description</label><input class="control" name="description"></div><label class="checkbox"><input type="checkbox" name="is_billable" value="1" checked> Billable</label><button class="btn btn-primary">Add Charge</button></form><div class="divider"></div>@foreach($job->charges as $line)<div class="total-row"><span>{{ $line->charge_type }} <small class="muted">{{ $line->description }}</small></span><span>AED {{ number_format((float)$line->amount,2) }} <form style="display:inline" method="post" action="{{ route('jobs.charges.destroy',[$job,$line]) }}">@csrf @method('delete')<button class="btn btn-danger btn-sm">×</button></form></span></div>@endforeach</div></div>
<div class="card"><div class="card-head"><strong>Additional Job Costs</strong></div><div class="card-body"><form method="post" action="{{ route('jobs.costs.store',$job) }}" class="form-grid">@csrf<div class="field"><label>Type</label><input class="control" name="cost_type" placeholder="Fuel, toll, loading" required></div><div class="field"><label>Amount</label><input class="control" type="number" step="0.01" name="amount" required></div><div class="field full"><label>Description</label><input class="control" name="description"></div><label class="checkbox"><input type="checkbox" name="is_accrued" value="1"> Accrued cost</label><button class="btn btn-primary">Add Cost</button></form><div class="divider"></div>@foreach($job->costs as $line)<div class="total-row"><span>{{ $line->cost_type }} <small class="muted">{{ $line->description }}</small></span><span>AED {{ number_format((float)$line->amount,2) }} <form style="display:inline" method="post" action="{{ route('jobs.costs.destroy',[$job,$line]) }}">@csrf @method('delete')<button class="btn btn-danger btn-sm">×</button></form></span></div>@endforeach</div></div></div>
<div class="card" style="margin-top:18px"><div class="card-head"><strong>Proof of Delivery</strong></div><div class="card-body"><form method="post" enctype="multipart/form-data" action="{{ route('jobs.pods.store',$job) }}" class="form-grid">@csrf<div class="field"><label>Trip</label><select class="control" name="trip_id"><option value="">Whole job</option>@foreach($job->trips as $trip)<option value="{{ $trip->id }}">{{ $trip->trip_number }}</option>@endforeach</select></div><div class="field"><label>Received Date</label><input class="control" type="date" name="received_date" value="{{ date('Y-m-d') }}"></div><div class="field"><label>Received By</label><input class="control" name="received_by"></div><div class="field"><label>Signed By</label><input class="control" name="signature_name"></div><div class="field"><label>POD File *</label><input class="control" type="file" name="file" accept=".pdf,.jpg,.jpeg,.png,.webp" required></div><div class="field"><label>Remarks</label><input class="control" name="remarks"></div><button class="btn btn-primary">Upload POD</button></form><div class="divider"></div><div class="actions">@forelse($job->pods as $pod)<a class="btn btn-secondary" href="{{ route('jobs.pods.download',[$job,$pod]) }}">POD {{ optional($pod->received_date)->format('d M Y') }}</a>@empty<span class="muted">No POD uploaded.</span>@endforelse</div></div></div>
@endsection
