<?php

namespace App\Http\Controllers;

use App\Models\Schedule;
use App\Http\Requests\StoreScheduleRequest;
use App\Http\Requests\UpdateScheduleRequest;
use Illuminate\Http\RedirectResponse;
use Carbon\Carbon;
use Illuminate\Http\Request;
use App\Models\Billet;
use App\Models\Chauffeur;
use Illuminate\View\View;

class ScheduleController extends Controller
{
    
    public function index()
    {
        $billets = Billet::all();
        return view('schedule.index', compact('billets'));
    }

    public function create(Request $request)
    {
        $chauffeurs = Chauffeur::all();

        return view('schedule.add', compact('chauffeurs'));
    }

    public function store(Request $request)
    {
        $color = '#c9ab81';
        $request['chauffeur_id'] = $request['vehicule'] ?? null;
    
    
        $ch=$request['chauffeur_id'];
        // dd($ch);
        $item = new Billet();
        $item->title = $request->title;
 

        $item->chauffeur_id = $request->chauffeur_id;
        $item->chauffeur1 = $request->chauffeur1;
        $item->telephone1 = $request->telephone1;
        $item->chauffeur2 = $request->chauffeur2;
        $item->telephone2 = $request->telephone2;
        $item->adresseclient = $request->adresseclient;
        $item->date = $request->date;
        $item->email = $request->email;

        $item->numero = $request->numero;
        $item->nom = $request->nom;
        $item->adresse = $request->adresse;
        $item->motif_deplacement = $request->motif_deplacement;
        $item->numero_telephone = $request->numero_telephone;
        $item->heure_debut = $request->heure_debut;
        $item->heure_fin = $request->heure_fin;
        $item->nbr_passagers = $request->nbr_passagers;

        $item->color = $color; 
        $item->chauffeur_id = $ch; 

        $item->save();
        

        return redirect('/fullcalender');
    }

    public function show(Billet $billet): View
    {
        $chauffeurs = Chauffeur::all();

        return view('schedule.print', compact('billet'), compact('chauffeurs'));
    }

    public function getEvents()
    {
        $schedules = Billet::all();
        return response()->json($schedules);
    }

    public function destroy($eventId): RedirectResponse
    {
        $item = Billet::find($eventId);
        $item->delete();

        return redirect()->route('evenst.index')
            ->with('success', 'Billet deleted successfully');
    }

    public function edit($evenstId): View
    {
        $chauffeurs = Chauffeur::all();
        $billet = Billet::find($evenstId); 
        $evenst=$billet;
        return view('schedule.update', ['evenst' => $evenst], compact('billet'), compact('chauffeurs'));
    }
    public function delete($evenstId): View
    {
        $chauffeurs = Chauffeur::all();
        $billet = Billet::find($evenstId); 
        $evenst=$billet;
        return view('schedule.delete', ['evenst' => $evenst], compact('billet'), compact('chauffeurs'));
    }
    public function update(Request $request, $eventId)
    {
        $color = '#c9ab81';
        $request['chauffeur_id'] = $request['vehicule'] ?? null;
    
     
        $item = Billet::find($eventId);
    
        if (!$item) {
        
            return redirect('/fullcalender')->with('error', 'Item not found');
        }
    
       
        $item->title = $request->title;
        $item->chauffeur_id = $request->chauffeur_id;
        $item->chauffeur1 = $request->chauffeur1;
        $item->telephone1 = $request->telephone1;
        $item->chauffeur2 = $request->chauffeur2;
        $item->telephone2 = $request->telephone2;
        $item->adresseclient = $request->adresseclient;
        $item->date = $request->date;
        $item->numero = $request->numero;
        $item->nom = $request->nom;
        $item->email = $request->email;

        $item->adresse = $request->adresse;
        $item->motif_deplacement = $request->motif_deplacement;
        $item->numero_telephone = $request->numero_telephone;
        $item->heure_debut = $request->heure_debut;
        $item->heure_fin = $request->heure_fin;
        $item->nbr_passagers = $request->nbr_passagers;
        $item->color = $color;
        $item->chauffeur_id = $request['chauffeur_id'];
    
        // Save the updated item
        $item->save();
    
        return redirect('/fullcalender');
    }
    
}