<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use DB;

class UtilisateurController extends Controller
{
    public function connexion(Request $request){
        $log = $request->nomutil;
        $md = $request->mdp;

        $cherch = DB::select("SELECT * FROM utilisateur WHERE  loginn='$log' AND mdp = '$md'");

        if (count($cherch)>0){

            foreach($cherch as $user){
                $nom = $user->nomcomplet;
                $ncc = $user->ncc;
            }

            $ch = DB::select("SELECT nom_contribuable FROM contribuable WHERE ncc='$ncc'")[0];
            $nom_con = $ch->nom_contribuable;

            $infos='';
            $data= $request->input();
            $request->session()->put('utilisateur',$nom);
            $request->session()->put('ncc',$ncc);
            $request->session()->put('nc',$nom_con);

            return response()->json([
                'status'=>200,
            ]);

        }else{

            $infos='Utilisateur ou mot de passe incorrect';

            return response()->json([
                'status'=>100,
                'info'=>$infos,
                'log'=>$log,
                'md'=>$md,
            ]);
        }
    }
}
