<?php

namespace App\Http\Controllers;

use App\Models\LierLigne;
use App\Models\Ligne;
use App\Models\Vehicule;
use Illuminate\Http\Request;

class LigneController extends Controller
{
    //
    public function index()
    {
        $allLignes = Ligne::all();
        $allVehicules = Vehicule::all();
        return view('ligne',[
            'allLignes' => $allLignes,
            'allVehicules' => $allVehicules
        ]);
    }

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