@extends('layouts.app')
@section('content')
    <div class="container-fluid p-4" style="background-color: #F5F5F5; border-radius: 10px;">
        <h3 class="fw-bold" style="color: #063986;">Selamat Datang Di Kos Calista</h3>

        {{-- Notifikasi sisa masa booking customer (kalau ada booking terakhir) --}}
        @isset($booking)
            @if (!is_null($daysLeft))
                @if ($daysLeft > 0)
                    <div class="alert alert-warning mt-3 fw-semibold text-dark">
                        Sisa booking customer: <strong>{{ $daysLeft }} hari</strong>
                    </div>
                @elseif ($daysLeft === 0)
                    <div class="alert alert-danger mt-3 fw-semibold">
                        Hari ini adalah <strong>hari terakhir</strong> masa booking customer!
                    </div>
                @else
                    <div class="alert alert-secondary mt-3 fw-semibold">
                        Masa booking customer sudah <strong>berakhir</strong>.
                    </div>
                @endif
            @endif
        @endisset

        <div class="row mt-4">
            {{-- Statistik --}}
            <div class="col-md-4">
                <div class="card border shadow-sm">
                    <div class="card-body text-center p-4">
                        <div class="d-flex justify-content-center">
                            <div class="rounded-circle d-flex align-items-center justify-content-center"
                                style="width: 60px; height: 60px; background-color: #0080B5;">
                                <i class="bi bi-person-badge-fill text-white" style="font-size: 30px;"></i>
                            </div>
                        </div>
                        <h3 class="fw-bold mt-3 mb-0">{{ $totalUsers }}</h3>
                        <p class="mb-0">Total Customer</p>
                    </div>
                </div>
            </div>
            <div class="col-md-4">
                <div class="card border shadow-sm">
                    <div class="card-body text-center p-4">
                        <div class="d-flex justify-content-center">
                            <div class="rounded-circle d-flex align-items-center justify-content-center"
                                style="width: 60px; height: 60px; background-color: #0080B5;">
                                <i class="bi bi-door-closed-fill text-white" style="font-size: 30px;"></i>
                            </div>
                        </div>
                        <h3 class="fw-bold mt-3 mb-0">{{ $totalRooms }}</h3>
                        <p class="mb-0">Total Kamar</p>
                    </div>
                </div>
            </div>
            <div class="col-md-4">
                <div class="card border shadow-sm">
                    <div class="card-body text-center p-4">
                        <div class="d-flex justify-content-center">
                            <div class="rounded-circle d-flex align-items-center justify-content-center"
                                style="width: 60px; height: 60px; background-color: #0080B5;">
                                <i class="bi bi-journal-check text-white" style="font-size: 30px;"></i>
                            </div>
                        </div>
                        <h3 class="fw-bold mt-3 mb-0">{{ $totalBookings }}</h3>
                        <p class="mb-0">Total Booking</p>
                    </div>
                </div>
            </div>

            {{-- Tabel Pending Booking --}}
            <div class="row mt-5">
                <div class="col-12">
                    <div class="card border shadow-sm">
                        <div class="card-header bg-white">
                            <h5 class="mb-0 fw-bold text-primary">Pemesanan Belum Dikonfirmasi</h5>
                        </div>
                        <div class="card-body p-4">
                            @if($pendingBookings->isEmpty())
                                <p class="text-muted mb-0">Belum ada pemesanan yang menunggu konfirmasi.</p>
                            @else
                                <div class="table-responsive">
                                    <table class="table table-bordered table-striped">
                                        <thead class="table-light">
                                            <tr>
                                                <th>No</th>
                                                <th>Nama Customer</th>
                                                <th>Kamar</th>
                                                <th>Tanggal Pemesanan</th>
                                                <th>Status</th>
                                                <th>Aksi</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            @foreach ($pendingBookings as $index => $booking)
                                                <tr>
                                                    <td>{{ $index + 1 }}</td>
                                                    <td>{{ $booking->user->name }}</td>
                                                    <td>{{ $booking->room->room_type }}</td>
                                                    <td>
                                                        {{ \Carbon\Carbon::parse($booking->tanggal_checkin)->format('d M Y') }}
                                                        -
                                                        {{ \Carbon\Carbon::parse($booking->tanggal_checkout)->format('d M Y') }}
                                                    </td>
                                                    <td>
                                                        <span class="badge bg-warning text-dark">Belum Dikonfirmasi</span>
                                                    </td>
                                                    <td class="flex gap-2 justify-start">
                                                        <!-- Tombol Konfirmasi -->
                                                        <form action="{{ route('admin.bookingconfirm', $booking->id_booking) }}" method="POST" class="flex items-center">
                                                            @csrf
                                                            @method('PUT')
                                                            <button type="submit" class="btn btn-sm btn-success flex items-center gap-1">
                                                                <i class="bi bi-check-circle-fill"></i> Konfirmasi
                                                            </button>
                                                        </form>
                                        
                                                        <!-- Tombol Batalkan -->
                                                        <form action="{{ route('admin.bookingcancel', $booking->id_booking) }}" method="POST" class="flex items-center" onsubmit="return confirm('Apakah Anda yakin ingin membatalkan booking ini?')">
                                                            @csrf
                                                            @method('PUT')
                                                            <button type="submit" class="btn btn-sm btn-danger flex items-center gap-1">
                                                                <i class="bi bi-x-circle-fill"></i> Batalkan
                                                            </button>
                                                        </form>
                                                    </td>
                                                </tr>
                                            @endforeach
                                        </tbody>
                                    </table>
                                </div>
                            @endif
                        </div>
                    </div>
                </div>
            </div>

            {{-- Tampilkan Booking yang sudah dikonfirmasi beserta sisa hari --}}
            <div class="row mt-5">
                <div class="col-12">
                    <h5 class="fw-bold text-primary mb-3">Sisa Hari Booking</h5>
                    @foreach ($confirmedBookings as $booking)
                        <div class="mb-3 p-3 border rounded bg-light">
                            <p><strong>Customer:</strong> {{ $booking->user->name }}</p>
                            <p><strong>Booking Kamar:</strong> {{ $booking->room->room_type }}</p>
                            @php
                                $daysLeft = $daysLeftPerBooking[$booking->id_booking] ?? null;
                            @endphp

                            @if(!is_null($daysLeft))
                                @if($daysLeft > 0)
                                    <div class="alert alert-warning mb-0">
                                        Sisa booking: <strong>{{ $daysLeft }} hari</strong>
                                    </div>
                                @elseif($daysLeft === 0)
                                    <div class="alert alert-danger mb-0">
                                        Hari ini <strong>terakhir</strong> masa booking!
                                    </div>
                                @else
                                    <div class="alert alert-secondary mb-0">
                                        Masa booking sudah <strong>berakhir</strong>
                                    </div>
                                @endif
                            @endif
                        </div>
                    @endforeach
                </div>
            </div>
        </div>
    </div>
@endsection
