<?php

namespace App\Http\Controllers;

use App\Models\Chauffeur;
use App\Models\LierChauffeur;
use App\Models\Vehicule;
use Illuminate\Http\Request;

class LierchauffeurController extends Controller
{
    //
    public function index()
    {
        $allChauffeurs = Chauffeur::orderByDesc('created_at')->with('personne')
                            ->get();
        $allVehicules = Vehicule::all();
        return view('lierchauffeur',[
            'allChauffeurs' => $allChauffeurs,
            'allVehicules' => $allVehicules
        ]);
    }


    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {    //dd($request); 
        $data = request()->validate([
            'vehicule' => ['required', 'string'],
            'chauffeur' => ['required', 'string'],
            'dayDeb' => ['required', 'string'],
            'dayFin' => ['required', 'string'],
            'remarque' => ['required', 'string'],
            'user_code' => ['required', 'string']
        ]);
        //dd($request); 
        try {
            $data['vehicule_id'] = $request->vehicule;
            $data['chauffeur_id'] = $request->chauffeur;
            $data['daydeb'] = $request->dayDeb;
            $data['dayfin'] = $request->dayFin;
            $data['remarque'] = $request->remarque;
            $data['user_code'] =  $request->user_code;
        } catch (\Throwable $th) {
            dd($th->getMessage());
        }
        LierChauffeur::create($data);
        return redirect('/lierchauffeur')->with('success', 'Opération effectuée avec succès!');
        dd($response->json());
    }
}
