<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Carbon\Carbon;
use Maatwebsite\Excel\Facades\Excel;
use App\Exports\BookingsExport;


class AdminBookingController extends Controller
{
    /**
     * Tampilkan semua booking untuk ruangan tertentu
     */
    public function index($room, Request $request)
{
    $query = DB::table($room);
    $today = Carbon::today();

    // ===============================
    // 1️⃣ Filter Cepat (tombol atas)
    // ===============================
    switch ($request->get('filter')) {
        case 'hari-ini':
            $query->whereDate('date', $today);
            break;
        case 'akan-datang':
            $query->whereDate('date', '>', $today);
            break;
        case 'selesai':
            $query->whereDate('date', '<', $today);
            break;
    }

    // ===============================
    // 2️⃣ Filter Manual (rentang tanggal & range cepat)
    // ===============================
    if ($request->filled('start_date')) {
        $query->whereDate('date', '>=', $request->start_date);
    }

    if ($request->filled('end_date')) {
        $query->whereDate('date', '<=', $request->end_date);
    }

    if ($request->range === 'today') {
        $query->whereDate('date', $today);
    } elseif ($request->range === 'week') {
        $query->whereBetween('date', [
            Carbon::now()->startOfWeek(),
            Carbon::now()->endOfWeek()
        ]);
    } elseif ($request->range === 'month') {
        $query->whereMonth('date', Carbon::now()->month);
    }

    // ===============================
    // 3️⃣ Ambil data dan urutkan
    // ===============================
    $bookings = $query->orderBy('date', 'desc')->get();

    return view('admin.bookings', compact('bookings', 'room'));

}


    public function export(Request $request, $room)
{
    $fileName = 'Booking_' . strtoupper($room) . '_' . now()->format('Ymd_His') . '.xlsx';

    return Excel::download(new BookingsExport($room, $request), $fileName);
}

    /**
     * Form edit booking
     */
    public function edit($room, $id)
    {
        $booking = DB::table($room)->where('id', $id)->first();

        if (!$booking) {
            return redirect()->route('booking-admin.bookings.index', $room)
                             ->with('error', 'Booking tidak ditemukan.');
        }

        return view('admin.edit', compact('booking', 'room'));
    }

    /**
     * Update data booking
     */
    public function update(Request $request, $room, $id)
    {
        $request->validate([
            'pic'        => 'required|string|max:255',
            'date'       => 'required|date',
            'start_time' => 'required',
            'end_time'   => 'required',
            'department' => 'required|string|max:100',
            'keterangan' => 'nullable|string',
        ]);

        DB::table($room)->where('id', $id)->update([
            'pic'        => $request->pic,
            'date'       => $request->date,
            'start_time' => $request->start_time,
            'end_time'   => $request->end_time,
            'department' => $request->department,
            'keterangan' => $request->keterangan,
        ]);

        return redirect()->route('booking-admin.bookings.index', $room)
                         ->with('success', 'Booking berhasil diperbarui.');
    }

    /**
     * Hapus booking
     */
    public function destroy($room, $id)
    {
        DB::table($room)->where('id', $id)->delete();

        return redirect()->route('booking-admin.bookings.index', $room)
                         ->with('success', 'Booking berhasil dihapus.');
    }
}
