<?php

namespace App\Http\Controllers;

use App\Models\User;
use Illuminate\Http\Request;

use Illuminate\Support\Str;
use Spatie\Permission\Models\Role;

class UserController extends Controller
{

public function index(){
    $user = User::with('roles')->get();
    return view('user.index',['title'=> 'Manajemen User | Admin Dashboard','user'=> $user]);
}

public function store(Request $request){
    $request->validate([
        'nip'=>'required|numeric',
        'nama_depan'=>'required',
        'nama_belakang'=>'required',
        'nama_gelar'=>'required',
        'email'=>'required|email',
        'ettd'=>'required',
    ],[
        'nip.required'=>'NIP Wajib Diisi',
        'nip.numeric'=>'NIP/NRP Wajib Berisi Angka saja',
        'nama_depan.required'=>'Nama Depan Wajib Diisi',
        'nama_belakang.required'=>'Nama Belakang Wajib Diisi',
        'nama_gelar.required'=>'Nama Dengan Gelar Lengkap Wajib Diisi',
        'email.required'=>'Email Wajib Diisi',
        'ettd.required'=>'E-TTD Wajib Diisi',
    ]);
    $user = new User;
    $user->nip = $request->nip;
    $user->nama_depan = $request->nama_depan;
    $user->nama_belakang = $request->nama_belakang;
    $user->nama_gelar = $request->nama_gelar;
    $user->email = $request->email;
    $user->ettd = $request->ettd;
    $user->password = $request->password;

    if($user->save()){
        return redirect()->route('users.index')->with('success','Data Telah Berhasil Disimpan');
    }
    return back()->withInput();
    

}

public function update(Request $request){
    $request->validate([
        'nip'=>'required|numeric',
        'nama_depan'=>'required',
        'nama_belakang'=>'required',
        'nama_gelar'=>'required',
        'email'=>'required|email',
        'ettd'=>'required',
    ],[
        'nip.required'=>'NIP Wajib Diisi',
        'nip.numeric'=>'NIP/NRP Wajib Berisi Angka saja',
        'nama_depan.required'=>'Nama Depan Wajib Diisi',
        'nama_belakang.required'=>'Nama Belakang Wajib Diisi',
        'nama_gelar.required'=>'Nama Dengan Gelar Lengkap Wajib Diisi',
        'email.required'=>'Email Wajib Diisi',
        'ettd.required'=>'E-TTD Wajib Diisi',
    ]);
    $user = User::find($request->id);
    $user->nip = $request->nip;
    $user->nama_depan = $request->nama_depan;
    $user->nama_belakang = $request->nama_belakang;
    $user->nama_gelar = $request->nama_gelar;
    $user->email = $request->email;
    $user->ettd = $request->ettd;

    if($request->password==""){
        $user->save();
        return redirect()->route('users.index')->with('success','Data Telah Berhasil Diubah');
    }
    
    $user->password = $request->password;
    if($user->save()){
        return redirect()->route('users.index')->with('success','Data Telah Berhasil Diubah');
    }
    return back()->withInput();
}

public function destroy(Request $request){
    $user = User::findOrFail($request->id);
    if($user->delete()){
        return redirect()->route('users.index')->with('success','Data Telah Dihapus');
    }
    return redirect()->route('users.index')->with('error','Data Gagal Dihapus');

}

public function userroles_index(){
    $user = User::select('id','nama_gelar')->get();
    $data = Role::get();
    return view('user.user_roles_index',['title'=> 'Manajemen Role User | Admin Dashboard','data'=> $data,'user'=>$user]);
}   

public function userroles_store(Request $request){
    $user_roles = new Role;
    $user_roles->name = $request->role_name;

    if($user_roles->save()){
        return redirect()->route('users-roles.index')->with('success','Data Telah Berhasil Disimpan');
    }
    return back()->withInput();
}

public function userroles_assign(Request $request){
    $listUser = $request->input('checkboxes', []);
    $listUser = $request->input('checkboxes', []);
    foreach($listUser as $listUsers) {
        $user = User::find($listUsers);
        $user->assignRole($request->roles_user);
    }
    return redirect()->route('users-roles.index')->with('success','Sukses Memberi Role User');
}

public function userroles_deassign(Request $request){
    $listUser = $request->input('checkboxes', []);
    $listUser = $request->input('checkboxes', []);
    foreach($listUser as $listUsers) {
        $user = User::find($listUsers);
        $user->removeRole($request->roles_user);
    }
    return redirect()->route('users-roles.index')->with('success','Sukses Memberi Role User');
}

}
