var manageOrderTable;

$(document).ready(function() {

	var divRequest = $(".div-request").text();

	// top nav bar 
	$("#navOrder").addClass('active');

	if(divRequest == 'add')  {
		// add order	
		// top nav child bar 
		$('#topNavAddOrder').addClass('active');	

		// request date picker
		//$("#requestDate").datepicker();
		
		

		// create order form function
		$("#createOrderForm").unbind('submit').bind('submit', function() {
			var form = $(this);

			$('.form-group').removeClass('has-error').removeClass('has-success');
			$('.text-danger').remove();
				
			//var requestDate = $("#requestDate").val();
			 var deptName = $("#dept_id").val();
			 var sectName = $("#sect_id").val();
			
			if(deptName == "") {
				$("#dept_id").after('<p class="text-danger"> Department Name is required </p>');
				$('#dept_id').closest('.form-group').addClass('has-error');
			} else {
				$('#dept_id').closest('.form-group').addClass('has-success');
			} // /else
              
			if(sectName == "") {
				$("#sect_id").after('<p class="text-danger"> Section Name is required </p>');
				$('#sect_id').closest('.form-group').addClass('has-error');
			} else {
				$('#sect_id').closest('.form-group').addClass('has-success');
			} // /else


			// array validation
			var productName = document.getElementsByName('designation_name[]');				
			var validateProduct;
			for (var x = 0; x < productName.length; x++) {       			
				var productNameId = productName[x].id;	    	
		    if(productName[x].value == ''){	    		    	
		    	$("#"+productNameId+"").after('<p class="text-danger"> Designation is required!! </p>');
		    	$("#"+productNameId+"").closest('.form-group').addClass('has-error');	    		    	    	
	      } else {      	
		    	$("#"+productNameId+"").closest('.form-group').addClass('has-success');	    		    		    	
	      }          
	   	} // for

	   	for (var x = 0; x < productName.length; x++) {       						
		    if(productName[x].value){	    		    		    	
		    	validateProduct = true;
	      } else {      	
		    	validateProduct = false;
	      }          
	   	} // for  


        // array validation
			var productName1 = document.getElementsByName('short_name[]');				
			var validateProduct1;
			for (var x = 0; x < productName1.length; x++) {       			
				var productName1Id = productName1[x].id;	    	
		    if(productName1[x].value == ''){	    		    	
		    	$("#"+productName1Id+"").after('<p class="text-danger"> Short Name is required!! </p>');
		    	$("#"+productName1Id+"").closest('.form-group').addClass('has-error');	    		    	    	
	      } else {      	
		    	$("#"+productName1Id+"").closest('.form-group').addClass('has-success');	    		    		    	
	      }          
	   	} // for

	   	for (var x = 0; x < productName1.length; x++) {       						
		    if(productName1[x].value){	    		    		    	
		    	validateProduct1 = true;
	      } else {      	
		    	validateProduct1 = false;
	      }          
	   	} // for  


        var procKind = document.getElementsByName('staffcate_id[]');		   	
	   	var validateprocKind;
	   	for (var x = 0; x < procKind.length; x++) {       
	 			var procKindId = procKind[x].id;
		    if(procKind[x].value == ''){	    	
		    	$("#"+procKindId+"").after('<p class="text-danger"> Staff Category is required!! </p>');
		    	$("#"+procKindId+"").closest('.form-group').addClass('has-error');	    		    		    	
	      } else {      	
		    	$("#"+procKindId+"").closest('.form-group').addClass('has-success');	    		    		    		    	
	      } 
	   	}  // for

	   	for (var x = 0; x < procKind.length; x++) {       						
		    if(procKind[x].value){	    		    		    	
		    	validateprocKind = true;
	      } else {      	
		    	validateprocKind = false;
	      }          
	   	} // for  		
	   	
		

			if(deptName&sectName){
				if(validateProduct == true&&validateProduct1 == true&&validateprocKind==true) {
					// create order button
					// $("#createOrderBtn").button('loading');

					$.ajax({
						url : form.attr('action'),
						type: form.attr('method'),
						data: form.serialize(),					
						dataType: 'json',
						success:function(response) {
							console.log(response);
							// reset button
							$("#createOrderBtn").button('reset');
							
							$(".text-danger").remove();
							$('.form-group').removeClass('has-error').removeClass('has-success');

							if(response.success == true) {
								
								// create order button
								$(".success-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 +
	            	//' <br /> <br /> <a type="button" onclick="printOrder('+response.order_id+')" class="btn btn-primary"> <i class="glyphicon glyphicon-print"></i> Print </a>'+
	            	'<a href="add_designation.php?o=add" class="btn btn-default" style="margin-left:10px;"> <i class="glyphicon glyphicon-plus-sign"></i> Add New Designation </a>'+
	            	
	   		       '</div>');
								
							$("html, body, div.panel, div.pane-body").animate({scrollTop: '0px'}, 100);

							// disabled te modal footer button
							$(".submitButtonFooter").addClass('div-hide');
							// remove the product row
							$(".removeProductRowBtn").addClass('div-hide');
								
							} else {
								alert(response.messages);								
							}
						} // /response
					}); // /ajax
				} // if array validate is true
			} // /if field validate is true
			

			return false;
		}); // /create order form function	
	
	}  	

}); // /documernt




