<?php

namespace App\Http\Controllers;

use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Http\Request;
use DB;

class HomeController extends BaseController
{
	function accueil()
    {
        if(session('user')== null) {
            return redirect('/');
        }
        return view('accueil');
    }


    function Deconnecter() {
        session(['user'=>null]);
        return redirect('/');
    }
	
	/*function Connecter (Request $request){

        $email= request("email");
        $password=request("pass");
        $user = DB::table('user')->where('login',$email)->where('password',$password )->first();
        if ($user) {
            session(['user' => $user]);
			if (session('user')->role == "Admin"){
                return redirect('/accueil');
            }
        else{
            return redirect('/nouvelleVente');
        } }
		else{
            return redirect('/')
            ->with('error', 'Login ou mot de passe invalide')
            ->withInput(
              $request->except('password')
            );
        }

    }*/
	//deconnexion
	function reset ($id) {

        $oldpassword = request("old");
        $newpassword = request("new");
        $confirmpassword= request("confirm");

        $userpass = session('user')->password;

        if( $oldpassword == $userpass){
           if($newpassword == $confirmpassword){
             DB::table('user')->where('id', $id)->update(
                [
                    'password' => $newpassword,
                ]
             );
             $message = [
                "title" => 'Success',
                "type" => 'success',
                "response" => 'Mot de passe rénitialisé avec succès !',
            ];

            return $message;

           }else{
            $message = [
                "title" => 'Erreur',
                "type" => 'error',
                "response" => 'Confirmation de mot de passe incorrect',
            ];
            return $message;
           }
        }else{
            $message = [
                "title" => 'Erreur',
                "type" => 'error',
                "response" => 'Ancien mot de passe invalide',
            ];
            return $message;
        }

    }
	
	 
	
}
