var manageProductTable;


$(document).ready(function(){
	
	
	
	// top nav bar 
	//$('#navProduct').addClass('active');
	// manage product data table
	//manageProductTable = $('#manageProductTable').DataTable({
		//'ajax': 'php_action/fetchStaff.php',
		//'order': []
	//});
	
	// order date picker
	//$("#BirthDate").datepicker();
	// order date picker
	//$("#EmploymentDate").datepicker();
	

	// add product modal btn clicked
	$("#addProductModalBtn").unbind('click').bind('click', function() {
		// // product form reset
		$("#submitProductForm")[0].reset();		

		// remove text-error 
		$(".text-danger").remove();
		// remove from-group error
		$(".form-group").removeClass('has-error').removeClass('has-success');

		$("#staffPhotoK").fileinput({
	      overwriteInitial: true,
		    maxFileSize: 2500,
		    showClose: false,
		    showCaption: false,
		    browseLabel: '',
		    removeLabel: '',
		    browseIcon: '<i class="glyphicon glyphicon-folder-open"></i>',
		    removeIcon: '<i class="glyphicon glyphicon-remove"></i>',
		    removeTitle: 'Cancel or reset changes',
		    elErrorContainer: '#kv-avatar-errors-1',
		    msgErrorClass: 'alert alert-block alert-danger',
		    defaultPreviewContent: '<img src="assests/images/photo_default.png" alt="Profile Image" style="width:100%;">',
		    layoutTemplates: {main2: '{preview} {remove} {browse}'},								    
	  		allowedFileExtensions: ["jpg", "png", "gif", "JPG", "PNG", "GIF"]
			});   

		// submit product form
		$("#submitProductForm").unbind('submit').bind('submit', function() {
			
		

			// form validation
			//var staffPhoto = $("#staffPhoto").val();
			var FirstName = $("#FirstName").val();
			var MiddleName = $("#MiddleName").val();
			var LastName = $("#LastName").val();
			var Gender = $("#gender_id").val();
			var StaffId = $("#StaffId").val();
			var gender = $("#gender_id").val();
			//var marital = $("#editStatus").val();
			//var birthDate = $("#birthDate").val();
			var Email = $("#Email").val();
			var PhoneNumber = $("#PhoneNumber").val();
			var BirthDate = $("#birthDate").val();
			var EmploymentDate = $("#employDate").val();
			var marital_status = $("#marital_id").val();
			var deptId = $("#dept_id").val();
			var desId = $("#des_id").val();
	
	/*
			if(staffPhoto == "") {
				$("#staffPhoto").closest('.center-block').after('<p class="text-danger">Staff Picture is required</p>');
				$('#staffPhoto').closest('.form-group').addClass('has-error');
			}	else {
				// remov error text field
				$("#staffPhoto").find('.text-danger').remove();
				// success out for form 
				$("#staffPhoto").closest('.form-group').addClass('has-success');	  	
			}	// /else
				
			*/

			if(FirstName == "") {
				$("#FirstName").after('<p class="text-danger">Staff FirstName is required</p>');
				$('#FirstName').closest('.form-group').addClass('has-error');
			}	else {
				// remov error text field
				$("#FirstName").find('.text-danger').remove();
				// success out for form 
				$("#FirstName").closest('.form-group').addClass('has-success');	  	
			}	// /else

            if(MiddleName == "") {
				$("#MiddleName").after('<p class="text-danger">Staff MiddleName is required</p>');
				$('#MiddleName').closest('.form-group').addClass('has-error');
			}	else {
				// remov error text field
				$("#MiddleName").find('.text-danger').remove();
				// success out for form 
				$("#MiddleName").closest('.form-group').addClass('has-success');	  	
			}	// /else
				
			
			if(LastName == "") {
				$("#LastName").after('<p class="text-danger">Staff LastName is required</p>');
				$('#LastName').closest('.form-group').addClass('has-error');
			}	else {
				// remov error text field
				$("#LastName").find('.text-danger').remove();
				// success out for form 
				$("#LastName").closest('.form-group').addClass('has-success');	  	
			}	// /else

            if(Gender == "") {
				$("#gender_id").after('<p class="text-danger">Gender is required</p>');
				$('#gender_id').closest('.form-group').addClass('has-error');
			}	else {
				// remov error text field
				$("#gender_id").find('.text-danger').remove();
				// success out for form 
				$("#gender_id").closest('.form-group').addClass('has-success');	  	
			}	// /else

            
			if(marital_status == "") {
				$("#marital_id").after('<p class="text-danger">Marital Status is required</p>');
				$('#marital_id').closest('.form-group').addClass('has-error');
			}	else {
				// remov error text field
				$("#marital_id").find('.text-danger').remove();
				// success out for form 
				$("#marital_id").closest('.form-group').addClass('has-success');	  	
			}	// /else

            if(BirthDate == "") {
				$("#birthDate").after('<p class="text-danger"> Birth Date is required</p>');
				$('#birthDate').closest('.form-group').addClass('has-error');
			}	else {
				// remov error text field
				$("#birthDate").find('.text-danger').remove();
				// success out for form 
				$("#birthDate").closest('.form-group').addClass('has-success');	  	
			}	// /else


           if(EmploymentDate == "") {
				$("#employDate").after('<p class="text-danger"> Employment Date is required</p>');
				$('#employDate').closest('.form-group').addClass('has-error');
			}	else {
				// remov error text field
				$("#employDate").find('.text-danger').remove();
				// success out for form 
				$("#employDate").closest('.form-group').addClass('has-success');	  	
			}	// /else

		

			if(Email == "") {
				$("#Email").after('<p class="text-danger">Email is required</p>');
				$('#Email').closest('.form-group').addClass('has-error');
			}	else {
				// remov error text field
				$("#Email").find('.text-danger').remove();
				// success out for form 
				$("#Email").closest('.form-group').addClass('has-success');	  	
			}	// /else
				
			
			if(PhoneNumber == "") {
				$("#PhoneNumber").after('<p class="text-danger">Phone Number is required</p>');
				$('#PhoneNumber').closest('.form-group').addClass('has-error');
			}	else {
				// remov error text field
				$("#PhoneNumber").find('.text-danger').remove();
				// success out for form 
				$("#PhoneNumber").closest('.form-group').addClass('has-success');	  	
			}	// /else

			
			
			if(deptId  == "") {
				$("#dept_id").after('<p class="text-danger">Department is required</p>');
				$('#dept_id').closest('.form-group').addClass('has-error');
			}	else {
				// remov error text field
				$("#dept_id").find('.text-danger').remove();
				// success out for form 
				$("#dept_id").closest('.form-group').addClass('has-success');	  	
			}	// /else
           

             if(desId  == "") {
				$("#des_id").after('<p class="text-danger">Designation is required</p>');
				$('#des_id').closest('.form-group').addClass('has-error');
			}	else {
				// remov error text field
				$("#des_id").find('.text-danger').remove();
				// success out for form 
				$("#des_id").closest('.form-group').addClass('has-success');	  	
			}	// /else


			
			if(FirstName && MiddleName && LastName && Gender && marital_status && BirthDate && EmploymentDate && Email && PhoneNumber && deptId&&desId) {
				// submit loading button
				$("#createProductBtn").button('loading');

				var form = $(this);
				var formData = new FormData(this);

				$.ajax({
					url : form.attr('action'),
					type: form.attr('method'),
					data: formData,
					dataType: 'json',
					cache: false,
					contentType: false,
					processData: false,
					success:function(response) {

						if(response.success == true) {
							// submit loading button
							$("#createProductBtn").button('reset');
							
							$("#submitProductForm")[0].reset();

							$("html, body, div.modal, div.modal-content, div.modal-body").animate({scrollTop: '0'}, 100);
																	
							// shows a successful message after operation
							$('#add-product-messages').html('<div class="alert alert-success">'+
		            '<button type="button" class="close" data-dismiss="alert">&times;</button>'+
		            '<strong><i class="glyphicon glyphicon-ok-sign"></i></strong> '+ response.messages +
		          '</div>');

							// remove the mesages
		          $(".alert-success").delay(500).show(10, function() {
								$(this).delay(3000).hide(10, function() {
									$(this).remove();
								});
							}); // /.alert

		          // reload the manage student table
							manageProductTable.ajax.reload(null, true);

							// remove text-error 
							$(".text-danger").remove();
							// remove from-group error
							$(".form-group").removeClass('has-error').removeClass('has-success');

						} // /if response.success
						
					} // /success function
				}); // /ajax function
			}	 // /if validation is ok 					

			return false;
		}); // /submit product form

	}); // /add product modal btn clicked
	

	// remove product 	

}); // document.ready fucntion


