<!DOCTYPE html>
<?php
require_once('../Db/dbconfig.php');
@$userid=$_GET['userid'];
@$dptId=$_GET['dptId'];
?>
<html>
<head>
<link href="../css/styles.css" rel="stylesheet" />
<link rel="stylesheet" href="https://cdn.datatables.net/1.10.4/css/jquery.dataTables.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://cdn.datatables.net/1.10.4/js/jquery.dataTables.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
<script src="https://kit.fontawesome.com/c17eae44a3.js" crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.13.1/css/bootstrap-select.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.13.1/js/bootstrap-select.min.js"></script>

<style type="text/css">
select.rou{
 border: 1px solid #ccc;
  -moz-border-radius: 10px;
-webkit-border-radius: 10px;
border-radius: 10px;

-moz-box-shadow: 2px 2px 3px #666;
-webkit-box-shadow: 2px 2px 3px #666;
box-shadow: 2px 2px 3px #666;
font-size: 18px;
padding: 4px 7px;
outline: 0;
-webkit-appearance: none;
}
#round{
 border: 1px solid #ccc;
 -moz-border-radius: 10px;
 -webkit-border-radius: 10px;
 border-radius: 10px;
	
 -moz-box-shadow: 2px 2px 3px #666;
 -webkit-box-shadow: 2px 2px 3px #666;
 box-shadow: 2px 2px 3px #666;
 font-size: 15px;
 padding: 4px 7px;
 outline: 0;
 -webkit-appearance: none;
}
</style>
</head>
<body>
<div class="page-header">
  <h3 class="page-title"  >
    <span class="page-title-icon bg-gradient-primary text-white mr-2">
    <i class="fas fa-cart-arrow-down fa-xs" style="color: black"></i>
    </span><b>Multiple Raw Material Send</b></h3>
</div>
 <div class="card-body" style="background-color:#F0FFFF;overflow: auto;">
 <form action="" method="post" enctype='multipart/form-data' id="packageForm">
 	<div class="row" style="margin-left: 0 !important;width: 100%;">
		<div class="form-group col-6">
		    <label>Send To Department :</label>
		    <select style="width:170px;" id="dpt" name="dpt" onchange="departmentCheck()" class="rou" required>
	        <?php 
	        if($dptId == '2'){
	        	$sql1="select * from department where id = 3";
	        }else{
				$sql1="select * from department where id not in (".@$dptId.",3,5)";
			}
			$res=mysqli_query($con,$sql1);
			while($row1 = mysqli_fetch_array($res))
			{
		        	
			?>
			<option value="<?php echo $row1['id']; ?>"><?php echo $row1['depart']; ?></option> 
			<?php
	        	
				}
			?>
	        </select>
		    <button type="button" class="btn btn-primary" style="display: none" id="othrdept" data-toggle="tooltip" data-placement="bottom" title="Add Other Department" ><i class="ace-icon fa fa-plus bigger-130"></i></button>
		</div>
		<div class="form-group col-6">
		    <label>Lot Number:</label>
		    <input type="text" style="width:150px;" maxlength="10" class="rounded" id="lotnumber" name="lotnumber" title="lotnumber" value="">
		</div>

	</div>
	
	
	
	<table id="item_table1" class="table table-condensed">
    <tr id="rowf0">
	<td>
	<label>Final Product :</label>
		    <select name="fin_product[]" id="fin_product0" class="rou">
			<?php 
			 $sql1="select * from final_product";
			   $res=mysqli_query($con,$sql1);
				while($row1 = mysqli_fetch_array($res))
				{
				?>

				<option value="<?php echo $row1['id']; ?>"><?php echo $row1['item']; ?></option> 

				<?php
				}
			?>
			</select>
			
		   <button type="button" class="btn btn-primary finalproductBtn" data-toggle="tooltip" data-placement="bottom" title="Add product" ><i class="ace-icon fa fa-plus bigger-130"></i></button>
	  </td>
	
	<td>
	 <input type="hidden" class="rounded" style="width:100px;"  id="sqty0" name="sqty[]" readonly>
		    <label>Quantity Type :</label>
		     <select style="width:150px;" class="rou" id="sendqty_typ0" name="sendqty_typ[]" onchange="changefunc(0,2)" required>
			  <?php 
				$sql1="select * from quantity_type";
			    $res=mysqli_query($con,$sql1);
				while($row1 = mysqli_fetch_array($res))
				{
				?>
				<option value="<?php echo $row1['id']; ?>"><?php echo $row1['q_name']; ?></option> 
			    <?php
				}
				?>
		    </select>
	    </td>
		
		<td>
		<label for="qty">Send Quantity:</label><br>
		<div class="row" style="margin-left: 0;">
		
		<div class="form-group" id="qty210">
			<input type="text" maxlength="6" class="rounded" id="pko0" pattern="^(0|[1-9][0-9]*)$" title="Enter a Valid Input" value="0"><label>Kilo</label>
		    <input type="text" style="width:204px;margin-left: 0px;" maxlength="3" class="rounded" id="pgr0" pattern="^(0|[1-9][0-9]*)$" value ="0"><label>gram</label>
			
		</div>
		<div class="form-group col-4" id="qty220" style="display:none;">
			<input type="text" style="width:150px;" maxlength="6" class="rounded" id="ppiec0" pattern="^[0-9]+(\.[0-9]{1,4})?$" title="Enter a Valid Input" value="0"><label>Pieces</label>
		</div>
		<div class="form-group col-4" id="qty230" style="display:none;">
			<div class="row" style="margin-left: 0 !important;">
			<input type="text" style="width:120px;" maxlength="6" class="rounded" id="plitr0" pattern="^(0|[1-9][0-9]*)$" title="Enter a Valid Input" value="0"><label>Litre</label>
			<input type="text" style="width:100px;" maxlength="3" class="rounded" id="pmltr0" pattern="^(0|[1-9][0-9]*)$" value ="0">ml
			</div>
		</div>
		<div class="form-group col-4" id="qty240" style="display:none;">
			<input type="text" style="width:150px;" maxlength="6" class="rounded" id="pmtr0" pattern="^[0-9]+(\.[0-9]{1,4})?$" title="Enter a Valid Input" value="0"><label>Meter</label>
		</div>
		<div class="form-group col-4" id="qty250" style="display:none;">
			<input type="text" style="width:150px;" maxlength="6" class="rounded" id="pft0" pattern="^[0-9]+(\.[0-9]{1,4})?$" title="Enter a Valid Input" value="0"><label>Feet</label>
		</div>
	</div>
		</td>
		
		<td> 
		<button type="button" name="addfinalpp" id="addfinalpp" class="btn btn-success" style="margin-top: 15%;">Add More</button>
		</td>
	
	</tr>
	
	
	<tr id="roww0">
	<td>
	<label style="width:122px;" for="qty">Waste Type :</label>
			 <select style="width:170px;" id="wtype0" name="wtype[]" class="rou">
	      <option value="0" selected>Please select....</option>
	      <?php 
        
					$sql1="select * from waster_type";
					
			    $res=mysqli_query($con,$sql1);
				while($row1 = mysqli_fetch_array($res))
				{
				?>
				<option value="<?php echo $row1['id']; ?>"><?php echo $row1['name']; ?></option> 
			    <?php
				}
				?>
	        </select>
	</td>
	<td>
	<label style="width:166px;" for="qty">Waste Quantity Type :</label>
			 <select style="width:170px;" id="waste_qty_typ0" name="waste_qty_typ[]" class="rou" onchange="changefuncWaste(0,3)">
	      	 <?php 
				$sql1="select * from quantity_type";
			    $res=mysqli_query($con,$sql1);
				while($row1 = mysqli_fetch_array($res))
				{
				?>
				<option value="<?php echo $row1['id']; ?>"><?php echo $row1['q_name']; ?></option> 
			    <?php
				}
				?>
	        </select>
	</td>
	
	<td>
	<div class="row">
		
	
	<div class="form-group col-6" id="qty310">
			<input type="hidden" class="rounded" style="width:100px;"  id="wqty0" name="wqty[]" readonly>
			<div class="row" style="margin-left: 0 !important;">
			<label style="width:136px;" for="qty">Waste Quantity:</label>
		    <input type="text" style="width:114px;" maxlength="6" class="rounded" id="wko0" pattern="^(0|[1-9][0-9]*)$" title="Enter a Valid Input" value="0"><label>Kilo</label>
		    <input type="text" style="width:114px;" maxlength="3" class="rounded" id="wgr0" pattern="^(0|[1-9][0-9]*)$" value ="0"><label>gram</label>
			</div>
		</div>
		<div class="form-group" id="qty320" style="display:none;">
			<label style="width:116px;" for="qty">Waste Quantity:</label>
			<input type="text" style="width:150px;" maxlength="6" class="rounded" id="wpiec0" pattern="^[0-9]+(\.[0-9]{1,4})?$" title="Enter a Valid Input" value="0"><label>Pieces</label>
		</div>
		<div class="form-group" id="qty330" style="display:none;">
			<label style="width:116px;" for="qty">Waste Quantity:</label>
			<div class="row" style="margin-left: 0 !important;">
			<input type="text" style="width:120px;" maxlength="6" class="rounded" id="wlitr0" pattern="^(0|[1-9][0-9]*)$" title="Enter a Valid Input" value="0"><label>Litre</label>
			<input type="text" style="width:100px;" maxlength="3" class="rounded" id="wmltr0" pattern="^(0|[1-9][0-9]*)$" value ="0">ml
			</div>
		</div>
		<div class="form-group" id="qty340" style="display:none;">
			<label style="width:116px;" for="qty">Waste Quantity:</label>
			<input type="text" style="width:150px;" maxlength="6" class="rounded" id="wmtr0" pattern="^[0-9]+(\.[0-9]{1,4})?$" title="Enter a Valid Input" value="0"><label>Meter</label>
		</div>
		<div class="form-group" id="qty350" style="display:none;">
			<label style="width:116px;" for="qty">Waste Quantity:</label>
			<input type="text" style="width:150px;" maxlength="6" class="rounded" id="wft0" pattern="^[0-9]+(\.[0-9]{1,4})?$" title="Enter a Valid Input" value="0"><label>Feet</label>
		</div>
		</div>
	</td>
	
	</tr>
	

    </table>
	
	
