<?php

namespace App\Http\Controllers;

use App\Models\LierPersonne;
use App\Models\Personne;
use App\Models\Vehicule;
use Illuminate\Http\Request;

class ProprietaireController extends Controller
{
    //
    public function index()
    {
        $allPersonnes = Personne::all();
        $allVehicules = Vehicule::all();
        return view('proprietaire',[
            'allPersonnes' => $allPersonnes,
            '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'],
            'personne' => ['required', 'string'],
            'dayDeb' => ['required', 'string'],
            'dayFin' => ['required', 'string'],
            'remarque' => ['required', 'string'],
            'user_code' => ['required', 'string']
        ]);
        //dd($data); 
        try {
            $data['vehicule_id'] = $request->vehicule;
            $data['personne_id'] = $request->personne;
            $data['daydeb'] = $request->dayDeb;
            $data['dayfin'] = $request->dayFin;
            $data['remarque'] = $request->remarque;
            $data['user_code'] =  $request->user_code;
        } catch (\Throwable $th) {
            dd($th->getMessage());
        }
        LierPersonne::create($data);
        return redirect('/proprietaire')->with('success', 'Opération effectuée avec succès!');
        dd($response->json());
    }
}
