import { Component, Input, OnInit, inject } from '@angular/core' import { 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 { OrgSelectComponent } from '../org-select/org-select.component' import { SelectUserByOrgComponent } from '../select-user-by-org/select-user-by-org.component' import { PositionSelectComponent } from '../position-select/position-select.component' import { NzSafeAny } from 'ng-zorro-antd/core/types' import { NZ_MODAL_DATA } from 'ng-zorro-antd/modal' import { SupplierSelectComponent } from '../supplier-select/supplier-select.component' import { AssetSelectComponent } from '../asset-select/asset-select.component' import { WarehouseSelectComponent } from '../warehouse-select/warehouse-select.component' @Component({ selector: 'app-asset-business-allot-form', standalone: true, imports: [ SharedModule, SelectUserByOrgComponent, SupplierSelectComponent, AssetSelectComponent, OrgSelectComponent, PositionSelectComponent, WarehouseSelectComponent, ], templateUrl: './asset-business-allot-form.component.html', styleUrl: './asset-business-allot-form.component.less', }) export class AssetBusinessAllotFormComponent { constructor( private fb: FormBuilder, private api: ApiService, private msg: NzMessageService, ) {} readonly data: NzSafeAny = inject(NZ_MODAL_DATA) formGroup!: FormGroup groupIndex = 0 uploadLoading = false ngOnInit(): void { this.formGroup = this.fb.group({ id: this.fb.control(null, []), name: this.fb.control('', [FormValidators.required('请输入')]), // useUserId: this.fb.control(null, [FormValidators.required('请选择')]), // useOrganizationId: this.fb.control(null, [FormValidators.required('请选择')]), // businessGeneratedDate: this.fb.control(null, [FormValidators.required('请选择')]), notes: this.fb.control(null, []), inWarehouseId: this.fb.control(null, [FormValidators.required('请选择')]), outWarehouseId: this.fb.control(null, [FormValidators.required('请选择')]), attach: this.fb.control('', []), assetIdList: this.fb.control([], []), }) this.patchValues() } patchValues() { const { value: data, preview } = this.data if (data) { this.formGroup.patchValue({ ...data, useUserId: data._useUser?.userId ? [data._useUser?.userId] : [], positionId: data._position?.positionId + '', useOrganizationId: data._useOrganization?.organizationId + '', }) } if (preview) { this.formGroup.disable() } } public getValues() { let values = null if (FormValidators.validateFormGroup(this.formGroup)) { const v = this.formGroup.value values = { ...v, useUserId: v.useUserId?.[0], manager: v.manager?.[0], categoryId: v.categoryId?.[0], ownCompanyId: v.ownCompanyId?.[0], positionId: Number(v.positionId?.[0]), useOrganizationId: Number(v.useOrganizationId?.[0]), assetIdList: JSON.stringify(v.assetIdList), } } return values } onFileChange(e: Event) { const target = e.target as HTMLInputElement const file = target.files![0] target.value = '' const formdata = new FormData() formdata.append('file', file) this.api.upload(formdata).subscribe((res) => { this.formGroup.get('attach')?.setValue(res.body.fileName) }) } }