<table id="item_table" class="table table-condensed">
<tr id="row0">
	<td>
		<div class="form-group">
		    <label  style="width:120px;"for="finalProdct"><b>Product Name:</b></label>
		    <input style="width:200px;" type="text" class="rounded" id="prodctVal0" name="prodctVal[]" readonly required>
		    <input type="hidden" class="rounded" id="adminId0" name="adminId[]">
		  <button id="opentablbtn0" onclick="openTableM(0)" type="button" class="btn btn-primary" data-toggle="tooltip" data-placement="bottom" title="Add Product" style="background-color: #b134eb;"><i class="ace-icon fa fa-plus bigger-130"></i></button>
		</div>
	</td>
	<td>
		<div class="form-group">
		  <label><b>Product Type : </b></label>
		  <input type="text" style="width:100px;" class="rounded" id="ptype0" disabled>
		  <input type="hidden" class="rounded" id="ptypeId0" disabled>
		</div>
	</td>
	<td>
		<div class="form-group">
		    <label><b>Total Quantity : </b></label>
		    <input type="text" style="width:100px;" class="rounded" id="totalQty0" disabled>
		    <label id="totalQtyType0"></label>
			<input type="hidden" class="rounded" style="width:100px;"  id="qty0" name="qty[]" readonly>
			<input type="hidden" class="rounded" style="width:100px;"  id="qtytypeId0" readonly>
		</div>
	</td>
	<td>
		<div class="form-group" id="qty110">
			<div class="row" style="margin-left: 0 !important;">
			<div class="col-sm-3">
			<label for="qty"><b>Input Quantity:</b></label>
			</div>
			<div class="col-sm-5">
				<input type="text" style="width:120px;" maxlength="6" class="rounded" id="ko0" pattern="^(0|[1-9][0-9]*)$" title="Enter a Valid Input" value="0"><label>Kilo</label>
			</div>
			<div class="col-sm-4">
		    	<input type="text" style="width:100px;" maxlength="3" class="rounded" id="gr0" pattern="^(0|[1-9][0-9]*)$" value ="0"><label>gram</label>
			</div>
			</div>
		</div>
		<div class="form-group" id="qty120" style="display:none;">
			<label style="width:240px;" for="qty"><b>Input Quantity:</b></label>
			<input type="text" style="width:150px;" maxlength="6" class="rounded" id="piec0" pattern="^[0-9]+(\.[0-9]{1,4})?$" title="Enter a Valid Input" value="0"><label>Pieces</label>
		</div>
		<div class="form-group" id="qty130" style="display:none;">
			<div class="row" style="margin-left: 0 !important;">
			<div class="col-sm-3">
			<label style="width:240px;" for="qty"><b>Input Quantity:</b></label>
			</div>
			<div class="col-sm-5">
			<input type="text" style="width:120px;" maxlength="6" class="rounded" id="litr0" pattern="^(0|[1-9][0-9]*)$" title="Enter a Valid Input" value="0"><label>Litre</label>
			</div>
			<div class="col-sm-4">
			<input type="text" style="width:100px;" maxlength="3" class="rounded" id="mltr0" pattern="^(0|[1-9][0-9]*)$" value ="0">ml
			</div>
		</div>
		<div class="form-group" id="qty140" style="display:none;">
			<label style="width:240px;" for="qty"><b>Input Quantity:</b></label>
			<input type="text" style="width:150px;" maxlength="6" class="rounded" id="mtr0" pattern="^[0-9]+(\.[0-9]{1,4})?$" title="Enter a Valid Input" value="0"><label>Meter</label>
		</div>
		<div class="form-group" id="qty150" style="display:none;">
			<label style="width:240px;" for="qty"><b>Input Quantity:</b></label>
			<input type="text" style="width:150px;" maxlength="6" class="rounded" id="ft0" pattern="^[0-9]+(\.[0-9]{1,4})?$" title="Enter a Valid Input" value="0"><label>Feet</label>
		</div>
	</td>
	<td>
		<div class="form-group" id="wateractivityDiv0" style="display: none;">
      	<input type="hidden" class="rounded" style="width:100px;"  id="extrawaterwt0" name="extrawaterwt[]" value="0" readonly>
		<div class="col-sm-12">
        	<label for="extrawaterwt0"><b>Extra Weight Added by Water : </b></label>
		</div>
		<div class="col-sm-4">
        	<input type="text" style="width:120px;" maxlength="6" class="rounded" id="extrawaterwtko0" pattern="^(0|[1-9][0-9]*)$" title="Enter a Valid Input" value="0"><label>Kilo</label>
		</div>	
		<div class="col-sm-4">
			<input type="text" style="width:100px;" maxlength="3" class="rounded" id="extrawaterwtgr0" pattern="^(0|[1-9][0-9]*)$" value ="0"><label>gram</label>
		</div>
      </div>
	</td>
	
	<td><button type="button" name="addpp" id="addpp" class="btn btn-success" style="margin-top: 25%;">Add More</button></td>
	
