<?php

namespace App\Http\Controllers\Fournisseur;
use App\Http\Controllers\Controller;
use App\Models\User;
use Exception;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use RealRashid\SweetAlert\Facades\Alert;

class ProfileController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        return view('Admin.profile.profil');
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }


    public function userInformation(Request $request)
    {
        $user = User::find($request->input('id'));
    
        if ($user) {
            try {
                $user->name = $request->input('name');
                $user->email = $request->input('email');
    
                if ($request->hasFile('images')) {
                    $file = $request->file('images');
                   
                    $extension = $file->getClientOriginalExtension();
                    
                    $filename = time() . "." . $extension;
                   
                    $file->move(public_path('img/'), $filename);
                    // Correct the file move path
                    
                    $user->images = $filename;
                }
                
                $user->update();
    
                Alert::success('Success', 'User updated successfully.');
                return redirect()->route('Gestion+Profil.index');
            } catch (Exception $e) {
                Alert::error('Error', 'An error occurred. Please try again later.');
                return redirect()->back();
            }
        } else {
            Alert::warning('Warning', 'User not found.');
            return redirect()->back();
        }
    }
    


    public function updatePassword(Request $request){
        $user = User::find($request->input('id'));

        if ($user) {
            // Vérification du mot de passe actuel
            if (Hash::check($request->input('current_password'), $user->password)) {
                try {
                    $user->password = Hash::make($request->input('password'));
                    $user->save();

                    Alert::success('Success', 'Password updated successfully.');
                    return redirect()->route('Gestion+Profil.index');
                } catch (Exception $e) {
                    Alert::error('Error', 'An error occurred. Please try again later.');
                    return redirect()->back();
                }
            } else {
                Alert::warning('Warning', 'Current password is incorrect.');
                return redirect()->back();
            }
        } else {
            Alert::warning('Warning', 'User not found.');
            return redirect()->back();
        }
    }


    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
 public function update(Request $request, $id){

 }
    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */



}
