{{-- Smart Field Component with State and Role Awareness --}} {{-- Usage: @include('tramites.partials.smart-field', ['field' => 'nombre_producto']) --}} @php $stateHandler = app(\App\Services\TramiteStateHandler::class); $stateId = $tramite->estado_tramite_id ?? 1; $userRole = $stateHandler->getUserRole(); // Check if field should be hidden if ($stateHandler->isFieldHidden($stateId, $field, $userRole)) { return; // Don't render hidden fields } // Get field definition and permissions $fieldDef = $stateHandler->getFieldDefinition($field); $isEditable = $stateHandler->isFieldEditable($stateId, $field, $userRole); $attributes = $stateHandler->getFieldAttributes($stateId, $field, $userRole); // Get field value and handle array/null cases // Special handling for fields that are now in the producto_especifico $productFields = ['num_registro', 'fecha_vencimiento_registro', 'estado_registro_id', 'nombre_producto', 'marca', 'unidad_de_negocio', 'expediente', 'ingrediente_principal', 'concentracion', 'indicacion', 'via_administracion', 'condicion_de_venta', 'tiempo_de_vida_util', 'condicion_de_almacenamiento', 'titular', 'importador', 'fabricante', 'acondicionador']; // Special handling for producto_id field if ($field === 'producto_id' && $tramite->productoEspecifico && $tramite->productoEspecifico->tipoProducto) { // Get tipo_producto from producto_especifico $fieldValue = old($field, $tramite->productoEspecifico->tipoProducto->id); } elseif ($field === 'nombre_producto' && $tramite->productoEspecifico) { // Special handling for nombre_producto - use 'nombre' field from producto_especifico $fieldValue = old($field, $tramite->productoEspecifico->nombre); } elseif (in_array($field, $productFields) && $tramite->productoEspecifico) { // Get value from producto_especifico $fieldValue = old($field, $tramite->productoEspecifico->{$field}); } else { // Get value from tramite (default behavior) $fieldValue = old($field, $tramite->{$field}); } if (is_array($fieldValue)) { $fieldValue = ''; } if ($fieldValue === null) { $fieldValue = ''; } // Merge with any additional attributes passed $additionalAttributes = $additionalAttributes ?? []; // Separate container attributes from input attributes $containerAttributes = []; $inputAttributes = []; foreach ($additionalAttributes as $key => $value) { if (in_array($key, ['class', 'data-depends-on', 'data-show-when', 'style'])) { $containerAttributes[$key] = $value; } else { $inputAttributes[$key] = $value; } } // Merge input attributes $attributes = array_merge($attributes, $inputAttributes); // Get field configuration $type = $type ?? $fieldDef['type'] ?? 'text'; $label = $label ?? $fieldDef['label'] ?? strtoupper(str_replace('_', ' ', $field)); $colSize = $colSize ?? '4'; $rows = $rows ?? $fieldDef['rows'] ?? '3'; // Define fields that should be uppercase without accents in TICKET state $uppercaseFields = ['indicacion', 'via_administracion', 'condicion_de_venta', 'condicion_de_almacenamiento', 'titular', 'importador', 'fabricante', 'acondicionador']; $shouldApplyUppercase = $stateId == 8 && in_array($field, $uppercaseFields) && $isEditable; // Define fields that should be uppercase without accents in any state $globalUppercaseFields = ['responsable_entregar_informacion']; $shouldApplyGlobalUppercase = in_array($field, $globalUppercaseFields) && $isEditable; // Add field definition attributes (min, max, placeholder, etc.) $fieldDefAttributes = []; // For fields that depend on user assignment, only add min/max if user is assigned $userDependentFields = ['fecha_planeada_de_entrega_del_dossier', 'fecha_maxima_de_radicacion', 'carga_laboral_en_horas']; $hasUserAssigned = !empty($tramite->usuario_responsable_id); if (in_array($field, $userDependentFields) && !$hasUserAssigned) { // Don't add min/max validation for user-dependent fields when no user is assigned if (isset($fieldDef['max'])) $fieldDefAttributes['max'] = $fieldDef['max']; if (isset($fieldDef['step'])) $fieldDefAttributes['step'] = $fieldDef['step']; } else { // Add all field definition attributes normally if (isset($fieldDef['min'])) $fieldDefAttributes['min'] = $fieldDef['min']; if (isset($fieldDef['max'])) $fieldDefAttributes['max'] = $fieldDef['max']; if (isset($fieldDef['step'])) $fieldDefAttributes['step'] = $fieldDef['step']; } if (isset($fieldDef['placeholder'])) $fieldDefAttributes['placeholder'] = $fieldDef['placeholder']; // Merge field definition attributes with other attributes $attributes = array_merge($fieldDefAttributes, $attributes); // Build attribute string for input $attributeString = ''; foreach ($attributes as $key => $value) { if ($key === 'class') { $attributeString .= ' class="form-control ' . $value . '"'; } else { $attributeString .= ' ' . $key . '="' . $value . '"'; } } // If no class was set, add default if (!isset($attributes['class'])) { $attributeString .= ' class="form-control"'; } // Build container attribute string $containerAttributeString = ''; foreach ($containerAttributes as $key => $value) { $containerAttributeString .= ' ' . $key . '="' . $value . '"'; } // Handle special field types if ($type === 'hidden') { $colSize = '0'; // Hidden fields don't take space } @endphp @if($type === 'hidden') @else