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 Email = $("#Email").val();
			var PhoneNumber = $("#PhoneNumber").val();
			var ClientType = $("#client_type").val();
			var ClientSource = $("#client_source").val();
			var clientTin = $("#client_tin").val();
	

			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(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(ClientType  == "") {
				$("#client_type").after('<p class="text-danger">Client Type is required</p>');
				$('#client_type').closest('.form-group').addClass('has-error');
			}	else {
				// remov error text field
				$("#client_type").find('.text-danger').remove();
				// success out for form 
				$("#client_type").closest('.form-group').addClass('has-success');	  	
			}	// /else
           

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


			
			if(FirstName && Email && PhoneNumber && ClientType&&ClientSource&&clientTin) {
				// 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/fetchSelectedClients.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
				$("#editFirstName").val(response.client_fname);
				$("#editMiddleName").val(response.client_mname);
				$("#editLastName").val(response.client_lname);
				$("#editEmail").val(response.client_email);
				$("#editPhoneNumber").val(response.client_phone);
				$("#editTIN").val(response.client_tin);
				$("#editAddress").val(response.client_address);
				$("#editclient_type").val(response.client_type);
				$("#editclient_source").val(response.client_source);
				
				//$("#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.client_id+'" />');


				// submit of edit categories form
				$("#editCategoriesForm").unbind('submit').bind('submit', function() {
					var FirstName = $("#editFirstName").val();
					var Email = $("#editEmail").val();
					var PhoneNumber = $("#editPhoneNumber").val();
					var tin = $("#editTIN").val();
					var clientType = $("#editclient_type").val();
					var clientSource = $("#editclient_source").val();

					if(FirstName == "") {
						$("#editFirstName").after('<p class="text-danger">FirstName is required</p>');
						$('#editFirstName').closest('.form-group').addClass('has-error');
					} else {
						// remov error text field
						$("#editFirstName").find('.text-danger').remove();
						// success out for form 
						$("#editFirstName").closest('.form-group').addClass('has-success');	  	
					}
					
					
					
					
					if(Email == "") {
						$("#editEmail").after('<p class="text-danger">Email is required</p>');
						$('#editEmail').closest('.form-group').addClass('has-error');
					} else {
						// remov error text field
						$("#editEmail").find('.text-danger').remove();
						// success out for form 
						$("#editEmail").closest('.form-group').addClass('has-success');	  	
					}
					
					if(PhoneNumber == "") {
						$("#editPhoneNumber").after('<p class="text-danger">Phone Number is required</p>');
						$('#editPhoneNumber').closest('.form-group').addClass('has-error');
					} else {
						// remov error text field
						$("#editPhoneNumber").find('.text-danger').remove();
						// success out for form 
						$("#editPhoneNumber").closest('.form-group').addClass('has-success');	  	
					}
					
					
					if(tin == "") {
						$("#editTIN").after('<p class="text-danger">Tin is required</p>');
						$('#editTIN').closest('.form-group').addClass('has-error');
					} else {
						// remov error text field
						$("#editTIN").find('.text-danger').remove();
						// success out for form 
						$("#editTIN").closest('.form-group').addClass('has-success');	  	
					}
					
					
					if(clientType == "") {
						$("#editclient_type").after('<p class="text-danger">Client Type is required</p>');
						$('#editclient_type').closest('.form-group').addClass('has-error');
					} else {
						// remov error text field
						$("#editclient_type").find('.text-danger').remove();
						// success out for form 
						$("#editclient_type").closest('.form-group').addClass('has-success');	  	
					}
					
					if(clientSource == "") {
						$("#editclient_source").after('<p class="text-danger">Client Source is required</p>');
						$('#editclient_source').closest('.form-group').addClass('has-error');
					} else {
						// remov error text field
						$("#editclient_source").find('.text-danger').remove();
						// success out for form 
						$("#editclient_source").closest('.form-group').addClass('has-success');	  	
					}
					
					
					

					if(FirstName&&Email&&PhoneNumber&&tin&&clientType&&clientSource) {
						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/removeClient.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
}