import { Component, ElementRef, OnInit } from '@angular/core'; import { CommonService } from '../../../../services/common.service'; import { ActivatedRoute, Router } from "@angular/router"; import { FormBuilder, Validators, FormGroup } from '@angular/forms'; import { PolicyStatementService } from '../../../../services/admin/system/policy-statement/policy-statement.service'; import { PolicyStatement } from "../../../../models/admin/system/policy-statement/policy-statement"; import { TermsConditions } from "../../../../models/admin/system/policy-statement/policy-statement"; import { PolicyTerms } from "../../../../models/admin/system/policy-statement/policy-statement"; import { CustomValidator } from '../../../../core/custom-validator'; @Component({ selector: 'app-policy-statement', templateUrl: './policy-statement.component.html', styleUrls: ['./policy-statement.component.css'] }) export class PolicyStatementComponent implements OnInit { tinyMCEInitOptions; modelPolicyStatement = new PolicyStatement(); modelTermsConditions = new TermsConditions(); modeldata = new PolicyTerms(); formData: FormData = new FormData(); SelectedPolicyStatement: PolicyStatement; SelectedTermsConditions: TermsConditions; IsForEdit: boolean = false; IsWrongFileFormate: boolean = false; imageFile: { link: string, file: any, name: string }; File: File = null; imageData_PS: string = ''; imageData_TC: string = ''; imageName_PS: string = ''; imageName_TC: string = ''; //isVideo_PS: boolean = false; //isVideo_TC: boolean = false; previewPolicyStatement: string = ''; previewTermsConditions: string = ''; PolicyStatementForm: FormGroup; TermsConditionsForm: FormGroup; PolicyStatementAPIResponse: any; _paramId: number = 0; previewModal; previewContent: string = ''; format_PS: string = ''; format_TC: string = ''; showModalPolicy: boolean = false; showModalTerms: boolean = false; constructor( private formBuilder: FormBuilder, private route: ActivatedRoute, private service: PolicyStatementService, private common: CommonService, private router: Router, private elementRef: ElementRef, ) { } ngOnInit(): void { this.tinyMCEInitOptions = this.common.tinyMCEOptions(); this._paramId = Number(this.route.snapshot.params.id); this.setFormBuilderPolicyStatement(); this.setFormBuilderTermsCondition(); if (this._paramId == 0) { this.IsForEdit = false; this.setAddFormPolicyStatement(); this.setAddFormTermsCondition(); } else { this.IsForEdit = true; this.service.getById(this._paramId, data => { this.PolicyStatementAPIResponse = data; this.modeldata.modelPolicyStatement = this.PolicyStatementAPIResponse.objResponse.responseContent.modelPolicyStatement; this.modeldata.modelTermsConditions = this.PolicyStatementAPIResponse.objResponse.responseContent.modelTermsConditions; if (this.modeldata.modelPolicyStatement.ImageData != null) { this.imageData_PS = this.modeldata.modelPolicyStatement.ImageData; this.imageName_PS = this.modeldata.modelPolicyStatement.FileName; this.format_PS = (this.imageName_PS.split('.').slice(-1)[0] == 'mp4') ? 'video' : 'image'; } if (this.modeldata.modelTermsConditions.ImageData != null) { this.imageData_TC = this.modeldata.modelTermsConditions.ImageData; this.imageName_TC = this.modeldata.modelTermsConditions.FileName; this.format_TC = (this.imageName_TC.split('.').slice(-1)[0] == 'mp4') ? 'video' : 'image'; } this.setEditFormPolicyStatement(); this.setEditFormTermsConditions(); }, (error) => { console.error('Error: ' + error); return error(error); }); } } setFormBuilderPolicyStatement() { this.PolicyStatementForm = this.formBuilder.group({ PolicyStatementId: [''], Title: ['', [Validators.required, Validators.maxLength(50),CustomValidator.noWhitespaceValidator]], Content: ['', Validators.required], File: [''], FileName: [''], FilePath: [''] }); } setFormBuilderTermsCondition() { this.TermsConditionsForm = this.formBuilder.group({ TermsConditionsId: [''], Title: ['', [Validators.required, Validators.maxLength(50),CustomValidator.noWhitespaceValidator]], Content: ['', Validators.required], File: [''], FileName: [''], FilePath: [''] }); } setAddFormPolicyStatement() { this.PolicyStatementForm.setValue({ PolicyStatementId: 0, Title: '', Content: '', File: '', FileName: '', FilePath: '' }); } setAddFormTermsCondition() { this.TermsConditionsForm.setValue({ TermsConditionsId: 0, Title: '', Content: '', File: '', FileName: '', FilePath: '' }); } checkFile_PS(fileInput_PS: any) { const reader = new FileReader(); let FileFormet = fileInput_PS.target.files[0].name.split('.'); let formetType = FileFormet.slice(-1)[0]; if (formetType == 'jpg' || formetType == 'png' || formetType == 'mp4') { this.IsWrongFileFormate = false; this.File = fileInput_PS.target.files[0]; this.imageName_PS = this.File.name; reader.readAsDataURL(this.File); reader.onload = () => { this.imageData_PS = reader.result as string; this.format_PS = (formetType == 'mp4') ? 'video' : 'image'; }; } else { this.IsWrongFileFormate = true; this.common.showError("Please select valid file.", "Wrong File Format!") } } checkFile_TC(fileInput_TC: any) { const reader = new FileReader(); let FileFormet = fileInput_TC.target.files[0].name.split('.'); let formetType = FileFormet.slice(-1)[0]; if (formetType == 'jpg' || formetType == 'png' || formetType == 'mp4') { this.IsWrongFileFormate = false; this.File = fileInput_TC.target.files[0]; this.imageName_TC = this.File.name; reader.readAsDataURL(this.File); reader.onload = () => { this.imageData_TC = reader.result as string; this.format_TC = (formetType == 'mp4') ? 'video' : 'image'; }; } else { this.IsWrongFileFormate = true; this.common.showError("Please select valid file.", "Wrong File Format!") } } showPreviewModal_PS() { this.showModalTerms = false; this.showModalPolicy = true; this.previewContent = this.PolicyStatementForm.get("Content").value; this.previewModal = { content: this.previewContent, file: this.imageData_PS, fileFormat: this.format_PS } } getModalToggleValuePolicy(data) { this.showModalPolicy = data.toggle; } showPreviewModal_TC() { this.showModalPolicy = false; this.showModalTerms = true; this.previewContent = this.TermsConditionsForm.get("Content").value; this.previewModal = { content: this.previewContent, file: this.imageData_TC, fileFormat: this.format_TC } } getModalToggleValueTerms(data) { this.showModalTerms = data.toggle; } onPreviewClickPolicyStatement() { this.previewPolicyStatement = this.PolicyStatementForm.get("Content").value; } onPreviewClickTermsCondition() { this.previewTermsConditions = this.TermsConditionsForm.get("Content").value; } setEditFormPolicyStatement() { this.PolicyStatementForm.setValue({ PolicyStatementId: this.modeldata.modelPolicyStatement.PolicyStatementId, Title: this.modeldata.modelPolicyStatement.Title == null ? "" : this.modeldata.modelPolicyStatement.Title, Content: this.modeldata.modelPolicyStatement.Content == null ? "" : this.modeldata.modelPolicyStatement.Content, File: '', FileName: this.modeldata.modelPolicyStatement.FileName == null ? "" : this.modeldata.modelPolicyStatement.FileName, FilePath: this.modeldata.modelPolicyStatement.FilePath == null ? "" : this.modeldata.modelPolicyStatement.FilePath, }); } setEditFormTermsConditions() { this.TermsConditionsForm.setValue({ TermsConditionsId: this.modeldata.modelTermsConditions.TermsConditionsId, Title: this.modeldata.modelTermsConditions.Title == null ? "" : this.modeldata.modelTermsConditions.Title, Content: this.modeldata.modelTermsConditions.Content == null ? "" : this.modeldata.modelTermsConditions.Content, File: '', FileName: this.modeldata.modelTermsConditions.FileName == null ? "" : this.modeldata.modelTermsConditions.FileName, FilePath: this.modeldata.modelTermsConditions.FilePath == null ? "" : this.modeldata.modelTermsConditions.FilePath, }); } submitform_PS() { if (this.PolicyStatementForm.valid) { this.modelPolicyStatement = this.PolicyStatementForm.value; if (this.modelPolicyStatement.Title.trim() == "") { this.common.showError("please enter vaild Description.", "Validation Fail") } else if (this.modelPolicyStatement.Content.replace(/]*>/g, 'IMAGE ').replace(/<[^>]*>| /g, '').trim() == "") { this.common.showError("please enter vaild Content.", "Validation Fail") } else { this.setModelBeforeSubmit_PS(); this.service.SavePolicyStatement(this.formData, data => { this.PolicyStatementAPIResponse = data; if (this.PolicyStatementAPIResponse.objResponse.statusCode == "200") { this.gotoList(); this.common.showSuccess("PolicyStatement changes applied successfully.", "Success") } else { this.common.showError("Transaction not completed.", "Transaction Fail") } }, (error) => { console.error('Error: ' + error); return error(error); }); } } else { this.common.validateAllFormFields(this.PolicyStatementForm); this.common.validationfocus(this.PolicyStatementForm, this.elementRef); this.common.showError("Please fill required fields.", "Validation Alert!") } } submitform_TC() { if (this.TermsConditionsForm.valid) { this.modelTermsConditions = this.TermsConditionsForm.value; if (this.modelTermsConditions.Title.trim() == "") { this.common.showError("Please enter vaild description.", "Validation alert!") } else if (this.modelTermsConditions.Content.replace(/]*>/g, 'IMAGE ').replace(/<[^>]*>| /g, '').trim() == "") { this.common.showError("Please enter vaild content.", "Validation Fail") } else { this.setModelBeforeSubmit_TC(); this.service.SaveTermsConditions(this.formData, data => { this.PolicyStatementAPIResponse = data; if (this.PolicyStatementAPIResponse.objResponse.statusCode == "200") { this.gotoList(); this.common.showSuccess("TermsConditions changes applied successfully.", "Success") } else { this.common.showError("Transaction not completed.", "Transaction Fail") } }, (error) => { console.error('Error: ' + error); return error(error); }); } } else { this.common.validateAllFormFields(this.TermsConditionsForm); this.common.validationfocus(this.TermsConditionsForm, this.elementRef); this.common.showError("Please fill required fields.", "Validation Alert!") } } setModelBeforeSubmit_PS() { this.formData = new FormData(); if (this.imageName_PS == null) { this.modelPolicyStatement.FileName = null; this.modelPolicyStatement.FilePath = null; } this.formData.append('PolicyStatementId', String(this.modelPolicyStatement.PolicyStatementId)); this.formData.append('Title', String(this.modelPolicyStatement.Title)); this.formData.append('Content', String(this.modelPolicyStatement.Content)); this.formData.append('File', this.File); this.formData.append('FileName', String(this.modelPolicyStatement.FileName)); this.formData.append('FilePath', String(this.modelPolicyStatement.FilePath)); } setModelBeforeSubmit_TC() { this.formData = new FormData(); if (this.imageName_TC == null) { this.modelPolicyStatement.FileName = null; this.modelPolicyStatement.FilePath = null; } this.formData.append('TermsConditionsId', String(this.modelTermsConditions.TermsConditionsId)); this.formData.append('Title', String(this.modelTermsConditions.Title)); this.formData.append('Content', String(this.modelTermsConditions.Content)); this.formData.append('File', this.File); this.formData.append('FileName', String(this.modelTermsConditions.FileName)); this.formData.append('FilePath', String(this.modelTermsConditions.FilePath)); } gotoList() { this.router.navigate(["/admin"]); } displayFieldCss_PS(field: string, ctrlType: string) { if (ctrlType.toLowerCase() == "txt") { return { 'plain-valid': this.PolicyStatementForm.get(field).valid, 'is-invalid': (this.PolicyStatementForm.get(field).invalid && (this.PolicyStatementForm.get(field).dirty || this.PolicyStatementForm.get(field).touched)) }; } else { return { 'plain-valid': this.PolicyStatementForm.get(field).valid, 'plain-invalid': (this.PolicyStatementForm.get(field).invalid && (this.PolicyStatementForm.get(field).dirty || this.PolicyStatementForm.get(field).touched)) }; } } displayFieldCss_TC(field: string, ctrlType: string) { if (ctrlType.toLowerCase() == "txt") { return { 'plain-valid': this.TermsConditionsForm.get(field).valid, 'is-invalid': (this.TermsConditionsForm.get(field).invalid && (this.TermsConditionsForm.get(field).dirty || this.TermsConditionsForm.get(field).touched)) }; } else { return { 'plain-valid': this.TermsConditionsForm.get(field).valid, 'plain-invalid': (this.TermsConditionsForm.get(field).invalid && (this.TermsConditionsForm.get(field).dirty || this.TermsConditionsForm.get(field).touched)) }; } } }