@extends('admin.layout') 

@section('title', 'Booking - ' . strtoupper(str_replace('_', ' ', $room)))

@section('content')
    <div class="d-flex justify-content-between align-items-center mb-4">
        <h1 class="h4 fw-bold mb-0">
            <i class="bi bi-calendar-event"></i>
            Booking - {{ strtoupper(str_replace('_', ' ', $room)) }}
        </h1>
    </div>

    {{-- Filter Buttons --}}
    <div class="mb-3 d-flex gap-2 flex-wrap">
        <a href="{{ url('/booking-admin/' . $room . '/bookings') }}" 
           class="btn btn-sm {{ request('filter') ? 'btn-outline-secondary' : 'btn-success' }}">
           Semua
        </a>
        <a href="{{ url('/booking-admin/' . $room . '/bookings?filter=hari-ini') }}" 
           class="btn btn-sm {{ request('filter') === 'hari-ini' ? 'btn-success' : 'btn-outline-secondary' }}">
           Hari Ini
        </a>
        <a href="{{ url('/booking-admin/' . $room . '/bookings?filter=akan-datang') }}" 
           class="btn btn-sm {{ request('filter') === 'akan-datang' ? 'btn-success' : 'btn-outline-secondary' }}">
           Akan Datang
        </a>
        <a href="{{ url('/booking-admin/' . $room . '/bookings?filter=selesai') }}" 
           class="btn btn-sm {{ request('filter') === 'selesai' ? 'btn-success' : 'btn-outline-secondary' }}">
           Selesai
        </a>
    </div>

    {{-- Filter Export --}}
    <div class="card shadow-sm mb-4">
        <div class="card-body d-flex flex-wrap align-items-end gap-3">
            <form action="{{ url('/booking-admin/' . $room . '/bookings') }}" method="GET" class="d-flex flex-wrap gap-2">
                <div>
                    <label class="form-label fw-semibold mb-1">Rentang Tanggal</label>
                    <div class="d-flex gap-2">
                        <input type="date" name="start_date" class="form-control form-control-sm"
                               value="{{ request('start_date') }}">
                        <input type="date" name="end_date" class="form-control form-control-sm"
                               value="{{ request('end_date') }}">
                    </div>
                </div>

                <div>
                    <label class="form-label fw-semibold mb-1">Filter Cepat</label>
                    <select name="range" class="form-select form-select-sm">
                        <option value="">-- Pilih --</option>
                        <option value="today" {{ request('range') == 'today' ? 'selected' : '' }}>Hari Ini</option>
                        <option value="week" {{ request('range') == 'week' ? 'selected' : '' }}>Minggu Ini</option>
                        <option value="month" {{ request('range') == 'month' ? 'selected' : '' }}>Bulan Ini</option>
                    </select>
                </div>

                <div class="d-flex gap-2 align-items-end">
                    <button type="submit" class="btn btn-sm btn-success">
                        <i class="bi bi-funnel"></i> Tampilkan
                    </button>

                    {{-- tombol export --}}
                    <a href="{{ url('/booking-admin/' . $room . '/bookings/export?start_date=' . request('start_date') . '&end_date=' . request('end_date') . '&range=' . request('range')) }}"
                        class="btn btn-sm btn-outline-primary">
                        <i class="bi bi-file-earmark-excel"></i> Export Excel
                    </a>
                </div>
            </form>
        </div>
    </div>

    {{-- Alert sukses --}}
    @if(session('success'))
        <div class="alert alert-success alert-dismissible fade show shadow-sm" role="alert">
            <i class="bi bi-check-circle-fill"></i> {{ session('success') }}
            <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
        </div>
    @endif

    {{-- Alert error --}}
    @if(session('error'))
        <div class="alert alert-danger alert-dismissible fade show shadow-sm" role="alert">
            <i class="bi bi-exclamation-triangle-fill"></i> {{ session('error') }}
            <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
        </div>
    @endif

    <div class="table-responsive shadow-sm rounded">
        <table class="table table-hover align-middle mb-0">
            <thead class="table-success text-dark">
                <tr>
                    <th>PIC</th>
                    <th>Tanggal</th>
                    <th>Jam</th>
                    <th>Departemen</th>
                    <th>Keterangan</th>
                    <th class="text-center">Aksi</th>
                </tr>
            </thead>
            <tbody>
                @forelse($bookings as $booking)
                    @php
                        $today = \Carbon\Carbon::today();
                        $date = \Carbon\Carbon::parse($booking->date);
                        $rowClass = '';
                        $statusBadge = '';

                        if ($date->isToday()) {
                            $rowClass = 'table-success';
                            $statusBadge = '<span class="badge bg-success">Hari ini</span>';
                        } elseif ($date->isPast()) {
                            $rowClass = 'table-secondary';
                            $statusBadge = '<span class="badge bg-secondary">Selesai</span>';
                        } else {
                            $statusBadge = '<span class="badge bg-info text-dark">Akan datang</span>';
                        }
                    @endphp

                    <tr class="{{ $rowClass }}">
                        <td>{{ $booking->pic }}</td>
                        <td>
                            <i class="bi bi-calendar-date text-success"></i>
                            {{ $date->format('d M Y') }}
                            {!! $statusBadge !!}
                        </td>
                        <td>
                            <span class="badge bg-primary">
                                {{ $booking->start_time }} - {{ $booking->end_time }}
                            </span>
                        </td>
                        <td>{{ $booking->department ?? '-' }}</td>
                        <td>{{ $booking->keterangan ?? '-' }}</td>
                        <td class="text-center">
                            <div class="d-flex justify-content-center gap-2">
                                <a href="{{ url('/booking-admin/' . $room . '/bookings/' . $booking->id . '/edit') }}"
                                   class="btn btn-sm btn-outline-primary">
                                   <i class="bi bi-pencil-square"></i> Edit
                                </a>
                                <form action="{{ url('/booking-admin/' . $room . '/bookings/' . $booking->id) }}"
                                      method="POST" onsubmit="return confirm('Yakin ingin hapus booking ini?')">
                                    @csrf
                                    @method('DELETE')
                                    <button type="submit" class="btn btn-sm btn-outline-danger">
                                        <i class="bi bi-trash"></i> Hapus
                                    </button>
                                </form>
                            </div>
                        </td>
                    </tr>
                @empty
                    <tr>
                        <td colspan="6" class="text-center text-muted py-4">
                            <i class="bi bi-calendar-x fs-4"></i> <br>
                            Belum ada data booking
                        </td>
                    </tr>
                @endforelse
            </tbody>
        </table>
    </div>
@endsection
