import { Component, OnInit, ViewChild } from '@angular/core'; import { EMployeeViewModel } from '../employee-class'; import { AccountService } from '../../accounts/accounts-services'; import { Subject, Subscription } from 'rxjs'; import { debounceTime } from 'rxjs/operators'; import { StoreService } from '../../store/store.services'; import { NotificationService } from '../../../_services/notification.service'; import { EmployeeServices } from '../employee-services'; import { ActivatedRoute } from '@angular/router'; import { ItemViewModel } from '../../store/store-model'; import { LocalService } from '../../../_services/local.service'; import { NgForm } from '@angular/forms'; import { FormCanDeactivate } from '../../../_guards/form-can-deactivate'; @Component({ selector: 'app-add-employee', templateUrl: './add-employee.component.html', }) export class AddEmployeeComponent extends FormCanDeactivate implements OnInit { @ViewChild('f') form: NgForm; public searchEvent = new Subject(); public searchSubcription: Subscription = null; public saving: any; public preHeadsList: any[]; public visaStatusList: any[]; public heads1List: any[]; public heads2List: any[]; public AccCodeValid: any; public catlogList: any[]; public nowDate = new Date(); public employeeViewModel = new EMployeeViewModel(); constructor(public _accountService: AccountService, public _storeService: StoreService, public _notificationService: NotificationService, public _employeeService: EmployeeServices, private activatedRoute: ActivatedRoute, private _localService: LocalService) { super(); } ngOnInit() { this.activatedRoute.queryParams.subscribe(params => { const accId = params['id']; if (accId != null) this.getEMployee(accId); else { this.validateAccCode("-1"); this.employeeViewModel.FK_Visa_Status = new ItemViewModel(); this.employeeViewModel.FK_Visa_Status.Id = 2; this.employeeViewModel.FK_Visa_Status.Name = "Own"; this.employeeViewModel.Annual_Leave = "Not Allowed"; this.employeeViewModel.Ticket = "Not Allowed"; //Head Fixing this.employeeViewModel.Fk_PreHead = new ItemViewModel(); this.employeeViewModel.Fk_PreHead.Id = 10; this.employeeViewModel.Fk_PreHead.Name = 'Liabilities'; this.employeeViewModel.Fk_Head1 = new ItemViewModel(); this.employeeViewModel.Fk_Head1.Id = 16; this.employeeViewModel.Fk_Head1.Name = 'Short Term Liabilities'; this.employeeViewModel.Fk_Head2 = new ItemViewModel(); this.employeeViewModel.Fk_Head2.Id = 41; this.employeeViewModel.Fk_Head2.Name = 'Employees'; } }); this.getPreHeads(); this.getVisaStatus(); this.handleSearchEvents(); } public getEMployee(id: number) { this._employeeService.getEmployeeById(id).subscribe((res: any) => { if (res) { this.employeeViewModel = res.emp; this.employeeViewModel.DOB = new Date(res.emp.DOB); this.employeeViewModel.PassPort_Exp = new Date(res.emp.PassPort_Exp); this.employeeViewModel.Visa_Exp = new Date(res.emp.Visa_Exp); this.employeeViewModel.Driving_Licenc_Exp = new Date(res.emp.Driving_Licenc_Exp); this.employeeViewModel.Health_Card_Exp = new Date(res.emp.Health_Card_Exp); this.employeeViewModel.Insurance__Exp = new Date(res.emp.Insurance__Exp); this.employeeViewModel.dateAcc = new Date(res.emp.dateAcc); this.AccCodeValid = true; } }, err => { }); } public ClearForm() { this.employeeViewModel = new EMployeeViewModel(); this.employeeViewModel.FK_Visa_Status = new ItemViewModel(); this.employeeViewModel.FK_Visa_Status.Id = 2; this.employeeViewModel.FK_Visa_Status.Name = "Own"; this.employeeViewModel.Annual_Leave = "Not Allowed"; this.employeeViewModel.Ticket = "Not Allowed"; } public onSubmit() { this.saving = true; let emp: any = Object.assign({}, this.employeeViewModel); if (this.employeeViewModel.DOB) emp.DOB = this.employeeViewModel.DOB.toDateString(); if (this.employeeViewModel.dateAcc) emp.dateAcc = this.employeeViewModel.dateAcc.toDateString(); if (this.employeeViewModel.PassPort_Exp) emp.PassPort_Exp = this.employeeViewModel.PassPort_Exp.toDateString(); if (this.employeeViewModel.Visa_Exp) emp.Visa_Exp = this.employeeViewModel.Visa_Exp.toDateString(); if (this.employeeViewModel.Driving_Licenc_Exp) emp.Driving_Licenc_Exp = this.employeeViewModel.Driving_Licenc_Exp.toDateString(); if (this.employeeViewModel.Insurance__Exp) emp.Insurance__Exp = this.employeeViewModel.Insurance__Exp.toDateString(); this._employeeService.saveEmployee(emp).subscribe((res: any) => { this._notificationService.notify("success", "Employee Save Successfully!"); this.saving = false; this.employeeViewModel = new EMployeeViewModel(); this.validateAccCode("-1"); this.employeeViewModel.FK_Visa_Status = new ItemViewModel(); this.employeeViewModel.FK_Visa_Status.Id = 2; this.employeeViewModel.FK_Visa_Status.Name = "Own"; this.employeeViewModel.Annual_Leave = "Not Allowed"; this.employeeViewModel.Ticket = "Not Allowed"; //Head Fixing this.employeeViewModel.Fk_PreHead = new ItemViewModel(); this.employeeViewModel.Fk_PreHead.Id = 10; this.employeeViewModel.Fk_PreHead.Name = 'Liabilities'; this.employeeViewModel.Fk_Head1 = new ItemViewModel(); this.employeeViewModel.Fk_Head1.Id = 16; this.employeeViewModel.Fk_Head1.Name = 'Short Term Liabilities'; this.employeeViewModel.Fk_Head2 = new ItemViewModel(); this.employeeViewModel.Fk_Head2.Id = 41; this.employeeViewModel.Fk_Head2.Name = 'Employees'; }, err => { this._notificationService.notify("danger", err.msg); this.saving = false; this.employeeViewModel = new EMployeeViewModel(); this.validateAccCode("-1"); this.employeeViewModel.FK_Visa_Status = new ItemViewModel(); this.employeeViewModel.FK_Visa_Status.Id = 2; this.employeeViewModel.FK_Visa_Status.Name = "Own"; this.employeeViewModel.Annual_Leave = "Not Allowed"; this.employeeViewModel.Ticket = "Not Allowed"; //Head Fixing this.employeeViewModel.Fk_PreHead = new ItemViewModel(); this.employeeViewModel.Fk_PreHead.Id = 10; this.employeeViewModel.Fk_PreHead.Name = 'Liabilities'; this.employeeViewModel.Fk_Head1 = new ItemViewModel(); this.employeeViewModel.Fk_Head1.Id = 16; this.employeeViewModel.Fk_Head1.Name = 'Short Term Liabilities'; this.employeeViewModel.Fk_Head2 = new ItemViewModel(); this.employeeViewModel.Fk_Head2.Id = 41; this.employeeViewModel.Fk_Head2.Name = 'Employees'; }); } public getPreHeads() { this._accountService.getPreHeads().subscribe((res: any) => { if (res) { this.preHeadsList = res; } }, err => { }); } public getVisaStatus() { this._accountService.getVisaStatus().subscribe((res: any) => { if (res) { this.visaStatusList = res; } }, err => { }); } public getHead1(e: number) { this._accountService.getHead1(e).subscribe((res: any) => { if (res) { this.heads1List = res; } }, err => { }); } public getHead2(e: number) { this._accountService.getHead2(e).subscribe((res: any) => { if (res) { this.heads2List = res; } }, err => { this._notificationService.notify("danger", err); }); } public dropDownChange(e) { console.log(e); if (e.filter == "ph") { this.getHead1(e.event.Id); } if (e.filter == "h1") { this.getHead2(e.event.Id); } } public getCatlogs(x: string, event: string) { this._storeService.getFilterdCatlogs(x, event).subscribe((res: any) => { if (res) { this.catlogList = res; } }, err => { this._notificationService.notify("danger", err.msg); }); } public validateAccCode(x) { debugger; this._accountService.validateAccCode(x).subscribe((res: any) => { if (res.msg == "Valid") { this.AccCodeValid = true; this.employeeViewModel.Acc_Code = res.code; } else { this.AccCodeValid = false; } }, err => { this._notificationService.notify("danger", err); }); } public handleSearchEvents() { this.searchSubcription = this.searchEvent.pipe( debounceTime(400)).subscribe((x: any) => { console.log(x); debugger; if (x) { if (x.filter == "City") { this.getCatlogs("City", x.event); } if (x.filter == "code") { this.validateAccCode(x.event); } } }); } }