<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\User;
use App\Models\Room;
use App\Models\Booking;
use Carbon\Carbon;

class DashboardAdminController extends Controller
{
    public function index()
{
    $totalUsers = User::count();
    $totalRooms = Room::count();
    $totalBookings = Booking::count();

    // Ambil semua booking yang sudah dikonfirmasi
    $confirmedBookings = Booking::with('user', 'room')
        ->where('status_booking', 'Dikonfirmasi')
        ->get();

    // Buat array untuk simpan sisa hari per booking
    $daysLeftPerBooking = [];

    foreach ($confirmedBookings as $booking) {
        if ($booking->tanggal_checkout) {
            $daysLeftPerBooking[$booking->id_booking] = now()->diffInDays(Carbon::parse($booking->tanggal_checkout), false);
        } else {
            $daysLeftPerBooking[$booking->id_booking] = null;
        }
    }

    // Ambil booking pending untuk ditampilkan di tabel
    $pendingBookings = Booking::where('status_booking', 'pending')->with('user', 'room')->get();

    return view('admin.dashboard', compact(
        'totalUsers',
        'totalRooms',
        'totalBookings',
        'pendingBookings',
        'confirmedBookings',
        'daysLeftPerBooking'
    ));
}

}
