angular.module('checklistsCtrl',[]).controller('checklistsController',function($scope,$http,$window,$interval,Login, Checklists,Upload){
		
		$scope.currentPage =1;
		var pager = localStorage.getItem('checklists_pagesize');
		$scope.itemsPerPage= pager?pager:5;
		$scope.entryLimit =$scope.itemsPerPage; 
		$scope.searchChecklists={rows:$scope.itemsPerPage,current_page:$scope.currentPage,search:""};

		/*$scope.getUsers = function(){
			User.get().success(function(data){
				$scope.users= data;
			});
		}
		$scope.getUsers();
		*/
			$scope.getParents = function(){
				Checklists.getSelect().success(function(data){
					$scope.parents= data;
				});
			}
	    $scope.getParents();



		$scope.getChecklists = function(){
			$('.loader').show();
			Checklists.getPaging($scope.searchChecklists).success(function(data){
				$scope.checklists= data.data;
				$('.loader').hide();
				$scope.currentPage = data.page; 
				$scope.itemsPerPage = $scope.searchChecklists.rows; 
				checklists_pagesize.val($scope.itemsPerPage);
				$scope.totalItems = data.total_rows;
				$('#tableChecklists').tableRespondsive();
			});
		};

		$scope.searchChanged=function(){
			$scope.getChecklists();
		}

		/*$scope.getSelectChecklists = function(){
			Checklists.getSelect().success(function(data){
				$scope.selectChecklists = data;
			});
		}
		$scope.getSelecChecklists();*/
		/*
		xem demo upload file tai day:
		http://jsfiddle.net/danialfarid/1m6fn6re/
		$scope.upload = function (file) {
			Upload.upload({
				url: '/todo',
				fields: $scope.todoData,
				file: file
			}).progress(function (evt) {
				var progressPercentage = parseInt(100.0 * evt.loaded / evt.total);
				console.log('progress: ' + progressPercentage + '% ' + evt.config.file.name);
			}).success(function (data, status, headers, config) {
				console.log('file ' + config.file.name + 'uploaded. Response: ' + data);
			}).error(function (data, status, headers, config) {
				console.log('error status: ' + status);
			})
		};
		*/
		
		$scope.sizeChanged = function(){
			$scope.searchChecklists.rows = $scope.entryLimit;
			checklists_pagesize.val($scope.searchChecklists.rows);
			localStorage.setItem('checklists_pagesize',$scope.searchChecklists.rows);
			$scope.searchChecklists.current_page=1;
			$scope.getChecklists();
		}
		$scope.pageChanged  = function() {
			console.log($scope.currentPage);
			$scope.searchChecklists.current_page = $scope.currentPage;
			$scope.getChecklists();
		};
		$scope.getChecklists();
		$scope.setPage = function(pageNo) {
			$scope.currentPage = pageNo;
			console.log('doi page');
		};

		$scope.addChecklistsData = {};
		$scope.createChecklists = function(){
			console.log($scope.addChecklistsData);
			Checklists.save($scope.addChecklistsData).success(function(data){
				$scope.addChecklistsData = {};
				$scope.getChecklists();
				if(data.status){
					toastr.success(data.msg);
				}else{
					toastr.error(data.msg);
				}
			});
		}
		$scope.editChecklistsData = {};
		$scope.editChecklists = function(id){
			$('.loader').show();
			Checklists.show(id).success(function(data){
				$('.loader').hide();
				$scope.editChecklistsData = data;
			});
		}
		$scope.updateChecklists = function(){
			var id = $scope.editChecklistsData.id;
			$('.loader').show();
			Checklists.update(id,$scope.editChecklistsData).success(function(data){
				$('.loader').hide();
				if(data.status){
					$scope.editChecklistsData = {};
					//$('#modal_edit_checklists').modal('hide');
					toastr.success(data.msg);
				}else{
					toastr.error(data.msg);
				}
				
				$scope.getChecklists();
			});
		}
		$scope.deleteChecklistsData = {};
		$scope.deleteChecklistsData = {};
		$scope.confrimChecklists = function(id,title, content){
			$scope.deleteChecklistsData={id:id};
			var confirm = $.confirm(title, '<div class="text-content baseline">' + content + '</div>', {
				messageType: 'warning',
				titleIcon: 'remove',
				overlayTransparent: .75
			},
			function() {
				$scope.deleteChecklists(id,confirm);
			});

		 }

		$scope.deleteChecklists = function(id,confirm){
			Checklists.destroy(id).success(function(data){
				$scope.deleteChecklistsData = {};
				if(data.status){
					$.notification('<div class="text-content">'+data.msg+'</div>', {
							messageType: 'message-success',
							autoClose: true});                       
                     confirm.switchStateDialog("close"); 
				}else{
					$.notification('<div class="text-content">'+data.msg+'</div>', {
					messageType: 'message-success',
					autoClose: true}); 
					confirm.switchStateDialog("close");            
				}
				$scope.getChecklists();
			});
		}

		$scope.getUserLogin =function(){
			return Login.getUserLogin().success(function(data){
				if(data==false){
					$scope.displayname = "Profile";	
				}else{
				var displayname=(data.first_name!=null?data.first_name:'') +' '+ (data.last_name==null?'':data.last_name);
				if($.trim(displayname)==""){
					$scope.displayname = "Hello, "+ data.email;
				}else{
					$scope.displayname= "Hello, "+ displayname;
				}}
				
			});
		}

		$scope.getUserLogin();

		$scope.logout =function(){
			return Login.logout().success(function(data){
				$window.location.href = url_login;	
			});
		}

		$scope.checkLogin =function(){
			return Login.checkLogin().success(function(data){
				if(data.status==false){
					$window.location.href = url_login;	
				}
			});			
		}
		
		$scope.checkLogin();

		stopTime = $interval($scope.checkLogin, time_refresh);

		/*
		<p>Selected: {{address.selectAddesses}}</p>
				<ui-select multiple 
					ng-model="address.selectAddesses"
					theme="bootstrap"
					ng-disabled="false" 
					close-on-select="false" 
					style="width: 300px;"
					title="Choose an address">
					<ui-select-match placeholder="Enter an address...">{{$item.name}}</ui-select-match>
					<ui-select-choices repeat="add in addresses| propsFilter: {name: $select.search, id: $select.search}"
						refresh="refreshAddresses($select.search)"
						refresh-delay="0">
						<div ng-bind-html="add.name | highlight: $select.search"></div>
					</ui-select-choices>
				</ui-select> 
		$scope.address = {};
		$scope.address.selectAddesses = [{id:"1",name:"abc"}];
		$scope.refreshAddresses = function(address) {
			if(address.length>0){
				var params = {search: address};
				Colors.getSelect2(params).success(function(data){
				 var diff = differentArray(data,$scope.address.selectAddesses);
					$scope.addresses =diff;
				})	
			}
		};*/

	})