</tr>
</table>
    <button type="submit"  class="btn btn-primary" id = "packageSubmit" name="submit">Submit</button>
    <!-- <button class="btn btn-danger">Cancel</button> -->
</form>
</div>

<!-- Add Table Modal -->
<div id="tableModal" class="modal fade" role="dialog" tabindex="-1" aria-hidden="true" style="overflow: auto;text-align: center;"> 
  <div class="modal-dialog" style="text-align: left; max-width: 100%;width: auto !important; display: inline-block;">
    <!-- Modal content-->
    <div class="modal-content">
      <div class="modal-header">
         <h4 class="modal-title">Select Raw Product</h4>
		 <button type="button" class="close" data-dismiss="modal">&times;</button>
       	<input type="hidden" id="maintablerow" readonly>
      </div>
      <div class="modal-body">
       	<table id="datatableid" class="table table-striped table-bordered table-hover">
			<thead class="thead-dark">
				<tr style="background-color:#048065">
					<th class="center">
						
					</th>
					<th>Sr. No</th>
					<th>Sent From</th>
					<th>Product Name</th>
					<th>Quantity</th>	
					<th>Quantity Type</th>
					<th>Sent DateTime</th>
					<th>Select</th>
					<th style="display: none;"></th>
					<th style="display: none;"></th>
					<th style="display: none;"></th>
					<th style="display: none;"></th>
					
				</tr>
			</thead>
			<tbody>
			<?php 
				$sql = "select group_concat(ad.id order by ad.id asc) as ids,ad.storageId,p2.item as ptypename,p2.id as ptypeId,ad.id ,frmd.depart as frmdpt,p.description as prodname,sum(ad.send_to_quantity) as send_to_quantity,sendqt.q_name,ad.created_at as senddttime ,ad.waste,ad.water_wt,
				sendqt.id as qtytypeid 
				from admin_dpts ad ,department frmd,productitem p ,quantity_type sendqt,productype p2 
				where ad.send_from = frmd.id and ad.productItem_id = p.id and ad.send_to_qtny_type = sendqt.id and ad.ptype = p2.id and p.item = 1 and (ad.admindpt_parent_id = 0 or ad.send_from =11)  and ad.send_to = '".@$dptId."' and ad.send_to_quantity > 0 group by ad.productItem_id ,ad.send_to_qtny_type,ad.ptype having sum(ad.send_to_quantity) > 0";
				$result = $con->query($sql);

				if ($result->num_rows > 0) 
				{
				    // output data of each row
		    		while($row = $result->fetch_assoc()) 
		    		{
			?>
			<tr style="background-color:#AFF3CA" id="t<?php echo $row['ids'];?>">
				<td class="center">
					
					<p style="display: none"><?php echo $row['ids'];?></p>
				</td>
				<td>
					<p><?php echo $row['id'];?></p>
				</td>
				<td>
					<p><?php echo $row['frmdpt'];?></p>
				</td>
				<td>
					<p><?php echo $row['prodname'];?></p>
				</td>
				<td>
					<p><?php 

					if($row['qtytypeid'] == '1' or $row['qtytypeid'] == '3'){
						if($row['send_to_quantity'] != 0){
							echo ($row['send_to_quantity']/1000);
						}else{
							echo $row['send_to_quantity'];
						}
					}else{
						echo $row['send_to_quantity'];
					}
					?>
					</p>
				</td>
				<td>
					<p><?php echo $row['q_name'];?></p>
				</td>
				<td>
					<p><?php echo $row['senddttime'];?></p>
				</td>
				<td>
					<button type="button" class="btn btn-primary sendbtn"><i class="ace-icon fa fa-check bigger-130"></i></button>
				</td>
				<td style="display: none;"><?php echo $row['ptypename']?></td>
				<td style="display: none;"><?php echo $row['ptypeId']?></td>
				<td style="display: none;"><?php echo $row['qtytypeid']?></td>
				<td style="display: none;"><?php echo $row['storageId']?></td>
				
			</tr>
			<?php
					}
				}

			?>
			</tbody>
		</table>
	  </div>
      <div class="modal-footer">
       
      </div>
    </div>

  </div>
</div>

<!-- Add Final Product Modal -->
<div id="ad_product" class="modal fade" role="dialog" tabindex="-1" aria-hidden="true"> 
  <div class="modal-dialog">
    <!-- Modal content-->
    <div class="modal-content">
      <div class="modal-header">
        <h4 class="modal-title">Add Final Product</h4>
		<button type="button" onclick="clearForm('user_form')" class="close" data-dismiss="modal">&times;</button>
        
      </div>
      <div class="modal-body">
       
		<form method="POST" id="user_form">
		
		<div class="form-group">
            <label>Product Name</label>
            <input type="text" class="form-control" id="pname" name="pname" required>
        </div>
        <b id="mesg"></b>
		<input type="button" name="add_product" id="add_product" style="background-color:#FF6666" class="btn btn-default" value="Submit"/>
		<button type="button" style="background:radial-gradient(#FFF176, #ff3300)" class="btn btn-default" data-dismiss="modal" onclick="clearForm('user_form')">Close</button>
		</form> 

      </div>
      <div class="modal-footer">
       
      </div>
    </div>

  </div>
</div>

<!-- Add Other Departments Modal -->
<div id="ad_dpt" class="modal fade" role="dialog" tabindex="-1" aria-hidden="true"> 
  <div class="modal-dialog">
    <!-- Modal content-->
    <div class="modal-content">
      <div class="modal-header">
        <h4 class="modal-title">Add Other Department</h4>
		<button type="button" class="close" onclick="clearForm('dpt_form')" data-dismiss="modal">&times;</button>
        
      </div>
      <div class="modal-body">
       
		<form method="POST" id="dpt_form">
		
		<div class="form-group">
            <label>Department Name</label>
            <input type="text" class="form-control" id="dname" name="dname" required>
        </div>
        <b id="mesgdpt"></b>
		<input type="button" name="add_dptbtn" id="add_dptbtn" style="background-color:#FF6666" class="btn btn-default" value="Submit"/>
		<button type="button" style="background:radial-gradient(#FFF176, #ff3300)" class="btn btn-default" data-dismiss="modal" onclick="clearForm('dpt_form')">Close</button>
		</form> 

      </div>
      <div class="modal-footer">
       
      </div>
    </div>

  </div>
</div>

</body>

