<?php

namespace App\Http\Controllers;
use App\Http\Controllers\Controller\view;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use App\Models\User;
use App\Models\Kriteria;
use App\Models\Alternatif;
use App\Models\Role;
use App\Http\Requests\UserRequest;
use App\Http\Requests\KriteriaRequest;
use App\Http\Requests\AlternatifRequest;
use App\Http\Requests\RoleRequest;
use App\Http\Requests\UpdateUserRequest;
use App\Http\Requests\UpdateKriteriaRequest;
use App\Http\Requests\UpdateAlternatifRequest;
use App\Http\Requests\UpdateRoleRequest;



class AdminController extends Controller
{
    //
    public function admindash(){
        return view('admin.admindash');
    }



    public function datauser(){
    $users = User::with('role')->get();  // Eager load the role relationship
    return view('admin.datauser.datauser', compact('users'));
}


    public function edituser(User $user){
        // Ambil semua role dari tabel roles
        $roles = Role::pluck('name', 'id'); // Mengambil nama dan id role
        return view('admin.datauser.edituser', compact('user', 'roles'));
    }
    public function tambahuser(){
        $roles = Role::all(); // Ambil data roles
        return view('admin.datauser.tambahuser', compact('roles'));
    }
    
    public function storeuser(UserRequest $request)
{
    // Validasi data
    $validatedData = $request->validated();

    // Simpan password tanpa hashing
    if ($request->filled('password')) {
        $validatedData['password'] = $request->input('password');  // Tidak melakukan hashing pada password
    }

    // Pastikan role_id adalah ID, bukan nama
    $validatedData['role_id'] = $request->input('role_id');  // Simpan role_id, bukan nama

    // Membuat user baru
    User::create($validatedData);
    return redirect()->route('datauser')->with('success', 'User added successfully!');
}

public function updateuser(UpdateUserRequest $request, User $user)
{
    // Validasi data
    $validatedData = $request->validated();

    // Simpan password tanpa hashing
    if ($request->filled('password')) {
        $validatedData['password'] = $request->input('password');  // Tidak melakukan hashing pada password
    }

    // Pastikan role_id adalah ID, bukan nama
    $validatedData['role_id'] = $request->input('role_id');  // Simpan role_id, bukan nama

    // Mengupdate user dengan data yang sudah divalidasi
    $user->update($validatedData);
    return redirect()->route('datauser');
}


    public function deleteuser(User $user) {
        $user->delete();
        return redirect()->route('datauser')->with('success', 'user deleted successfully');
    }





    public function role(){
        $roles = Role::all();
        return view('admin.role.role', compact('roles'));
    }
    public function editrole(Role $role){
        return view('admin.role.editrole', compact('role'));
    }
    public function tambahrole(){
        return view('admin.role.tambahrole');
    }
    public function storerole(RoleRequest $request){
        $validatedData = $request->validated();
        Role::create($validatedData);
        return redirect()->route('role');
    }
    public function updaterole(UpdateRoleRequest $request, Role $role){
        $validatedData = $request->validated();
        $role->update($validatedData);
        return redirect()->route('role');
    }
    public function deleterole(Role $role) {
        $role->delete();
        return redirect()->route('role')->with('success', 'user deleted successfully');
    }




    public function kriteria(){
        $kriterias = Kriteria::all();
        return view('admin.kriteria.kriteria', compact('kriterias'));
    }
    public function editkriteria(Kriteria $kriteria){
        return view('admin.kriteria.editkriteria', compact('kriteria'));
    }
    public function tambahkriteria(){
        return view('admin.kriteria.tambahkriteria');
    }
    public function storekriteria(KriteriaRequest $request){
        $validatedData = $request->validated();
        Kriteria::create($validatedData);
        return redirect()->route('kriteria');
    }
    public function updatekriteria(UpdateKriteriaRequest $request, Kriteria $kriteria){
        $validatedData = $request->validated();
        $kriteria->update($validatedData);
        return redirect()->route('kriteria');
    }
    public function deletekriteria(Kriteria $kriteria) {
        $kriteria->delete();
        return redirect()->route('kriteria')->with('success', 'user deleted successfully');
    }



    

    public function alternatif(){
        return view('admin.alternatif.alternatif');
    }
    public function editalternatif(Alternatif $alternatif){
        return view('admin.alternatif.editalternatif', compact('alternatif'));
    }
    public function tambahalternatif(){
        return view('admin.alternatif.tambahalternatif');
    }
    public function storealternatif(AlternatifRequest $request){
        $validatedData = $request->validated();
        Alternatif::create($validatedData);
        return redirect()->route('alternatif');
    }
    public function updatealternatif(UpdateAlternatifRequest $request, Alternatif $alternatif){
        $validatedData = $request->validated();
        $alternatif->update($validatedData);
        return redirect()->route('alternatif');
    }
    public function deletealternatif(Alternatif $alternatif) {
        $alternatif->delete();
        return redirect()->route('alternatif')->with('success', 'user deleted successfully');
    }
    
}
