/// <reference path="../../includes.ts" />
declare function unescape(es: any): any;

module acctMgmtFindPatientCtrl {


	interface IAcctMgmtFindPatientController {
		search(): any;
	}

	interface IAcctMgmtFindPatientScope extends angular.IScope{
		myForm : angular.IFormController
	}

	class AcctMgmtFindPatientController implements IAcctMgmtFindPatientController {
	
		static $inject = [
			'$scope',
			'apiSrvc',
			'mydnaApis',
			'errorHandlerSrvc',
			'scrollerSrvc',
			'$rootScope',
			'appMemorySrvc',
			'authenSrvc',
			'locationSrvc',
			'mydnaValues',
			'$filter'
		];
		
		frm: any;
		showErrors: any;
		errorCode: any;
		tabIndexValue: any;
		UrlParameters:any;
		error:any;
		fromRegLink:boolean;
		formattedDate:any;

		constructor(
				private $scope: IAcctMgmtFindPatientScope,
				private apiSrvc: apiSrvc.IApiService,
				private mydnaApis: ImyDNAApis,
				private errorHandlerSrvc: errorHandlerSrvc.IErrorHandlerService,
				private scrollerSrvc: scrollerSrvc.IScrollerService,
				private $rootScope: angular.IRootScopeService,
				private appMemorySrvc: appMemorySrvc.IAppMemoryService,
				private authenSrvc: authenSrvc.IAuthenService,
				private locationSrvc: locationSrvc.ILocationService,
				private mydnaValues: ImyDNAValues,
				private $filter: angular.IFilterService
			){

			var vm = this;
			vm.error ={};
			vm.fromRegLink = false;
			$rootScope.$on("resetTabIndexValue", (event, message) => {
				vm.tabIndexValue = "0";
			});
			
			$rootScope.$on("setTabIndexValue", (event, message) => {
				vm.tabIndexValue = "-1";
			});
			vm.frm={}
			vm.frm.dob = "";
			vm.formattedDate = "";
			vm.UrlParameters = vm.locationSrvc.getParams();
			if (vm.UrlParameters.patientNumber!='undefined' && vm.UrlParameters.patientNumber!="" && vm.UrlParameters.patientNumber!=null){
				vm.frm = {
					LastName : unescape(vm.UrlParameters.lastName),
					PatientNumber : unescape(vm.UrlParameters.patientNumber),
					dob : unescape(vm.UrlParameters.dob),
					grant_type : "password",
					auth_type : "registration"
				};				
				vm.fromRegLink = true;			
			 }else{
				vm.frm = {
					LastName : "",
					PatientNumber : "",
					dob : "",
					grant_type : "password",
					auth_type : "registration"
				};
			}


			vm.showErrors = false;
			vm.errorCode = '';
			this.appMemorySrvc.setGenericFlag(false);
		
			if (vm.fromRegLink){
				vm.validateUser();
			}		

		}

		search(){

			this.scrollerSrvc.TopScroll();

			this.showErrors = this.showErrors == true ?  false : "";

			if(this.$scope.myForm.$valid){
				this.$scope.$emit("disable-on-save-on");				
				this.apiSrvc.registration_requests_Other(
					this.mydnaApis.AcctMgmtToken
					, this.frm, "POST", "url_encoded").then(data => {						
						this.authenSrvc.setOauth(data.data);
						this.appMemorySrvc.setGenericFlag(true);
						this.$scope.$emit("disable-on-save-off");
						this.locationSrvc.goto("acctmgmt/register", "");
					}, err => {						
						this.error= err;
						if(err.status == 400){
							if(err.data.error == "409"){
								this.errorCode = "409";								
							}
							else if (err.data.error == "invalid_grant"){
								this.errorCode = "404";
							}
							else{
								this.errorHandlerSrvc.errorHandler(err, 'debug', '');		
							}
						}
						else{
							this.error= err;
							this.errorHandlerSrvc.errorHandler(err, 'debug', '');
							
						}
						
						this.$scope.$emit("disable-on-save-off");
						this.appMemorySrvc.setGenericFlag(false);
					});
			}
			else{
				this.showErrors = true;
				this.errorCode = "";
			}
		}

		validateUser(){
			this.scrollerSrvc.TopScroll();
			if(this.fromRegLink){
				this.$scope.$emit("disable-on-save-on");				
				this.apiSrvc.registration_requests_Other(
					this.mydnaApis.AcctMgmtToken
					, this.frm,"POST", "url_encoded").then(data => {
						this.$scope.$emit("disable-on-save-off");
					}, err => {				
						this.error= err;
						if(err.status == 400){
							if(err.data.error == "409"){
								this.locationSrvc.goto('login', "");
							}
							else if (err.data.error == "invalid_grant"){
							}
							else{
								this.errorHandlerSrvc.errorHandler(err, 'debug', '');		
							}
						}
						else{						
							this.errorHandlerSrvc.errorHandler(err, 'debug', '');
						}
						this.$scope.$emit("disable-on-save-off");						
					});
			}
		}

	
		// formatDateASddMMyyyy(){
		// 	if (this.frm.dob!=""){				
		// 		this.frm.dob  = this.$filter('date')(new Date(this.frm.dob), 'dd/MM/yyyy');					
		// 		}
		// }


	}

	angular.module('app').controller('acctMgmtFindPatientCtrl', AcctMgmtFindPatientController);

}