<script>
$(document).ready(function(){
	var userid = '<?php echo @$userid; ?>';
	var t = $('#datatableid').DataTable( {
	rowReorder: true,
	"order": [[ 0, "desc" ]],
	columnDefs: [
	    { orderable: true, targets: '_all' }
	    
	]
	} );

	$('.finalproductBtn').on('click',function() {
		$('#ad_product').modal('show');
	});

	addMoreItems(t);

	$('.addpurchaser').on('click',function() {
			
		$('#add_sup').modal('show');
	});

	$('#datatableid').on('click','.sendbtn',function() {
		
		$tr = $(this).closest('tr');

		var data = $tr.children('td').map(function(){
			return $(this).text();
		}).get();

		var mainrowId = $('#maintablerow').val();
		

		$('#adminId'+mainrowId).val(data[0].trim());
		$('#prodctVal'+mainrowId).val(data[3].trim());
		
		$('#totalQty'+mainrowId).val(data[4].trim());

		$('#totalQtyType'+mainrowId).html(data[5].trim());
		
		$('#qtytypeId'+mainrowId).val(data[10].trim());
		
		$('#ptype'+mainrowId).val(data[8].trim());

		var ptypId = data[9].trim();
		$('#ptypeId'+mainrowId).val(ptypId);
		if(ptypId == '2'){
				document.getElementById("wateractivityDiv"+mainrowId).style.display = "block";
			}else{
				document.getElementById("wateractivityDiv"+mainrowId).style.display = "none";
			}

		changefuncInput(mainrowId,1);

		//if($('#adminId'+mainrowId).val() !=''){
			var tablerow = document.getElementById('t'+$('#adminId'+mainrowId).val());
			//console.log('table',tablerow);
			if(!$('#t'+$('#adminId'+mainrowId).val()).is(':visible')){
				tablerow.style.display = "revert";
			}
			
		//}
		
		$('#tableModal').modal('hide');	

	});

	$('#add_product').click(function(){
		var data=$('#user_form').serialize()+'&type=add_final_product';
		var productnm = $('#pname').val();
		$.ajax({
			url:'../controller/purchase_controller.php',
			type:'POST',
			data:data,
			success:function(response){
				console.log('Result',response);
				if(response.status == true){
					alert(response.msg);
					$('#fin_product').append($('<option>', {
					    value: response.rowId,
					    text: productnm
					}));
										
					
					$('#ad_product').modal('hide');
					clearForm('user_form');
										
				}else{
					alert(response.msg);
				}
			}
		});
	});

	$("#packageForm").submit(function(e) {
    document.getElementById("packageSubmit").disabled=true;
    //prevent Default functionality
    e.preventDefault();
    //do your own request an handle the results

    var totalBoxes = 0;
    var totalsendngQty = 0;

	var rowid =$("#item_table").find("tr").last().attr('id').replace("row", "");
	var selectedValue = $('#qtytypeId'+rowid).val();
	var totalqt  = $('#totalQty'+rowid).val();
	if(selectedValue == "1")
	{
		var kilogrm = (parseInt($('#ko'+rowid).val()) * 1000)+parseInt($('#gr'+rowid).val());
		$('#qty'+rowid).val(kilogrm);
		totalqt = totalqt * 1000;

	}else if(selectedValue == "3")
	{
		var milliltr = (parseInt($('#litr'+rowid).val()) * 1000)+parseInt($('#mltr'+rowid).val());
		$('#qty'+rowid).val(milliltr);
		totalqt = totalqt * 1000;

	}else if(selectedValue == "2")
	{
		$('#qty'+rowid).val($('#piec'+rowid).val());
		
	}else if(selectedValue == "4")
	{
		$('#qty'+rowid).val($('#mtr'+rowid).val());
	}else if(selectedValue == "5")
	{
		$('#qty'+rowid).val($('#ft'+rowid).val());
	}

	var rowidf =$("#item_table1").find("tr").last().attr('id').replace("roww", "");
	var selectedValue2 = $('#sendqty_typ'+rowidf).val();
	if(selectedValue2 == "1")
	{
		var kilogrm = (parseInt($('#pko'+rowidf).val()) * 1000)+parseInt($('#pgr'+rowidf).val());
		$('#sqty'+rowidf).val(kilogrm);
		//totalqt = totalqt * 1000;

	}else if(selectedValue2 == "3")
	{
		var milliltr = (parseInt($('#plitr'+rowidf).val()) * 1000)+parseInt($('#pmltr'+rowidf).val());
		$('#sqty'+rowidf).val(milliltr);
		//totalqt = totalqt * 1000;

	}else if(selectedValue2 == "2")
	{
		$('#sqty'+rowidf).val($('#ppiec'+rowidf).val());
		
	}else if(selectedValue2 == "4")
	{
		$('#sqty'+rowidf).val($('#pmtr'+rowidf).val());
	}else if(selectedValue2 == "5")
	{
		$('#sqty'+rowidf).val($('#pft'+rowidf).val());
	}
	
	var selectedValue3 = $('#waste_qty_typ'+rowidf).val();
	if(selectedValue3 == "1")
	{
		var kilogrm = (parseInt($('#wko'+rowidf).val()) * 1000)+parseInt($('#wgr'+rowidf).val());
		$('#wqty'+rowidf).val(kilogrm);
		//totalqt = totalqt * 1000;

	}else if(selectedValue3 == "3")
	{
		var milliltr = (parseInt($('#wlitr'+rowidf).val()) * 1000)+parseInt($('#wmltr'+rowidf).val());
		$('#wqty'+rowidf).val(milliltr);
		//totalqt = totalqt * 1000;

	}else if(selectedValue3 == "2")
	{
		$('#wqty'+rowidf).val($('#wpiec'+rowidf).val());
		
	}else if(selectedValue3 == "4")
	{
		$('#wqty'+rowidf).val($('#wmtr'+rowidf).val());
	}else if(selectedValue3 == "5")
	{
		$('#wqty'+rowidf).val($('#wft'+rowidf).val());
	}

	var extraWaterWt = 0;

	if($('#ptypeId'+rowid).val().includes('2')){
			var kilogrm = (parseInt($('#extrawaterwtko'+rowid).val()) * 1000)+parseInt($('#extrawaterwtgr'+rowid).val());
			extraWaterWt = kilogrm;
	} 

	var totalsendngQty = parseFloat($('#qty'+rowid).val());
	if(totalsendngQty > totalqt){
		alert('Sending Quantity cannot exceed Total Quantity');
		document.getElementById("packageSubmit").disabled=false;
	}else if(totalsendngQty == 0 || isNaN(totalsendngQty)){
		alert('Input Quantity cannot be 0 or Empty');
		document.getElementById("packageSubmit").disabled=false;
	}else if($('#ptypeId'+rowid).val().includes('2') && (isNaN(extraWaterWt) || extraWaterWt === "")){
		alert('Invalid or Empty Value in Extra Water Weight Quantity');
		document.getElementById("packageSubmit").disabled=false;
	}else if(parseFloat($('#sqty'+rowidf).val()) == 0){
		alert('Sending Quantity cannot be 0');
		document.getElementById("packageSubmit").disabled=false;
	}else if(parseFloat($('#wqty'+rowidf).val()) != 0 && $('#wtype'+rowidf).val() == '0'){
		alert('Please select a waste type since you have entered waste details');
		document.getElementById("packageSubmit").disabled=false;
	}else{

		if($('#wtype'+rowidf).val() == null){
			$('#wtype'+rowidf).val('');
		}

		var formData = new FormData(this);
		$.ajax( {
	        url: '../controller/admin_controller.php',
	        type: 'POST',
	        data: 'lotnumber='+$('#lotnumber').val()+'&type=checkLotNumber',
	      
	        success: function(data){
	          
	          if(data.status == true){
		        $('#extrawaterwt'+rowid).val(extraWaterWt);
				formData.append('type','multipleRawMaterialSend');
				formData.append('userId',userid);
				formData.append('sendFrom','<?php echo @$dptId; ?>');

				$.ajax( {
					url: '../controller/multiple_raw_material_controller.php',
					type: 'POST',
					data: formData,
					processData: false,
					contentType: false,

					success: function(dataM){
					console.log('Msg',dataM);
					if(dataM.status == true){
						alert('Product sent to '+dataM.dpt); 
						window.location.reload();
					}else{
						alert('Failed send product(s)'); 
						document.getElementById("packageSubmit").disabled=false;
					}
					},
					error: function(r) {
					   alert('Unable to Add Items'); 
					   document.getElementById("packageSubmit").disabled=false;
					}
				} );
	      	  }else{
	      	  	alert(data.msg); 
	      	  	document.getElementById("packageSubmit").disabled=false;
	      	  }
	        },
	        error: function(r) {
	          alert('Unable to validate lot number'); 
	          document.getElementById("packageSubmit").disabled=false;
	        }
	      } );

	}
   	
	});

$(document).on('click','.btn_remove_f',function(){
		var button_id=$(this).attr("id");

		$("#rowf"+button_id+"").remove();		
		$("#roww"+button_id+"").remove();
	});

	$('#add_dptbtn').click(function(){
		var data=$('#dpt_form').serialize()+'&type=add_other_department';
		var productnm = $('#dname').val();
		$.ajax({
			url:'../controller/storage_controller.php',
			type:'POST',
			data:data,
			success:function(response){
				console.log('Result',response);
				if(response.status == true){
					alert(response.msg);
					$('#dpt').append($('<option>', {
					    value: response.rowId,
					    text: productnm
					}));

					$('#rdpt').append($('<option>', {
					    value: response.rowId,
					    text: productnm
					}));
					
					$('#add_dptbtn').modal('hide');
					clearForm('dpt_form');
					$('#single_send').modal('show');

				}else{
					alert(response.msg);
				}
			}
		});
	});

});


