import { DatePipe } from '@angular/common'; import { Component, ElementRef, OnInit } from '@angular/core'; import { FormBuilder, FormGroup, Validators } from '@angular/forms'; import { ActivatedRoute, Router } from '@angular/router'; import { CustomValidator } from '../../../../../core/custom-validator'; import { NotificationsAdmin } from '../../../../../models/admin/company/notifications-admin/notifications-admin'; import { NotificationsAdminService } from '../../../../../services/admin/company/notifications-admin/notifications-admin.service'; import { CommonService } from '../../../../../services/common.service'; @Component({ selector: 'app-notifications-admin-add', templateUrl: './notifications-admin-add.component.html', styleUrls: ['./notifications-admin-add.component.css'] }) export class NotificationsAdminAddComponent implements OnInit { modelNotificationsAdmin = new NotificationsAdmin(); NotificationsAdminForm: FormGroup; NotificationsAdminAPIResponse: any; tinyMCEInitOptions; FeaturedImage: File = null; _paramId: number = 0; previewContent: string = ''; DefaultDateTime: string; count: number = 0; companyid: number = 1; constructor( private formBuilder: FormBuilder, private route: ActivatedRoute, private router: Router, private service: NotificationsAdminService, private common: CommonService, private datePipe: DatePipe, private elementRef: ElementRef, ) { } ngOnInit(): void { this.DefaultDateTime = this.getDate(new Date()); this._paramId = Number(this.route.snapshot.params.id); this.setFormBuilder(); this.tinyMCEInitOptions = this.common.tinyMCEOptions(); if (this._paramId == 0) { this.setAddForm(); } else { this.service.getById(this._paramId, data => { this.NotificationsAdminAPIResponse = data; this.modelNotificationsAdmin = this.NotificationsAdminAPIResponse.objResponse.responseContent; this.setEditForm(); }, (error) => { console.error('Error: ' + error); return error(error); }); } } setFormBuilder() { this.NotificationsAdminForm = this.formBuilder.group({ CompanyNotificationId: [''], CompanyNotificationName: ['', [Validators.required, CustomValidator.noWhitespaceValidator]], IsActive: [''], Content: ['', Validators.required], StartDate: ['', Validators.required], EndDate: ['', Validators.required], CompanyId: [''], SortOrder: ['', Validators.compose([Validators.required, CustomValidator.numberValidator])], UserId: [''], IpAddress: [''], }); } setAddForm() { this.NotificationsAdminForm.setValue({ CompanyNotificationId: 0, CompanyNotificationName: '', IsActive: 1, Content: '', StartDate: this.DefaultDateTime, EndDate: this.DefaultDateTime, CompanyId: this.companyid, SortOrder: 0, UserId: 1, IpAddress: '123.456.789.10' }); } setEditForm() { this.NotificationsAdminForm.setValue({ CompanyNotificationId: this.modelNotificationsAdmin.CompanyNotificationId, CompanyNotificationName: this.modelNotificationsAdmin.CompanyNotificationName == null ? "" : this.modelNotificationsAdmin.CompanyNotificationName, IsActive: this.modelNotificationsAdmin.IsActive == true ? 1 : 0, Content: this.modelNotificationsAdmin.Content == null ? "" : this.modelNotificationsAdmin.Content, StartDate: this.modelNotificationsAdmin.StartDate == null ? "" : this.getDate(this.modelNotificationsAdmin.StartDate), EndDate: this.modelNotificationsAdmin.EndDate == null ? "" : this.getDate(this.modelNotificationsAdmin.EndDate), CompanyId: this.companyid, SortOrder: this.modelNotificationsAdmin.SortOrder, UserId: 1, IpAddress: '12.46.7.10' }); } submitform() { if (this.NotificationsAdminForm.valid) { this.modelNotificationsAdmin = this.NotificationsAdminForm.value; if (this.NotificationsAdminForm.value.StartDate >= this.modelNotificationsAdmin.EndDate) { this.common.showError("EndDate should be greater then StartDate.", "Validation Alert!") } else if (this.modelNotificationsAdmin.CompanyNotificationName.trim() == "") { this.common.showError("Please enter vaild Notification Name.", "Validation Alert!") } else if (this.modelNotificationsAdmin.Content.replace(/]*>/g, 'IMAGE ').replace(/<[^>]*>| /g, '').trim()== "") { this.common.showError("Please enter vaild Content.", "Validation Alert!") } else { this.setModelBeforeSubmit(); this.service.SaveNotificationsAdmin(this.modelNotificationsAdmin, data => { this.NotificationsAdminAPIResponse = data; if (this.NotificationsAdminAPIResponse.objResponse.statusCode == "200") { this.common.showSuccess("Notification Admin Changes Applied Successfully.", "Success") this.router.navigate(["/admin/admin-company/notifications-admin-list"]); } else { this.common.showError("Transaction not completed.", "Transaction Fail") } }, (error) => { console.error('Error: ' + error); return error(error); }); } } else { this.submitValidation(); } } submitValidation() { this.common.validateAllFormFields(this.NotificationsAdminForm); this.common.validationfocus(this.NotificationsAdminForm, this.elementRef); this.common.showError("Please fill required fields.", "Validation Alert!") } setModelBeforeSubmit() { this.modelNotificationsAdmin.CompanyNotificationId = Number(this.modelNotificationsAdmin.CompanyNotificationId); this.modelNotificationsAdmin.CompanyNotificationName = String(this.modelNotificationsAdmin.CompanyNotificationName); this.modelNotificationsAdmin.IsActive = Boolean(Number(this.modelNotificationsAdmin.IsActive)); this.modelNotificationsAdmin.Content = String(this.modelNotificationsAdmin.Content); this.modelNotificationsAdmin.StartDate = String(this.setDate(this.modelNotificationsAdmin.StartDate)); this.modelNotificationsAdmin.EndDate = String(this.setDate(this.modelNotificationsAdmin.EndDate)); this.modelNotificationsAdmin.CompanyId = Number(this.companyid); this.modelNotificationsAdmin.SortOrder = Number(this.modelNotificationsAdmin.SortOrder); } displayFieldCss(field: string, ctrlType: string) { if (ctrlType.toLowerCase() == "txt") { return { 'plain-valid': this.NotificationsAdminForm.get(field).valid, 'is-invalid': (this.NotificationsAdminForm.get(field).invalid && (this.NotificationsAdminForm.get(field).dirty || this.NotificationsAdminForm.get(field).touched)) }; } else { return { 'plain-valid': this.NotificationsAdminForm.get(field).valid, 'plain-invalid': (this.NotificationsAdminForm.get(field).invalid && (this.NotificationsAdminForm.get(field).dirty || this.NotificationsAdminForm.get(field).touched)) }; } } setDate(date) { let _pickYear = 0; let _pickMonth = 0; let _pickDay = 0; let _pickHour = 0; let _pickMin = 0; if (date != undefined && date != "" && date != null) { _pickYear = Number(this.datePipe.transform(date, 'yyyy')); _pickMonth = Number(this.datePipe.transform(date, 'MM')); _pickDay = Number(this.datePipe.transform(date, 'dd')); _pickHour = Number(this.datePipe.transform(date, 'HH')); _pickMin = Number(this.datePipe.transform(date, 'mm')); return _pickMonth + "/" + _pickDay + "/" + _pickYear + " " + _pickHour + ":" + _pickMin; } else return null; } getDate(date) { let _pickYear = ""; let _pickMonth = ""; let _pickDay = ""; let _pickHour = ""; let _pickMin = ""; if (date != undefined && date != "" && date != null) { _pickYear = this.datePipe.transform(date, 'yyyy'); _pickMonth = this.datePipe.transform(date, 'MM'); _pickDay = this.datePipe.transform(date, 'dd'); _pickHour = this.datePipe.transform(date, 'HH'); _pickMin = this.datePipe.transform(date, 'mm'); return _pickYear + "-" + _pickMonth + "-" + _pickDay + "T" + _pickHour + ":" + _pickMin; } else return null; } }