236 lines
6.1 KiB
PHP
236 lines
6.1 KiB
PHP
<?php
|
|
|
|
namespace App\Livewire;
|
|
|
|
use Livewire\Component;
|
|
|
|
class CodeEdit extends Component
|
|
{
|
|
public $componentId; // Nuevo: ID del componente padre
|
|
public $name = '';
|
|
public $codeInput = '';
|
|
public $labelInput = '';
|
|
public $maxLength = 3;
|
|
public $documentTypes = [];
|
|
public $sortBy = 'code';
|
|
public $sortDirection = 'asc';
|
|
|
|
protected $rules = [
|
|
'name' => 'required|string|min:2|max:50',
|
|
'codeInput' => 'required|string',
|
|
'labelInput' => 'required|string',
|
|
'maxLength' => 'required|integer|min:2|max:12',
|
|
];
|
|
|
|
public function mount($componentId, $initialName = '')
|
|
{
|
|
$this->componentId = $componentId;
|
|
$this->name = $initialName;
|
|
$this->maxLength = 3;
|
|
|
|
// Disparar evento inicial para establecer el nombre
|
|
$this->dispatch('nameUpdated',
|
|
componentId: $this->componentId,
|
|
data: [
|
|
'name' => $this->name
|
|
]
|
|
);
|
|
}
|
|
|
|
public function updateName()
|
|
{
|
|
$this->validate([
|
|
'name' => 'required|string|min:2|max:50',
|
|
]);
|
|
|
|
$this->dispatch('nameUpdated',
|
|
componentId: $this->componentId,
|
|
data: [
|
|
'name' => $this->name
|
|
]
|
|
);
|
|
}
|
|
|
|
public function updateMaxLength()
|
|
{
|
|
$this->validate([
|
|
'maxLength' => 'integer|min:2|max:12',
|
|
]);
|
|
|
|
if (strlen($this->codeInput) > $this->maxLength) {
|
|
$this->codeInput = substr($this->codeInput, 0, $this->maxLength);
|
|
}
|
|
}
|
|
|
|
public function addField()
|
|
{
|
|
$this->validate([
|
|
'codeInput' => "required|string|size:{$this->maxLength}",
|
|
'labelInput' => 'required|string|min:1',
|
|
], [
|
|
'codeInput.size' => "El código debe tener exactamente {$this->maxLength} caracteres",
|
|
]);
|
|
|
|
$this->documentTypes[] = [
|
|
'code' => $this->codeInput,
|
|
'label' => $this->labelInput,
|
|
'max_length' => $this->maxLength,
|
|
];
|
|
|
|
$this->sortList();
|
|
$this->reset(['codeInput', 'labelInput']);
|
|
|
|
$this->dispatch('componentUpdated',
|
|
componentId: $this->componentId, // Cambiado aquí
|
|
data: [
|
|
'documentTypes' => $this->documentTypes,
|
|
'maxLength' => $this->maxLength
|
|
]
|
|
);
|
|
}
|
|
|
|
public function addCode()
|
|
{
|
|
$this->validate([
|
|
'codeInput' => "required|string|size:{$this->maxLength}",
|
|
], [
|
|
'codeInput.size' => "El código debe tener exactamente {$this->maxLength} caracteres",
|
|
]);
|
|
|
|
$this->dispatch('focus-label-input');
|
|
}
|
|
|
|
public function addLabel()
|
|
{
|
|
$this->validate([
|
|
'labelInput' => 'required|string|min:1',
|
|
]);
|
|
|
|
if (!empty($this->codeInput) && !empty($this->labelInput)) {
|
|
$this->addField();
|
|
}
|
|
}
|
|
|
|
public function removeField($index)
|
|
{
|
|
if (isset($this->documentTypes[$index])) {
|
|
unset($this->documentTypes[$index]);
|
|
$this->documentTypes = array_values($this->documentTypes);
|
|
|
|
$this->dispatch('componentUpdated',
|
|
componentId: $this->componentId, // Cambiado aquí
|
|
data: [
|
|
'documentTypes' => $this->documentTypes,
|
|
'maxLength' => $this->maxLength
|
|
]
|
|
);
|
|
}
|
|
}
|
|
|
|
public function updatedMaxLength($value)
|
|
{
|
|
$this->validate([
|
|
'maxLength' => 'integer|min:2|max:12',
|
|
]);
|
|
|
|
if (strlen($this->codeInput) > $value) {
|
|
$this->codeInput = substr($this->codeInput, 0, $value);
|
|
}
|
|
}
|
|
|
|
public function updatedCodeInput($value)
|
|
{
|
|
if (strlen($value) > $this->maxLength) {
|
|
$this->codeInput = substr($value, 0, $this->maxLength);
|
|
}
|
|
|
|
$this->codeInput = strtoupper($value);
|
|
}
|
|
|
|
public function sortByCode()
|
|
{
|
|
if ($this->sortBy === 'code') {
|
|
$this->sortDirection = $this->sortDirection === 'asc' ? 'desc' : 'asc';
|
|
} else {
|
|
$this->sortBy = 'code';
|
|
$this->sortDirection = 'asc';
|
|
}
|
|
$this->sortList();
|
|
}
|
|
|
|
public function sortByLabel()
|
|
{
|
|
if ($this->sortBy === 'label') {
|
|
$this->sortDirection = $this->sortDirection === 'asc' ? 'desc' : 'asc';
|
|
} else {
|
|
$this->sortBy = 'label';
|
|
$this->sortDirection = 'asc';
|
|
}
|
|
$this->sortList();
|
|
}
|
|
|
|
private function sortList()
|
|
{
|
|
$direction = $this->sortDirection === 'asc' ? SORT_ASC : SORT_DESC;
|
|
|
|
if ($this->sortBy === 'code') {
|
|
array_multisort(
|
|
array_column($this->documentTypes, 'code'),
|
|
$direction,
|
|
SORT_STRING,
|
|
$this->documentTypes
|
|
);
|
|
} else {
|
|
array_multisort(
|
|
array_column($this->documentTypes, 'label'),
|
|
$direction,
|
|
SORT_STRING,
|
|
$this->documentTypes
|
|
);
|
|
}
|
|
}
|
|
|
|
public function getSortedDocumentTypesProperty()
|
|
{
|
|
$sorted = $this->documentTypes;
|
|
$direction = $this->sortDirection === 'asc' ? SORT_ASC : SORT_DESC;
|
|
|
|
if ($this->sortBy === 'code') {
|
|
array_multisort(
|
|
array_column($sorted, 'code'),
|
|
$direction,
|
|
SORT_STRING,
|
|
$sorted
|
|
);
|
|
} else {
|
|
array_multisort(
|
|
array_column($sorted, 'label'),
|
|
$direction,
|
|
SORT_STRING,
|
|
$sorted
|
|
);
|
|
}
|
|
|
|
return $sorted;
|
|
}
|
|
|
|
public function getTotalDocumentTypesProperty()
|
|
{
|
|
return count($this->documentTypes);
|
|
}
|
|
|
|
public function getSortIcon($column)
|
|
{
|
|
if ($this->sortBy !== $column) {
|
|
return 'sort';
|
|
}
|
|
|
|
return $this->sortDirection === 'asc' ? 'sort-up' : 'sort-down';
|
|
}
|
|
|
|
public function render()
|
|
{
|
|
return view('livewire.code-edit');
|
|
}
|
|
}
|