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 CurrencyName = $("#CurrencyName").val();
			var BankName = $("#BankName").val();
			var BankAccount = $("#BankAccount").val();
			var accountName = $("#accountName").val();
			var SwiftCode = $("#SwiftCode").val();
			var BankCode = $("#BankCode").val();
			var BranchCode = $("#BranchCode").val();
			
	

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

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


		

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

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

            

			
			if(CurrencyName && BankName && accountName && BankAccount && SwiftCode && BranchCode && BankCode) {
				// 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/fetchSelectedCurrency.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
				$("#editCurrencyName").val(response.currencyName);
				$("#editBankName").val(response.bank_name);
				$("#editBankAccount").val(response.bank_account);
				$("#editAccountName").val(response.account_name);
				$("#editSwiftCode").val(response.swiftCode);
				$("#editBankCode").val(response.bank_code);
				$("#editBranchCode").val(response.branch_code);
				$("#editAmount").val(response.amount);
				
				
				//$("#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.currency_id +'" />');


				// submit of edit categories form
				$("#editCategoriesForm").unbind('submit').bind('submit', function() {
					var CurrencyName = $("#editCurrencyName").val();
					var BankName = $("#editBankName").val();
					var BankAccount = $("#editBankAccount").val();
					var AccountName = $("#editAccountName").val();
					var SwiftCode = $("#editSwiftCode").val();
					var BankCode = $("#editBankCode").val();
					var BranchCode = $("#editBranchCode").val();
					var Amount = $("#editAmount").val();
					

					if(CurrencyName == "") {
						$("#editCurrencyName").after('<p class="text-danger">Currency Name is required</p>');
						$('#editCurrencyName').closest('.form-group').addClass('has-error');
					} else {
						// remov error text field
						$("#editCurrencyName").find('.text-danger').remove();
						// success out for form 
						$("#editCurrencyName").closest('.form-group').addClass('has-success');	  	
					}
					
					
					if(BankName == "") {
						$("#editBankName").after('<p class="text-danger">Bank Name is required</p>');
						$('#editBankName').closest('.form-group').addClass('has-error');
					} else {
						// remov error text field
						$("#editBankName").find('.text-danger').remove();
						// success out for form 
						$("#editBankName").closest('.form-group').addClass('has-success');	  	
					}
					
					if(AccountName == "") {
						$("#editAccountName").after('<p class="text-danger">Account Name is required</p>');
						$('#editAccountName').closest('.form-group').addClass('has-error');
					} else {
						// remov error text field
						$("#editAccountName").find('.text-danger').remove();
						// success out for form 
						$("#editAccountName").closest('.form-group').addClass('has-success');	  	
					}
					
					if(BankAccount == "") {
						$("#editBankAccount").after('<p class="text-danger">Bank Account is required</p>');
						$('#editBankAccount').closest('.form-group').addClass('has-error');
					} else {
						// remov error text field
						$("#editBankAccount").find('.text-danger').remove();
						// success out for form 
						$("#editBankAccount").closest('.form-group').addClass('has-success');	  	
					}
					
					
					if(SwiftCode == "") {
						$("#editSwiftCode").after('<p class="text-danger">Swift Code is required</p>');
						$('#editSwiftCode').closest('.form-group').addClass('has-error');
					} else {
						// remov error text field
						$("#editSwiftCode").find('.text-danger').remove();
						// success out for form 
						$("#editSwiftCode").closest('.form-group').addClass('has-success');	  	
					}
					
					if(BankCode == "") {
						$("#editBankCode").after('<p class="text-danger">Bank Code is required</p>');
						$('#editBankCode').closest('.form-group').addClass('has-error');
					} else {
						// remov error text field
						$("#editBankCode").find('.text-danger').remove();
						// success out for form 
						$("#editBankCode").closest('.form-group').addClass('has-success');	  	
					}
					
					
					if(BranchCode == "") {
						$("#editBranchCode").after('<p class="text-danger">Branch Code is required</p>');
						$('#editBranchCode').closest('.form-group').addClass('has-error');
					} else {
						// remov error text field
						$("#editBranchCode").find('.text-danger').remove();
						// success out for form 
						$("#editBranchCode").closest('.form-group').addClass('has-success');	  	
					}
					
					
					if(Amount == "") {
						$("#editAmount").after('<p class="text-danger">Bank Amount is required</p>');
						$('#editAmount').closest('.form-group').addClass('has-error');
					} else {
						// remov error text field
						$("#editAmount").find('.text-danger').remove();
						// success out for form 
						$("#editAmount").closest('.form-group').addClass('has-success');	  	
					}
					
					

					if(CurrencyName&&BankName&&AccountName&&BankAccount&&SwiftCode&&BankCode&&BranchCode&&Amount) {
						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/removeCurrency.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
}