<?php

use App\Enums\TravelStatus;
use App\Models\TravelRequest;
use Illuminate\Support\Facades\Auth;
use Livewire\Attributes\Layout;
use Livewire\Component;
use Livewire\WithPagination;

new #[Layout('components.layouts.app')] class extends Component {
    use WithPagination;

    public function render(): mixed
    {
        $user = Auth::user();
        $query = TravelRequest::query()->with(['user', 'approvals']);

        if (! $user->hasAnyRole(['travel_approver', 'administrator'])) {
            $query->where('user_id', $user->id);
        }

        return view('livewire.dashboard', [
            'requests' => $query->latest()->paginate(15),
            'statuses' => TravelStatus::cases(),
        ]);
    }
}
?>

<div>
    <div class="d-flex justify-content-between align-items-center mb-4">
        <h2 class="h4 mb-0">Travel Requests</h2>
        <a href="{{ route('travel-requests.create') }}" class="btn btn-primary">
            + New Request
        </a>
    </div>

    @if ($requests->isEmpty())
        <div class="card">
            <div class="card-body text-center py-5 text-muted">
                <p class="mb-0">No travel requests yet.</p>
                <a href="{{ route('travel-requests.create') }}" class="btn btn-primary mt-3">Submit Your First Request</a>
            </div>
        </div>
    @else
        <div class="card">
            <div class="table-responsive">
                <table class="table table-hover mb-0">
                    <thead class="table-light">
                        <tr>
                            <th>#</th>
                            <th>Applicant</th>
                            <th>Reason</th>
                            <th>Status</th>
                            <th>Submitted</th>
                            <th></th>
                        </tr>
                    </thead>
                    <tbody>
                        @foreach ($requests as $request)
                            <tr>
                                <td class="text-muted">{{ $request->id }}</td>
                                <td>{{ $request->user->name }}</td>
                                <td>{{ Str::limit($request->reason_summary, 60) }}</td>
                                <td>
                                    @php
                                        $badgeClass = match($request->status) {
                                            \App\Enums\TravelStatus::Draft => 'secondary',
                                            \App\Enums\TravelStatus::Pending => 'warning',
                                            \App\Enums\TravelStatus::Approved => 'success',
                                            \App\Enums\TravelStatus::Rejected => 'danger',
                                        };
                                    @endphp
                                    <span class="badge bg-{{ $badgeClass }}">{{ $request->status->value }}</span>
                                </td>
                                <td>{{ $request->submitted_at?->format('d M Y') ?? '—' }}</td>
                                <td>
                                    <a href="{{ route('travel-requests.show', $request) }}" class="btn btn-sm btn-outline-secondary">View</a>
                                    @if($request->status === \App\Enums\TravelStatus::Pending && auth()->user()->hasAnyRole(['travel_approver', 'administrator']))
                                        <a href="{{ route('travel-requests.approve', $request) }}" class="btn btn-sm btn-outline-primary ms-1">Review</a>
                                    @endif
                                </td>
                            </tr>
                        @endforeach
                    </tbody>
                </table>
            </div>
        </div>

        <div class="mt-3">
            {{ $requests->links() }}
        </div>
    @endif
</div>