function openTableM(rowid){
	$('#maintablerow').val(rowid);
	$('#tableModal').modal('show');

}

function changefunc(rowid,divNum) {
	
	var selectedValue = $('#sendqty_typ'+rowid).val();
	console.log('RowId!!','qty'+divNum+'1'+rowid);
	console.log('Selected RowId',selectedValue);

	if(selectedValue == "1")
	{
		$('#qty'+divNum+'1'+rowid).show();
		$('#qty'+divNum+'2'+rowid).hide();
		$('#qty'+divNum+'3'+rowid).hide();
		$('#qty'+divNum+'4'+rowid).hide();
		$('#qty'+divNum+'5'+rowid).hide();
		
	}else if(selectedValue == "2")
	{
		$('#qty'+divNum+'2'+rowid).show();
		$('#qty'+divNum+'1'+rowid).hide();
		$('#qty'+divNum+'3'+rowid).hide();
		$('#qty'+divNum+'4'+rowid).hide();
		$('#qty'+divNum+'5'+rowid).hide();
		
	}else if(selectedValue == "4")
	{
		$('#qty'+divNum+'2'+rowid).hide();
		$('#qty'+divNum+'1'+rowid).hide();
		$('#qty'+divNum+'3'+rowid).hide();
		$('#qty'+divNum+'4'+rowid).show();
		$('#qty'+divNum+'5'+rowid).hide();
		
	}else if(selectedValue == "5")
	{
		$('#qty'+divNum+'2'+rowid).hide();
		$('#qty'+divNum+'1'+rowid).hide();
		$('#qty'+divNum+'3'+rowid).hide();
		$('#qty'+divNum+'4'+rowid).hide();
		$('#qty'+divNum+'5'+rowid).show();
		
	}else
	{
		$('#qty'+divNum+'3'+rowid).show();
		$('#qty'+divNum+'1'+rowid).hide();
		$('#qty'+divNum+'2'+rowid).hide();
		$('#qty'+divNum+'4'+rowid).hide();
		$('#qty'+divNum+'5'+rowid).hide();
		
	}
}

function changefuncWaste(rowid,divNum) {
	console.log('RowId',rowid);
	//alert(rowid);
	var selectedValue = $('#waste_qty_typ'+rowid).val();
	console.log('RowId',selectedValue);
	if(selectedValue == "1")
	{
		$('#qty'+divNum+'1'+rowid).show();
		$('#qty'+divNum+'2'+rowid).hide();
		$('#qty'+divNum+'3'+rowid).hide();
		$('#qty'+divNum+'4'+rowid).hide();
		$('#qty'+divNum+'5'+rowid).hide();
		
	}else if(selectedValue == "2")
	{
		$('#qty'+divNum+'2'+rowid).show();
		$('#qty'+divNum+'1'+rowid).hide();
		$('#qty'+divNum+'3'+rowid).hide();
		$('#qty'+divNum+'4'+rowid).hide();
		$('#qty'+divNum+'5'+rowid).hide();
		
	}else if(selectedValue == "4")
	{
		$('#qty'+divNum+'2'+rowid).hide();
		$('#qty'+divNum+'1'+rowid).hide();
		$('#qty'+divNum+'3'+rowid).hide();
		$('#qty'+divNum+'4'+rowid).show();
		$('#qty'+divNum+'5'+rowid).hide();
		
	}else if(selectedValue == "5")
	{
		$('#qty'+divNum+'2'+rowid).hide();
		$('#qty'+divNum+'1'+rowid).hide();
		$('#qty'+divNum+'3'+rowid).hide();
		$('#qty'+divNum+'4'+rowid).hide();
		$('#qty'+divNum+'5'+rowid).show();
		
	}else
	{
		$('#qty'+divNum+'3'+rowid).show();
		$('#qty'+divNum+'1'+rowid).hide();
		$('#qty'+divNum+'2'+rowid).hide();
		$('#qty'+divNum+'4'+rowid).hide();
		$('#qty'+divNum+'5'+rowid).hide();
		
	}
}

function changefuncInput(rowid,divNum) {
	console.log('RowId',rowid);
	
	var selectedValue = $('#qtytypeId'+rowid).val();
	console.log('RowId',selectedValue);
	if(selectedValue == "1")
	{
		$('#qty'+divNum+'1'+rowid).show();
		$('#qty'+divNum+'2'+rowid).hide();
		$('#qty'+divNum+'3'+rowid).hide();
		$('#qty'+divNum+'4'+rowid).hide();
		$('#qty'+divNum+'5'+rowid).hide();
		
	}else if(selectedValue == "2")
	{
		$('#qty'+divNum+'2'+rowid).show();
		$('#qty'+divNum+'1'+rowid).hide();
		$('#qty'+divNum+'3'+rowid).hide();
		$('#qty'+divNum+'4'+rowid).hide();
		$('#qty'+divNum+'5'+rowid).hide();
		
	}else if(selectedValue == "4")
	{
		$('#qty'+divNum+'2'+rowid).hide();
		$('#qty'+divNum+'1'+rowid).hide();
		$('#qty'+divNum+'3'+rowid).hide();
		$('#qty'+divNum+'4'+rowid).show();
		$('#qty'+divNum+'5'+rowid).hide();
		
	}else if(selectedValue == "5")
	{
		$('#qty'+divNum+'2'+rowid).hide();
		$('#qty'+divNum+'1'+rowid).hide();
		$('#qty'+divNum+'3'+rowid).hide();
		$('#qty'+divNum+'4'+rowid).hide();
		$('#qty'+divNum+'5'+rowid).show();
		
	}else
	{
		$('#qty'+divNum+'3'+rowid).show();
		$('#qty'+divNum+'1'+rowid).hide();
		$('#qty'+divNum+'2'+rowid).hide();
		$('#qty'+divNum+'4'+rowid).hide();
		$('#qty'+divNum+'5'+rowid).hide();
		
	}
}

