138 lines
3.5 KiB
PHP
Executable File
138 lines
3.5 KiB
PHP
Executable File
<?php
|
|
|
|
namespace App\Http\Livewire;
|
|
|
|
use App\Models\Role;
|
|
use App\Models\User;
|
|
use Livewire\Component;
|
|
use Jantinnerezo\LivewireAlert\LivewireAlert;
|
|
|
|
class ShowRoles extends Component
|
|
{
|
|
use LivewireAlert;
|
|
|
|
public $editar = false;
|
|
public $nombre;
|
|
public $add;
|
|
public $ro;
|
|
public $filtro_id;
|
|
|
|
protected $nombres_reservados = ['super', 'administrador'];
|
|
|
|
public function render()
|
|
{
|
|
if (auth()->user()->rol_id == 5) {
|
|
|
|
$query = Role::withCount('usuarios');
|
|
if ($this->filtro_id) {
|
|
$query->where('subvendedor_id', $this->filtro_id);
|
|
}
|
|
$consulta_roles = $query->get();
|
|
|
|
$consulta_usuarios = User::orderBy('name', 'asc')->get();
|
|
} else {
|
|
$consulta_roles = Role::where('subvendedor_id', auth()->user()->id)->withCount('usuarios')->get();
|
|
$consulta_usuarios = null;
|
|
}
|
|
|
|
return view('livewire.show-roles', [
|
|
'roles' => $consulta_roles,
|
|
'usuarios' => $consulta_usuarios
|
|
]);
|
|
}
|
|
|
|
public function editar($id)
|
|
{
|
|
$this->nombre = Role::find($id)->nombre;
|
|
$this->ro = $id;
|
|
$this->editar = true;
|
|
}
|
|
|
|
public function crear()
|
|
{
|
|
$nombre_limpio = strtolower(trim($this->nombre));
|
|
|
|
if (in_array($nombre_limpio, $this->nombres_reservados)) {
|
|
$this->alert('warning', 'Este nombre de rol está reservado y no se puede usar.', [
|
|
'position' => 'top'
|
|
]);
|
|
return;
|
|
}
|
|
|
|
if (!empty($this->nombre)) {
|
|
$crear_rol = new Role;
|
|
$crear_rol->nombre = $this->nombre;
|
|
$crear_rol->subvendedor_id = (auth()->user()->rol_id == 5) ? null : auth()->user()->id;
|
|
$crear_rol->save();
|
|
|
|
$this->alert('success', 'Rol creado correctamente!', [
|
|
'position' => 'top'
|
|
]);
|
|
$this->add = false;
|
|
} else {
|
|
$this->alert('warning', 'Campo Nombre sin diligenciar!', [
|
|
'position' => 'top'
|
|
]);
|
|
}
|
|
}
|
|
|
|
public function eliminar($id)
|
|
{
|
|
$this->ro = $id;
|
|
$this->alert('warning', '¿Eliminar rol?', [
|
|
'position' => 'center',
|
|
'timer' => '10000',
|
|
'toast' => false,
|
|
'text' => 'Esta seguro',
|
|
'showConfirmButton' => true,
|
|
'onConfirmed' => 'confirmed',
|
|
'showDenyButton' => false,
|
|
'onDenied' => '',
|
|
'showCancelButton' => true,
|
|
'onDismissed' => '',
|
|
'cancelButtonText' => 'Salir',
|
|
'confirmButtonText' => 'Si',
|
|
'customClass' => [
|
|
'confirmButton' => 'bg-green-500',
|
|
'denyButton' => 'bg-red-500',
|
|
'cancelButton' => 'bg-gray-500',
|
|
]
|
|
]);
|
|
}
|
|
|
|
public function getListeners()
|
|
{
|
|
return [
|
|
'confirmed'
|
|
];
|
|
}
|
|
|
|
public function confirmed()
|
|
{
|
|
$eliminar = Role::find($this->ro)->delete();
|
|
$this->alert('success', 'Rol eliminado!', [
|
|
'position' => 'top'
|
|
]);
|
|
}
|
|
|
|
|
|
public function limpiarInputAdd()
|
|
{
|
|
$this->editar = false;
|
|
$this->add = false;
|
|
}
|
|
|
|
public function actualizar()
|
|
{
|
|
$eliminar = Role::find($this->ro);
|
|
//dd($eliminar);
|
|
$eliminar->update(['nombre' => $this->nombre]);
|
|
|
|
$this->alert('success', 'Rol actualizado!', [
|
|
'position' => 'top'
|
|
]);
|
|
|
|
$this->editar = false;
|
|
}
|
|
}
|