<?php

namespace App\Http\Controllers;

use App\Models\Chauffeur;
use App\Models\Commune;
use App\Models\Personne;
use Illuminate\Http\Request;

class DataController extends Controller
{
    //
     public function index()
    {
        $allPersonnes = Personne::all();
        $allCommunes = Commune::all();
        return view('data',[
            'allPersonnes' => $allPersonnes,
            'allCommunes' => $allCommunes
        ]);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //($request); 
        $data = request()->validate([
            'personne' => ['required', 'string'],
            'commune' => ['required', 'string'],
            'cni' => ['required', 'string'],
            'permis' => ['required', 'string'],
            'situationgeo' => ['required', 'string'],
            'user_code' => ['required', 'string']
        ]);
        try {
            $data['personne_id'] = $request->personne;
            $data['cni'] = $request->cni;
            $data['permis'] = $request->permis;
            $data['dateadh'] = date("Y-m-d H:i:s");
            $data['commune_id'] = $request->commune;
            $data['situationgeo'] = $request->situationgeo;
            $data['photo'] = " ";
            $data['etat'] = 'NEW';
            $data['user_code'] =  $request->user_code;
        } catch (\Throwable $th) {
            dd($th->getMessage());
        }
        Chauffeur::create($data);
        return redirect('/data')->with('success', 'Opération effectuée avec succès!');
        dd($response->json());
    }
}