function disableDivs(rowid,divNum) {
	console.log('disableRowId',rowid);
	
	var selectedValue = $('#qtytypeId'+rowid).val();
	
	if(selectedValue == "1")
	{
		console.log('selectedValue',selectedValue);
		$('#qty'+divNum+'1'+rowid).find('*').prop('disabled',true);
		
	}else if(selectedValue == "2")
	{
		$('#qty'+divNum+'2'+rowid).children().prop('disabled',true);
	}else if(selectedValue == "4")
	{
		$('#qty'+divNum+'4'+rowid).children().prop('disabled',true);
	}else if(selectedValue == "5")
	{
		$('#qty'+divNum+'5'+rowid).children().prop('disabled',true);
	}else
	{
		$('#qty'+divNum+'3'+rowid).find('*').prop('disabled',true);
	}

	$('#extrawaterwtko'+rowid).prop('readonly',true);
	$('#extrawaterwtgr'+rowid).prop('readonly',true);
}

function enableDivs(rowid,divNum) {
	console.log('enableRowId',rowid);
	
	var selectedValue = $('#qtytypeId'+rowid).val();
	
	if(selectedValue == "1")
	{
		$('#qty'+divNum+'1'+rowid).find('*').prop('disabled',false);
		
	}else if(selectedValue == "2")
	{
		$('#qty'+divNum+'2'+rowid).children().prop('disabled',false);
	}else if(selectedValue == "4")
	{
		$('#qty'+divNum+'4'+rowid).children().prop('disabled',false);
	}else if(selectedValue == "5")
	{
		$('#qty'+divNum+'5'+rowid).children().prop('disabled',false);
	}else
	{
		$('#qty'+divNum+'3'+rowid).find('*').prop('disabled',false);
	}

	$('#extrawaterwtko'+rowid).prop('readonly',false);
	$('#extrawaterwtgr'+rowid).prop('readonly',false);

}

function clearForm(formname){
	$("#"+formname).trigger("reset");

	if(formname == 'user_form'){
		$('#mesg').text('');
		$('#add_product').show();
	}else if(formname == 'user_form2'){
		$('#mesg2').text('');
		$('#productype').show();
	}else if(formname == 'dpt_form'){
		$('#mesgdpt').text('');
		$('#add_dptbtn').show();
	}
}

function addMoreItems(table){
	var i=1;
	$('#addpp').click(function(){
		var rowid =$("#item_table").find("tr").last().attr('id').replace("row", "");
		if($('#totalQty'+rowid).val() ==''){
			alert('First please select a raw material to add more');
		}else{
			var totalqt  = $('#totalQty'+rowid).val();
			var selectedValue = $('#qtytypeId'+rowid).val();

			if(selectedValue == "1")
			{
				var kilogrm = (parseInt($('#ko'+rowid).val()) * 1000)+parseInt($('#gr'+rowid).val());
				$('#qty'+rowid).val(kilogrm);
				totalqt = totalqt * 1000;

			}else if(selectedValue == "3")
			{
				var milliltr = (parseInt($('#litr'+rowid).val()) * 1000)+parseInt($('#mltr'+rowid).val());
				$('#qty'+rowid).val(milliltr);
				totalqt = totalqt * 1000;

			}else if(selectedValue == "2")
			{
				$('#qty'+rowid).val($('#piec'+rowid).val());
			}else if(selectedValue == "4")
			{
				$('#qty'+rowid).val($('#mtr'+rowid).val());
			
			}else if(selectedValue == "5")
			{
				$('#qty'+rowid).val($('#ft'+rowid).val());
			}

			var extraWaterWt = 0;

			if($('#ptypeId'+rowid).val() == '2'){
				var kilogrm = (parseInt($('#extrawaterwtko'+rowid).val()) * 1000)+parseInt($('#extrawaterwtgr'+rowid).val());
				extraWaterWt = kilogrm;
			} 

			var totalsendngQty = parseFloat($('#qty'+rowid).val());
			
			console.log('Sending Qty',totalsendngQty);
			if(totalsendngQty > totalqt){
				alert('Sending Quantity cannot exceed Total Quantity');
				console.log('kilogram',totalsendngQty);
				console.log('totalqt',totalqt);
			}else if(totalsendngQty == 0 || isNaN(totalsendngQty)){
				alert('Input Quantity cannot be 0 or Empty');
			}else if($('#ptypeId'+rowid).val().includes('2') && (isNaN(extraWaterWt) || extraWaterWt === "")){
				alert('Invalid or Empty Value in Extra Water Weight Quantity');
			}else{
				$('#extrawaterwt'+rowid).val(extraWaterWt);
				var tablerow = document.getElementById('t'+$('#adminId'+rowid).val())
				if(tablerow != null){
					tablerow.style.display ="none";
				}
				i++;                                                
				$('#item_table').append('<tr id="row'+i+'"><td><div class="form-group"> <label style="width:120px;"for="finalProdct"><b>Product Name:</b></label> <input style="width:200px;" type="text" class="rounded" id="prodctVal'+i+'" name="prodctVal[]" readonly required> <input type="hidden" class="rounded" id="adminId'+i+'" name="adminId[]"> <button id="opentablbtn'+i+'" onclick="openTableM('+i+')" type="button" class="btn btn-primary" data-toggle="tooltip" data-placement="bottom" title="Add Product" style="background-color: #b134eb;"><i class="ace-icon fa fa-plus bigger-130"></i></button></div></td><td><div class="form-group"> <label><b>Product Type : </b></label> <input type="text" style="width:100px;" class="rounded" id="ptype'+i+'" disabled> <input type="hidden" class="rounded" id="ptypeId'+i+'" disabled></div></td><td><div class="form-group"> <label><b>Total Quantity : </b></label> <input type="text" style="width:100px;" class="rounded" id="totalQty'+i+'" disabled> <label id="totalQtyType'+i+'"></label><input type="hidden" class="rounded" style="width:100px;" id="qty'+i+'" name="qty[]" readonly><input type="hidden" class="rounded" style="width:100px;" id="qtytypeId'+i+'" readonly></div></td><td><div class="form-group" id="qty11'+i+'"><div class="row" style="margin-left: 0 !important;"><div class="col-sm-3"><label for="qty"><b>Input Quantity:</b></label></div><div class="col-sm-5"><input type="text" style="width:120px;" maxlength="6" class="rounded" id="ko'+i+'" pattern="^(0|[1-9][0-9]*)$" title="Enter a Valid Input" value="0"><label>Kilo</label></div><div class="col-sm-4"> <input type="text" style="width:100px;" maxlength="3" class="rounded" id="gr'+i+'" pattern="^(0|[1-9][0-9]*)$" value ="0"><label>gram</label></div></div></div><div class="form-group" id="qty12'+i+'" style="display:none;"><label style="width:240px;" for="qty"><b>Input Quantity:</b></label><input type="text" style="width:150px;" maxlength="6" class="rounded" id="piec'+i+'" pattern="^[0-9]+(\.[0-9]{1,4})?$" title="Enter a Valid Input" value="0"><label>Pieces</label></div><div class="form-group" id="qty13'+i+'" style="display:none;"><div class="row" style="margin-left: 0 !important;"><div class="col-sm-3"><label style="width:240px;" for="qty"><b>Input Quantity:</b></label></div><div class="col-sm-5"><input type="text" style="width:120px;" maxlength="6" class="rounded" id="litr'+i+'" pattern="^(0|[1-9][0-9]*)$" title="Enter a Valid Input" value="0"><label>Litre</label></div><div class="col-sm-4"><input type="text" style="width:100px;" maxlength="3" class="rounded" id="mltr'+i+'" pattern="^(0|[1-9][0-9]*)$" value ="0">ml</div></div><div class="form-group" id="qty14'+i+'" style="display:none;"><label style="width:240px;" for="qty"><b>Input Quantity:</b></label><input type="text" style="width:150px;" maxlength="6" class="rounded" id="mtr'+i+'" pattern="^[0-9]+(\.[0-9]{1,4})?$" title="Enter a Valid Input" value="0"><label>Meter</label></div><div class="form-group" id="qty15'+i+'" style="display:none;"><label style="width:240px;" for="qty"><b>Input Quantity:</b></label><input type="text" style="width:150px;" maxlength="6" class="rounded" id="ft'+i+'" pattern="^[0-9]+(\.[0-9]{1,4})?$" title="Enter a Valid Input" value="0"><label>Feet</label></div></td><td><div class="form-group" id="wateractivityDiv'+i+'" style="display: none;"> <input type="hidden" class="rounded" style="width:100px;" id="extrawaterwt'+i+'" name="extrawaterwt[]" value="0" readonly><div class="col-sm-12"> <label for="extrawaterwt'+i+'"><b>Extra Weight Added by Water : </b></label></div><div class="col-sm-4"> <input type="text" style="width:120px;" maxlength="6" class="rounded" id="extrawaterwtko'+i+'" pattern="^(0|[1-9][0-9]*)$" title="Enter a Valid Input" value="0"><label>Kilo</label></div><div class="col-sm-4"><input type="text" style="width:100px;" maxlength="3" class="rounded" id="extrawaterwtgr'+i+'" pattern="^(0|[1-9][0-9]*)$" value ="0"><label>gram</label></div> </div></td><td><button name="remove" id="'+i+'" class="btn btn-danger btn_remove">X</button></td></tr>');

				document.getElementById('opentablbtn'+rowid).disabled = true;
				disableDivs(rowid,1);
							
			}
			
		}
		 
	});
	$(document).on('click','.btn_remove',function(){
		var button_id=$(this).attr("id");

		var adminId = 't'+$('#adminId'+button_id).val();
		var currentTotalQty = $('#totalQty'+button_id).val();
		$("#row"+button_id+"").remove();

		if(currentTotalQty !=''){
			var table = document.getElementById('item_table');

			var rowLength = table.rows.length;

			var isPresent = false;

			for(var i=0; i<rowLength; i+=1){
				var row = table.rows[i];

				var rowid = row.id.replace("row", "");
				
				if('t'+$('#adminId'+i).val() == adminId){
					isPresent =true;
					break;
				}
			}

			console.log('Ispresent',isPresent);
			if(!isPresent){
				document.getElementById(adminId).style.display ="revert";
			}
		}

		var lastrowid =$("#item_table").find("tr").last().attr('id').replace("row", "");
		
		enableDivs(lastrowid,1);
		enableDivs(lastrowid,2);
		enableDivs(lastrowid,3);
		document.getElementById('opentablbtn'+lastrowid).disabled = false;

		
	});

	
	$('#othrdept').on('click',function() {
			
		$('#ad_dpt').modal('show');
	});

}

