import { EquipmentCheckPlanSheetDto } from './../../emp-model'; import { Component, OnInit } from '@angular/core'; import { Validators, UntypedFormBuilder, UntypedFormGroup } from '@angular/forms'; import { RhEquipmentInventoryService } from '../equipment-inventory.service'; import { OpMode, RhBaseDto, RhLoginedUserDto, RhSelectableDto, RhSelectItem } from 'rh-base/model'; import { FormHelper, MsgHelper, RhStorageService } from 'rh-base/core'; type tabType = 'addPlanTab' | 'auditTab'; type pageType = 'audit' | 'see'; @Component({ selector: 'rh-equipment-inventory-plan', templateUrl: './equipment-inventory-plan.component.html', styleUrls: ['./equipment-inventory-plan.component.less'] }) export class RhEquipmentInventoryPlanComponent implements OnInit { selectedIndex = 0; editDto: RhBaseDto = {}; editForm!: UntypedFormGroup; currentUser!: RhLoginedUserDto; currentEquipmentCheckPlanCode = ''; currentTab: tabType = 'addPlanTab'; auditPageType: pageType = 'see'; submitLoading = false; reQueryTime = new Date(); constructor(public storageSer: RhStorageService, private fb: UntypedFormBuilder, private operator: RhEquipmentInventoryService) {} ngOnInit(): void { this.currentUser = this.storageSer.getUserSession()?.User as RhLoginedUserDto; } openEditTab(data?: RhSelectableDto) { if (data) { this.editDto = data; this.editDto.OpSign = OpMode.OpEdit; } else { this.editDto = {}; this.editDto.OpSign = OpMode.OpAdd; } this.editDto['ResponsibleEmployeeCode'] = this.currentUser.UserName; this.editDto['ResponsibleEmployeeName'] = this.currentUser.DisplayName; this.createEditForm(this.editDto as EquipmentCheckPlanSheetDto); this.currentTab = 'addPlanTab'; this.selectedIndex = 1; } closeEditTab() { this.selectedIndex = 0; if (this.currentTab === 'addPlanTab') { this.resetEditForm(null); } this.reQueryTime = new Date(); } initEditDto(): EquipmentCheckPlanSheetDto { // 使用构造函数初始化对象 const dto = EquipmentCheckPlanSheetDto.create(); return dto; } createEditForm(dto: EquipmentCheckPlanSheetDto) { this.editForm = this.fb.group({ /** 设备盘点计划名称 */ EquipmentCheckPlanName: [dto.EquipmentCheckPlanName, [Validators.required]], /** 设备盘点计划类型 */ EquipmentCheckPlanType: [dto.EquipmentCheckPlanType, [Validators.required]] }); } submitEditForm($event: Event) { if ($event) { $event.preventDefault(); } FormHelper.YGSubmitForm(this.editDto, this.editForm, (dto) => { this.submitLoading = true; this.operator.StoreEquipmentCheckPlanSheetData(dto as EquipmentCheckPlanSheetDto).subscribe( (result) => { if (result.Success) { const msg = this.editDto.OpSign === OpMode.OpAdd ? '新增' : '编辑'; MsgHelper.ShowSuccessMessage(`${msg}数据成功`); if (this.editDto.OpSign === OpMode.OpEdit) { this.selectedIndex = 0; } this.resetEditForm(null); this.reQueryTime = new Date(); } else { MsgHelper.ShowWarningMessage(`数据线存储失败!${result.Message}`); } this.submitLoading = false; }, () => { this.submitLoading = false; } ); }); } resetEditForm($event: Event | null) { if ($event) { $event.preventDefault(); } this.editDto = this.initEditDto() as RhBaseDto; this.editDto['ResponsibleEmployeeCode'] = this.currentUser.UserName; this.editDto['ResponsibleEmployeeName'] = this.currentUser.DisplayName; this.editDto.OpSign = OpMode.OpAdd; this.editForm.reset(this.editDto); } planTypeChange(type: RhSelectItem) { if (type) { const year = new Date().getFullYear(); const typeText = type.Text; this.editForm.get('EquipmentCheckPlanName')?.setValue(`${year}${typeText}盘点`); } } openAuditTab(item?: EquipmentCheckPlanSheetDto) { if (item) { this.currentEquipmentCheckPlanCode = item.EquipmentCheckPlanCode as string; } this.currentTab = 'auditTab'; this.selectedIndex = 1; } getHandleSign(sign: { handleType: string; handleData: RhSelectableDto }) { if (sign.handleType === 'new') { this.openEditTab(); } else if (sign.handleType === 'audit') { this.openAuditTab(sign.handleData as EquipmentCheckPlanSheetDto); } else if (sign.handleType === 'edit') { this.openEditTab(sign.handleData); } } }