95 lines
2.6 KiB
PHP
95 lines
2.6 KiB
PHP
<?php
|
|
|
|
namespace App\Livewire\Directories;
|
|
|
|
use App\Models\Status;
|
|
use Illuminate\Validation\Rule;
|
|
use Livewire\Attributes\Layout;
|
|
use Livewire\Component;
|
|
|
|
#[Layout('layouts.app')]
|
|
class StatusesPage extends Component
|
|
{
|
|
public string $name = '';
|
|
|
|
public string $colorHex = '#3B82F6';
|
|
|
|
public string $slug = '';
|
|
|
|
public bool $isFinal = false;
|
|
|
|
public int $sortOrder = 1;
|
|
|
|
public ?int $editingId = null;
|
|
|
|
public function mount(): void
|
|
{
|
|
abort_unless(auth()->user()->isAdmin(), 403);
|
|
}
|
|
|
|
public function edit(int $id): void
|
|
{
|
|
$model = Status::query()->findOrFail($id);
|
|
$this->editingId = $model->id;
|
|
$this->name = $model->name;
|
|
$this->colorHex = $model->color_hex;
|
|
$this->slug = $model->slug;
|
|
$this->isFinal = $model->is_final;
|
|
$this->sortOrder = $model->sort_order;
|
|
}
|
|
|
|
public function save(): void
|
|
{
|
|
$slugRule = Rule::unique('statuses', 'slug');
|
|
if ($this->editingId) {
|
|
$slugRule = $slugRule->ignore($this->editingId);
|
|
}
|
|
|
|
$this->validate([
|
|
'name' => ['required', 'string', 'max:255'],
|
|
'colorHex' => ['required', 'regex:/^#[0-9A-Fa-f]{6}$/'],
|
|
'slug' => ['required', 'string', $slugRule],
|
|
'isFinal' => ['boolean'],
|
|
'sortOrder' => ['required', 'integer', 'min:1', 'max:100'],
|
|
]);
|
|
|
|
Status::query()->updateOrCreate(
|
|
['id' => $this->editingId],
|
|
[
|
|
'name' => $this->name,
|
|
'color_hex' => $this->colorHex,
|
|
'slug' => $this->slug,
|
|
'is_final' => $this->isFinal,
|
|
'sort_order' => $this->sortOrder,
|
|
]
|
|
);
|
|
|
|
$this->reset(['name', 'slug', 'editingId']);
|
|
$this->colorHex = '#3B82F6';
|
|
$this->isFinal = false;
|
|
$this->sortOrder = 1;
|
|
session()->flash('success', 'Статус сохранен.');
|
|
}
|
|
|
|
public function delete(int $id): void
|
|
{
|
|
$status = Status::query()->findOrFail($id);
|
|
|
|
if ($status->requests()->exists()) {
|
|
session()->flash('error', 'Нельзя удалить статус, который используется в заявках.');
|
|
|
|
return;
|
|
}
|
|
|
|
$status->delete();
|
|
session()->flash('success', 'Статус удален.');
|
|
}
|
|
|
|
public function render()
|
|
{
|
|
return view('livewire.directories.statuses-page', [
|
|
'items' => Status::query()->orderBy('sort_order')->get(),
|
|
])->title('Справочник: Статусы');
|
|
}
|
|
}
|