You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
85 lines
2.1 KiB
85 lines
2.1 KiB
<?php
|
|
|
|
namespace App\Components\Permission;
|
|
|
|
use Livewire\Component;
|
|
use Livewire\Attributes\On;
|
|
use Livewire\WithPagination;
|
|
use Spatie\Permission\Models\Permission;
|
|
|
|
class Manager extends Component
|
|
{
|
|
use WithPagination;
|
|
|
|
protected string $paginationTheme = 'bootstrap';
|
|
|
|
public string $mode = 'index'; // 'index' hoặc 'form'
|
|
public ?int $editingId = null; // id đang edit
|
|
public string $search = ''; // search query
|
|
public string $name = ''; // dùng cho form
|
|
|
|
// Khi sự kiện 'permissionSaved' được dispatch, gọi showIndex()
|
|
#[On('permissionSaved')]
|
|
public function showIndex()
|
|
{
|
|
$this->mode = 'index';
|
|
$this->resetPage();
|
|
$this->name = '';
|
|
$this->editingId = null;
|
|
}
|
|
|
|
// Khi search thay đổi, reset pagination
|
|
public function updatingSearch()
|
|
{
|
|
$this->resetPage();
|
|
}
|
|
|
|
// Chuyển sang form (create/edit)
|
|
public function showForm(int $id = null)
|
|
{
|
|
$this->mode = 'form';
|
|
$this->editingId = $id;
|
|
$this->name = $id
|
|
? Permission::findOrFail($id)->name
|
|
: '';
|
|
}
|
|
|
|
// Xóa
|
|
public function delete(int $id)
|
|
{
|
|
Permission::findOrFail($id)->delete();
|
|
session()->flash('message', 'Permission deleted.');
|
|
$this->resetPage();
|
|
}
|
|
|
|
// Lưu form
|
|
public function save()
|
|
{
|
|
$rules = [
|
|
'name' => 'required|string|unique:permissions,name'
|
|
. ($this->editingId ? ",{$this->editingId}" : '')
|
|
];
|
|
|
|
$this->validate($rules);
|
|
|
|
Permission::updateOrCreate(
|
|
['id' => $this->editingId],
|
|
['name' => $this->name]
|
|
);
|
|
|
|
session()->flash('message', 'Permission saved.');
|
|
|
|
// Gọi sự kiện nội bộ Livewire 3
|
|
$this->dispatch('permissionSaved');
|
|
}
|
|
|
|
// Render view
|
|
public function render()
|
|
{
|
|
$permissions = Permission::where('name', 'like', "%{$this->search}%")
|
|
->orderByDesc('id')
|
|
->paginate(10);
|
|
|
|
return view('components.permission.manager', compact('permissions'));
|
|
}
|
|
}
|
|
|