72 lines
1.9 KiB
PHP
72 lines
1.9 KiB
PHP
<?php
|
|
|
|
namespace App\Livewire\Directories;
|
|
|
|
use App\Models\Priority;
|
|
use Illuminate\Validation\Rule;
|
|
use Livewire\Attributes\Layout;
|
|
use Livewire\Component;
|
|
|
|
#[Layout('layouts.app')]
|
|
class PrioritiesPage extends Component
|
|
{
|
|
public string $name = '';
|
|
|
|
public string $colorHex = '#3B82F6';
|
|
|
|
public int $level = 1;
|
|
|
|
public ?int $editingId = null;
|
|
|
|
public function mount(): void
|
|
{
|
|
abort_unless(auth()->user()->isAdmin(), 403);
|
|
}
|
|
|
|
public function edit(int $id): void
|
|
{
|
|
$model = Priority::query()->findOrFail($id);
|
|
$this->editingId = $model->id;
|
|
$this->name = $model->name;
|
|
$this->colorHex = $model->color_hex;
|
|
$this->level = $model->level;
|
|
}
|
|
|
|
public function save(): void
|
|
{
|
|
$levelRule = Rule::unique('priorities', 'level');
|
|
if ($this->editingId) {
|
|
$levelRule = $levelRule->ignore($this->editingId);
|
|
}
|
|
|
|
$this->validate([
|
|
'name' => ['required', 'string', 'max:255'],
|
|
'colorHex' => ['required', 'regex:/^#[0-9A-Fa-f]{6}$/'],
|
|
'level' => ['required', 'integer', 'between:1,4', $levelRule],
|
|
]);
|
|
|
|
Priority::query()->updateOrCreate(
|
|
['id' => $this->editingId],
|
|
['name' => $this->name, 'color_hex' => $this->colorHex, 'level' => $this->level]
|
|
);
|
|
|
|
$this->reset(['name', 'editingId']);
|
|
$this->colorHex = '#3B82F6';
|
|
$this->level = 1;
|
|
session()->flash('success', 'Приоритет сохранен.');
|
|
}
|
|
|
|
public function delete(int $id): void
|
|
{
|
|
Priority::query()->findOrFail($id)->delete();
|
|
session()->flash('success', 'Приоритет удален.');
|
|
}
|
|
|
|
public function render()
|
|
{
|
|
return view('livewire.directories.priorities-page', [
|
|
'items' => Priority::query()->orderBy('level')->get(),
|
|
])->title('Справочник: Приоритеты');
|
|
}
|
|
}
|