function addRow() {
	$("#addRowBtn").button("loading");

	var tableLength = $("#productTable tbody tr").length;

	var tableRow;
	var arrayNumber;
	var count;

	if(tableLength > 0) {		
		tableRow = $("#productTable tbody tr:last").attr('id');
		arrayNumber = $("#productTable tbody tr:last").attr('class');
		count = tableRow.substring(3);	
		count = Number(count) + 1;
		arrayNumber = Number(arrayNumber) + 1;					
	} else {
		// no table row
		count = 1;
		arrayNumber = 0;
	}

	$.ajax({
		url: 'php_action/fetchDes.php',
		type: 'post',
		dataType: 'json',
		success:function(response) {
			$("#addRowBtn").button("reset");			

			var tr = '<tr id="row'+count+'" class="'+arrayNumber+'">'+			  				
				 '<td style="padding-left:20px;">'+
					'<div class="form-group">'+
					''+count+''+
					'</div>'+
				'</td>'+

                '<td style="padding-left:20px;"">'+
					'<input type="text" name="designation_name[]" class="form-control" id="designation_name'+count+'"   placeholder="Designation" autocomplete="off"/>'+
				'</td style="padding-left:20px;">'+
				
				'<td style="padding-left:20px;"">'+
					'<input type="text" name="short_name[]" class="form-control" id="short_name'+count+'"   placeholder="Short Name" autocomplete="off"/>'+
				'</td style="padding-left:20px;">'+
			
			   '<td>'+
					'<div class="form-group">'+

					'<select class="form-control" name="staffcate_id[]" id="staffcate_id'+count+'" >'+
						'<option value="">~~SELECT~~</option>';
						// console.log(response);
						$.each(response, function(index, value) {
							tr += '<option value="'+value[0]+'">'+value[1]+'</option>';							
						});
													
					tr += '</select>'+
					'</div>'+
				'</td>'+
			
				'<td>'+
					'<button class="btn btn-default removeProductRowBtn" type="button" onclick="removeProductRow('+count+')"><i class="glyphicon glyphicon-trash"></i></button>'+
				'</td>'+
			'</tr>';
			if(tableLength > 0) {							
				$("#productTable tbody tr:last").after(tr);
			} else {				
				$("#productTable tbody").append(tr);
			}		

		} // /success
	});	// get the product data

} // /add row

function removeProductRow(row = null) {
	if(row) {
		$("#row"+row).remove();


		subAmount();
	} else {
		alert('error! Refresh the page again');
	}
}


