define(['app'], function (app) {
	app.directive('uploadImgFile', function ($rootScope) {
		return {
			restrict: 'A',
			scope: true,
			link: function (scope, element, attr) {
				element.on('change', function (event) {
					var files = event.target.files;
					if ($rootScope.imageFormat.indexOf(files[0].type) != -1) {
						$rootScope.prodimage = files[0];
					}else {
						$rootScope.$apply(function () {
							$rootScope.modalShown = true;
							$rootScope.message = "Please select Image format only"
							angular.element("input[type='file']").val(null);
						})
					}
				})
			}
		}
	})


	function readURL(input) {
		var num = current_imageObject.attr('data-id');
		if (input.files && input.files[0]) {
			var reader = new FileReader();
			reader.onload = function (e) {
				$('#store_image_' + num)
					.attr('src', e.target.result)
					.width(119)
					.height(112);
			}
			reader.readAsDataURL(input.files[0]);
		}
	}
	app.directive('thumbUploadFile', function ($rootScope) {
		return {
			restrict: 'A',
			scope: true,
			link: function (scope, element, attr) {
				element.on('change', function (event) {
					var files = event.target.files;
					if ($rootScope.imageFormat.indexOf(files[0].type) != -1) {
						$rootScope.uploadthumbimage.push(files[0]);
					}else {
						$rootScope.$apply(function () {
							$rootScope.modalShown = true;
							$rootScope.message = "Please select excel file format only"
							angular.element("input[type='file']").val(null);
						})
					}
				})
			}
		}
	})
	app.directive('uploadingFileContent', function ($rootScope) {
		return {
			restrict: 'A',
			scope: true,
			link: function (scope, element, attr) {
				element.on('change', function (event) {
					var files = event.target.files;
					if ($rootScope.filetype_format == 1) {

					}if ($rootScope.file_Format.indexOf(files[0].type) != -1) {
						$rootScope.uploadfile.push(files[0]);
					}else {
						$rootScope.$apply(function () {
							$rootScope.modalShown = true;
							$rootScope.message = "Please select Excel format only"
							angular.element("input[type='file']").val(null);
						})
					}
				})
			}
		}
	})
	app.service("UploadProdService", function ($http, $rootScope, $timeout) {
		var service = {}
		service.upload = upload;
		var returnfun;
		function upload(formData, scope) {
			//console.log("File : ",formData);
			returnfun = scope
			$rootScope.loadingCont = true;
			var xhr;
			if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
				xhr = new XMLHttpRequest();
			}else {// code for IE6, IE5
				xhr = new ActiveXObject("Microsoft.XMLHTTP");
			}
			//this._scope['formLoad'] = false;
			xhr.upload.addEventListener("progress", uploadProgress, false)
			xhr.addEventListener("load", function (evt) {
				returnfun(evt.target.responseText);
			}, false)
			xhr.addEventListener("error", uploadFailed, false)
			xhr.addEventListener("abort", uploadCanceled, false)
			xhr.open("POST", "../services/excelupload.php")
			$rootScope.progressVisible = true
			xhr.send(formData);

		}
		function uploadProgress(evt) {
			$rootScope.$apply(function () {
				if (evt.lengthComputable) {
				} else {
				}
			})

		}
		function uploadComplete(evt) {

		}
		function uploadFailed(evt) {
			$rootScope.$apply(function () {
				$rootScope.loadingCont = false;
			})
		}
		function uploadCanceled(evt) {
			$rootScope.$apply(function () {
				$rootScope.loadingCont = false;
			})
		}
		return service;
	})
	app.controller('SalesCtrl', function ($scope, $rootScope, AppData, $location, UploadProdService) {
		//Every module Set Same Variable//
		$rootScope.menucont = true;
		$rootScope.pageLoadComp = false;
		$scope.formLoad = false;
		$scope.listFound = false;
		$scope.pageData = notificationData[$rootScope.selLanguage];
		$scope.formAction = "Add";
		$rootScope.sideselect = $scope.pageData.sidemenu[0];
		$scope.itemdetail = [];
		$scope.selectedIndex = null;
		$rootScope.cofirmWindow = false;
		$scope.submenu = $scope.pageData.sidemenu;
		$scope.tblheading = $rootScope.sideselect.headingText;
		$scope.url = "../senddata/log/";
		
		$scope.commonurl = "../senddata/common/";

		$scope.cat_error = '';
		$scope.topic_error = '';

		//filter data
		$scope.common_filter = [];
		$scope.zonearr = [];
		$scope.search_box = true;
		$rootScope.modalShown = false;
		$rootScope.cofirmWindow = false;
		//pagination
		$scope.viewby = '10';
		$scope.currentPage = 1;
		$scope.pageSize = 10; 
		$scope.maxSize = 5;
		$scope.itemsPerPage = $scope.viewby;
		$scope.maxSize = 5; 
		$scope.choice = {};
		$scope.choice.filelang = '';

		//Check Session//
		$scope.formData = {}
		$rootScope.file_Format = ["xls", "xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"];
		$rootScope.uploadfile = [];
		$rootScope.formData = {}
		$scope.formData.roleList = [];
		$rootScope.formData.roleList = [];
		$scope.topic_error = '';
		$scope.formData.ismenuload = "Yes";
		if ($rootScope.items == undefined || $rootScope.items == null) {
			$scope.formData.ismenuload = "No";
		}
		$scope.formData.funName = "checksession";
		$scope.formData.lang = $rootScope.selLanguage;
		// $scope.formData.start = 0;
		// $scope.formData.limit = 2;

		AppData.sendData($scope.url, $scope.formData, function (res) {
			if (res.cont) {
				if ($rootScope.items == undefined || $rootScope.items == null) {
					$rootScope.items = res.item.control;
				}
				for (var i = 0; i < $rootScope.items.length; i++) {
					if ($location.path() == $rootScope.items[i].modulepath) {
						$rootScope.modulename = $rootScope.items[i].modulename;
						$scope.usercontrol = String($rootScope.items[i].permission).split("|");
					}
				}
				$scope.listFound = true;
				$scope.statusobj = res.item.status;
				$rootScope.username = res.item.username;

			

			} else {
				$location.path("/login");
			}
		})
        

		$scope.filesubmmit = function () {
			
			if ($scope.fieldValidationTopic()) {			
			var formData = new FormData();	
			formData.append("funName", "dsedata");		
							
			if ($rootScope.uploadfile.length != 0) {
				formData.append("file", $rootScope.uploadfile[0]);
				// console.log($rootScope.uploadfile[0])
			}
		
			console.log(formData);
			UploadProdService.upload(formData, function (data) {
				//console.log(data)
				var response = JSON.parse(data)

				$scope.$apply(function () {	
                    $scope.formData = {}
					$rootScope.modalShown = true;
					$rootScope.message = response.mess;
					//$scope.filesubmmit();
                    
                    $scope.formLoad = false;
                    $rootScope.loadingCont = false;
                    $rootScope.pageLoadComp = true;
					 
				})
			});
		}
	}

	$scope.fieldValidationTopic = function () {	
		$scope.topic_image_error = ($rootScope.uploadfile[0] == undefined || $rootScope.uploadfile[0] == '') ? 'Please choose the file' : '';
		return ($scope.topic_image_error == '') ? true : false;
	}




        
    
        
    });
}); 

