<?php

namespace App\Http\Controllers;

use App\Models\SqlLog;
use App\Models\TransactionStatus;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Session;

class UserController extends Controller
{
     /**
     * Display a listing of the resource.
     */

     public function index()
    {
        $nbr_user=User::where('status',1)->count();
        $nbr_trans=TransactionStatus::count();

        SqlLog::LogSql();
        return view('admin.dash',['nbr_user'=>$nbr_user, 'nbr_trans'=>$nbr_trans]);
    }

    public function new_user()
    {
        return view('admin.new_user');
    }

    public function liste_user()
    {
        $users=User::where('status',1)->get();
        SqlLog::LogSql();
        return view('admin.liste_user',compact('users'));
    }

    public function edit_user($id)
    {
        $user=User::find($id);
        return view('admin.edit_user',compact('user'));
    }

    public function login(Request $request)
    {
        $request->validate([
            'email' => 'required:users',
            'password' => 'required|min:4',
        ],
        [
            'email.required' => 'Veullez renseignez l\'email !',
            'password.required' => 'Veullez renseignez le mot de passe !',
            'password.min' => 'Le mot de passe doit contenir au minimum 4 caractères'
        ]);

        $credentials = $request->only('email', 'password');
        $remember=$request->input('remenber_me');
        if(Auth::attempt($credentials, $remember)){
            SqlLog::LogSql();
            if(Auth::user()->status==1){
                //Check secure delais login
            return redirect('dash')->with('success', 'Vous êtes connectés');
            }
            else{
            return back()->with('error', 'Votre compte est inactif !');
            }
        }
        else{
            SqlLog::LogSql();
            return back()->with('error', 'Nom d\'utilisateur ou Mot de passe incorrect !');
        }
    }

    /**
     * Show the form for creating a new resource.
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $request)
    {

        $user=new User();
        $user->nom=$request->input('nom');
        $user->prenom=$request->input('prenom');
        $user->phone=$request->input('phone');
        $user->adresse=$request->input('adresse');
        $user->email=$request->input('email');
        //--------------------
        $user->password=Hash::make($request->input('password'));
        $user->save();

        SqlLog::LogSql();
        return redirect('/liste_user')->with('success', 'Utilisateur ajouté avec succès');
    }

    /**
     * Display the specified resource.
     */
    public function show(string $id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     */
    public function edit(string $id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(Request $request)
    {
        $id=$request->id;
        $user=User::find($id);
        $user->nom=$request->input('nom');
        $user->prenom=$request->input('prenom');
        $user->phone=$request->input('phone');
        $user->adresse=$request->input('adresse');
        $user->email=$request->input('email');
        //--------------------
        if($request->input('password') && $request->input('password')!="") $user->password=Hash::make($request->input('password'));
        $user->save();

        SqlLog::LogSql();
        return redirect('/liste_user')->with('success', 'Utilisateur modifié avec succès');

    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy($id)
    {
        User::where('id',$id)->update(['status'=>0]);
        SqlLog::LogSql();
        return back()->with('success', 'Utilisateur supprimé avec succès');

    }

    public function logout() {
        Session::flush();
        Auth::logout();
        SqlLog::LogSql();

        return redirect(asset('login'))->with('success', 'Session fermée avec succè');
    }
}
