@php
    $docTypes = ['mot' => 'MOT', 'insurance' => 'Insurance', 'phv_badge' => 'PHV Plate/Badge', 'log_book' => 'Log Book (V5C)', 'maintenance' => 'Maintenance/Service Due'];
    $statusBadge = ['pending' => 'warning', 'active' => 'success', 'rejected' => 'danger'];
@endphp
<div class="card">
    <div class="card-body">
        <h5 class="fw-bold mb-4">Documents</h5>

        <div class="table-responsive mb-4">
            <table class="table align-middle datatable">
                <thead>
                    <tr>
                        <th>Type</th>
                        <th>Number</th>
                        <th>Expiry</th>
                        <th>Status</th>
                        <th class="text-end">Actions</th>
                    </tr>
                </thead>
                <tbody>
                    @forelse ($vehicle->documents as $document)
                        <tr>
                            <td>{{ $docTypes[$document->type] ?? $document->type }}</td>
                            <td>{{ $document->document_number ?: '—' }}</td>
                            <td>
                                {{ optional($document->expiry_date)->format('d M Y') ?: '—' }}
                                @if ($document->isExpired())
                                    <span class="badge bg-danger ms-1">Expired</span>
                                @endif
                            </td>
                            <td><span class="badge bg-{{ $statusBadge[$document->status] ?? 'secondary' }} text-uppercase">{{ $document->status }}</span></td>
                            <td class="text-end">
                                <div class="d-flex justify-content-end gap-1 flex-wrap">
                                    @if ($document->file_path)
                                        <a href="{{ route('admin.vehicles.documents.download', [$vehicle, $document]) }}" class="btn btn-sm btn-outline-secondary">
                                            <i class="bi bi-download"></i>
                                        </a>
                                    @endif
                                    @if ($document->status !== 'active')
                                        <form method="POST" action="{{ route('admin.vehicles.documents.update-status', [$vehicle, $document]) }}">
                                            @csrf @method('PATCH')
                                            <input type="hidden" name="status" value="active">
                                            <button type="submit" class="btn btn-sm btn-outline-success">Approve</button>
                                        </form>
                                    @endif
                                    @if ($document->status !== 'rejected')
                                        <form method="POST" action="{{ route('admin.vehicles.documents.update-status', [$vehicle, $document]) }}">
                                            @csrf @method('PATCH')
                                            <input type="hidden" name="status" value="rejected">
                                            <button type="submit" class="btn btn-sm btn-outline-danger">Reject</button>
                                        </form>
                                    @endif
                                    <form method="POST" action="{{ route('admin.vehicles.documents.destroy', [$vehicle, $document]) }}"
                                          data-confirm-title="Remove document?" data-confirm-text="Remove this document?" data-confirm-button="Remove">
                                        @csrf @method('DELETE')
                                        <button type="submit" class="btn btn-sm btn-outline-secondary"><i class="bi bi-trash"></i></button>
                                    </form>
                                </div>
                            </td>
                        </tr>
                    @empty
                        <tr><td colspan="5" class="text-center text-muted py-3">No documents uploaded yet.</td></tr>
                    @endforelse
                </tbody>
            </table>
        </div>

        <form method="POST" action="{{ route('admin.vehicles.documents.store', $vehicle) }}" enctype="multipart/form-data" class="row g-2 align-items-end">
            @csrf
            <div class="col-md-3">
                <label class="form-label fw-semibold small">Type</label>
                <select name="type" class="form-select" required>
                    @foreach ($docTypes as $value => $label)
                        <option value="{{ $value }}">{{ $label }}</option>
                    @endforeach
                </select>
            </div>
            <div class="col-md-3">
                <label class="form-label fw-semibold small">Document Number</label>
                <input type="text" name="document_number" class="form-control">
            </div>
            <div class="col-md-2">
                <label class="form-label fw-semibold small">Expiry Date</label>
                <input type="date" name="expiry_date" class="form-control">
            </div>
            <div class="col-md-2">
                <label class="form-label fw-semibold small">File</label>
                <input type="file" name="file" class="form-control" accept=".jpg,.jpeg,.png,.pdf" required>
            </div>
            <div class="col-md-2">
                <button type="submit" class="btn btn-dark w-100">Upload</button>
            </div>
        </form>
    </div>
</div>
