import { Component, OnInit, Input, Output, EventEmitter, ViewChild, OnChanges, SimpleChanges } from '@angular/core'; import { MainService } from '../../../services/main.service'; import { Router } from '@angular/router'; import { ToolService } from 'src/app/services/tool.service'; import { NzMessageService, UploadFile, UploadXHRArgs } from 'ng-zorro-antd'; import cloneDeep from 'lodash-es/cloneDeep' import { debounceTime } from 'rxjs/operators'; import { Subject } from 'rxjs'; import { HttpClient, HttpEvent, HttpResponse, HttpEventType, HttpRequest } from '@angular/common/http'; @Component({ selector: 'app-incident-handle-info-simple', templateUrl: './incident-handle-info-simple.component.html', styleUrls: ['./incident-handle-info-simple.component.less'] }) export class IncidentHandleInfoSimpleComponent implements OnInit, OnChanges { @Input() incidentData:any = {}; @Output() closeModelHs = new EventEmitter();//1.组件暴露一个 EventEmitter 属性,当事件发生时,子组件利用该属性 emits(向上弹射)事件 @Output() confirmModelHs = new EventEmitter();//1.组件暴露一个 EventEmitter 属性,当事件发生时,子组件利用该属性 emits(向上弹射)事件 constructor( private mainService: MainService, private router: Router, public tool: ToolService, private message: NzMessageService, private http: HttpClient, ) { } incidentDataCopy:any = {}; changeInpSubject = new Subject(); //防抖 ngOnChanges(changes: SimpleChanges){ console.log('changes:', changes) // 初始化 if(changes.incidentData && !changes.incidentData.firstChange && changes.incidentData.currentValue && changes.incidentData.previousValue && !changes.incidentData.previousValue.id){ this.init(); } if(changes.incidentData && changes.incidentData.firstChange && changes.incidentData.currentValue && !changes.incidentData.previousValue){ this.init(); } } ngOnInit() { //防抖 this.changeInpSubject.pipe(debounceTime(500)).subscribe((v) => { if(v[0] === 'synergetic'){ this.getSynergeticList(v[1]); }else if(v[0] === 'assetId'){ this.getAssetProductList(v[1]); } }); } init(){ console.log('初始化') this.hosId = this.tool.getCurrentHospital().id; this.incidentDataCopy = cloneDeep(this.incidentData); this.incidentDataCopy.synergetic = []; this.getHandleCategorys(); this.getSynergeticList(); this.getAssetProductList(); } // 边输边搜节流阀 changeInp(model, e) { this.isLoading = true; this.changeInpSubject.next([model, e]); } // 关闭弹窗 hideModal() { this.closeModelHs.emit(JSON.stringify({ show: false }));//emits(向上弹射)事件 } // 获取处理方式列表 handleCategoryList:any[] = []; getHandleCategorys(){ this.mainService.getDictionary("list", "incident_handle_type").subscribe((data) => { this.handleCategoryList = data || []; console.log(this.incidentDataCopy) if(!this.incidentDataCopy.handleCategory){ let handleCategory = this.handleCategoryList.find(v => v.value == '1'); this.incidentDataCopy.handleCategory = handleCategory ? handleCategory.id : undefined; } }); } // 获取协同人员 hosId:any; isLoading = false; synergeticList: any = []; getSynergeticList(keyword = '') { let postData = { user: { hospital: { id: this.hosId }, name: keyword, simpleQuery: true, roleCodes: 'first-line support', engineer: 1, }, idx: 0, sum: 20, }; this.mainService .getFetchDataList("simple/data", "user", postData) .subscribe((data) => { this.synergeticList = data.list; this.isLoading = false; }); } // 获取资产列表 assetProducList: any = []; getAssetProductList(keyword = '') { let postData = { assetProduct: { hosId: this.hosId, name: keyword, }, idx: 0, sum: 20, }; this.mainService .getFetchDataList("simple/data", "assetProduct", postData) .subscribe((data) => { this.assetProducList = data.list; }); } // 图片相关 showUploadList = { showPreviewIcon: true, showRemoveIcon: true, hidePreviewIconInNonImage: true }; fileList = [ // { // uid: -1, // name: 'xxx.png', // status: '1', // url: 'https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png' // } ]; previewImage: string | undefined = ''; previewVisible = false; handlePreview = (file: UploadFile) => { console.log('file1:', file) this.previewImage = file.url || file.thumbUrl; this.previewVisible = true; }; beforeUpload = (file: UploadFile): boolean => { console.log('file2:', file) this.fileList = [...this.fileList, file]; setTimeout(async () => { file.url = await this.getBase64(file); }, 0); console.log('this.fileList:', this.fileList) return true; }; // customReq = (item: UploadXHRArgs) => { // console.log(item); // // Create a FormData here to store files and other parameters. // const formData = new FormData(); // // tslint:disable-next-line:no-any // formData.append('file', item.file as any); // formData.append('filename', item.file.name); // const req = new HttpRequest('POST', item.action!, formData, { // reportProgress: true, // withCredentials: true // }); // // Always returns a `Subscription` object. nz-upload would automatically unsubscribe it at correct time. // return this.http.request(req).subscribe( // // tslint:disable-next-line no-any // (event: HttpEvent) => { // if (event.type === HttpEventType.UploadProgress) { // if (event.total! > 0) { // // tslint:disable-next-line:no-any // (event as any).percent = (event.loaded / event.total!) * 100; // } // item.onProgress!(event, item.file!); // } else if (event instanceof HttpResponse) { // item.onSuccess!(event.body, item.file!, event); // } // }, // err => { // item.onError!(err, item.file!); // } // ); // }; getBase64(file: any): Promise { return new Promise((resolve, reject) => { const reader = new FileReader(); reader.readAsDataURL(file); reader.onload = () => resolve(reader.result); reader.onerror = error => reject(error); }); } // 临时上传图片 temporarilyUrl = this.mainService.returnUploadUrl('temporarily', 0); }