myApp.controller("assignAccountsController",function($scope, myFactory, $mdDialog, $http, appConfig, $timeout, $element, $window){
	$scope.records = [];
	$scope.parentData = {
			"accountId": "",
			"accountName": "",
			"industryType":"",
			"status":"",
			"deliveryManagers":[],
			"action":"",
			"clientAddress":""
	};
	var getCellTemplate = '<p class="col-lg-12"><i class="fa fa-pencil-square-o fa-2x" aria-hidden="true" style="font-size:1.5em;margin-top:3px;cursor:pointer;" data-placement="center" title="Edit" onmouseenter="$(this).tooltip(\'show\')" ng-click="grid.appScope.getRowData(row,\'Update\')"></i>'+
	'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<i class="fa fa-minus-circle fa-2x" aria-hidden="true" style="font-size:1.5em;margin-top:3px;cursor:pointer;" data-placement="left" title="Delete" onmouseenter="$(this).tooltip(\'show\')" ng-click="grid.appScope.getRowData(row,\'Delete\')"></i></p>';
	$scope.gridOptions = {
			paginationPageSizes : [ 10, 20, 30, 40, 50, 100],
			paginationPageSize : 50,
		    pageNumber: 1,
			pageSize:10,
			enableFiltering: true,
			columnDefs : [ 
				{field : 'accountId',displayName: 'Account ID', enableColumnMenu: true, enableSorting: true,enableFiltering: false, width:120,cellClass: 'grid-align'},
				{field : 'accountName',displayName: 'Account Name', enableColumnMenu: false, enableSorting: false,enableFiltering: true,cellClass: 'grid-align'},
				{field : 'industryType',displayName: 'Industry Type', enableColumnMenu: false, enableSorting: true,enableFiltering: true,cellClass: 'grid-align'}, 
				{field : 'status',displayName: 'Status', enableColumnMenu: false, enableSorting: true,enableFiltering: true,cellClass: 'grid-align'}, 
				{field : 'deliveryManagers',displayName: 'Delivery Managers',  cellTemplate: '<div ng-repeat= "item in row.entity[col.field]">{{item.employeeName}}<span ng-hide="$last">,</span></div>' ,enableColumnMenu: false, enableSorting: true,enableFiltering: false,cellClass: 'grid-align'}, 
				{name : 'Actions', displayName: 'Actions',cellTemplate: getCellTemplate, enableColumnMenu: false, enableSorting: false, enableFiltering:false,width:130} 
			]
		};
	$scope.nameFields = ['employeeName']
	$scope.gridOptions.data = $scope.records;
	$scope.getRowData = function(row, action){
		$scope.parentData.id = row.entity.id;
		$scope.parentData.accountId = row.entity.accountId;
		$scope.parentData.accountName = row.entity.accountName;
		$scope.parentData.industryType = row.entity.industryType;
		$scope.parentData.deliveryManagers = row.entity.deliveryManagers;
		$scope.parentData.status = row.entity.status;
		$scope.parentData.clientAddress = row.entity.clientAddress;
		if(action == "Update")
			$scope.addAccount(action, $scope.parentData);
		else if(action == "Delete")
			$scope.deleteAccount(row,action);
	}
	$scope.refreshPage = function(){
		$scope.getAccountDetails();
	}
	$scope.getAccountDetails = function(){
		$http({
	        method : "GET",
	        url : appConfig.appUri + "account/accounts"
	    }).then(function mySuccess(response) {
	    	  $scope.gridOptions.data=response.data;
	    	  myFactory.setAccounts(response.data);
	    }, function myError(response) {
	    	showAlert("Something went wrong while fetching data!!!");
	    	$scope.gridOptions.data = [];
	    });
	};
	$scope.addAccount = function(action, userData){
		$('#home').addClass('md-scroll-mask');
		userData.action = action;
		$mdDialog.show ({
            clickOutsideToClose: false,
            scope: $scope,        
            preserveScope: true,           
            templateUrl: 'templates/newAccount.html',
            controller: addController,
            locals:{dataToPass: userData,gridOptionsData: $scope.gridOptions.data},
         }).then(function(result) {
		    	if(result == "Add") showAlert('Account assigned successfully');
		    	else if(result == "Update") showAlert('Account updated successfully');
		    	else if(result == "Cancelled") console.log(result);
		    	else showAlert('Account assigning/updation failed!!!');
		    });
	}
	function showAlert(message) {
		$mdDialog.show($mdDialog.alert().parent(
				angular.element(document.querySelector('#popupContainer')))
				.clickOutsideToClose(true).textContent(message).ariaLabel(
						'Alert Dialog').ok('Ok'));
	}
	$scope.deleteAccount = function(row,action){
		$('#home').addClass('md-scroll-mask');
	    var confirm = $mdDialog.confirm()
	    	  .clickOutsideToClose(true)
	          .textContent('Are you sure you want to delete this account?')
	          .ok('Ok')
	          .cancel('Cancel');
	    $mdDialog.show(confirm).then(function() {
	    	deleteAccountRole(row.entity.accountId);
	    	$timeout(function(){updateGridAfterDelete()},500);
	    }, function() {
	    	console.log("Cancelled dialog");
	    });
	};
	function deleteAccountRole(accountId){
		var req = {
				method : 'DELETE',
				url : appConfig.appUri+ "account/accounts?accountId="+accountId
			}
			$http(req).then(function mySuccess(response) {
				$scope.result = "Success";
			}, function myError(response){
				$scope.result = "Error";
			});
	}
	function updateGridAfterDelete(){
		if($scope.result == "Success"){
			$scope.refreshPage();
			showAlert('Account deleted successfully');
		}else if($scope.result == "Error"){
			showAlert('Something went wrong while deleting the account.')
		}
	}
	
	function addController($scope, $mdDialog, dataToPass, gridOptionsData, $mdSelect) {
		$scope.selectedEmployeeNames = [];
		$scope.accountName;
		$scope.industryType;
		$scope.clientAddress;
		$scope.searchTerm = '';
		$scope.templateTitle=dataToPass.action;
		$scope.accountValidationMessage='';
		$scope.addButtonvisible=false;
		$scope.managerDetails = [];
		$scope.managersSelectedList = [];
        $scope.employeeInTeam = []; 
		$scope.industryTypesList = ['Retail','Finance', 'HealthCare','Government','Insurance'];
		$window.addEventListener('click',function(e){
			if(e.target.type !== 'search'){
				 $mdSelect.hide();
			}
		})
		if(dataToPass.action == "Add"){
			$scope.accountId = "";
			$scope.accountName = "";
			$scope.industryType = "";
			$scope.clientAddress = "";
			$scope.deliveryManagers = [];
			$scope.alertMsg = "";
 			
		}else if(dataToPass.action == "Update"){
			$scope.accountId = dataToPass.accountId;
			$scope.accountName = dataToPass.accountName;
			$scope.industryType = dataToPass.industryType;
			$scope.clientAddress = dataToPass.clientAddress;
			//$scope.managersSelectedList = dataToPass.deliveryManagers;
			for (key in dataToPass.deliveryManagers) {
				$scope.selectedEmployeeNames.push(dataToPass.deliveryManagers[key].employeeName);
			}
		}
		$scope.ifEmployeeNameExists = function(empName) {
			return $scope.selectedEmployeeNames.includes(empName);
		}
		$scope.getDeliveryManagers = function(){
			$http({
		        method : "GET",
		        url : appConfig.appUri + "/projectTeam/getEmployeesToTeam"
		    }).then(function mySuccess(response) {
		    	$scope.managerDetails=response.data;
		    }, function myError(response) {
		    	showAlert("Something went wrong while fetching data!!!");
		    	$scope.gridOptions.data = [];
		    });
		};

		$scope.closeSelectBox = function () {
			$mdSelect.hide();
		}
		$scope.cancel = function(){
			var showConfirmDialog = false;
			var totalFields = $scope.myForm.$$controls;
			for (key in totalFields) {
				console.log("Model value of" + key + " ==>" + totalFields[key].$modelValue)
				if(totalFields[key].$modelValue !== '' && totalFields[key].$modelValue !== undefined && totalFields[key].$modelValue.length !== 0) {
					console.log("Came here!");
					showConfirmDialog = true;
				}
			}
			if(showConfirmDialog){
				$mdDialog.show($mdDialog.confirm({
					skipHide: true,
					textContent: 'Are you sure you want to cancel this?',
					ok: 'ok',
					cancel:'cancel'
				  })).then(function(){
					  $mdDialog.hide('Cancelled');
			})
				}
				else{
					$mdDialog.hide('Cancelled'); 
				}
		}
	    $scope.updateSearch = function (e) {
	            e.stopPropagation();
	    }
	    $scope.searchFilter = function (obj) {
	    	    var stringToGoIntoTheRegex = $scope.searchTerm;
	            var regex = new RegExp("^" + stringToGoIntoTheRegex,"i");
	            var filteredWord =  !$scope.searchTerm || obj.employeeName.match(regex);
	            return filteredWord;  
	    };
        $scope.removeSelectedLead = function(item){
            var index = $scope.managersSelectedList.indexOf(item);
            $scope.employeeInTeam.splice(index,1);
            $scope.managersSelectedList.splice(index,1);
        }
        $scope.getSelectedLead = function(){
            $scope.managersSelectedList.forEach(function(manager){
                if(!$scope.employeeInTeam.includes(manager.employeeId))
                $scope.employeeInTeam.push(manager.employeeId)      
            })
            console.log($scope.employeeInTeam);
        }
		$scope.getIndustryTypeSelected = function(){
			if ($scope.industryType !== undefined) {
				$scope.industryType = $scope.industryType;
				return $scope.industryType;
			} else {
				return "Please select a Industry Type";
			}
		};
	    $scope.clearSearchTerm = function() {
	        $scope.searchTerm = '';
	    };
	    $element.find('input').on('keydown', function(ev) {
	          ev.stopPropagation();
	    });
	  
      $scope.validateFields = function(action){
    	  var managersSelectedList = $scope.managersSelectedList;
    	  var accountName = $scope.accountName;
    	  var industryType = $scope.industryType;
    	  var clientAddress = $scope.clientAddress;
    	  $scope.accountManagers = function(){
				var employeeIdsArray = [];
				for(i=0;i< $scope.managersSelectedList.length;i++){
					 employeeIdsArray[i] =$scope.managersSelectedList[i].employeeId;
				}
				return employeeIdsArray;
		  }
  		  if(action == "Add"){
			if(managersSelectedList == undefined){
				$scope.alertMsg = "Please select a accountManager";
			document.getElementById('selectManager').focus();
			}else if(accountName == undefined){
				$scope.alertMsg = "Please enter the account Name";
				document.getElementById('accountName').focus();
			}else if(industryType == undefined){
				$scope.alertMsg = "Please enter the industry type";
				document.getElementById('industryType').focus();
			}else if(clientAddress == undefined){
				$scope.alertMsg = "Please enter the client address";
				document.getElementById('clientAddress').focus();
			}
			else{
				$scope.alertMsg = "";
				var record = {"accountName":$scope.accountName,"industryType":$scope.industryType,"clientAddress":$scope.clientAddress,"deliveryManagers":$scope.accountManagers()};
		
				addOrUpdateAccount(record,$scope.templateTitle,"N");
				$timeout(function(){updateGrid($scope.templateTitle, record)},200);
			}
          }else if(action == "Update"){
        	  if(managersSelectedList == undefined){
  				$scope.alertMsg = "Please select a accountManager";
  			document.getElementById('selectManager').focus();
  			}else if(accountName == undefined){
  				$scope.alertMsg = "Please enter the account Name";
  				document.getElementById('accountName').focus();
  			}else if(industryType == undefined){
  				$scope.alertMsg = "Please enter the industry type";
  				document.getElementById('industryType').focus();
  			}else if(clientAddress == undefined){
  				$scope.alertMsg = "Please enter the client address";
  				document.getElementById('clientAddress').focus();
  			}else{
        	  $scope.alertMsg = "";
        	  var record =  {"accountId":$scope.accountId, "accountName":$scope.accountName,"industryType":$scope.industryType,"clientAddress":$scope.clientAddress,"deliveryManagers":$scope.accountManagers()};
   
        	  record.id = $scope.parentData.id;
        	  addOrUpdateAccount(record,$scope.templateTitle,"U");
        	  $timeout(function(){updateGrid($scope.templateTitle, record)},200);
  			}
          }
		};
		addOrUpdateAccount = function(record,action,flag){
			var urlRequest  = "";
			if(action == "Add" || action == "Update"){
				urlRequest = appConfig.appUri+"account/accounts?action="+flag ;
			}
			var req = {
				method : 'POST',
				url : urlRequest,
				headers : {
					"Content-type" : "application/json"
				},
				data : record
			}
			$http(req).then(function mySuccess(response) {
				$scope.result = "Success";
				if(response.data =='Account already exist'){
		    		$scope.alertMsg=response.data;
		    	}else{
		    		$scope.alertMsg="";
		    	}	
				}, function myError(response){
				$scope.result = "Error";
			});
		}
		function updateGrid(action, record){
			if($scope.alertMsg == ""){
				if($scope.result == "Success"){
					if(action == "Add" || action == "Delete"){
						$scope.refreshPage();
				    }
					else if(action == "Update"){
						$scope.refreshPage();
					}
					$mdDialog.hide(action);
				}else{
					$mdDialog.hide("Error");
				}
				
			}
		}	
	}
});

