import { Component, ViewChild, Injector, Output, EventEmitter } from '@angular/core'; import { ModalDirective } from 'ngx-bootstrap'; import { finalize } from 'rxjs/operators'; import { OrderNotificationOrderStatusTypeLookupTableDto, UserListDto, OrganizationUnitDto, OrderNotificationSmsTemplateLookupTableDto, OrderNotificationOrderTypeLookupTableDto, OrderNotificationsServiceProxy, CreateOrEditOrderNotificationDto, OrderNotificationOrderNotificationTemplateLookupTableDto , OrganizationUnitServiceProxy, CreateOrEditOrganizationUnitOrderNotificationDto, OrganizationUnitOrderNotificationsServiceProxy, OrganizationUnitOrderNotificationDto, ReportScheduleServiceProxy, CreateOrEditReportScheduleDto, CreateOrEditReportRecipientDto, ReportSchedulerServiceProxy, LocationServiceProxy, LocationNameDto, ParameterValueNameDto } from '@shared/service-proxies/service-proxies'; import { AppComponentBase } from '@shared/common/app-component-base'; import * as moment from 'moment'; import { NUMBER_TYPE } from '@angular/compiler/src/output/output_ast'; import { ColorTransformCollection } from 'devexpress-richedit/lib/core/model/drawing/transform/color-transform-collection'; import { value } from 'devexpress-dashboard/model/index.metadata'; @Component({ selector: 'reportScheduleModalComponent', templateUrl: './report-schedule-modal.component.html', providers: [ReportScheduleServiceProxy, ReportSchedulerServiceProxy] }) export class ReportScheduleModalComponent extends AppComponentBase { @ViewChild('createOrEditModal', { static: true }) modal: ModalDirective; @Output() modalSave: EventEmitter = new EventEmitter(); active = false; saving = false; delete: any; form: any; isEmail: boolean; isSMS: boolean; organizationUnitId: any; // allOrganizationUnits: any; orderNotification: CreateOrEditOrderNotificationDto = new CreateOrEditOrderNotificationDto(); activeTabIndex: number = 0; orderNotificationTemplateDescription = ''; orderStatusTypeDescription = ''; smsTemplateDescription = ''; orderTypeDescription = ''; OrganizationalUnitNotification: any; organizationUnitOrderNotification: any; orgUnits: CreateOrEditOrganizationUnitOrderNotificationDto = new CreateOrEditOrganizationUnitOrderNotificationDto(); allOrderNotificationTemplates: OrderNotificationOrderNotificationTemplateLookupTableDto[]; allOrderStatusTypes: OrderNotificationOrderStatusTypeLookupTableDto[]; allSmsTemplates: OrderNotificationSmsTemplateLookupTableDto[]; allOrderTypes: OrderNotificationOrderTypeLookupTableDto[]; allUsers: UserListDto[]; allOrganizationUnits: OrganizationUnitDto[]; addOrg: any; input: any; // input: CreateOrEditReportScheduleDto= new CreateOrEditReportScheduleDto(); reportId: number; schedulerDescription: string; schedulerStartDate: Date; schedulerStartTime: any; repeat: number; startDay : number; exportType: any; schedulerIsActive: boolean; recipientInput: any reportScheduleRecipient: any; exists: boolean = false params: any reportsId: number getLocationNames: LocationNameDto[]; dropdownValidation : boolean = false parameterNameValue: ParameterValueNameDto = new ParameterValueNameDto(); listParameterValueName : Array<{ pname: string, pvalue: string }> = new Array(); constructor( injector: Injector, private _orderNotificationsServiceProxy: OrderNotificationsServiceProxy, private _reportScheduleServiceProxy : ReportScheduleServiceProxy, private _organizationUnitService: OrganizationUnitServiceProxy, private _organizationUnitOrderNotification: OrganizationUnitOrderNotificationsServiceProxy, private _scheduler: ReportSchedulerServiceProxy, private _locationServiceProxy: LocationServiceProxy ) { super(injector); } ngOnInit(): void { $('.kt-select2').select2(); $('.organization-unit').select2(); $('.user').select2(); } onShown(): void { let s = this; $('.kt-select2').select2(); $('.organization-unit').select2(); $('.user').select2(); // this.getParams(); if(this.listParameterValueName != null){ setTimeout(() => { this.listParameterValueName.forEach(element => { $('#'+element.pname).val(element.pvalue) // console.log(element.pvalue+"onshown") $('.kt-select2').select2(); }); }, 2000); } // $('.kt-select2').on('select2:select', function (e) { // console.log(s.organizationUnitOrderNotification) // }); } show(reportId: number): void { // $('.kt-select2').select2(); let s = this; this.reportsId = reportId // this.organizationUnitOrderNotification = []; this.reportScheduleRecipient = [] this.spinnerService.show(); this._reportScheduleServiceProxy.getReportScheduleIfExisting(reportId) .subscribe(result=> { this.exists = result if (result){ this._reportScheduleServiceProxy.getReportScheduleForEdit(reportId) .subscribe(result=> { this.exportType=[] let startTime = result.startDate.toDate().toDateString() + ' ' + result.startTime; this.reportId = result.reportId this.schedulerDescription = result.description // this.schedulerStartDate = new Date(result.) this.schedulerStartDate = result.startDate.toDate() this.schedulerStartTime = moment(startTime).toDate(); this.repeat = result.repeat; this.startDay = result.startDay this.exportType = result.exportType.split(",") // console.log(result.exportType) // console.log(this.exportType) this.schedulerIsActive = result.isActive; result.recipients.forEach(element => { this.reportScheduleRecipient.push(element) }); // let a = 0 this.listParameterValueName=[] this._reportScheduleServiceProxy.getReportParameters(this.reportsId) .pipe() .subscribe(result2=>{ this.params = result2 // console.log(this.params) for(let a = 0; a < result.parameterNameValue.length ; a++){ if(this.params.map(function(e) { return e.name; }).indexOf(result.parameterNameValue[a].parameterName) >-1){ this.listParameterValueName.push({pname:result.parameterNameValue[a].parameterName, pvalue:result.parameterNameValue[a].parameterValue}) } } if(this.listParameterValueName != null){ setTimeout(() => { this.listParameterValueName.forEach(element => { $('#'+element.pname).val(element.pvalue) // console.log(element.pvalue+"onshown") $('.kt-select2').select2(); }); }, 2000); } }) this.active = true; this.spinnerService.hide() this.modal.show(); }) } else{ this.reportScheduleRecipient = [] this.exportType = [] // this.isEmail = false; // this.isSMS = false; this.organizationUnitId = ""; this.reportId = reportId; this.schedulerIsActive = false this.repeat = null; this.schedulerDescription= "" this.schedulerStartDate = null; this.schedulerStartTime = null; this.active = true; this.getParams(); this.modal.show(); } }) this._orderNotificationsServiceProxy.getAllUserForTableDropdown().subscribe(result => { this.allUsers = result; }); this._orderNotificationsServiceProxy.getAllOrgUnitsForTableDropdown().subscribe(result => { this.allOrganizationUnits = result; }); // this._orderNotificationsServiceProxy.getAllOrderTypeForTableDropdown().subscribe(result => { // this.allOrderTypes = result; // }); } updateUnits(id, data): void { // this.organizationUnitOrderNotification[id].organizationUnitId = data; this.reportScheduleRecipient[id].organizationUnitId = data; setTimeout(() => { $('.organization-unit').select2(); $('.user').select2(); }, 100); } updateUsers(id, data): void { // this.organizationUnitOrderNotification[id].userId = data; this.reportScheduleRecipient[id].userId = data; setTimeout(() => { $('.organization-unit').select2(); $('.user').select2(); }, 100); } updateRecipientType(id, data): void { let s = this; // this.organizationUnitOrderNotification[id].code = data; this.reportScheduleRecipient[id].code = data; setTimeout(() => { $('.organization-unit').select2(); $('.user').select2(); $('.organization-unit').on('select2:select', function (e) { s.updateUnits($(e.currentTarget).attr('id'), $(e.currentTarget).val()); }); $('.user').on('select2:select', function (e) { s.updateUsers($(e.currentTarget).attr('id'), $(e.currentTarget).val()); }); }, 100); } // updateSms(id, data): void { // this.organizationUnitOrderNotification[id].isSMS = data; // } // updateEmail(id, data): void { // this.organizationUnitOrderNotification[id].isEmail = data; // } updateIsActive(id, data): void { this.reportScheduleRecipient[id].isActive = data; } addRecipient(): void { let s = this; this.recipientInput = new CreateOrEditReportRecipientDto(); this.recipientInput.emailAddress = "" this.recipientInput.isActive = false this.recipientInput.organizationUnitId = null // this.recipientInput.reportScheduleId = null this.recipientInput.userId = null this.recipientInput.code = null // this.addOrg.isEmail = false; // this.addOrg.isSMS = false; // this.addOrg.isActive = false; // this.addOrg.organizationUnitId = null; // this.addOrg.emailAddress = ""; // this.addOrg.phoneNumber = ""; // this.addOrg.code = null; // this.addOrg.userId = null; // this.organizationUnitOrderNotification.push(this.addOrg); this.reportScheduleRecipient.push(this.recipientInput); setTimeout(() => { $('.organization-unit').select2(); $('.user').select2(); }, 10); } deleteRecipient(id: number): void { // this.delete = this.organizationUnitOrderNotification[id]; this.delete = this.reportScheduleRecipient[id]; // this._organizationUnitOrderNotification.delete(this.delete.id) // .subscribe(() => { // this.notify.success(this.l('SuccessfullyDeleted')); // }); this._reportScheduleServiceProxy.deleteReportScheduleRecipient(this.delete.id) .subscribe(result => { this.notify.success(this.l('SuccessfullyDeleted')); }); this.reportScheduleRecipient.splice(id, 1); } save(): void { this.saving = true; this.input = new CreateOrEditReportScheduleDto(); this.input.reportId = this.reportId this.input.description = this.schedulerDescription; this.input.startDate = this.schedulerStartDate; this.input.startDate = new Date(this.schedulerStartDate.setHours(this.schedulerStartDate.getHours() - this.schedulerStartDate.getTimezoneOffset() / 60)); this.input.startTime = new Date(this.schedulerStartTime).toLocaleTimeString('en-US', { hour12: true, hour: "numeric", minute: "numeric", second: "numeric" }) as any; this.input.repeat = $('#repeatInput').val(); this.input.startDay = $('#startDayInput').val(); this.exportType = $('#exportTypeInput').val(); this.input.exportType = this.exportType.join(",").replace(/[^a-zA-Z,]/g,""); this.input.isActive = this.schedulerIsActive; this.input.recipients = new CreateOrEditReportRecipientDto(); this.input.recipients = this.reportScheduleRecipient; this.input.parameterNameValue = [] // this.params.forEach(element => { // var parameterNameValue: ParameterValueNameDto = new ParameterValueNameDto(); // parameterNameValue.parameterName=element.toString(); // parameterNameValue.parameterValue=$('#'+element).val().toString(); // this.input.parameterNameValue.push(parameterNameValue) // // console.log($('#'+element).val()) // }); for(var a = 0 ; a < this.params.length ; a++){ console.log("save") var parameterNameValue: ParameterValueNameDto = new ParameterValueNameDto(); parameterNameValue.parameterName=this.params[a].name.toString(); parameterNameValue.parameterValue=$('#'+this.params[a].name).val().toString(); this.input.parameterNameValue.push(parameterNameValue) } // console.log(this.input.parameterNameValue) if(!this.exists){ this._reportScheduleServiceProxy.createReportSchedule(this.input) .subscribe(result=>{ this.notify.info(this.l('SavedSuccessfully')); this.close(); this.modalSave.emit(null); }) } else{ this._reportScheduleServiceProxy.updateReportSchedule(this.input) .subscribe(result=>{ this.notify.info(this.l('SavedSuccessfully')); this.close(); this.modalSave.emit(null); }) } } ngAfterViewInit(): void { // $('.kt-select2').select2(); // $('.organization-unit').select2(); // $('.user').select2(); } close(): void { // console.log("closed") this.active = false; this.modal.hide(); } // get params getParams(){ this._reportScheduleServiceProxy.getReportParameters(this.reportsId) .pipe() .subscribe(result=>{ this.params = result setTimeout(() => { $('.kt-select2').select2(); }, 2000); this.spinnerService.hide() }) } }