<?php

namespace App\Http\Controllers\Admin;

use App\Models\TimeSlot;

use App\Lib\RequiredConfig;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class TimeSlotController extends Controller {

    public function timeSlots() {
        $pageTitle = 'All Time Slots';
        $timeSlots = TimeSlot::orderBy('id', 'desc')->paginate(getPaginate());
        return view('admin.trip.time_slot', compact('pageTitle', 'timeSlots'));
    }

    public function timeSlotStore(Request $request, $id = 0) {
        $request->validate([
            'time' => 'required|unique:time_slots,time,' . $id,
        ]);

        if ($id) {
            $schedule = TimeSlot::findOrFail($id);
            $message  = 'Time slot updated successfully';
        } else {
            $schedule = new TimeSlot();
            $message  = 'Time slot created successfully';
        }

        $schedule->time = $request->time;
        $schedule->save();

        RequiredConfig::configured('time_slot');
        $notify[] = ['success', $message];
        return back()->withNotify($notify);
    }

    public function timeSlotStatus($id) {
        return TimeSlot::changeStatus($id);
    }
}
