@extends('layouts.employee')

@section('content')

@if(session('error'))

<div style="
    background:#f8d7da;
    color:#721c24;
    padding:12px;
    border-radius:8px;
    margin-bottom:15px;
">

    {{ session('error') }}

</div>

@endif

<h2 style="margin-bottom:25px;">Request saya</h2>

@php
$pending = $requests->where('status', 'pending');

$active = $requests->where('status', 'approved');

$history = $requests->filter(function($item){
    return in_array($item->status, [
        'rejected',
        'returned',
        'verification_pending'
    ]);
});
@endphp

<!-- Pending -->
<div class="card" style="margin-bottom:25px;">
<h3 style="margin-bottom:15px;">Menunggu Persetujuan</h3>

<table width="100%" cellpadding="10">
<tr style="background:#f1f5f9;">
<th align="left">Kendaraan</th>
<th align="left">Asal</th>
<th align="left">Tujuan</th>
<th align="left">Mulai Perjalanan</th>
<th align="left">Akhir Perjalanan</th>
<th align="left">Tujuan Penggunaan</th>
</tr>

@forelse($pending as $item)
<tr style="border-top:1px solid #e5e7eb;">
<td>{{ optional($item->vehicle)->vehicle_name ?? 'Vehicle Deleted' }}</td>
<td>{{ $item->origin ?? '-' }}</td>
<td>{{ $item->destination ?? '-' }}</td>
<td>{{ $item->start_date }}</td>
<td>{{ $item->end_date }}</td>
<td>{{ $item->purpose }}</td>
</tr>
@empty
<tr>
<td colspan="6" style="padding:15px; color:#64748b;">Tidak ada request menunggu</td>
</tr>
@endforelse
</table>
</div>

                                                                                                                
<!-- Active -->
<div class="card" style="margin-bottom:25px;">
<h3 style="margin-bottom:15px;">Penggunaan aktif</h3>

<table width="100%" cellpadding="10">
<tr style="background:#f1f5f9;">
<th align="left">Kendaraan</th>
<th align="left">Asal</th>
<th align="left">Tujuan</th>
<th align="left">Awal Penggunaan</th>
<th align="left">Akhir Penggunaan</th>
<th align="left">Status</th>
<th align="left">Tindakan</th>

</tr>

@forelse($active as $item)
<tr style="border-top:1px solid #e5e7eb;">
<td>{{ optional($item->vehicle)->vehicle_name ?? 'Vehicle Deleted' }}</td>
<td>{{ $item->origin ?? '-' }}</td>
<td>{{ $item->destination ?? '-' }}</td>
<td>{{ $item->start_date }}</td>
<td>{{ $item->end_date }}</td>
<td style="color:#16a34a; font-weight:600;">
    Disetujui
</td>

<td>

    <a href="{{ route('returns.create', $item->id) }}"
    style="
    background:#f59e0b;
    color:white;
    padding:8px 14px;
    border-radius:8px;
    text-decoration:none;
    font-size:14px;
    font-weight:600;
    ">

    Kembalikan Kendaraan

    </a>

</td>
</tr>
@empty
<tr>
<td colspan="7" style="padding:15px; color:#64748b;"></td>
</tr>
@endforelse
</table>
</div>


<!-- History -->
<div class="card">
<h3 style="margin-bottom:15px;">History</h3>

<table width="100%" cellpadding="10">
<tr style="background:#f1f5f9;">
<th align="left">Kendaraan</th>
<th align="left">Asal</th>
<th align="left">Tujuan</th>
<th align="left">Tujuan Penggunaan</th>
<th align="left">Status</th>
</tr>

@forelse($history as $item)

<tr style="border-top:1px solid #e5e7eb;">
<td>{{ optional($item->vehicle)->vehicle_name ?? 'Vehicle Deleted' }}</td>
<td>{{ $item->origin ?? '-' }}</td>
<td>{{ $item->destination ?? '-' }}</td>
<td>{{ $item->purpose }}</td>

<td>
@if($item->status == 'rejected')
<span style="color:#dc2626; font-weight:600;">Ditolak</span>
@elseif($item->status == 'returned' || $item->status == 'verification_pending')
<span style="color:#2563eb; font-weight:600;">Dikembalikan</span>
@endif
</td>

</tr>

@empty
<tr>
<td colspan="5" style="padding:15px; color:#64748b;">Belum ada History </td>
</tr>
@endforelse
</table>
</div>

@endsection