function departmentCheck(){
  var x = document.getElementById("dpt").value;

  if( x == '5'){
    document.getElementById("othrdept").style.display = "initial";
  }else{
    document.getElementById("othrdept").style.display = "none";
  }

}

var j=0;
  
// jQuery button click event to add a row.
$('#addfinalpp').on('click', function () {
	//alert(j);
	var rowidlast =$("#item_table1").find("tr").last().attr('id').replace("roww", "");

	var selectedValue2 = $('#sendqty_typ'+rowidlast).val();
	if(selectedValue2 == "1")
	{
		var kilogrm = (parseInt($('#pko'+rowidlast).val()) * 1000)+parseInt($('#pgr'+rowidlast).val());
		$('#sqty'+rowidlast).val(kilogrm);
		//totalqt = totalqt * 1000;

	}else if(selectedValue2 == "3")
	{
		var milliltr = (parseInt($('#plitr'+rowidlast).val()) * 1000)+parseInt($('#pmltr'+rowidlast).val());
		$('#sqty'+rowidlast).val(milliltr);
		//totalqt = totalqt * 1000;

	}else if(selectedValue2 == "2")
	{
		$('#sqty'+rowidlast).val($('#ppiec'+rowidlast).val());
		
	}else if(selectedValue2 == "4")
	{
		$('#sqty'+rowidlast).val($('#pmtr'+rowidlast).val());
	}else if(selectedValue2 == "5")
	{
		$('#sqty'+rowidlast).val($('#pft'+rowidlast).val());
	}

	var selectedValue3 = $('#waste_qty_typ'+rowidlast).val();
	if(selectedValue3 == "1")
	{
		var kilogrm = (parseInt($('#wko'+rowidlast).val()) * 1000)+parseInt($('#wgr'+rowidlast).val());
		$('#wqty'+rowidlast).val(kilogrm);
		//totalqt = totalqt * 1000;

	}else if(selectedValue3 == "3")
	{
		var milliltr = (parseInt($('#wlitr'+rowidlast).val()) * 1000)+parseInt($('#wmltr'+rowidlast).val());
		$('#wqty'+rowidlast).val(milliltr);
		//totalqt = totalqt * 1000;

	}else if(selectedValue3 == "2")
	{
		$('#wqty'+rowidlast).val($('#wpiec'+rowidlast).val());
		
	}else if(selectedValue3 == "4")
	{
		$('#wqty'+rowidlast).val($('#wmtr'+rowidlast).val());
	}else if(selectedValue3 == "5")
	{
		$('#wqty'+rowidlast).val($('#wft'+rowidlast).val());
	}

	if(parseFloat($('#sqty'+rowidlast).val()) == 0){
		alert('Sending Quantity cannot be 0');
		return;
	}else if(parseFloat($('#wqty'+rowidlast).val()) != 0 && $('#wtype'+rowidlast).val() == '0'){
		alert('Please select a waste type since you have entered waste details');
		return;
	}

	if($('#wtype'+rowidlast).val() == null){
		$('#wtype'+rowidlast).val('0');
	} 

    j++;
    // Adding a row inside the tbody.
    $('#item_table1').append(`<tr id="rowf${j}"><td><label>Final Product :</label>
		    <select name="fin_product[]" id="fin_product${j}" class="rou">
			<?php 
			 $sql1="select * from final_product";
			   $res=mysqli_query($con,$sql1);
				while($row1 = mysqli_fetch_array($res))
				{
				?>

				<option value="<?php echo $row1['id']; ?>"><?php echo $row1['item']; ?></option> 

				<?php
				}
			?>
			</select>
			
		   <button type="button" class="btn btn-primary finalproductBtn" data-toggle="tooltip" data-placement="bottom" title="Add product" ><i class="ace-icon fa fa-plus bigger-130"></i></button>
	  </td>
	
	<td>
	 <input type="hidden" class="rounded" style="width:100px;"  id="sqty${j}" name="sqty[]" readonly>
		    <label>Quantity Type :</label>
		     <select style="width:150px;" class="rou" id="sendqty_typ${j}" name="sendqty_typ[]" onchange="changefunc(${j},2)" required>
			  <?php 
				$sql1="select * from quantity_type";
			    $res=mysqli_query($con,$sql1);
				while($row1 = mysqli_fetch_array($res))
				{
				?>
				<option value="<?php echo $row1['id']; ?>"><?php echo $row1['q_name']; ?></option> 
			    <?php
				}
				?>
		    </select>
	    </td>
		
		<td>
		<div class="row" style="margin-left: 0;">
		
		<div class="form-group" id="qty21${j}">
			<label for="qty">Send Quantity:</label><br>
			<input type="text" maxlength="6" class="rounded" id="pko${j}" pattern="^(0|[1-9][0-9]*)$" title="Enter a Valid Input" value="0"><label>Kilo</label>
		    <input type="text" style="width:204px;margin-left: 0px;" maxlength="3" class="rounded" id="pgr${j}" pattern="^(0|[1-9][0-9]*)$" value ="0"><label>gram</label>
			
		</div>
		<div class="form-group col-4" id="qty22${j}" style="display:none;">
			<label style="width:132px;" for="qty">Send Quantity:</label>
			<input type="text" style="width:150px;" maxlength="6" class="rounded" id="ppiec${j}" pattern="^[0-9]+(\.[0-9]{1,4})?$" title="Enter a Valid Input" value="0"><label>Pieces</label>
		</div>
		<div class="form-group col-4" id="qty23${j}" style="display:none;">
			<label style="width:132px;" for="qty">Send Quantity:</label>
			<div class="row" style="margin-left: 0 !important;">
			<input type="text" style="width:120px;" maxlength="6" class="rounded" id="plitr${j}" pattern="^(0|[1-9][0-9]*)$" title="Enter a Valid Input" value="0"><label>Litre</label>
			<input type="text" style="width:100px;" maxlength="3" class="rounded" id="pmltr${j}" pattern="^(0|[1-9][0-9]*)$" value ="0">ml
			</div>
		</div>
		<div class="form-group col-4" id="qty24${j}" style="display:none;">
			<label style="width:132px;" for="qty">Send Quantity:</label>
			<input type="text" style="width:150px;" maxlength="6" class="rounded" id="pmtr${j}" pattern="^[0-9]+(\.[0-9]{1,4})?$" title="Enter a Valid Input" value="0"><label>Meter</label>
		</div>
		<div class="form-group col-4" id="qty25${j}" style="display:none;">
			<label style="width:132px;" for="qty">Send Quantity:</label>
			<input type="text" style="width:150px;" maxlength="6" class="rounded" id="pft${j}" pattern="^[0-9]+(\.[0-9]{1,4})?$" title="Enter a Valid Input" value="0"><label>Feet</label>
		</div>
	</div>
		</td>
		<td></td>
	</tr>
	<tr id="roww${j}">
	<td>
	<label style="width:122px;" for="qty">Waste Type :</label>
			 <select style="width:170px;" id="wtype${j}" name="wtype[]" class="rou">
	      <option value="0" selected>Please select....</option>
	      <?php 
        
					$sql1="select * from waster_type";
					
			    $res=mysqli_query($con,$sql1);
				while($row1 = mysqli_fetch_array($res))
				{
				?>
				<option value="<?php echo $row1['id']; ?>"><?php echo $row1['name']; ?></option> 
			    <?php
				}
				?>
	        </select>
	</td>
	<td>
	<label style="width:166px;" for="qty">Waste Quantity Type :</label>
			 <select style="width:170px;" id="waste_qty_typ${j}" name="waste_qty_typ[]" class="rou" onchange="changefuncWaste(${j},3)">
	      	 <?php 
				$sql1="select * from quantity_type";
			    $res=mysqli_query($con,$sql1);
				while($row1 = mysqli_fetch_array($res))
				{
				?>
				<option value="<?php echo $row1['id']; ?>"><?php echo $row1['q_name']; ?></option> 
			    <?php
				}
				?>
	        </select>
	</td>
	<td>
	<div class="row">
		
	
	<div class="form-group col-6" id="qty31${j}">
			<input type="hidden" class="rounded" style="width:100px;"  id="wqty${j}" name="wqty[]" readonly>
			<div class="row" style="margin-left: 0 !important;">
			<label style="width:136px;" for="qty">Waste Quantity:</label>
		    <input type="text" style="width:114px;" maxlength="6" class="rounded" id="wko${j}" pattern="^(0|[1-9][0-9]*)$" title="Enter a Valid Input" value="0"><label>Kilo</label>
		    <input type="text" style="width:114px;" maxlength="3" class="rounded" id="wgr${j}" pattern="^(0|[1-9][0-9]*)$" value ="0"><label>gram</label>
			</div>
		</div>
		<div class="form-group" id="qty32${j}" style="display:none;">
			<label style="width:116px;" for="qty">Waste Quantity:</label>
			<input type="text" style="width:150px;" maxlength="6" class="rounded" id="wpiec${j}" pattern="^[0-9]+(\.[0-9]{1,4})?$" title="Enter a Valid Input" value="0"><label>Pieces</label>
		</div>
		<div class="form-group" id="qty33${j}" style="display:none;">
			<label style="width:116px;" for="qty">Waste Quantity:</label>
			<div class="row" style="margin-left: 0 !important;">
			<input type="text" style="width:120px;" maxlength="6" class="rounded" id="wlitr${j}" pattern="^(0|[1-9][0-9]*)$" title="Enter a Valid Input" value="0"><label>Litre</label>
			<input type="text" style="width:100px;" maxlength="3" class="rounded" id="wmltr${j}" pattern="^(0|[1-9][0-9]*)$" value ="0">ml
			</div>
		</div>
		<div class="form-group" id="qty34${j}" style="display:none;">
			<label style="width:116px;" for="qty">Waste Quantity:</label>
			<input type="text" style="width:150px;" maxlength="6" class="rounded" id="wmtr${j}" pattern="^[0-9]+(\.[0-9]{1,4})?$" title="Enter a Valid Input" value="0"><label>Meter</label>
		</div>
		<div class="form-group" id="qty35${j}" style="display:none;">
			<label style="width:116px;" for="qty">Waste Quantity:</label>
			<input type="text" style="width:150px;" maxlength="6" class="rounded" id="wft${j}" pattern="^[0-9]+(\.[0-9]{1,4})?$" title="Enter a Valid Input" value="0"><label>Feet</label>
		</div>
		</div>
	</td>
	<td><button name="remove" id="${j}" class="btn btn-danger btn_remove_f">X</button></td>
	</tr>`);
});


</script>
</html>