<?php

namespace App\Http\Controllers;

use App\Models\Pelaporan;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class ProfileController extends Controller
{
    public function index(Request $request){
        $user = $request->user();
        $laporan = Pelaporan::with('tindakan','user')->get();
        $profile  = User::where('id', $user->id)->get();

        return view('umum.profile.index', compact('user','laporan', 'profile'));
    }

    public function update(Request $request, $id)
    {
        $request->validate([
            'foto' => 'nullable|image|mimes:jpeg,png,jpg,gif',
        ],[
            'foto.nullable' => 'Foto Tidak Boleh Kosong',
            'foto.image' => 'Foto Harus Berupa Image ',
            'foto.mimes' => 'Foto Harus JPEG, PNG, JPG, GIF',
        ]);

        $user = User::findOrFail($id);

        // Perbarui data pengguna
        $user->update($request->all());

        if ($request->hasFile('foto')) {
            // Simpan foto baru dan perbarui kolom foto di database
            $fotoPath = $request->file('foto')->store('uploads');
            $user->foto = $fotoPath;
            $user->save();
        }

        return redirect()->back()->with('success', 'Foto profil berhasil diperbarui');
    }


}
