<?php

namespace App\Http\Controllers;
use Illuminate\Support\Facades\Auth;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;

class ProfileController extends Controller
{
    public function index()
    {
       
        $user = Auth::user();

        // Cek role-nya
        if ($user->role === 'admin') {
            // Tampilkan view untuk admin
            return view('admin.profile.index', compact('user'));
        } else {
            // Tampilkan view untuk user biasa
            return view('user.profile.index', compact('user'));
        }
        
    }
    public function updatePhoto(Request $request)
{
    $request->validate([
        'photo' => 'image|mimes:jpeg,png,jpg|max:2048',
    ]);

    $user = Auth::user();

    if ($request->hasFile('photo')) {
        $filename = time() . '.' . $request->photo->extension();
        $request->photo->move(public_path('assets/images'), $filename);
        $user->photo = 'assets/images/'.$filename;
        $user->save();
    }
    return redirect()->back()->with('success', 'Foto profil berhasil diperbarui.');
}

public function updatePassword(Request $request)
{
    $request->validate([
        'current_password' =>'required',
        'new_password' => 'required', 'min:8', 'confirmed',
        'new_password_confirmation' => 'required',
    ]);

    $user = Auth::user();

    if (!Hash::check($request->current_password, $user->password)) {
        return back()->withErrors(['current_password' => 'Password lama tidak cocok.']);
    }

    // Update password baru
    $user->update([
        'password' => Hash::make($request->new_password),
    ]);

    return redirect()->back()->with('success', 'Password berhasil diperbarui!');
}


}