// edit categories function
function editCategories(categoriesId = null) {
	if(categoriesId) {
		// remove the added categories id 
		$('#editCategoriesId').remove();
		// reset the form text
		$("#editCategoriesForm")[0].reset();
		// reset the form text-error
		$(".text-danger").remove();
		// reset the form group errro		
		$('.form-group').removeClass('has-error').removeClass('has-success');

		// edit categories messages
		$("#edit-categories-messages").html("");
		// modal spinner
		$('.modal-loading').removeClass('div-hide');
		// modal result
		$('.edit-categories-result').addClass('div-hide');
		//modal footer
		$(".editCategoriesFooter").addClass('div-hide');		

		$.ajax({
			url: 'php_action/fetchSelectedTermStaff.php',
			type: 'post',
			data: {categoriesId: categoriesId},
			dataType: 'json',
			success:function(response) {

				// modal spinner
				$('.modal-loading').addClass('div-hide');
				// modal result
				$('.edit-categories-result').removeClass('div-hide');
				//modal footer
				$(".editCategoriesFooter").removeClass('div-hide');	

				// set the categories name
				$("#editStaff").val(response.staff_id);
				$("#editTermDate").val(response.termination_date);
				$("#editRemarks").val(response.remarks);
				
				
				//$("#editPhoneNumber").val(response.dept_id);
				// set the categories status
				//$("#editAccessLevel").val(response.access_status);
				// add the categories id 
				$(".editCategoriesFooter").after('<input type="hidden" name="editCategoriesId" id="editCategoriesId" value="'+response.staff_termId+'" />');


				// submit of edit categories form
				$("#editCategoriesForm").unbind('submit').bind('submit', function() {
					var Staff = $("#editStaff").val();
					var TermDate = $("#editTermDate").val();
					var Remarks = $("#editRemarks").val();
					

					if(Staff == "") {
						$("#editStaff").after('<p class="text-danger"> Select Staff</p>');
						$('#editStaff').closest('.form-group').addClass('has-error');
					} else {
						// remov error text field
						$("#editStaff").find('.text-danger').remove();
						// success out for form 
						$("#editStaff").closest('.form-group').addClass('has-success');	  	
					}
					
					
					if(TermDate == "") {
						$("#editTermDate").after('<p class="text-danger">Termination Date is required</p>');
						$('#editTermDate').closest('.form-group').addClass('has-error');
					} else {
						// remov error text field
						$("#editTermDate").find('.text-danger').remove();
						// success out for form 
						$("#editTermDate").closest('.form-group').addClass('has-success');	  	
					}
					
					if(Remarks == "") {
						$("#editRemarks").after('<p class="text-danger">Remarks is required</p>');
						$('#editRemarks').closest('.form-group').addClass('has-error');
					} else {
						// remov error text field
						$("#editRemarks").find('.text-danger').remove();
						// success out for form 
						$("#editRemarks").closest('.form-group').addClass('has-success');	  	
					}
					
					
					

					if(Staff&&TermDate&&Remarks) {
						var form = $(this);
						// button loading
						$("#editCategoriesBtn").button('loading');

						$.ajax({
							url : form.attr('action'),
							type: form.attr('method'),
							data: form.serialize(),
							dataType: 'json',
							success:function(response) {
								// button loading
								$("#editCategoriesBtn").button('reset');

								if(response.success == true) {
									// reload the manage member table 
									manageCategoriesTable.ajax.reload(null, false);									  	  			
									
									// remove the error text
									$(".text-danger").remove();
									// remove the form error
									$('.form-group').removeClass('has-error').removeClass('has-success');
			  	  			
			  	  			$('#edit-categories-messages').html('<div class="alert alert-success">'+
			            '<button type="button" class="close" data-dismiss="alert">&times;</button>'+
			            '<strong><i class="glyphicon glyphicon-ok-sign"></i></strong> '+ response.messages +
				          '</div>');

			  	  			$(".alert-success").delay(500).show(10, function() {
										$(this).delay(3000).hide(10, function() {
											$(this).remove();
										});
									}); // /.alert
								}  // if

							} // /success
						}); // /ajax	
					} // if


					return false;
				}); // /submit of edit categories form

			} // /success
		}); // /fetch the selected categories data

	} else {
		alert('Oops!! Refresh the page');
	}
} // /edit categories function

