import { Component, ElementRef, NgZone, OnInit } from '@angular/core'; import { FormBuilder, FormControl, FormGroup, Validators } from '@angular/forms'; import { ActivatedRoute, Router } from '@angular/router'; import { CustomValidator } from '../../../../../core/custom-validator'; import { PricingSegment } from '../../../../../models/admin/company/pricing-segments-manager/pricing-segment'; import { PricingSegmentService } from '../../../../../services/admin/company/pricing-segments-manager/pricing-segment.service'; import { CommonService } from '../../../../../services/common.service'; @Component({ selector: 'app-pricing-segments-add', templateUrl: './pricing-segments-add.component.html', styleUrls: ['./pricing-segments-add.component.css'] }) export class PricingSegmentsAddComponent implements OnInit { private _paramId: any; PricingSegmentServiceAPIResponse: any; modelPricingSegment = new PricingSegment(); PricingSegmentForm: FormGroup; companyid: number = 1; constructor( private formBuilder: FormBuilder, private route: ActivatedRoute, private router: Router, private service: PricingSegmentService, private common: CommonService, private ngZone: NgZone, private elementRef: ElementRef ) { } ngOnInit(): void { this._paramId = Number(this.route.snapshot.params.id); this.setFormBuilder(); if (this._paramId == 0) { this.setAddForm(); } else { this.service.getPricingSegmentById(this._paramId, data => { this.PricingSegmentServiceAPIResponse = data; this.modelPricingSegment = this.PricingSegmentServiceAPIResponse.objResponse.responseContent; this.setEditForm(); }, (error) => { console.error('Error: ' + error); return error(error); }); } } setFormBuilder() { this.PricingSegmentForm = this.formBuilder.group({ PricingSegmentId: [''], CompanyId: [''], Description: ['', Validators.compose([Validators.required, Validators.maxLength(50), CustomValidator.noWhitespaceValidator])], SortOrder: ['', Validators.compose([Validators.pattern("^[0-9]*$"), Validators.min(0),Validators.max(99999)]) ], IsDefault: true, IsForComponents: true, IsActive: true, }); } setAddForm() { this.PricingSegmentForm.setValue({ PricingSegmentId: 0, Description:'', SortOrder: '', IsDefault: 1, IsForComponents: true, IsActive: 1, CompanyId: this.companyid, }); } setEditForm() { this.PricingSegmentForm.setValue({ PricingSegmentId: this.modelPricingSegment.PricingSegmentId, Description: this.modelPricingSegment.Description !=null?this.modelPricingSegment.Description:"", CompanyId: this.companyid, SortOrder: this.modelPricingSegment.SortOrder, IsDefault: this.modelPricingSegment.IsDefault, IsForComponents: this.modelPricingSegment.IsForComponents, IsActive: this.modelPricingSegment.IsActive, }); } submitform() { if (this.PricingSegmentForm.valid) { this.modelPricingSegment = this.PricingSegmentForm.value; this.setModelBeforeSubmit(); this.service.SavePricingSegment(this.modelPricingSegment, data => { this.PricingSegmentServiceAPIResponse = data; if (this.PricingSegmentServiceAPIResponse.objResponse.statusCode == "400") { this.common.showError("Description is already exist", "Transaction Fail!"); }else if (Number(this.PricingSegmentServiceAPIResponse.objResponse.statusCode) == 200) { this.common.showSuccess("Pricing Segment changes applied successfully.", "Success") this.ngZone.run(() => this.navigateTo('/admin/admin-company/pricing-segments-list-component')); } else { this.common.showError("Transaction not completed.", "Transaction Fail!") } }, (error) => { console.error('Error: ' + error); return error(error); }); } else { this.common.validateAllFormFields(this.PricingSegmentForm); this.common.validationfocus(this.PricingSegmentForm, this.elementRef); this.common.showError('Description must be required.', 'Validation alert!') } } navigateTo(url) { this.router.navigate([url]); } setModelBeforeSubmit() { this.modelPricingSegment.PricingSegmentId = Number(this.modelPricingSegment.PricingSegmentId); this.modelPricingSegment.Description = String(this.modelPricingSegment.Description.trim()); this.modelPricingSegment.SortOrder = Number(this.modelPricingSegment.SortOrder); this.modelPricingSegment.IsDefault = Boolean(this.modelPricingSegment.IsDefault); this.modelPricingSegment.IsForComponents = Boolean(this.modelPricingSegment.IsForComponents); this.modelPricingSegment.IsActive = Boolean(this.modelPricingSegment.IsActive); this.modelPricingSegment.UserId = Number(2); this.modelPricingSegment.IpAddress = String("127.0.0.1") this.modelPricingSegment.CompanyId = Number(this.companyid) } displayFieldCss(field: string, ctrlType: string) { if (ctrlType.toLowerCase() == "txt") { return { 'plain-valid': this.PricingSegmentForm.get(field).valid, 'is-invalid': (this.PricingSegmentForm.get(field).invalid && (this.PricingSegmentForm.get(field).dirty || this.PricingSegmentForm.get(field).touched)) }; } else { return { 'plain-valid': this.PricingSegmentForm.get(field).valid, 'plain-invalid': (this.PricingSegmentForm.get(field).invalid && (this.PricingSegmentForm.get(field).dirty || this.PricingSegmentForm.get(field).touched)) }; } } }