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,IncidentStatusListDropdown,EmployeeIncidentDropDownList,DriverIncidentDropDownList } 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'; @Component({ selector: 'createOrEditOrderNotificationModal', templateUrl: './create-or-edit-orderNotification-modal.component.html' }) export class CreateOrEditOrderNotificationModalComponent 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; allOrderIncidentStatusType:IncidentStatusListDropdown[]; allDriverIncidentStatusType:DriverIncidentDropDownList[]; allEmployeeIncidentStatusType:EmployeeIncidentDropDownList[]; targetType:number; constructor( injector: Injector, private _orderNotificationsServiceProxy: OrderNotificationsServiceProxy, private _organizationUnitService: OrganizationUnitServiceProxy, private _organizationUnitOrderNotification: OrganizationUnitOrderNotificationsServiceProxy ) { super(injector); } ngOnInit(): void { $('.kt-select2').select2(); $('.organization-unit').select2(); $('.user').select2(); $('#incidentType').select2(); $('#orderStatusTypeId').select2(); } onShown(): void { let that = this; $('.kt-select2').select2(); $('.organization-unit').select2(); $('.user').select2(); // $('.kt-select2').on('select2:select', function (e) { // console.log(s.organizationUnitOrderNotification) // }); $('#notificationTargetType').on('select2:select', function (e){ var data = e.params.data; that.targetType = parseInt(data.id); if(data.id == "1"){ $('#incidentType').select2(); $('#orderStatusTypeId').select2(); } }); } show(orderNotificationId?: number): void { $('.kt-select2').select2(); let s = this; this.organizationUnitOrderNotification = []; this.isEmail = false; this.isSMS = false; this.organizationUnitId = ""; this.targetType = null; if (!orderNotificationId) { this.orderNotification = new CreateOrEditOrderNotificationDto(); this.orderNotification.id = orderNotificationId; this.orderNotificationTemplateDescription = ''; this.orderStatusTypeDescription = ''; this.smsTemplateDescription = ''; this.orderTypeDescription = ''; this.active = true; this.modal.show(); } else { this._orderNotificationsServiceProxy.getOrderNotificationForEdit(orderNotificationId).subscribe(result => { this.organizationUnitOrderNotification = result.orderNotification.organizationUnitOrderNotification; this.orderTypeDescription = result.orderTypeDescription; this.orderNotification = result.orderNotification; this.orderNotificationTemplateDescription = result.orderNotificationTemplateDescription; this.orderStatusTypeDescription = result.orderStatusTypeDescription; this.smsTemplateDescription = result.smsTemplateDescription; this.orderTypeDescription = result.orderTypeDescription; this.targetType = result.orderNotification.targetType; this.active = true; this.modal.show(); }); } this._orderNotificationsServiceProxy.getAllOrderNotificationTemplateForTableDropdown().subscribe(result => { this.allOrderNotificationTemplates = result; }); this._orderNotificationsServiceProxy.getAllOrderStatusTypeForTableDropdown().subscribe(result => { this.allOrderStatusTypes = result; }); this._orderNotificationsServiceProxy.getAllSmsTemplateForTableDropdown().subscribe(result => { this.allSmsTemplates = result; }); this._orderNotificationsServiceProxy.getAllUserForTableDropdown().subscribe(result => { this.allUsers = result; }); this._orderNotificationsServiceProxy.getAllOrgUnitsForTableDropdown().subscribe(result => { this.allOrganizationUnits = result; }); this._orderNotificationsServiceProxy.getAllOrderTypeForTableDropdown().subscribe(result => { this.allOrderTypes = result; }); this._orderNotificationsServiceProxy.getAllIncidentStatusForTableDropdown().subscribe(result => { this.allOrderIncidentStatusType = result; }); this._orderNotificationsServiceProxy.getAllDriverIncidentStatusForTableDropdown().subscribe(result => { this.allDriverIncidentStatusType = result; }); this._orderNotificationsServiceProxy.getAllEmployeeIncidentStatusForTableDropdown().subscribe(result => { this.allEmployeeIncidentStatusType = result; }); // let that = this; } copy(orderNotificationId?: number): void { $('.kt-select2').select2(); $('.organization-unit').select2(); $('.user').select2(); let s = this; this.organizationUnitOrderNotification = []; this._orderNotificationsServiceProxy.getOrderNotificationForEdit(orderNotificationId).subscribe(result => { for (let index = 0; index < result.orderNotification.organizationUnitOrderNotification.length; index++) { result.orderNotification.organizationUnitOrderNotification[index].id = 0; result.orderNotification.organizationUnitOrderNotification[index].orderNotificationId = null; } this.organizationUnitOrderNotification = result.orderNotification.organizationUnitOrderNotification; this.orderNotification = result.orderNotification; this.orderNotification.id = null; this.orderNotification.description = result.orderNotification.description + '-COPY'; this.orderNotification.code = result.orderNotification.code + '-COPY'; this.active = true; this.modal.show(); }); this._orderNotificationsServiceProxy.getAllOrderNotificationTemplateForTableDropdown().subscribe(result => { this.allOrderNotificationTemplates = result; }); this._orderNotificationsServiceProxy.getAllOrderStatusTypeForTableDropdown().subscribe(result => { this.allOrderStatusTypes = result; }); this._orderNotificationsServiceProxy.getAllSmsTemplateForTableDropdown().subscribe(result => { this.allSmsTemplates = result; }); 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; setTimeout(() => { $('.organization-unit').select2(); $('.user').select2(); }, 100); } updateUsers(id, data): void { this.organizationUnitOrderNotification[id].userId = data; setTimeout(() => { $('.organization-unit').select2(); $('.user').select2(); }, 100); } updateRecipientType(id, data): void { let s = this; this.organizationUnitOrderNotification[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; } addRecipient(): void { let s = this; this.addOrg = new CreateOrEditOrganizationUnitOrderNotificationDto(); 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); setTimeout(() => { $('.organization-unit').select2(); $('.user').select2(); }, 10); } deleteRecipient(id: number, organizationUnitOrderNotif: OrganizationUnitOrderNotificationDto): void { this.delete = this.organizationUnitOrderNotification[id]; this._organizationUnitOrderNotification.delete(this.delete.id) .subscribe(() => { this.notify.success(this.l('SuccessfullyDeleted')); }); this.organizationUnitOrderNotification.splice(id, 1); } save(): void { this.saving = true; this.form = new CreateOrEditOrderNotificationDto(); if (this.orderNotification.id) { this.form.id = this.orderNotification.id; } this.form.code = this.orderNotification.code; this.form.isActive = this.orderNotification.isActive; this.form.description = this.orderNotification.description; this.form.development = this.orderNotification.development; this.form.organizationUnitOrderNotification = new CreateOrEditOrganizationUnitOrderNotificationDto(); this.form.organizationUnitOrderNotification = this.organizationUnitOrderNotification; this.form.orderNotificationTemplateId = Number((document.getElementById('orderNotificationTemplateId')).value); this.form.smsTemplateId = Number((document.getElementById('smsTemplateId')).value); if(document.getElementById('orderStatusTypeId') == null){ this.form.orderStatusTypeId = null; } else { this.form.orderStatusTypeId = Number((document.getElementById('orderStatusTypeId')).value); } if(document.getElementById('incidentType') == null){ this.form.incidentTypeId = null; } else { this.form.incidentTypeId = Number((document.getElementById('incidentType')).value); } if(document.getElementById('driverIncidentType') == null){ this.form.driverIncidentTypeId = null; } else { this.form.driverIncidentTypeId = Number((document.getElementById('driverIncidentType')).value); } if(document.getElementById('employeeIncidentType') == null){ this.form.employeeIncidentTypeId = null; } else { this.form.employeeIncidentTypeId = Number((document.getElementById('employeeIncidentType')).value); } if(document.getElementById('orderTypeId') == null){ this.form.orderTypeId = null; } else { this.form.orderTypeId = Number((document.getElementById('orderTypeId')).value); } if(document.getElementById('notificationTargetType') == null){ this.form.targetType = null; } else { this.form.targetType = Number((document.getElementById('notificationTargetType')).value); } // console.log(this.form); if(this.form.targetType == 0 || this.form.targetType == null){ this.message.info("Please select notification target type"); this.saving = false; } else { this._orderNotificationsServiceProxy.createOrEdit(this.form) .pipe(finalize(() => { this.saving = false; })) .subscribe(() => { this.notify.info(this.l('SavedSuccessfully')); this.close(); this.modalSave.emit(null); }); } } ngAfterViewInit(): void { $('.kt-select2').select2(); $('.organization-unit').select2(); $('.user').select2(); } close(): void { this.active = false; this.modal.hide(); } }