<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use DB;

class AuthentificationController extends Controller
{
    //


    function login () {

        $today = date('Y-m-d H:i:s');

        $datecreation = "2026-01-01 09:00:00" ;
        $datefinCarbon = (new \Carbon\Carbon($datecreation))->addMonth(48);
        if( $today <= $datefinCarbon){
            return view('login',[
				'datefinCarbon'=>$datefinCarbon,
			]);
        }else{

            return view('error');
        }
    }

    function pass () {

        return view('password');
    }

    function reset ($id) {
        return view('reset', [

            'iduser' => $id,

        ]);
    }


    function resetpass ($id) {

        $newpassword = md5(request("newpassword"));
        $confirmpass= md5(request("confirmpass"));

        if($newpassword == $confirmpass){

            DB::table('users')->where('id', $id)->update(
                [
                    'password' => $newpassword,
                ]
            );

         return redirect('/')->with('status', 'Mot de passe rénitialisé avec succes !');

        }else{

            return redirect()->route('reset', ['id' => $id ])->with('error', 'Mots de passe sont incorrects');

        }
           

    }


    function verifier (Request $request){

        $login= request("login");

        $verification = DB::table('users')->where('login',$login)->first();

        if ($verification) {

            $iduser = DB::table('users')->where('login',$login)->first()->id;

            return redirect()->route('reset', ['id' => $iduser ]);
        }
        else{
            return redirect('/forgot_password')->with('error', 'Login invalide');

        }

    }

    function Connecter (Request $request){

        $login= request("email");
        $password=md5(request("password"));
        $user = DB::table('users')->where('login',$login)->where('password',$password )->first();
        if ($user) {
				session(['user' => $user]);
				if (session('user')->roles=="Admin" || session('user')->roles=="Gérant"){
				   
					return redirect('/accueil');
				} else {
					return redirect('/vente-code');
				}   	
            }
        else{
            return redirect('/')->with('error', 'Login ou mot de passe invalide');
        }

    }

    function deconnecter() {
        session(['user'=>null]);
        return redirect('/');
    }

    
    function save()
    {
        if(session('user')== null) {
            return redirect('/');
        }
		$magasin = DB::table('magasin')->get();
	return view('users/register',[
		'magasin' => $magasin,
	]);
    }



    function enregistrer (Request $request){

        $nom= request("nom");
        $nom = strtoupper($nom);
        $login= request("login");
        $password=md5(request("password"));
        $confirm=md5(request("confirm"));
        $role= request("role");
        $magasin= request("magasin");
		

        $veri = DB::table('users')->where('login', $login)->first();

        if($veri){

            return redirect()->route('Register')->with('error', 'Ce login existe déja');

        }else{

            if ($confirm == $password){

                DB::table('users')->insert(
                    [   
					
						'nom' => $nom,
                        'login' => $login,
                        'password' => $password,
                        'roles' => $role,
                        'roles' => $role,
                        'magasin_id' => $magasin,
                    ]
                );
    
                return redirect()->route('Register')->with('status', 'Utilisateur enregisté avec succes!');
    
            }else{
                return redirect()->route('Register')->with('error', 'Enregistrement échoué! les mots de passe sont incorrectes');
            }

        }

    }


    function modifier($id) {

        if(session('user')== null) {
            return redirect('/');
        }
       
        $users = DB::table('users')->where('id', $id)->first();
        $magasin = DB::table('magasin')->get();

        return view('users/modification',[
            'user' => $users,
            'magasin' => $magasin,
        ]);
    }


    function valider ($id){

        $nom= request("nom");
        $nom = strtoupper($nom);
        $magasin= request("magasin");
        $role= request("role");
        $login= request("login");

        DB::table('users')->where('id', $id)->update(
            [   'nom' => $nom,
                'login' => $login,
                'roles' => $role,
                'magasin_id' => $magasin,
            ]
        );
        return redirect()->route('Liste_des_utilisateurs')->with('status', 'Utilisateur modifié avec succes!');

    }


    function voirpassword ($id) {

        if(session('user')== null) {
            return redirect('/');
        }

        return view('change', [
            'iduser' => $id,
        ]);
    }


    function changer (Request $request, $id){

        $oldpassword=md5(request("oldpassword"));

        $newpassword=md5(request("newpassword"));

        $confirmpass= md5(request("confirmpass"));

        $veri = DB::table('users')->where('id', $id)->first()->password;

        if( $veri == $oldpassword){

            if ($newpassword == $confirmpass) {

                DB::table('users')->where('id', $id)->update(
                    [
                        'password' => $newpassword,
                    ]
                );
    
                return redirect('/')->with('status', 'Mot de passe réinitialisé avec succes');
            }
            else{
                return redirect()->route('change_password', ['id' => $id ])->with('error', 'Confirmation mot de passe incorrect');
    
            }

        }else{
            return redirect()->route('change_password', ['id' => $id ])->with('error', 'Ancien mot de passe incorrect');
        }

    }


    function VoirSuppression ($id) {

        $users= DB::table('users')->where('id',$id)->first();
        return [
            'user' => $users,
        ];
    }

    function supprimer () {

        $id=request('id');

        DB::table('users')->where('id', $id)->delete();

        return redirect()->route('Liste_des_utilisateurs')->with('status', 'Utilisateur supprimé avec succes');
    }



    function liste() {

        if(session('user')== null) {
            return redirect('/');
        }
       
        $users = DB::table('users')->orderBydesc('id')->get();
        return view('users/liste',[
            'user' => $users,
        ]);
    }


    function accueil()
    {

        if(session('user')== null) {
            return redirect('/');
        }
        
        return view('accueil', [

            ]);
    }



    

}
