import { Component, Input, OnInit, ViewChild, inject } from '@angular/core' import { FormArray, FormBuilder, FormGroup } from '@angular/forms' import { ApiService } from 'app/services' import { SharedModule } from 'app/shared/shared.module' import { FormValidators, Utils } from 'app/utils' import { NzMessageService } from 'ng-zorro-antd/message' import { AssetCategorySelectComponent } from '../asset-category-select/asset-category-select.component' import { OrgSelectComponent } from '../org-select/org-select.component' import { ASSET_SOURCE_MAP, ASSET_STATUS, MAINTENANCE_STATUS, MAINTENANCE_TYPE, MAX_PAGE_SIZE } from 'app/constants' import { SelectUserByOrgComponent } from '../select-user-by-org/select-user-by-org.component' import { ManufacturerSelectComponent } from '../manufacturer-select/manufacturer-select.component' import { PositionSelectComponent } from '../position-select/position-select.component' import { MaintenanceSelectComponent } from '../maintenance-select/maintenance-select.component' import { NzSafeAny } from 'ng-zorro-antd/core/types' import { NZ_MODAL_DATA } from 'ng-zorro-antd/modal' import { AssetOperationRecordsComponent } from '../asset-operation-records/asset-operation-records.component' import { AssetRepairRecordsComponent } from '../asset-repair-records/asset-repair-records.component' import { UploadComponent } from '../../shared/components/upload/upload.component' @Component({ selector: 'app-asset-form', standalone: true, imports: [ SharedModule, AssetCategorySelectComponent, OrgSelectComponent, SelectUserByOrgComponent, ManufacturerSelectComponent, PositionSelectComponent, MaintenanceSelectComponent, AssetOperationRecordsComponent, AssetRepairRecordsComponent, UploadComponent, ], templateUrl: './asset-form.component.html', styleUrl: './asset-form.component.less', }) export class AssetFormComponent implements OnInit { constructor( private fb: FormBuilder, private api: ApiService, private msg: NzMessageService, ) {} @ViewChild('assetCategorySelect') assetCategorySelect!: AssetCategorySelectComponent readonly data: NzSafeAny = inject(NZ_MODAL_DATA) formGroup!: FormGroup groupIndex = 0 ASSET_STATUS = ASSET_STATUS ASSET_SOURCE_MAP = ASSET_SOURCE_MAP MAINTENANCE_STATUS = MAINTENANCE_STATUS MAINTENANCE_TYPE = MAINTENANCE_TYPE financialCategory: NzSafeAny[] = [] flowForms: NzSafeAny[] = [] extraFields: NzSafeAny[] = [] ngOnInit(): void { // this.api.getFlowFormList().subscribe((res) => { // this.flowForms = res.body // }) this.formGroup = this.fb.group({ assetId: this.fb.control(null, []), name: this.fb.control('', [FormValidators.required('请输入')]), categoryId: this.fb.control(null, [FormValidators.required('请选择')]), ownCompanyId: this.fb.control(null, [FormValidators.required('请选择')]), status: this.fb.control(null, [FormValidators.required('请选择')]), // flowForm: this.fb.control(null, [FormValidators.required('请选择')]), sourceId: this.fb.control(null, []), unit: this.fb.control(null, []), positionId: this.fb.control(null, []), positionDetail: this.fb.control(null, []), serialNumber: this.fb.control(null, []), manufacturersVendorId: this.fb.control(null, []), useUserId: this.fb.control(null, []), useOrganizationId: this.fb.control(null, []), manager: this.fb.control(null, []), purpose: this.fb.control(null, []), model: this.fb.control(null, []), maintenanceVendor: this.fb.control(null, []), contactor: this.fb.control(null, []), contact: this.fb.control(null, []), responsiblePerson: this.fb.control(null, []), maintenanceStartDate: this.fb.control(null, []), maintenanceEndDate: this.fb.control(null, []), maintenanceStatus: this.fb.control(null, []), maintenanceType: this.fb.control(null, []), suggestMaintenanceType: this.fb.control(null, []), maintenanceNotes: this.fb.control(null, []), financialNotes: this.fb.control(null, []), financialCategoryId: this.fb.control(null, []), residualsRate: this.fb.control(null, []), financialOption: this.fb.control(null, []), expenseItem: this.fb.control(null, []), navPrice: this.fb.control(null, []), customerInfo: this.fb.control(null, []), taxAmountRate: this.fb.control(null, []), totalAmountPrice: this.fb.control(null, []), registerDate: this.fb.control(null, []), maintenanceManual: this.fb.control(null, []), userManual: this.fb.control(null, []), img: this.fb.control(null, []), _extInfo: this.fb.array([]), }) this.patchValues() this.api.getBasicFinancialCategory({}).subscribe((res) => { this.financialCategory = res.body }) } get extInfo(): FormArray { return this.formGroup.get('_extInfo') as FormArray } onExtraChange(v: any) { let extraFields: NzSafeAny[] = [] try { const formValue = this.assetCategorySelect.originTreeData.find((f) => f.categoryId === Number(v)) ?._assetExtTemp extraFields = formValue ?? [] } catch (error) {} this.setExtraFields(extraFields) } ext = null setExtraFields(fields: NzSafeAny[]) { this.extInfo.clear() this.extraFields = fields .sort((a, b) => a.sort - b.sort) .map((i) => { let val = i.value if (i.type === 'RADIO') { val = i.value?.value } else if (i.type === 'DATE') { val = new Date(i.value) } this.extInfo.push( this.fb.group({ key: this.fb.control(i.key), name: this.fb.control(i.name), remark: this.fb.control(i.remark), sort: this.fb.control(i.sort), type: this.fb.control(i.type), value: this.fb.control(val), fields: this.fb.control(i.type === 'RADIO' ? i.value?.fields : []), }), ) return i }) } patchValues() { const { value: data, preview } = this.data if (data) { this.formGroup.patchValue({ ...data, useUserId: data._useUser?.userId ? [data._useUser?.userId] : [], manager: data._manager?.userId ? [data._manager?.userId] : [], responsiblePerson: data._responsiblePerson?.userId ? [data._responsiblePerson?.userId] : [], categoryId: data._category?.categoryId ? data._category?.categoryId + '' : null, positionId: data._position?.positionId ? data._position?.positionId + '' : null, ownCompanyId: data._ownCompany?.organizationId ? data._ownCompany?.organizationId + '' : null, useOrganizationId: data._useOrganization?.organizationId ? data._useOrganization?.organizationId + '' : null, maintenanceVendor: data._maintenanceVendor?.maintenanceVendorId, manufacturersVendorId: data._manufacturersVendor?.manufacturersVendorId, }) this.setExtraFields(data._extInfo) } if (preview) { this.formGroup.disable() } } public getValues() { let values = null if (FormValidators.validateFormGroup(this.formGroup)) { const v = this.formGroup.getRawValue() values = { ...v, useUserId: v.useUserId?.[0], manager: v.manager?.[0], categoryId: v.categoryId, ownCompanyId: v.ownCompanyId, positionId: v.positionId, useOrganizationId: v.useOrganizationId, responsiblePerson: v.responsiblePerson?.[0], _extInfo: this.extInfo.value.map((v: any) => { if (v.type === 'RADIO') { return { ...v, value: { value: v.value, fields: v.fields.map((i: string) => i), }, } } return v }), } } return values } }