<?php

namespace App\Http\Controllers;

use App\Models\Chauffeur;
use App\Models\Gare;
use App\Models\LierChauffeur;
use App\Models\LierGare;
use App\Models\LierPersonne;
use App\Models\Personne;
use App\Models\Vehicule;
use Illuminate\Http\Request;

class VehiculeController extends Controller
{
    //
     public function index()
    {
         $allChauffeurs = Chauffeur::orderByDesc('created_at')->with('personne')
                            ->get();
        $allGares = Gare::all();
        $allPersonnes = Personne::all();
        return view('vehicule',[
            'allChauffeurs' => $allChauffeurs,
            'allGares' => $allGares,
            'allPersonnes' => $allPersonnes
        ]);
    }


     /* Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        try {
        $data = request()->validate([
            'num_chassi' => ['required', 'string'], 
            'modele' => ['required', 'string'], 
            'marque' => ['required', 'string'],
            'anne_mc' => ['required', 'string'],
            'gare' => ['required', 'string'],
            'personne' => ['required', 'string'],
            'chauffeur' => ['required', 'string'],
        ]);
        //dd($request);
        $chevaux ="Cheveaux";
        $data['num_chassi'] = $request->num_chassi;
        $data['modele'] = $request->modele;
        $data['marque'] = $request->marque;
        $data['anne_mc'] = $request->anne_mc;
        $data['num_imma'] = $request->num_imma;
        $data['typ_energie'] = $request->typ_energie;
        $data['typ_vehicule'] = $request->typ_vehicule;
        $data['puis_vehicule'] = $request->puis_vehicule.' ' .$chevaux;
        Vehicule::create($data);

        $IdV = Vehicule::where('num_chassi', $request->num_chassi)
        ->select('id')
        ->get();

        //Save rattechements
         $data['vehicule_id'] = $IdV;
         foreach ($IdV as $value) {

            //Save rattechement chauffeur
            $data['vehicule_id'] = $value->id;
            $data['chauffeur_id'] = $request->chauffeur;
            $data['daydeb'] = $request->datDeb0;
            $data['dayfin'] = $request->datFin0;
            $data['remarque'] = " ";
            $data['user_code'] =  $request->user_code;
            LierChauffeur::create($data);

            //Save rattechement Acuqereur
            $data['vehicule_id'] = $value->id;
            $data['personne_id'] = $request->personne;
            $data['daydeb'] = $request->datDeb1;
            $data['dayfin'] = $request->datFin1;
            $data['remarque'] = " ";
            $data['user_code'] =  $request->user_code;
            LierPersonne::create($data);

            //Save rattechement Gare
            $data['vehicule_id'] = $value->id;
            $data['gare_id'] = $request->gare;
            $data['daydeb'] = $request->datDeb2;
            $data['dayfin'] = $request->datFin2;
            $data['remarque'] = " ";
            $data['user_code'] =  $request->user_code;
            LierGare::create($data);
         }
            
        } catch (\Throwable $th) {
            dd($th->getMessage());
        }
        
        return redirect('/vehicule')->with('success', 'Merci pour votre enregistrement');
        dd($response->json());
    }
}
