275 lines
8.3 KiB
PHP
275 lines
8.3 KiB
PHP
<?php
|
|
|
|
namespace App\Livewire;
|
|
|
|
use Livewire\Component;
|
|
use App\Models\Project;
|
|
use App\Models\ProjectCodingConfig;
|
|
|
|
class ProjectNameCoder extends Component
|
|
{
|
|
public $components = [];
|
|
public $nextId = 1;
|
|
public $project;
|
|
|
|
protected $listeners = [
|
|
'nameUpdated' => 'headerLabelUpdate',
|
|
'componentUpdated' => 'handleComponentUpdate',
|
|
'removeComponent' => 'removeComponent'
|
|
];
|
|
|
|
public function mount(Project $project)
|
|
{
|
|
// Inicializar con un componente vacío
|
|
$this->project = $project;
|
|
|
|
if ($project->codingConfig) {
|
|
$this->loadDatabaseConfiguration();
|
|
} else {
|
|
$this->addComponent();
|
|
}
|
|
}
|
|
|
|
private function loadDatabaseConfiguration()
|
|
{
|
|
// Buscar la configuración de codificación del proyecto
|
|
$config = $this->project->codingConfig;
|
|
|
|
if ($config && isset($config->elements['components'])) {
|
|
$this->components = $config->elements['components'];
|
|
$this->nextId = count($this->components) + 1;
|
|
|
|
// Asegurar que cada componente tenga los campos necesarios
|
|
foreach ($this->components as &$component) {
|
|
$component['data'] = $component['data'] ?? [];
|
|
$component['order'] = $component['order'] ?? 0;
|
|
$component['headerLabel'] = $component['headerLabel'] ?? '';
|
|
$component['documentTypes'] = $component['documentTypes'] ?? [];
|
|
}
|
|
} else {
|
|
// Si no hay configuración, inicializar con un componente vacío
|
|
$this->addComponent();
|
|
}
|
|
}
|
|
|
|
public function addComponent()
|
|
{
|
|
$id = $this->nextId++;
|
|
$this->components[] = [
|
|
'id' => $id,
|
|
'data' => [],
|
|
'order' => count($this->components),
|
|
'headerLabel' => ''
|
|
];
|
|
}
|
|
|
|
public function removeComponent($componentId)
|
|
{
|
|
$this->components = array_filter($this->components, function($component) use ($componentId) {
|
|
return $component['id'] != $componentId;
|
|
});
|
|
|
|
// Reindexar el orden
|
|
$this->reorderComponents();
|
|
}
|
|
|
|
public function headerLabelUpdate($componentId, $data)
|
|
{
|
|
foreach ($this->components as &$component) {
|
|
if ($component['id'] == $componentId) {
|
|
$component['headerLabel'] = $data['name'];
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
public function handleComponentUpdate($componentId, $data)
|
|
{
|
|
foreach ($this->components as &$component) {
|
|
if ($component['id'] == $componentId) {
|
|
$component['data'] = $data;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
public function updateComponentOrder($orderedIds)
|
|
{
|
|
foreach ($orderedIds as $index => $id) {
|
|
foreach ($this->components as &$component) {
|
|
if ($component['id'] == $id) {
|
|
$component['order'] = $index;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
// Ordenar el array por el campo 'order'
|
|
usort($this->components, function($a, $b) {
|
|
return $a['order'] - $b['order'];
|
|
});
|
|
}
|
|
|
|
private function reorderComponents()
|
|
{
|
|
foreach ($this->components as $index => &$component) {
|
|
$component['order'] = $index;
|
|
}
|
|
}
|
|
|
|
public function moveComponentUp($componentId)
|
|
{
|
|
$index = $this->findComponentIndex($componentId);
|
|
|
|
if ($index > 0) {
|
|
// Intercambiar con el componente anterior
|
|
$temp = $this->components[$index];
|
|
$this->components[$index] = $this->components[$index - 1];
|
|
$this->components[$index - 1] = $temp;
|
|
|
|
// Actualizar órdenes
|
|
$this->reorderComponents();
|
|
}
|
|
}
|
|
|
|
public function moveComponentDown($componentId)
|
|
{
|
|
$index = $this->findComponentIndex($componentId);
|
|
|
|
if ($index < count($this->components) - 1) {
|
|
// Intercambiar con el componente siguiente
|
|
$temp = $this->components[$index];
|
|
$this->components[$index] = $this->components[$index + 1];
|
|
$this->components[$index + 1] = $temp;
|
|
|
|
// Actualizar órdenes
|
|
$this->reorderComponents();
|
|
}
|
|
}
|
|
|
|
private function findComponentIndex($componentId)
|
|
{
|
|
foreach ($this->components as $index => $component) {
|
|
if ($component['id'] == $componentId) {
|
|
return $index;
|
|
}
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
public function getComponentsCountProperty()
|
|
{
|
|
return count($this->components);
|
|
}
|
|
|
|
public function getTotalDocumentTypesProperty()
|
|
{
|
|
$total = 0;
|
|
foreach ($this->components as $component) {
|
|
if (isset($component['data']['documentTypes'])) {
|
|
$total += count($component['data']['documentTypes']);
|
|
}
|
|
}
|
|
return $total;
|
|
}
|
|
|
|
public function saveConfiguration()
|
|
{
|
|
try {
|
|
// Preparar la configuración completa
|
|
$configData = [
|
|
'components' => $this->components,
|
|
'total_components' => $this->componentsCount,
|
|
//'total_document_types' => $this->totalDocumentTypes,
|
|
//'generated_format' => $this->generateFormatString(),
|
|
//'last_updated' => now()->toDateTimeString(),
|
|
];
|
|
|
|
// Buscar o crear la configuración de codificación
|
|
$codingConfig = ProjectCodingConfig::firstOrNew(['project_id' => $this->project->id]);
|
|
|
|
// Actualizar los campos
|
|
$codingConfig->fill([
|
|
'elements' => $configData,
|
|
'format' => $this->generateFormatString(),
|
|
'auto_generate' => true,
|
|
]);
|
|
|
|
$codingConfig->save();
|
|
|
|
// Emitir evento de éxito
|
|
$this->dispatch('configurationSaved', [
|
|
'message' => 'Configuración guardada exitosamente',
|
|
'format' => $this->generateFormatString()
|
|
]);
|
|
|
|
} catch (\Exception $e) {
|
|
$this->dispatch('configurationError', [
|
|
'message' => 'Error al guardar la configuración: ' . $e->getMessage()
|
|
]);
|
|
}
|
|
}
|
|
|
|
private function autoSave()
|
|
{
|
|
// Auto-guardar cada 30 segundos de inactividad o cuando haya cambios importantes
|
|
// Esto es opcional pero mejora la experiencia de usuario
|
|
$this->dispatch('configurationAutoSaved');
|
|
}
|
|
|
|
private function generateFormatString()
|
|
{
|
|
$formatParts = [];
|
|
|
|
// Agregar el código del proyecto
|
|
$formatParts[] = $this->project->code ?? $this->project->reference ?? 'PROJ';
|
|
|
|
// Agregar cada componente
|
|
foreach ($this->components as $component) {
|
|
if (!empty($component['headerLabel'])) {
|
|
$formatParts[] = '[' . strtoupper($component['headerLabel']) . ']';
|
|
}
|
|
}
|
|
|
|
// Agregar el nombre del documento
|
|
$formatParts[] = '[DOCUMENT_NAME]';
|
|
|
|
return implode('-', $formatParts);
|
|
}
|
|
|
|
public function getExampleCodeAttribute()
|
|
{
|
|
$exampleParts = [];
|
|
|
|
// Agregar el código del proyecto
|
|
$exampleParts[] = $this->project->code ?? $this->project->reference ?? 'PROJ';
|
|
|
|
// Agregar cada componente con un valor de ejemplo
|
|
foreach ($this->components as $component) {
|
|
if (!empty($component['headerLabel'])) {
|
|
$exampleParts[] = $this->getExampleForComponent($component);
|
|
}
|
|
}
|
|
|
|
// Agregar nombre de documento de ejemplo
|
|
$exampleParts[] = 'Documento-Ejemplo';
|
|
|
|
return implode('-', $exampleParts);
|
|
}
|
|
|
|
private function getExampleForComponent($component)
|
|
{
|
|
if (isset($component['data']['documentTypes']) && count($component['data']['documentTypes']) > 0) {
|
|
// Tomar el primer tipo de documento como ejemplo
|
|
$firstType = $component['data']['documentTypes'][0];
|
|
return $firstType['code'] ?? $firstType['name'] ?? 'TIPO';
|
|
}
|
|
|
|
return 'VALOR';
|
|
}
|
|
|
|
public function render()
|
|
{
|
|
return view('livewire.project-name-coder');
|
|
}
|
|
}
|