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 image 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.fileFormat.indexOf(files[0].type) != -1) {
						$rootScope.uploadfile.push(files[0]);
					}else {
						$rootScope.$apply(function () {
							$rootScope.modalShown = true;
							$rootScope.message = "Please select Pdf/Video/Doc 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/products.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('NotificationCtrl', 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.pageurl = "../senddata/notification/";
		$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.formData = {}
		$scope.formData.roleList = [];
		$rootScope.formData.roleList = [];

		$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;

				$scope.notificationList = function () {
					$scope.formData.pn = $scope.currentPage;
					$scope.formData.nr = $scope.itemsPerPage;
					$scope.searchvalue = ($scope.formData.mymodel != '') ? $scope.formData.mymodel : "";
					$scope.formData.funName = "notificationlist";
					$scope.formData.key = $scope.searchvalue;

					AppData.sendData($scope.pageurl, $scope.formData, function (res) {
						console.log(res);
						if (res.cont) {
							if (res.item != undefined) {
								$scope.notificationListdata = res.item.notification;
								console.log($scope.notificationListdata);
								if ($rootScope.sideselect.id != 2) {
									$scope.totalItems = res.item.total_records;
								}
							}
						} else {
							$scope.listFound = false;

						}
					})
				}
				$scope.notificationList();
				$scope.setPage = function (pageNo) {
					$scope.currentPage = pageNo;
				};

				$scope.pageChanged = function () {
					$scope.notificationList();
				};

				$scope.setItemsPerPage = function (num) {
					$scope.itemsPerPage = num;
					$scope.currentPage = 1; //reset to first page
					$scope.notificationList();
				}
				$scope.filterData = function () {
					$scope.notificationList();
				}

			} else {
				$location.path("/login");
			}
		})
        
        $scope.getRole = function () {
			var obj = {};
			// obj.funName = "role";
			obj.funName = "contentrole"; 
			obj.lang = $rootScope.selLanguage;
			obj.type = "dse"
			AppData.sendData($scope.commonurl, obj, function (res) {
				if (res.cont) {
					if (res.item != undefined) {
						$scope.role = res.item;
					}
				}
			})
		}
        $scope.addFun = function () {
			//$scope.choices = [{ "filelang": "1" }];
			//$scope.call($scope.choices)
			$scope.formLoad = true;
			$scope.listFound = false;
			$scope.formAction = "Add";
			$scope.is_edit_region = false;
			$scope.is_edit_dealer = false;
			//$scope.title = $rootScope.sideselect.title[0];
			$scope.formData = {};
			$scope.zoneList = [];
			//$rootScope.uploadfile = []
			//$rootScope.uploadthumbimage = [];
			$scope.formData.roleList = [];
			$rootScope.formData.roleList = [];
			$scope.formLoad = true;
			$scope.formData.funName = "addnotification";
			$scope.formTitle = "ADD NOTIFICATION";
			//angular.element("input[type='file']").val(null);
			//$scope.getLanguage();
			$scope.getRole();
			//$scope.getZone();
		}
        $rootScope.notifyChange = function () {
			if ($rootScope.formData.notification) {
				$rootScope.roleVisible = true;
				$rootScope.roleGroup = $scope.formData.roleList;
				if ($rootScope.roleGroup.length > 0) {
					angular.forEach($rootScope.roleGroup, function (element) {
						$rootScope.formData.roleList[element.id] = true;
					});
				}
			} else {
				$rootScope.roleVisible = false;
			}
		} 
      	$scope.deleterow = function (id) {
			$scope.formData.id = id;
			$scope.deleteFun();
		}
        $scope.editFun = function () {
			$scope.filerequired = false;
			$scope.thmbrequired = false;
			$scope.formAction = "Edit";
			$scope.is_edit_region = true;
			$scope.is_edit_dealer = true;
			//$(".clsbuttoncont .clsbutton").addClass("disable_btn");
			$scope.formLoad = true;
			$scope.listFound = false;
			//angular.element("input[type='file']").val(null);
			if ($scope.selectedIndex != null) {
				if ($rootScope.sideselect.id == 1) {
					$scope.formTitle = "EDIT TOPIC";
					$scope.categoryList();
					$scope.image_url = $scope.formData.image_url;
				} else if ($rootScope.sideselect.id == 2) {
					$scope.formTitle = "EDIT CONTENT";
					$scope.getLanguage();
					$scope.getRole();
					$scope.getZone();
					$rootScope.uploadfile = [];
					$rootScope.uploadthumbimage = [];
					$scope.zoneList = [];
					$scope.formData.zoneList = {};
					
					$scope.formData.roleList = $scope.formData.filter_role_name;
					//console.log($scope.formData.roleList, "$scope.formData.roleList ", $scope.formData.filter_role_name)
//					angular.forEach($scope.formData.file, function (element, i) {
//						$scope.filepath_url = element.filepath_url;
//						$scope.file_show = i;
//						//$scope.call(element.language_id)
//					});
//					$scope.choices = $scope.formData.file;
				}
			} else {
				$rootScope.modalShown = true;
				$rootScope.message = $scope.pageData.errorMsg[0];
			}
		}
        $scope.editrow = function (obj, i) {
			//console.log(obj);
			$scope.formData.assettopicid = obj.assettopicid;
			$scope.notificationList();
			$scope.formData = obj;
			//console.log($scope.formData.filetypeid)
			//$scope.formData.catename = obj.catename;
			//$scope.formData.filetypeid = obj.filetypeid;
			$scope.selectedIndex = i;
			$scope.search_box = false;
			$scope.listFound = false;
			$scope.formLoad = true;
			$scope.formAction = "Edit";
			//angular.element("input[type='file']").val(null);
			//$rootScope.prodimage = '';
			$scope.editFun();
		}
        
        $scope.deleteFun = function () {
			$scope.formData.funName = "deletenotification";
			$scope.formData.lang = $rootScope.selLanguage;
			AppData.sendData($scope.pageurl, $scope.formData, function (res) {
				if (res.cont) {
					var index = $scope.notificationListdata.indexOf($scope.formData.id);
					if (index == -1) {
						$scope.notificationListdata.splice(index, 1);
					}
				}
			});
		}
        $scope.listClick = function (obj) {
			$scope.searchtext = '';
			$scope.formData = {}
			$scope.currentPage = 1;
			$(".clsbuttoncont .clsbutton").removeClass("disable_btn");
			$scope.formLoad = false;
			$rootScope.sideselect = obj;
			$scope.selectedIndex = null;
			$rootScope.sideselect.name = obj.name;
			$scope.notificationList();
		}
        
        $scope.clearErrorMsg = function () {
			$scope.title_error = '';
			$scope.description_error = '';
			  
		}
        $scope.closeForm = function () {
			$scope.formLoad = false;
			$scope.formData = {};
			$scope.selectedIndex = null;
			$scope.search_box = true;
			$scope.listFound = true;
			 
			$scope.clearErrorMsg();
		}
        
        $scope.fieldValidation = function () {
			$scope.title_error = ($scope.formData.notificationtitle == undefined || $scope.formData.notificationtitle == '') ? $scope.pageData.errorMsg[0] : '';
            $scope.description_error = ($scope.formData.notificationtext == undefined || $scope.formData.notificationtext == '') ? $scope.pageData.errorMsg[1] : '';
			return ($scope.title_error == '' && $scope.description_error == '') ? true : false;
		}
        
        ///////// User Submit //////////////
		$scope.notificationSubmit = function(){
			$scope.clearErrorMsg();
			//console.log($scope.fieldValidation());
			if($scope.fieldValidation()){
                
                if ($scope.formData.roleList.length != 0) {
					var rolearr = [];
					for (var id = 0; id < $scope.formData.roleList.length; id++) {
						rolearr.push($scope.formData.roleList[id].id)
					}
					$scope.formData.filter_role =  rolearr.toString();
					 
				}
                
				AppData.sendData($scope.pageurl,$scope.formData,function(res){
					if(res.cont){
						$rootScope.modalShown =true;
						$rootScope.message=(res.mess);
						$scope.notificationList();
						$scope.selectedIndex = null;
						$scope.formLoad = false;
						$scope.listFound = true;
					}else{
						$rootScope.modalShown =true;
						$rootScope.message=(res.mess);
					}
				})
			}
		}
        
        
    });
}); 

