define(['app'], function (app) {
    app.controller('LoginCtrl', function ($scope,$rootScope,AppData,$location,$timeout) {
		$rootScope.menucont = true;
		$rootScope.modalShown =false;
		$rootScope.is_logged = false;
		$scope.formData = {}
		$rootScope.formData = {}
		$scope.formData.funName = "login";
		$scope.formData.lang = $rootScope.selLanguage;
		$rootScope.pageData = loginData[$rootScope.selLanguage];
		$scope.url = "../senddata/log/";
		console.log($rootScope.pageData.errorMsg[0])
		$scope.user_error = '';
		$scope.pass_error = '';
		$scope.clearErrorMsg = function(){
			$scope.user_error = '';
			$scope.pass_error = '';
		}
		$scope.fieldValidation = function(){
			console.log($scope.formData.emp_code)
			$scope.user_error = ($scope.formData.emp_code == undefined || $scope.formData.emp_code == '') ?  $rootScope.pageData.errorMsg[0] : '';
			$scope.pass_error = ($scope.formData.password == undefined || $scope.formData.password == '') ?  $rootScope.pageData.errorMsg[1] : '';
			return ($scope.user_error == '' && $scope.pass_error == '' ) ? true : false;
		}
		
      	$scope.loginSubmit = function(){
			  $scope.clearErrorMsg();
			  console.log("here"+$scope.fieldValidation())
			  if($scope.fieldValidation()){
				  console.log("here"+$scope.fieldValidation())
					AppData.sendData($scope.url,$scope.formData,function(res){
						if(res.cont){
							$rootScope.items = res.item.control;
							$rootScope.username = res.item.username;
							$location.path("/user")
							$rootScope.is_logged = true;
						}else{
							$scope.pass_error = res.mess;
						}
					});

			  }
		}
    });
}); 