// remove product 
function removeProduct(productId = null) {
	if(productId) {
		// remove product button clicked
		$("#removeProductBtn").unbind('click').bind('click', function() {
			// loading remove button
			$("#removeProductBtn").button('loading');
			$.ajax({
				url: 'php_action/removeStaff.php',
				type: 'post',
				data: {productId: productId},
				dataType: 'json',
				success:function(response) {
					// loading remove button
					$("#removeProductBtn").button('reset');
					if(response.success == true) {
						// remove product modal
						$("#removeProductModal").modal('hide');

						// update the product table
						manageProductTable.ajax.reload(null, false);

						// remove success messages
						$(".remove-messages").html('<div class="alert alert-success">'+
		            '<button type="button" class="close" data-dismiss="alert">&times;</button>'+
		            '<strong><i class="glyphicon glyphicon-ok-sign"></i></strong> '+ response.messages +
		          '</div>');

						// remove the mesages
	          $(".alert-success").delay(500).show(10, function() {
							$(this).delay(3000).hide(10, function() {
								$(this).remove();
							});
						}); // /.alert
					} else {

						// remove success messages
						$(".removeProductMessages").html('<div class="alert alert-success">'+
		            '<button type="button" class="close" data-dismiss="alert">&times;</button>'+
		            '<strong><i class="glyphicon glyphicon-ok-sign"></i></strong> '+ response.messages +
		          '</div>');

						// remove the mesages
	          $(".alert-success").delay(500).show(10, function() {
							$(this).delay(3000).hide(10, function() {
								$(this).remove();
							});
						}); // /.alert

					} // /error
				} // /success function
			}); // /ajax fucntion to remove the product
			return false;
		}); // /remove product btn clicked
	} // /if productid
} // /remove product function

function clearForm(oForm) {
	// var frm_elements = oForm.elements;									
	// console.log(frm_elements);
	// 	for(i=0;i<frm_elements.length;i++) {
	// 		field_type = frm_elements[i].type.toLowerCase();									
	// 		switch (field_type) {
	// 	    case "text":
	// 	    case "password":
	// 	    case "textarea":
	// 	    case "hidden":
	// 	    case "select-one":	    
	// 	      frm_elements[i].value = "";
	// 	      break;
	// 	    case "radio":
	// 	    case "checkbox":	    
	// 	      if (frm_elements[i].checked)
	// 	      {
	// 	          frm_elements[i].checked = false;
	// 	      }
	// 	      break;
	// 	    case "file": 
	// 	    	if(frm_elements[i].options) {
	// 	    		frm_elements[i].options= false;
	// 	    	}
	// 	    default:
	// 	        break;
	//     } // /switch
	// 	} // for
}