<?php

namespace App\Http\Controllers;

use App\Models\Roleuser;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Crypt;

class UserController extends Controller
{
    public function index()
    {
        $user = User::with('role')->get();
        $roleuser = Roleuser::all();
        return view('user', compact('user','roleuser'));
    }

    public function store(Request $request)
    {
        // variable yang di minta berdasarkan kolom
       // dd($request->all());
        $request->validate([
            'nama' => 'required',
            'username' => 'required',
            'email' => 'required|email|unique:users,email',
            'password' => 'required|min:6',
            'role' => 'required',
            'image' => 'nullable|image|mimes:jpg,jpeg,png|max:2048',
        ]);

        // Data awal tanpa gambar
        // mevariabelkan dari data model
        $data = [
            'name' => $request->nama,
            'username' => $request->username,
            'email' => $request->email,
            'password' => Crypt::encryptString($request->password),
            'id_roleuser' => $request->role
        ];

        // Cek jika ada file gambar diupload fungsi ini untuk gambar

        if ($request->hasFile('image')) {
             $foto = $request->file('image');
             $namaFoto = time() . '.' . $foto->getClientOriginalExtension();
             $foto->move(public_path('upload/user'), $namaFoto);
             $data['image'] = 'upload/user/' . $namaFoto; // Simpan path ke database
         }

        // Debugging sebelum simpan ke database
        // dd($data);

        // Simpan ke database
        User::create($data);

        return redirect()->back()->with('success', 'Data berhasil disimpan!');
    }

  public function update(Request $request, $id)
{
    // Debugging: Periksa request yang diterima
    // dd($request->all());

    $request->validate([
        'nama' => 'required',
        'username' => 'required|unique:users,username,' . $id . ',id',
        'email' => 'required',
        'password' => 'required|min:6',
        'image' => 'nullable|image|mimes:jpg,jpeg,png|max:2048',
    ]);

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

    $user->name = $request->nama;
    $user->username = $request->username;
    $user->email = $request->email;
    $user->password = Crypt::encryptString($request->password);


    // Jika ada file gambar yang diunggah
    if ($request->hasFile('image')) {
        // Hapus gambar lama jika ada
        if ($user->image && file_exists(public_path($user->image))) {
            unlink(public_path($user->image));
        }

        // Simpan gambar baru
        $foto = $request->file('image');
        $namaFoto = time() . '.' . $foto->getClientOriginalExtension();
        $foto->move(public_path('upload/user'), $namaFoto);
        $user->image = 'upload/user/' . $namaFoto; // Simpan path ke database
    }

    $user->save();

    return redirect()->back()->with('success', 'Data user berhasil diperbarui.');
}


    public function destroy($id)
{
    $user = User::findOrFail($id);
    $user->delete();

    return redirect()->back()->with('success', 'User berhasil dihapus');
}

}

// namespace App\Http\Controllers;

// use App\Models\Roleuser;
// use App\Models\User;
// use Illuminate\Http\Request;
// use Illuminate\Support\Facades\Crypt;
// use Illuminate\Support\Facades\Hash;
// use Illuminate\Auth\Events\Registered;
// use Illuminate\Support\Facades\Validator;

// class UserController extends Controller
// {
//     public function index()
//     {
//         $user = User::with('role')->get();
//         $roleuser = Roleuser::all();
//         return view('user', compact('user','roleuser'));
//     }

//     public function store(Request $request)
//     {
//         // Validasi input dari form registrasi
//         $request->validate([
//             'nama' => 'required',
//             'username' => 'required|unique:users,username',
//             'email' => 'required|email|unique:users,email',
//             'password' => 'required|min:6|confirmed',  // Pastikan password dikonfirmasi
//             'role' => 'required',
//             'image' => 'nullable|image|mimes:jpg,jpeg,png|max:2048',
//         ]);

//         // Data awal tanpa gambar
//         $data = [
//             'name' => $request->nama,
//             'username' => $request->username,
//             'email' => $request->email,
//             'password' => Hash::make($request->password), // Encrypt password menggunakan Hash
//             'id_roleuser' => $request->role,
//         ];

//         // Jika ada file gambar, proses upload
//         if ($request->hasFile('image')) {
//             $foto = $request->file('image');
//             $namaFoto = time() . '.' . $foto->getClientOriginalExtension();
//             $foto->move(public_path('upload/user'), $namaFoto);
//             $data['image'] = 'upload/user/' . $namaFoto; // Simpan path ke database
//         }

//         // Simpan data user baru ke database
//         $user = User::create($data);

//         // Jika sistem membutuhkan verifikasi email, kirimkan notifikasi verifikasi
//         if ($user) {
//             event(new Registered($user));  // Kirim email verifikasi
//         }

//         // Redirect kembali dengan pesan sukses
//         return redirect()->back()->with('success', 'Akun berhasil dibuat! Silakan cek email Anda untuk verifikasi.');
//     }

//     public function update(Request $request, $id)
//     {
//         // Validasi input untuk update data user
//         $request->validate([
//             'nama' => 'required',
//             'username' => 'required|unique:users,username,' . $id . ',id',
//             'email' => 'required',
//             'password' => 'required|min:6',
//             'image' => 'nullable|image|mimes:jpg,jpeg,png|max:2048',
//         ]);

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

//         $user->name = $request->nama;
//         $user->username = $request->username;
//         $user->email = $request->email;
//         $user->password = Crypt::encryptString($request->password);

//         // Jika ada file gambar yang diunggah
//         if ($request->hasFile('image')) {
//             // Hapus gambar lama jika ada
//             if ($user->image && file_exists(public_path($user->image))) {
//                 unlink(public_path($user->image));
//             }

//             // Simpan gambar baru
//             $foto = $request->file('image');
//             $namaFoto = time() . '.' . $foto->getClientOriginalExtension();
//             $foto->move(public_path('upload/user'), $namaFoto);
//             $user->image = 'upload/user/' . $namaFoto; // Simpan path ke database
//         }

//         $user->save();

//         return redirect()->back()->with('success', 'Data user berhasil diperbarui.');
//     }

//     public function destroy($id)
//     {
//         $user = User::findOrFail($id);
//         $user->delete();

//         return redirect()->back()->with('success', 'User berhasil dihapus');
//     }
// }
