<?php

namespace App\Http\Livewire\Administracion\Usuario;

use Livewire\Component;
use App\Models\User;
use Illuminate\Support\Facades\Hash;
use Spatie\Permission\Models\Role;
use Spatie\Permission\Models\Permission;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Validator;

class ShowUsuarios extends Component
{
    public $dataUsers;

    public $nombre, $username, $numdoc, $email, $rol, $estadoUser, $userId;

    protected $rule = [
        'nombre' => 'required',
        /* 'username' => 'required',
        'numdoc' => 'required',
        'email' => 'required',
        'rol' => 'required',
        'estadoUser' => 'required' */
    ];

    public function deleteUser($id)
    {
        /* dd($user); */
        /* $this->validate($this->rule); */

        try{
            $user = User::find($id);
            $user->update(['estado' => 0]);
            $this->emit('deleteUser');
            $this->mount();
        }catch(\Exception $e){
            $this->emit('error','No se puede eliminar el usuario');
        }
    }

    public function editUser($id)
    {
        $user = User::find($id);
        $this->nombre = $user->name;
        $this->username = $user->username;
        $this->numdoc = $user->numdoc;
        $this->email = $user->email;
        $this->rol = $user->rol;
        $this->estadoUser = $user->estado;
        $this->userId = $id;

        $this->emit('editUser');
    }

    public function updateUser($id)
    {
        /* dd($id); */
        try{
            $user = User::find($id);
            $user->update([
                'name' => $this->nombre,
                'username' => $this->username,
                'numdoc' => $this->numdoc,
                'email' => $this->email,
                'rol' => $this->rol,
                'estado' => $this->estadoUser
            ]);
            $user->syncRoles($this->rol);

        $this->emit('updateUser',$id);
        $this->emit('closeEditUser');
        $this->mount();
        }catch(\Exception $e){
            $this->emit('error','No se puede actualizar el usuario');
        }
    
    }

    public function enableUser($id)
    {
        $user = User::find($id);
        $user->update(['estado' => 1]);
        $this->emit('enableUser');
        $this->mount();
    }

    public function resetUser($id)
    {
        $user = User::find($id);
        $user->password = hash::make($user->numdoc);
        $user->save();
        $this->emit('resetUser');
        $this->mount();
    }

    public function mount()
    {
        $this->nombre = '';
        $this->username = '';
        $this->numdoc = '';
        $this->email = '';
        $this->rol = '';
        $this->estadoUser = '';
        $this->userId = '';

        $this->dataUsers = User::all()->where('rol','!=','root');
    }

    public function render()
    {
        return view('livewire.administracion.usuario.show-usuarios');
    }
}
