<!DOCTYPE html>
<?php
require_once('../Db/dbconfig.php');
@$name=$_GET['username'];
@$userid=$_GET['userid'];
@$dptId=$_GET['dptId'];
//echo "<script>console.log('mainuserid','".$name."')</script>";
if($dptId >= 6){
	$internalDpt = 6;
}else{
	$internalDpt = $dptId;
}

$sql = "select depart from department where id = '".$internalDpt."'";
$result = $con->query($sql);

if ($result->num_rows > 0) 
{
    // output data of each row
	while($row = $result->fetch_assoc()) 
	{
		@$dptName = $row['depart'];
	}
}
?>
<html>
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<link rel="stylesheet" href="https://cdn.datatables.net/1.10.4/css/jquery.dataTables.min.css">
<script src="https://kit.fontawesome.com/c17eae44a3.js" crossorigin="anonymous"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.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://cdn.datatables.net/1.10.4/js/jquery.dataTables.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 the tab */
.tab {
  overflow: hidden;
  border: 1px solid #ccc;
  background-color: #f1f1f1;
}

/* Style the buttons inside the tab */
.tab button {
  background-color: inherit;
  float: left;
  border: none;
  outline: none;
  cursor: pointer;
  padding: 14px 16px;
  transition: 0.3s;
  font-size: 17px;
}

/* Change background color of buttons on hover */
.tab button:hover {
  background-color: #ddd;
}

/* Create an active/current tablink class */
.tab button.active {
  background-color: #ccc;
}

/* Style the tab content */
.tabcontent {
  display: none;
  padding: 6px 12px;
  border: 1px solid #ccc;
  border-top: none;
}

@media only screen and (max-height: 700px) {
	.child{
	  width:1000px;
	  float:left;
	  font-size:15px;
	  font-family:arial;
	  padding:10px;
	  cursor: pointer;
	}
}
</style>
</head>
<body>
<div class="card">
	<div class="page-header">
      <h3 class="page-title"  >
        <span class="page-title-icon bg-gradient-primary text-white mr-2">
        <i class="fas fa-user-shield fa-xs" style="color: black"></i>
        </span><b><?php echo @$dptName; ?></b></h3>
	</div>
	<?php 
		if($dptId >= 6){
			$sql1="select depart,id from department where dpt_Id = 6 or id = 6";
			$res=mysqli_query($con,$sql1);
			if (mysqli_num_rows($res) > 0) 
			{
	?>
	<div class=" card-header d-flex flex-row-reverse">
		<select id="dptList" onchange="onchangeDepartList()" class="rou">
		   <?php 
		   if($dptId > 6){
		   	$departset= true;
		   }else{
		   	$departset= false;
		   }
				while($row1 = mysqli_fetch_array($res))
				{
        	if(!$departset){
        		$dptId = $row1['id'];
        		$departset = true;
        	}
			?>
			<option value="<?php echo $row1['id']; ?>"><?php echo $row1['depart']; ?></option> 
			<?php
	        	
				}
			?>
		</select>
		<label for="dptList" class="form-label p-2"><b>Departments :</b></label>
	</div>
	<?php 
			}
		}
	?>
	<?php 
		   if($dptId < 6){
	?>		
				<div class=" card-header d-flex flex-row-reverse">
					<a href="../view/multiple_raw_material_send.php?userid=<?php echo @$userid; ?>&dptId=<?php echo @$dptId; ?>"><button type="button" class="btn btn-primary sendbtn"><i class="ace-icon fa fa-share bigger-130"></i></button></a>
					<label class="p-2" style="width:236px;" for="dat"><b>Multiple Raw Material Send :</b></label>
				</div>
	<?php			
		   }
	?>
	<div class="card-body child" style="overflow: auto;">
		<div class="tab">
			<button class="tablinks" onclick="openTabs(event, 'comefrmStorage')" id="defaultOpen">From Storage</button>
			<button class="tablinks" onclick="openTabs(event, 'comefrmDepart')" id="dptOpenTab">From Departments</button>
		</div>
		<div id="comefrmDepart" class="tabcontent">
			<table id="datatableid" class="table table-striped table-bordered table-hover">
			<thead class="thead-dark">
				<tr style="background-color:#336699">
					<th class="center">
						
					</th>
					<th>Sr. No & Lot Number</th>
					<th>Product Name</th>
					<th>Final Product</th>
					<th>Quantity</th>	
					<th>Type</th>
					<th>Sent DateTime</th>
					<th>Water Weight</th>
					<th style="display: none;"></th>
					<th style="display: none;"></th>
					<th style="display: none;"></th>
					<th style="display: none;"></th>
					<th style="display: none;"></th>
					<th>Send To</th>
					<?php
						if(@$name == 'superadmin'){
					?>
					<th>Delete</th>
					<th>View</th>
					<?php
						}
					?>
					
				</tr>
			</thead>
			<tbody>
			<?php 
				$sql = "SELECT '1' AS is_single,ad.lotnumber, ad.id as ids,ad.ptype,p.item AS finalprodct,p.id AS finalproductId, sendqt.q_name,sendqt.id AS qtytypeid,
				ad.productItem_id , ad.send_to_quantity AS send_to_quantity,
				ad.created_at AS senddttime, ad.water_wt AS water_wt, ad.final_product,ad.ptype
				FROM admin_dpts ad ,final_product p ,quantity_type sendqt
				WHERE ad.final_product = p.id AND ad.send_to_qtny_type = sendqt.id AND ad.send_to = ".@$dptId." AND ad.send_to_quantity > 0
				UNION
				SELECT '0' AS is_single,ad.lotnumber, ad.id as ids,admrc.ptype,p.item AS finalprodct,p.id AS finalproductId, sendqt.q_name,sendqt.id AS qtytypeid, 
				GROUP_CONCAT(distinct(admrc.productItem_id) SEPARATOR ',') as productItem_id, ad.send_to_quantity AS send_to_quantity,
				ad.created_at AS senddttime, admrc.water_wt AS water_wt, ad.final_product,admrc.ptype
				FROM admin_dpts_mul_raw ad ,final_product p ,quantity_type sendqt,admin_dpts_mul_raw_child admrc 
				WHERE ad.final_product = p.id AND ad.send_to_qtny_type = sendqt.id and ad.id = admrc.admr_parent_id AND ad.send_to = ".@$dptId." AND ad.send_to_quantity > 0 group by ad.id";
				$result = $con->query($sql);

				if ($result->num_rows > 0) 
				{
				    // output data of each row
		    		while($row = $result->fetch_assoc()) 
		    		{
			?>
			<tr style="background-color:#AFF3CA">
				<td class="center">
					
					<p style="display: none"><?php echo $row['ids']?></p>
				</td>
				<td>
					<p><?php echo $row['ids'].'-'.$row['lotnumber']?></p>
				</td>
				<td>
					<p>
						<?php  
						$sql1="select group_concat(description) as items from productitem p where id in (".$row['productItem_id'].");";
					    $res=mysqli_query($con,$sql1);
						while($row1 = mysqli_fetch_array($res))
						{
							echo $row1['items'];
						} 
						?>
					</p>
				</td>

				<td>
					<p><?php echo $row['finalprodct']?></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 
						$date = new DateTime($row['senddttime'], new DateTimeZone('MST'));
						$date->setTimezone(new DateTimeZone('Asia/Kolkata'));
						echo $date->format('Y-m-d H:i:s');
					?></p>
				</td>
				<td>
					<p>
						<?php 

							if($row['water_wt'] != 0){
								echo ($row['water_wt']/1000);
							}else{
								echo $row['water_wt'];
							}
					
					?>
					
				</p>
				</td>
				<td style="display: none;">
					<?php  
						$sql1="select item as items from productype p where id in (".$row['ptype'].");";
					    $res=mysqli_query($con,$sql1);
						while($row1 = mysqli_fetch_array($res))
						{
							echo $row1['items'];
						} 
						?>
					</td>
				<td style="display: none;"><?php echo $row['ptype']?></td>
				<td style="display: none;"><?php echo $row['qtytypeid']?></td>
				<td style="display: none;"><?php echo $row['finalproductId']?></td>
				<td style="display: none;"><?php echo $row['is_single']?></td>
				<td>
						<button type="button" name="sendbtn" class="btn btn-primary sendbtn" title="Send"><i class="ace-icon fa fa-plus bigger-130"></i></button>
				</td> 
				<?php
					if(@$name == 'superadmin'){
				?>
				<td>
						<button type="button" class="btn btn-primary deletebtn" title="Delete"><i class="ace-icon fa fa-trash-o"></i></button>
				</td>
				<td>
						<button type="button" name="edit" class="btn btn-primary editbtn" title="Edit"><i class="ace-icon fa fa-search-plus bigger-130"></i></button>
				</td>
				<?php
					}
				?>
			</tr>
			<?php
					}
				}

			?>
			</tbody>
		</table>
		</div>
		<div id="comefrmStorage" class="tabcontent">
		  <table id="datatableid2" class="table table-striped table-bordered table-hover">
			<thead class="thead-dark">
				<tr style="background-color:#336699">
					<th class="center">
						
					</th>
					<th>Sr. No</th>
					<th>Sent From</th>
					<th>Product Name</th>
					<th>Quantity</th>	
					<th>Type</th>
					<th>Sent DateTime</th>
					<th style="display: none;"></th>
					<th style="display: none;"></th>
					<th style="display: none;"></th>
					<th style="display: none;"></th>
					<th>Send To</th>
					<?php
						if(@$name == 'superadmin'){
					?>
					<th>Delete</th>
					<th>Edit</th>
					<?php
						}
					?>
					
				</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 (ad.admindpt_parent_id = 0 or ad.send_from = 4 or ad.send_from = 5)  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">
				<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 
						$date = new DateTime($row['senddttime'], new DateTimeZone('MST'));
						$date->setTimezone(new DateTimeZone('Asia/Kolkata'));
						echo $date->format('Y-m-d H:i:s');
					?></p>
				</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>
				<td>
						<button type="button" name="sendbtn" class="btn btn-primary sendbtn" title="Send"><i class="ace-icon fa fa-plus bigger-130"></i></button>
				</td> 
				<?php
					if(@$name == 'superadmin'){
				?>
				<td>
						<button type="button" class="btn btn-primary deletebtn" title="Delete"><i class="ace-icon fa fa-trash-o"></i></button>
				</td>
				<td>
						<button type="button" name="edit" class="btn btn-primary editbtn" title="Edit"><i class="ace-icon fa fa-search-plus"></i></button>
				</td>
				<?php
					}
				?>
			</tr>
			<?php
					}
				}

			?>
			</tbody>
		</table>
		</div>
	</div>
</div>

<!--Send Modal-->
<div id="single_send" 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">Send</h4>
        <button type="button" class="close" data-dismiss="modal" onclick="clearForm('adminForm')">&times;</button>
        
      </div>
      <div class="modal-body">
       
		<form id="adminForm">
		<input type="hidden" class="form-control" id="adminId" name="adminId">
		<input type="hidden" class="form-control" id="is_single" name="is_single" value="1">
		<div class="form-group">
			<label for="item">Product Name</label>
		  <input type="text" class="form-control" id="item" name="item" readonly>
		</div>	
		<div class="form-group">
			<label for="item">Lot Number</label>
		  <input type="text" class="form-control" id="lotnumber" name="lotnumber">
		</div>
		<div class="row">
			<div class="form-group" style="width: 200px;margin-left: 10%;position: relative;">
				<label style="width:150px;" for="product_type">Product Type : </label>
			    <input style="width:100px;" type="text" class="rounded" id="product_type" name="product_type" disabled>
			</div>
			<div class="form-group" style="width: 220px">
			    <label style="width:150px;" for="quan">Total Quantity : </label>
				<input style="width:100px;" type="text" id="quan" name="quan" class="rounded" disabled>
				<input type="text" class="rounded" style="width:100px;"  id="qty_typ" name="qty_typ" disabled>
				<input type="hidden" class="rounded" id="qty_typId" name="qty_typId">
			</div>	
		</div>
		<div class="form-group">
		    <div class="form-group" id="qty21" >
			<label style="width:150px;" for="qty">Input Quantity :</label>
		    <input type="text" style="width:120px;" maxlength="6" class="rounded" id="iko" pattern="^(0|[1-9][0-9]*)$" title="Enter a Valid Input" value="0"><label>Kilo</label>
			<input type="text" style="width:100px;" maxlength="3" class="rounded" id="igr" pattern="^(0|[1-9][0-9]*)$" value ="0"><label>gram</label>
			
		</div>
		<div class="form-group" id="qty22" style="display:none">
			<label style="width:150px;" for="qty">Input Quantity :</label>
			<input type="text" style="width:150px;" maxlength="6" class="rounded" id="ipiec" pattern="^[0-9]+(\.[0-9]{1,4})?$" title="Enter a Valid Input" value="0"><label>Pieces</label>
		</div>
		<div class="form-group" id="qty23" style="display:none">
			<label style="width:150px;" for="qty">Input Quantity :</label>
			<input type="text" style="width:120px;" maxlength="6" class="rounded" id="ilitr" 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="imltr" pattern="^(0|[1-9][0-9]*)$" value ="0">ml
		</div>
		<div class="form-group" id="qty24" style="display:none">
			<label style="width:150px;" for="qty">Input Quantity :</label>
			<input type="text" style="width:150px;" maxlength="6" class="rounded" id="imtr" pattern="^[0-9]+(\.[0-9]{1,4})?$" title="Enter a Valid Input" value="0"><label>Meter</label>
		</div>
		<div class="form-group" id="qty25" style="display:none">
			<label style="width:150px;" for="qty">Input Quantity :</label>
			<input type="text" style="width:150px;" maxlength="6" class="rounded" id="ift" pattern="^[0-9]+(\.[0-9]{1,4})?$" title="Enter a Valid Input" value="0"><label>Feet</label>
		</div>
			<input type="hidden" id="initialqty" name="initialqty" class="rounded">
		</div>	
		<div class="dropdown-divider"></div>
		<div class="form-group">
	      <label style="width:150px;" for="no">Send To Department:</label>
	      <select style="width:170px;" id="dpt" name="dpt" onchange="departmentCheck()" class="rou" required>
	      <?php 
	        $sql1="select * from department where id not in (".@$dptId.",3)";
					
					$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" >
       <label  style="width:150px;"for="no"><b>Final Product :</b></label>
       <select name="fin_product" id="fin_product" 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" id="finalProdBtn" 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>
		</div>	
	
		<div class="form-group" id="sendqty_typDiv">
		    <label style="width:150px;"><b>Quantity Type</b></label>
		    <select style="width:150px;" class="rou" id="sendqty_typ" name="sendqty_typ" onchange="changefunc()" 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
				}
				$sendQtyLbl = 'Send Quantity';
				?>
		    </select>
		</div>
		<div class="form-group">
		    <input style="width:150px;" type="hidden" id="send_to_quantity" name="send_to_quantity">
		<div class="form-group" id="qty11" >
			<label style="width:150px;" for="qty"><b><?php echo $sendQtyLbl;?> :</b></label>
		    <input type="text" style="width:120px;" maxlength="6" class="rounded" id="ko" pattern="^(0|[1-9][0-9]*)$" title="Enter a Valid Input" value="0"><label>Kilo</label>
			<input type="text" style="width:100px;" maxlength="3" class="rounded" id="gr" pattern="^(0|[1-9][0-9]*)$" value ="0"><label>gram</label>
			
		</div>
		<div class="form-group" id="qty12" style="display:none">
			<label style="width:150px;" for="qty"><b><?php echo $sendQtyLbl;?> :</b></label>
			<input type="text" style="width:150px;" maxlength="6" class="rounded" id="piec" pattern="^[0-9]+(\.[0-9]{1,4})?$" title="Enter a Valid Input" value="0"><label>Pieces</label>
		</div>
		<div class="form-group" id="qty13" style="display:none">
			<label style="width:150px;" for="qty"><b><?php echo $sendQtyLbl;?> :</b></label>
			<input type="text" style="width:120px;" maxlength="6" class="rounded" id="litr" 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="mltr" pattern="^(0|[1-9][0-9]*)$" value ="0">ml
		</div>
		<div class="form-group" id="qty14" style="display:none">
			<label style="width:150px;" for="qty"><b><?php echo $sendQtyLbl;?> :</b></label>
			<input type="text" style="width:150px;" maxlength="6" class="rounded" id="mtr" pattern="^[0-9]+(\.[0-9]{1,4})?$" title="Enter a Valid Input" value="0"><label>Meter</label>
		</div>
		<div class="form-group" id="qty15" style="display:none">
			<label style="width:150px;" for="qty"><b><?php echo $sendQtyLbl;?> :</b></label>
			<input type="text" style="width:150px;" maxlength="6" class="rounded" id="ft" pattern="^[0-9]+(\.[0-9]{1,4})?$" title="Enter a Valid Input" value="0"><label>Feet</label>
		</div>
		</div>
		
		 <div class="form-group">
			 <label style="width:150px;" for="no"><b>Waste Type :</b></label>
       <select style="width:150px;" id="wtype" name="wtype" class="rou" required>
        <?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>	
		  </div>

      <div class="form-group" id="waste_qty_typDiv">
        <label style="width:150px;" for="waste_qty_typ"><b>Waste Quantity Type :</b></label>
        <select style="width:150px;" class="rou" id="waste_qty_typ" name="waste_qty_typ" onchange="changeWasteQtyType()" 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>
        <input type="hidden" class="rounded" style="width:100px;"  id="waste_qty" name="waste_qty" readonly>
      </div>

		   <div class="form-group">
		    <div class="form-group" id="qty16" >
			<label style="width:150px;" for="waste_qty"><b>Waste Quantity :</b></label>
            <input type="text" style="width:120px;" maxlength="6" class="rounded" id="wko" pattern="^(0|[1-9][0-9]*)$" title="Enter a Valid Input" value="0"><label>Kilo</label>
			<input type="text" style="width:100px;" maxlength="3" class="rounded" id="wgr" pattern="^(0|[1-9][0-9]*)$" value ="0"><label>gram</label>
			
			</div>
			<div class="form-group" id="qty17" style="display:none">
			<label style="width:150px;" for="waste_qty"><b>Waste Quantity :</b></label>
			<input type="text" style="width:150px;" maxlength="6" class="rounded" id="wpiec" pattern="^[0-9]+(\.[0-9]{1,4})?$" title="Enter a Valid Input" value="0"><label>Pieces</label>
			</div>
			<div class="form-group" id="qty18" style="display:none">
			<label style="width:150px;" for="waste_qty"><b>Waste Quantity :</b></label>
			<input type="text" style="width:120px;" maxlength="6" class="rounded" id="wlitr" 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" pattern="^(0|[1-9][0-9]*)$" value ="0">ml
			</div>
			 <div class="form-group" id="qty19" style="display:none">
			<label style="width:150px;" for="waste_qty"><b>Waste Quantity :</b></label>
			<input type="text" style="width:150px;" maxlength="6" class="rounded" id="wmtr" pattern="^[0-9]+(\.[0-9]{1,4})?$" title="Enter a Valid Input" value="0"><label>Meter</label>
			</div>
			<div class="form-group" id="qty20" style="display:none">
			<label style="width:150px;" for="waste_qty"><b>Waste Quantity :</b></label>
			<input type="text" style="width:150px;" maxlength="6" class="rounded" id="wft" pattern="^[0-9]+(\.[0-9]{1,4})?$" title="Enter a Valid Input" value="0"><label>Feet</label>
			</div>
		  </div>

      <div class="form-group" id="wateractivityDiv" style="display: none;">
      	<input type="hidden" class="rounded" style="width:100px;"  id="ptypeId" disabled>
      	<input type="hidden" class="rounded" style="width:100px;"  id="extrawaterwt" name="extrawaterwt" readonly>
        <label style="width:150px;" for="extrawaterwt"><b>Extra Weight Added by Water : </b></label>
        <input type="text" style="width:120px;" maxlength="6" class="rounded" id="extrawaterwtko" pattern="^(0|[1-9][0-9]*)$" title="Enter a Valid Input" value="0"><label>Kilo</label>
			<input type="text" style="width:100px;" maxlength="3" class="rounded" id="extrawaterwtgr" pattern="^(0|[1-9][0-9]*)$" value ="0"><label>gram</label>
      	</div>

      	<div class="form-group" id="packing_type_div" style="display:none">
            <label id="packaging_label" style="width:150px;" for="packaging_qt"><b>No. of Boxes</b></label>
            <input type="text" style="width:100px;" maxlength="6" class="rounded" id="packaging_qt" name="packaging_qt" pattern="^(0|[1-9][0-9]*)$" value ="0">
        </div>
       
	  <button type="submit" name="adminSend" id="adminSend" class="p-1 mb-1 bg-primary text-white rounded">Submit</button>
	  <button type="button" class="p-1 mb-2 bg-danger text-white rounded" data-dismiss="modal" onclick="clearForm('adminForm')">Close</button>
	</form> 

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

  </div>
</div>

<!--Edit Modal-->
<div id="editModal" 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">Edit Product</h4>
        <button type="button" class="close" data-dismiss="modal" onclick="clearForm('editForm')">&times;</button>
        
      </div>
      <div class="modal-body">
       
	<form id="editForm">
	<input type="hidden" class="form-control" id="editadminId" name="editadminId">
	<input type="hidden" class="form-control" id="editis_single" name="editis_single" value="1">
	<div class="form-group">
		    <label for="item">Product Name : </label>
		    <input type="text" class="form-control" id="edititem" name="edititem" disabled>
		</div>
		<div class="form-group" >
            <label  style="width:150px;"for="no"><b>Final Product :</b></label>
            <select name="editfin_product" id="editfin_product" class="rou" disabled>
			<?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>
		
		</div>	
		<div class="form-group">
		<label style="width:150px;" for="qty"><b>Old Send Quantity :</b></label>
			<input type="text" style="width:150px;" maxlength="6" class="rounded" id="oldqty" disabled><label id="oldqtytype"></label>
		</div>
	  <div class="form-group">
		    <label style="width:150px;"><b>Quantity Type</b></label>
		    <select style="width:150px;" class="rou" id="editsendqty_typ" name="editsendqty_typ" onchange="changefuncEdit()" 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>
		</div>
		<div class="form-group">
		    <input style="width:150px;" type="hidden" id="editsend_to_quantity" name="editsend_to_quantity">
		<div class="form-group" id="qty31" >
			<label style="width:150px;" for="qty"><b>Send Quantity :</b></label>
		    <input type="text" style="width:120px;" maxlength="6" class="rounded" id="eko" pattern="^(0|[1-9][0-9]*)$" title="Enter a Valid Input" value="0"><label>Kilo</label>
			<input type="text" style="width:100px;" maxlength="3" class="rounded" id="egr" pattern="^(0|[1-9][0-9]*)$" value ="0"><label>gram</label>
			
		</div>
		<div class="form-group" id="qty32" style="display:none">
			<label style="width:150px;" for="qty"><b>Send Quantity :</b></label>
			<input type="text" style="width:150px;" maxlength="6" class="rounded" id="epiec" pattern="^[0-9]+(\.[0-9]{1,4})?$" title="Enter a Valid Input" value="0"><label>Pieces</label>
		</div>
		<div class="form-group" id="qty33" style="display:none">
			<label style="width:150px;" for="qty"><b>Send Quantity :</b></label>
			<input type="text" style="width:120px;" maxlength="6" class="rounded" id="elitr" 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="emltr" pattern="^(0|[1-9][0-9]*)$" value ="0">ml
		</div>
		<div class="form-group" id="qty34" style="display:none">
			<label style="width:150px;" for="qty"><b>Send Quantity :</b></label>
			<input type="text" style="width:150px;" maxlength="6" class="rounded" id="emtr" pattern="^[0-9]+(\.[0-9]{1,4})?$" title="Enter a Valid Input" value="0"><label>Meter</label>
		</div>
		<div class="form-group" id="qty35" style="display:none">
			<label style="width:150px;" for="qty"><b>Send Quantity :</b></label>
			<input type="text" style="width:150px;" maxlength="6" class="rounded" id="eft" pattern="^[0-9]+(\.[0-9]{1,4})?$" title="Enter a Valid Input" value="0"><label>Feet</label>
		</div>
		</div>

        <div class="form-group" id="editwateractivityDiv" style="display: none;">
    	<div class="form-group">
		<label style="width:150px;" for="qty"><b>Old Extra Weight Added by Water :</b></label>
		<input type="text" style="width:150px;" maxlength="6" class="rounded" id="oldextrawaterwt" disabled><label>Kilogram</label>
		</div>
      	<input type="hidden" class="rounded" style="width:100px;"  id="editextrawaterwt" name="editextrawaterwt" readonly>
        <label style="width:150px;" for="extrawaterwt"><b>Extra Weight Added by Water : </b></label>
        <input type="text" style="width:120px;" maxlength="6" class="rounded" id="editextrawaterwtko" pattern="^(0|[1-9][0-9]*)$" title="Enter a Valid Input" value="0"><label>Kilo</label>
			<input type="text" style="width:100px;" maxlength="3" class="rounded" id="editextrawaterwtgr" pattern="^(0|[1-9][0-9]*)$" value ="0"><label>gram</label>
      	</div>

      <button type="submit" id = "updateProds" name="updateProds" class="btn btn-gradient-primary mr-2">Update</button>
		  <button type="button" class="btn btn-default" data-dismiss="modal" onclick="clearForm('editForm')">Close</button>
	</form> 

      </div>
     
    </div>

  </div>
</div>

<!-- Storage Edit Modal -->
<div id="storageEdit" 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">Edit Product</h4>
		<button type="button" class="close" data-dismiss="modal" onclick="clearForm('storageEditForm')">&times;</button>
        
      </div>
      <div class="modal-body">
       
		<form id="storageEditForm">
		
		<div class="form-group">
            <label>Product Name</label>
            <input type="text" class="form-control" id="storageEditpnm" name="storageEditpnm" disabled>
            <input type="hidden" id="storageEditAdminId" name="storageEditAdminId">
        </div>
        <div class="form-group">
		<label style="width:150px;" for="qty"><b>Total Quantity From Storage :</b></label>
		<input type="text" style="width:150px;" maxlength="6" class="rounded" id="totalstrqty" name ="totalstrqty" readonly><label id="editTotalStorageQtyType"></label>
		<input type="hidden" id="editTotalStorageQtyTypeId" disabled>
		</div>
		<div class="form-group">
		    <input style="width:150px;" type="hidden" id="editStorageQuantity" name="editStorageQuantity">
		<div class="form-group" id="qty41" >
			<label style="width:150px;" for="qty"><b>Quantity :</b></label>
		    <input type="text" style="width:120px;" maxlength="6" class="rounded" id="esko" pattern="^(0|[1-9][0-9]*)$" title="Enter a Valid Input" value="0"><label>Kilo</label>
			<input type="text" style="width:100px;" maxlength="3" class="rounded" id="esgr" pattern="^(0|[1-9][0-9]*)$" value ="0"><label>gram</label>
			
		</div>
		<div class="form-group" id="qty42" style="display:none">
			<label style="width:150px;" for="qty"><b>Quantity :</b></label>
			<input type="text" style="width:150px;" maxlength="6" class="rounded" id="espiec" pattern="^[0-9]+(\.[0-9]{1,4})?$" title="Enter a Valid Input" value="0"><label>Pieces</label>
		</div>
		<div class="form-group" id="qty43" style="display:none">
			<label style="width:150px;" for="qty"><b>Quantity :</b></label>
			<input type="text" style="width:120px;" maxlength="6" class="rounded" id="eslitr" 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="esmltr" pattern="^(0|[1-9][0-9]*)$" value ="0">ml
		</div>
		<div class="form-group" id="qty44" style="display:none">
			<label style="width:150px;" for="qty"><b>Quantity :</b></label>
			<input type="text" style="width:150px;" maxlength="6" class="rounded" id="esmtr" pattern="^[0-9]+(\.[0-9]{1,4})?$" title="Enter a Valid Input" value="0"><label>Meter</label>
		</div>
		<div class="form-group" id="qty45" style="display:none">
			<label style="width:150px;" for="qty"><b>Quantity :</b></label>
			<input type="text" style="width:150px;" maxlength="6" class="rounded" id="esft" pattern="^[0-9]+(\.[0-9]{1,4})?$" title="Enter a Valid Input" value="0"><label>Feet</label>
		</div>
		</div>
        <button type="submit" id = "storageEditbtn" name="storageEditbtn" class="btn btn-gradient-primary mr-2">Update</button>
		<button type="button" style="background:radial-gradient(#FFF176, #ff3300)" class="btn btn-default" data-dismiss="modal" onclick="clearForm('storageEditForm')">Close</button>
		</form> 

      </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>

<!-- 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">Final Product Details</h4>
		 <button type="button" class="close" data-dismiss="modal">&times;</button>
      </div>
      <div class="modal-body">
       	<table id="datatableid3" class="table table-striped table-bordered table-hover">
			<thead class="thead-dark">
				<tr style="background-color:#048065">
					<th>Sr. No</th>
					<th>Sent From</th>
					<th>Product Name</th>
					<th>Final Product</th>
					<th>Quantity</th>	
					<th>Type</th>
					<th>Sent DateTime</th>
					<th>Water Weight</th>
					<th style="display: none;"></th>
					<th style="display: none;"></th>
					<th style="display: none;"></th>
					<th style="display: none;"></th>
					<?php
						if(@$name == 'superadmin'){
					?>
					<th>Edit</th>
					<?php
						}
					?>
					
				</tr>
			</thead>
			<tbody>
			
			</tbody>
		</table>
	  </div>
      <div class="modal-footer">
       
      </div>
    </div>

  </div>
</div>

<!-- Add Table Modal -->
<div id="tableModalS" 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">Final Product Details</h4>
		 <button type="button" class="close" data-dismiss="modal">&times;</button>
      </div>
      <div class="modal-body">
       	<table id="datatableid4" class="table table-striped table-bordered table-hover">
			<thead class="thead-dark">
				<tr style="background-color:#048065">
					<th>Sr. No</th>
					<th>Product Name</th>
					<th>Quantity</th>	
					<th>Type</th>
					<th>Sent DateTime</th>
					<th style="display: none;"></th>
					<th style="display: none;"></th>
					<?php
						if(@$name == 'superadmin'){
					?>
					<th>Edit</th>
					<?php
						}
					?>
					
				</tr>
			</thead>
			<tbody>
			
			</tbody>
		</table>
	  </div>
      <div class="modal-footer">
       
      </div>
    </div>

  </div>
</div>

</body>
<script type="text/javascript">
$(document).ready(function(){
	var jvdptId = '<?php echo @$dptId; ?>';

	if(jvdptId > 6){
		$('#dptList').val(jvdptId);
	}

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

	var t2 = $('#datatableid2').DataTable( {
	rowReorder: true,
	"order": [[ 6, "desc" ]],
    columnDefs: [
        { orderable: true, targets: '_all' }
        
    ]
	} );

	var t3 = $('#datatableid3').DataTable( {
		rowReorder: true,
		"order": [[ 6, "desc" ]],
		columnDefs: [
			{ orderable: true, targets: '_all' }
			
		]
	} );

	var t4 = $('#datatableid4').DataTable( {
		rowReorder: true,
		"order": [[ 5, "desc" ]],
		columnDefs: [
			{ orderable: true, targets: '_all' }
			
		]
	} );

	// Get the element with id="defaultOpen" and click on it
	document.getElementById("defaultOpen").click();
	
	$('.finalproductBtn').on('click',function() {
		$('#ad_product').modal('show');
	});

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

    $('#datatableid').on('click','.sendbtn',function() {
			
		$('#single_send').modal('show');

		$tr = $(this).closest('tr');

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

		$('#adminId').val(data[0].trim());
		
		$('#product_type').val(data[8].trim());
		$('#quan').val(data[4].trim());
		$('#qty_typ').val(data[5].trim());

		var ptypId = data[9].trim();
		$('#qty_typId').val(data[10].trim());
		$('#ptypeId').val(ptypId);
		$('#fin_product').val(data[11].trim());
		document.getElementById('fin_product').disabled = true;
		document.getElementById('finalProdBtn').disabled = true;
		$('#is_single').val(data[12].trim());
		
		$('#item').val(data[2].trim());
		if(ptypId.includes('2')){
			document.getElementById("wateractivityDiv").style.display = "block";
		}else{
			document.getElementById("wateractivityDiv").style.display = "none";
		}
		
		changeInitialQtyType();
				
	});

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

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

		var adminIds = data[0].trim();

		var isSingle = data[12].trim();

		var url = '../controller/admin_controller.php';

		var tableEle = t3;

		if(isSingle == '0'){
			url = '../controller/multiple_raw_material_controller.php';
		}
		
		$.ajax( {
			url:url,
			type: 'POST',
			data: 'id='+adminIds+'&type=viewAdmin',

			success: function(data){
				
				if(data.status == true){
					//console.log(data.data);
					var rows = tableEle
					.rows()
					.remove()
					.draw();
					
					$.each(data.data,function(i,item){

						var finalqty = 0;
						if(item.qtytypeid == '1' || item.qtytypeid == '3'){
							finalqty = item.send_to_quantity/1000;
						}else{
							finalqty = item.send_to_quantity;
						}

						if(item.water_wt != '0'){
							finalwaterwt = item.water_wt/1000;
						}else{
								finalwaterwt = item.water_wt;
						}
						
						var rowNode = tableEle
						.row.add( [item.id, item.frmdpt,item.items,item.finalprodct,finalqty,item.q_name,item.senddttime,finalwaterwt,item.ptypeId,item.qtytypeid,item.finalproductId,isSingle,'<button type="button" name="edit" class="btn btn-primary editbtn" title="Edit"><i class="ace-icon fa fa-edit"></i></button>'])
						.draw()
						.node();
					
					});

					tableEle.columns([8,9,10,11]).visible(false);

					$('#tableModal').modal('show');
					

				}
			
			},
			error: function(r) {
			console.log(r);
			var rows = tableEle
					.rows()
					.remove()
					.draw();
			}
		});	
		
	});

	$('#datatableid2').on('click','.editbtn',function() {
			
		$('#tableModalS').modal('show');
		
		$tr = $(this).closest('tr');

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

		var adminIds = data[0].trim();

		var url = '../controller/admin_controller.php';

		var tableEle = t4;
		
		$.ajax( {
			url:url,
			type: 'POST',
			data: 'id='+adminIds+'&type=viewStorage',

			success: function(data){
				
				if(data.status == true){
					console.log(data.data);
					var rows = tableEle
					.rows()
					.remove()
					.draw();
					
					$.each(data.data,function(i,item){

						var finalqty = 0;
						if(item.qtytypeid == '1' || item.qtytypeid == '3'){
							finalqty = item.send_to_quantity/1000;
						}else{
							finalqty = item.send_to_quantity;
						}
						
						var rowNode = tableEle
						.row.add( [item.id,item.prodname,finalqty,item.q_name,item.senddttime,item.qtytypeid,item.storageId,'<button type="button" name="edit" class="btn btn-primary editbtn" title="Edit"><i class="ace-icon fa fa-edit"></i></button>'])
						.draw()
						.node();
					
					});

					tableEle.columns([5,6]).visible(false);

					$('#tableModalS').modal('show');
					

				}
			
			},
			error: function(r) {
			console.log(r);
			var rows = tableEle
					.rows()
					.remove()
					.draw();
			}
		});	
	
	});

	$('#datatableid2').on('click','.sendbtn',function() {
			
		$('#single_send').modal('show');

		$tr = $(this).closest('tr');

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

		$('#adminId').val(data[0].trim());
		$('#item').val(data[3].trim());
		$('#quan').val(data[4].trim());
		$('#qty_typ').val(data[5].trim());
		$('#product_type').val(data[7].trim());
		var ptypId = data[8].trim();
		$('#qty_typId').val(data[9].trim());
		$('#ptypeId').val(ptypId);

		document.getElementById('fin_product').disabled = false;
		document.getElementById('finalProdBtn').disabled = false;
		
		if(ptypId == '2'){
			document.getElementById("wateractivityDiv").style.display = "block";
		}else{
			document.getElementById("wateractivityDiv").style.display = "none";
		}
		
		
		changeInitialQtyType();
				
	});

	$('#datatableid3').on('click','.editbtn',function() {
			
		$('#tableModal').modal('hide');
		$('#editModal').modal('show');

		$tr = $(this).closest('tr');

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

		$('#editadminId').val(data[0].trim());
		$('#edititem').val(data[2].trim());
		$('#oldqty').val(data[4].trim());
		$('#oldqtytype').html(data[5].trim());
		$('#oldextrawaterwt').val(data[7].trim());

		var rowIndex = $(this).closest("tr").index();

		$('#editfin_product').val(t3.column(10).data()[rowIndex]);
		$('#editis_single').val(t3.column(11).data()[rowIndex]);
		
		var ptypId = t3.column(8).data()[rowIndex];
		$('#editsendqty_typ').val(t3.column(9).data()[rowIndex]);
		$('#ptypeId').val(ptypId);
		
		if(ptypId == '2'){
			document.getElementById("editwateractivityDiv").style.display = "block";
		}else{
			document.getElementById("editwateractivityDiv").style.display = "none";
		}

		changefuncEdit();
		
	});

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

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

		var adminId = datatb[0].trim();

		var rowIndex = $(this).closest("tr").index();

		$.ajax( {
	        url: '../controller/admin_controller.php',
	        method: 'POST',
	        data:'type=validateProductIsProcessed&adminId='+adminId,
	        
	        success: function(data1){
	          //console.log('Msg',data1);
	          if(data1.status == true){
		        alert('This Product has been processed furthur, first please delete from processed departments to edit this!.'); 
			      }else{
					
								console.log('rowIndex',rowIndex);

					      	$.ajax( {
						        url: '../controller/admin_controller.php',
						        method: 'POST',
						        data:'type=getStorageQuantity&storageId='+t4.column(6).data()[rowIndex],
						        
						        success: function(data2){
						          console.log('Msg',data2);
						          if(data2.status == true){
									$('#tableModalS').modal('hide');
						          	$('#storageEdit').modal('show');
							        $('#storageEditAdminId').val(adminId);
									$('#storageEditpnm').val(datatb[1].trim());

									var qtytypId = t4.column(5).data()[rowIndex];
									var totalstrqt = 0;

									if(qtytypId == '1' || qtytypId == '3'){
										totalstrqt = (parseFloat(data2.storageQty)/1000)+parseFloat(datatb[2].trim());
									}else{
										totalstrqt = parseFloat(data2.storageQty)+parseFloat(datatb[2].trim());
									}
									$('#totalstrqty').val(totalstrqt);
									$('#editTotalStorageQtyType').html(datatb[3].trim());
									$('#editTotalStorageQtyTypeId').val(qtytypId);

									changefuncEditFromStorage(qtytypId);
						      	  }else{
						      	  	alert('Failed To Get Product Details from Storage'); 
						      	  }
						        },
						        error: function(r) {
						          alert('Unable To Get Product Details from Storage'); 
						        }
						      } );
				      	  	
				      	  }
	        },
	        error: function(r) {
	          alert('Unable To Edit Product'); 
	        }
	      } );

	});

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

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

		var adminId = datatb[0].trim();
		if (confirm("Are You Sure?")) {
			var isSingle = datatb[12].trim();

			var url = '../controller/admin_controller.php';

			var tableEle = t3;

			if(isSingle == '0'){
				url = '../controller/multiple_raw_material_controller.php';
			}
			$.ajax( {
				url: url,
				method: 'POST',
				data:'type=validateProductIsProcessed&adminId='+adminId,
				
				success: function(data1){
				console.log('Msg',data1);
				if(data1.status == true){
					alert('This Product has been processed furthur, first please delete from processed departments to delete this!.'); 
				}else{

					$.ajax( {
						url: url,
						method: 'POST',
						data:'type=adminDelete&adminId='+adminId+'&userId='+userid,
						
						success: function(data2){
						console.log('Msg',data2);
						if(data2.status == true){
							alert('Product deleted and Quantity reverted to '+data2.revertDpt); 
							window.location.reload();
						}else{
							alert('Failed To Delete Product'); 
						}
						},
						error: function(r) {
						alert('Unable To Delete Product'); 
						}
					} );
					
				}
				},
				error: function(r) {
				alert('Unable To Edit Product'); 
				}
			} );
		}
	});

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

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

		var adminId = datatb[0].trim();
		if (confirm("Are You Sure?")) {
		$.ajax( {
	        url: '../controller/admin_controller.php',
	        method: 'POST',
	        data:'type=validateProductIsProcessed&adminId='+adminId,
	        
	        success: function(data1){
	          console.log('Msg',data1);
	          if(data1.status == true){
		        alert('This Product has been processed furthur, first please delete from processed departments to delete this!.'); 
		      }else{

		      	$.ajax( {
			        url: '../controller/admin_controller.php',
			        method: 'POST',
			        data:'type=adminDeleteFromStorage&adminId='+adminId+'&userId='+userid,
			        
			        success: function(data2){
			          console.log('Msg',data2);
			          if(data2.status == true){
			          	alert('Product deleted and Quantity reverted to Storage'); 
			          	window.location.reload();
			      	  }else{
			      	  	alert('Failed To Delete Product'); 
			      	  }
			        },
			        error: function(r) {
			          alert('Unable To Delete Product'); 
			        }
			      } );
	      	  	
	      	  }
	        },
	        error: function(r) {
	          alert('Unable To Delete Product'); 
	        }
	      } );
		}
	});

	$("#adminForm").submit(function(e) {
    document.getElementById("adminSend").disabled=true;
    //prevent Default functionality
    e.preventDefault();
    //do your own request an handle the results
   	
   	var selectedValue = $('#sendqty_typ').val();
   	console.log('Slected data',selectedValue);

	if($('#lotnumber').val() == ''){
		alert('Lot number cannot be empty');
		document.getElementById("adminSend").disabled=false;
		return;
	}

   if(selectedValue == "1")
	{
		var kilogrm = (parseInt($('#ko').val()) * 1000)+parseInt($('#gr').val());
		$('#send_to_quantity').val(kilogrm);
	}else if(selectedValue == "3")
	{
		var milliltr = (parseInt($('#litr').val()) * 1000)+parseInt($('#mltr').val());
		$('#send_to_quantity').val(milliltr);
	}else if(selectedValue == "2")
	{
		$('#send_to_quantity').val($('#piec').val());
	}else if(selectedValue == "4")
	{
		$('#send_to_quantity').val($('#mtr').val());
		
	}else if(selectedValue == "5")
	{
		$('#send_to_quantity').val($('#ft').val());
	}

	var selectedValue2 = $('#waste_qty_typ').val();
   	//console.log('Slected data',selectedValue);
   	if(selectedValue2 == "1")
	{
		var kilogrm = (parseInt($('#wko').val()) * 1000)+parseInt($('#wgr').val());
		$('#waste_qty').val(kilogrm);
	}else if(selectedValue2 == "3")
	{	var milliltr = (parseInt($('#wlitr').val()) * 1000)+parseInt($('#wmltr').val());
		$('#waste_qty').val(milliltr);
	}else if(selectedValue2 == "2")
	{
		$('#waste_qty').val($('#wpiec').val());
	}else if(selectedValue2 == "4")
	{
		$('#waste_qty').val($('#wmtr').val());
		
	}else if(selectedValue2 == "5")
	{
		$('#waste_qty').val($('#wft').val());
	}

	var totalqt = parseFloat($('#quan').val());
	var selectedValue3 = $('#qty_typId').val();
	if(selectedValue3 == "1")
	{
		var kilogrm = (parseInt($('#iko').val()) * 1000)+parseInt($('#igr').val());
		$('#initialqty').val(kilogrm);
		totalqt = totalqt * 1000;
	}else if(selectedValue3 == "3")
	{	var milliltr = (parseInt($('#ilitr').val()) * 1000)+parseInt($('#imltr').val());
		$('#initialqty').val(milliltr);
		totalqt = totalqt * 1000;
	}else if(selectedValue3 == "2")
	{
		$('#initialqty').val($('#ipiec').val());
	}else if(selectedValue3 == "4")
	{
		$('#initialqty').val($('#imtr').val());
		
	}else if(selectedValue3 == "5")
	{
		$('#initialqty').val($('#ift').val());
	}
	
	var formData = new FormData(this);

	var extraWaterWt = 0;
	
	if($('#ptypeId').val().includes('2')){
		var kilogrm = (parseInt($('#extrawaterwtko').val()) * 1000)+parseInt($('#extrawaterwtgr').val());
		//formData.set('extrawaterwt',kilogrm);
		extraWaterWt = kilogrm;
	} 

	formData.append('type','adminSend');
	formData.append('sendFrom',<?php echo @$dptId; ?>);
	formData.append('userId',userid);
	formData.append('lotnumber',$('#lotnumber').val());

	var sendqty = parseFloat($('#initialqty').val());
	console.log('sendqty data',sendqty);

	if(isNaN($('#send_to_quantity').val()) || $('#send_to_quantity').val() == ''){
		alert('Invalid or Empty Value in Sending Quantity');
		document.getElementById("adminSend").disabled=false;
	}else if(isNaN($('#initialqty').val()) || $('#initialqty').val() == ''){
		alert('Invalid or Empty Value in Input Quantity');
		document.getElementById("adminSend").disabled=false;
	}else if(isNaN($('#waste_qty').val()) || $('#waste_qty').val() == ''){
		alert('Invalid or Empty Value in Waste Quantity');
		document.getElementById("adminSend").disabled=false;
	}else if($('#ptypeId').val().includes('2') && (isNaN(extraWaterWt) || extraWaterWt === "")){
		alert('Invalid or Empty Value in Extra Water Weight Quantity');
		document.getElementById("adminSend").disabled=false;
	}else if(sendqty > totalqt){
		alert('Input Quantity cannot exceed Total Quantity');
		document.getElementById("adminSend").disabled=false;
	}else if(sendqty == 0){
		alert('Input Quantity cannot be 0');
		document.getElementById("adminSend").disabled=false;
	}else if(parseFloat($('#send_to_quantity').val()) == 0){
		alert('Send Quantity cannot be 0');
		document.getElementById("adminSend").disabled=false;
	}else{
		formData.set('extrawaterwt',extraWaterWt);

		$.ajax( {
	        url: '../controller/admin_controller.php',
	        type: 'POST',
	        data: 'lotnumber='+$('#lotnumber').val()+'&type=checkLotNumber',
	      
	        success: function(data){
	          
	          if(data.status == true){
		        $.ajax( {
					url: '../controller/admin_controller.php',
					type: 'POST',
					data: formData,
					processData: false,
					contentType: false,

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

	$("#editForm").submit(function(e) {
    document.getElementById("updateProds").disabled=true;
    //prevent Default functionality
    e.preventDefault();
    //do your own request an handle the results
   	
   	var selectedValue = $('#editsendqty_typ').val();
   	console.log('Slected data',selectedValue);
   if(selectedValue == "1")
	{
		var kilogrm = (parseInt($('#eko').val()) * 1000)+parseInt($('#egr').val());
		$('#editsend_to_quantity').val(kilogrm);
	}else if(selectedValue == "3")
	{
		var milliltr = (parseInt($('#elitr').val()) * 1000)+parseInt($('#emltr').val());
		$('#editsend_to_quantity').val(milliltr);
	}else if(selectedValue == "2")
	{
		$('#editsend_to_quantity').val($('#epiec').val());
	}else if(selectedValue == "4")
	{
		$('#editsend_to_quantity').val($('#emtr').val());
		
	}else if(selectedValue == "5")
	{
		$('#editsend_to_quantity').val($('#eft').val());
	}
	
	var formData = new FormData(this);
	if($('#ptypeId').val().includes('2')){
		var kilogrm = (parseInt($('#editextrawaterwtko').val()) * 1000)+parseInt($('#editextrawaterwtgr').val());
		formData.set('editextrawaterwt',kilogrm);
	} 

	formData.append('type','adminEdit');
	formData.append('userId',userid);

	var isSingle = $('#editis_single').val();

	var url = '../controller/admin_controller.php';

	if(isSingle == '0'){
		url = '../controller/multiple_raw_material_controller.php';
	}
		
	$.ajax( {
	        url:url,
	        method: 'POST',
	        data:'type=validateProductIsProcessed&adminId='+$('#editadminId').val(),
	        
	        success: function(data1){
	          console.log('Msg',data1);
	          if(data1.status == true){
		        alert('This Product has been processed furthur, first please delete from processed departments to Edit this!.'); 
		        document.getElementById("updateProds").disabled=false;
		      }else{

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

				        success: function(data){
				          console.log('Msg',data);
				          if(data.status == true){
					       		 alert('Product Updated!'); 
					        	window.location.reload();
				      	  }else{
				      	  	alert(data.msg); 
				      	  	document.getElementById("updateProds").disabled=false;
										window.location.reload();	
				      	  }
				        },
				        error: function(r) {
				          alert('Unable To Update Product'); 
				          document.getElementById("updateProds").disabled=false;
						 			 window.location.reload();
				        }
				    } );
	      	  	
	      	  }
	        },
	        error: function(r) {
	          alert('Unable To Edit Product'); 
	          document.getElementById("updateProds").disabled=false;
										window.location.reload();	
	        }
	      } );
	
});

	$('#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
					}));
					$('#editfin_product').append($('<option>', {
					    value: response.rowId,
					    text: productnm
					}));

					/*var table = document.getElementById('item_table');

					var rowLength = table.rows.length;

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

						var rowid = row.id.replace("row", "");
						$('#fin_product'+rowid).append($('<option>', {
						    value: response.rowId,
						    text: productnm
						}));
					}*/
					
					$('#ad_product').modal('hide');
					clearForm('user_form');
					
					$('#single_send').modal('show');
					
				}else{
					alert(response.msg);
				}
			}
		});
	});

	$("#storageEditForm").submit(function(e) {
	console.log('test data');
    document.getElementById("storageEditbtn").disabled=true;
    //prevent Default functionality
    e.preventDefault();
    //do your own request an handle the results
   	
   	var selectedValue = $('#editTotalStorageQtyTypeId').val();
   	var totalqt = parseFloat($('#totalstrqty').val());
   	console.log('Slected data',selectedValue);
   if(selectedValue == "1")
	{
		var kilogrm = (parseInt($('#esko').val()) * 1000)+parseInt($('#esgr').val());
		$('#editStorageQuantity').val(kilogrm);
		totalqt = totalqt * 1000;
	}else if(selectedValue == "3")
	{
		var milliltr = (parseInt($('#eslitr').val()) * 1000)+parseInt($('#esmltr').val());
		$('#editStorageQuantity').val(milliltr);
		totalqt = totalqt * 1000;
	}else if(selectedValue == "2")
	{
		$('#editStorageQuantity').val($('#espiec').val());
	}else if(selectedValue == "4")
	{
		$('#editStorageQuantity').val($('#esmtr').val());
		
	}else if(selectedValue == "5")
	{
		$('#editStorageQuantity').val($('#esft').val());
	}
	
	var sendqty = parseFloat($('#editStorageQuantity').val());
	console.log('sendqty data',sendqty);

	if(sendqty >totalqt){
    	alert('Quantity cannot exceed Total Quantity From Storage');
    	document.getElementById("storageEditbtn").disabled=false;
    }else if(sendqty == 0){
		alert('Quantity cannot be 0');
		document.getElementById("storageEditbtn").disabled=false;
    }else{
    	var formData = new FormData(this);
		
		formData.set('totalstrqty', totalqt);
		formData.append('type','adminEditFromStorage');
		formData.append('userId',userid);
			
	    $.ajax( {
	        url: '../controller/admin_controller.php',
	        type: 'POST',
	        data: formData,
	        processData: false,
	        contentType: false,

	        success: function(data){
	          console.log('Msg',data);
	          if(data.status == true){
		        alert('Product Updated!'); 
		        window.location.reload();
	      	  }else{
	      	  	alert(data.msg); 
	      	  	document.getElementById("storageEditbtn").disabled=false;
	      	  }
	        },
	        error: function(r) {
	          alert('Unable To Update Product'); 
	          document.getElementById("storageEditbtn").disabled=false;
	        }
	    } );
	}
	
});

	$('#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 changefunc() {
	var y = document.getElementById("sendqty_typ");
	var selectedValue = y.options[y.selectedIndex].value;
	
	if(selectedValue == "1")
	{
		$('#qty11').show();
		$('#qty12').hide();
		$('#qty13').hide();
		$('#qty14').hide();
		$('#qty15').hide();
		
	}else if(selectedValue == "2")
	{
		$('#qty12').show();
		$('#qty11').hide();
		$('#qty13').hide();
		$('#qty14').hide();
		$('#qty15').hide();
		
	}else if(selectedValue == "4")
	{
		$('#qty12').hide();
		$('#qty11').hide();
		$('#qty13').hide();
		$('#qty14').show();
		$('#qty15').hide();
		
	}else if(selectedValue == "5")
	{
		$('#qty12').hide();
		$('#qty11').hide();
		$('#qty13').hide();
		$('#qty14').hide();
		$('#qty15').show();
		
	}else
	{
		$('#qty13').show();
		$('#qty11').hide();
		$('#qty12').hide();
		$('#qty14').hide();
		$('#qty15').hide();
		
	}
}

function changeInitialQtyType() {
	
	var selectedValue = $('#qty_typId').val();
	
	if(selectedValue == "1")
	{
		$('#qty21').show();
		$('#qty22').hide();
		$('#qty23').hide();
		$('#qty24').hide();
		$('#qty25').hide();
		
	}else if(selectedValue == "2")
	{
		$('#qty22').show();
		$('#qty21').hide();
		$('#qty23').hide();
		$('#qty24').hide();
		$('#qty25').hide();
		
	}else if(selectedValue == "4")
	{
		$('#qty22').hide();
		$('#qty21').hide();
		$('#qty23').hide();
		$('#qty24').show();
		$('#qty25').hide();
		
	}else if(selectedValue == "5")
	{
		$('#qty22').hide();
		$('#qty21').hide();
		$('#qty23').hide();
		$('#qty24').hide();
		$('#qty25').show();
		
	}else
	{
		$('#qty23').show();
		$('#qty21').hide();
		$('#qty22').hide();
		$('#qty24').hide();
		$('#qty25').hide();
		
	}
}

function changefuncEdit(){
	var selectedValue = $('#editsendqty_typ').val();
	
	if(selectedValue == "1")
	{
		$('#qty31').show();
		$('#qty32').hide();
		$('#qty33').hide();
		$('#qty34').hide();
		$('#qty35').hide();
		
	}else if(selectedValue == "2")
	{
		$('#qty32').show();
		$('#qty31').hide();
		$('#qty33').hide();
		$('#qty34').hide();
		$('#qty35').hide();
		
	}else if(selectedValue == "4")
	{
		$('#qty32').hide();
		$('#qty31').hide();
		$('#qty33').hide();
		$('#qty34').show();
		$('#qty35').hide();
		
	}else if(selectedValue == "5")
	{
		$('#qty32').hide();
		$('#qty31').hide();
		$('#qty33').hide();
		$('#qty34').hide();
		$('#qty35').show();
		
	}else
	{
		$('#qty33').show();
		$('#qty31').hide();
		$('#qty32').hide();
		$('#qty34').hide();
		$('#qty35').hide();
		
	}
}

function changefuncEditFromStorage(selectedValue){
	
	if(selectedValue == "1")
	{
		$('#qty41').show();
		$('#qty42').hide();
		$('#qty43').hide();
		$('#qty44').hide();
		$('#qty45').hide();
		
	}else if(selectedValue == "2")
	{
		$('#qty42').show();
		$('#qty41').hide();
		$('#qty43').hide();
		$('#qty44').hide();
		$('#qty45').hide();
		
	}else if(selectedValue == "4")
	{
		$('#qty42').hide();
		$('#qty41').hide();
		$('#qty43').hide();
		$('#qty44').show();
		$('#qty45').hide();
		
	}else if(selectedValue == "5")
	{
		$('#qty42').hide();
		$('#qty41').hide();
		$('#qty43').hide();
		$('#qty44').hide();
		$('#qty45').show();
		
	}else
	{
		$('#qty43').show();
		$('#qty41').hide();
		$('#qty42').hide();
		$('#qty44').hide();
		$('#qty45').hide();
		
	}
}

function changeWasteQtyType(){
	var y = document.getElementById("waste_qty_typ");
	var selectedValue = y.options[y.selectedIndex].value;
	
	if(selectedValue == "1")
	{
		$('#qty16').show();
		$('#qty17').hide();
		$('#qty18').hide();
		$('#qty19').hide();
		$('#qty20').hide();
		
	}else if(selectedValue == "2")
	{
		$('#qty17').show();
		$('#qty16').hide();
		$('#qty18').hide();
		$('#qty19').hide();
		$('#qty20').hide();
		
	}else if(selectedValue == "4")
	{
		$('#qty16').hide();
		$('#qty17').hide();
		$('#qty18').hide();
		$('#qty19').show();
		$('#qty20').hide();
		
	}else if(selectedValue == "5")
	{
		$('#qty16').hide();
		$('#qty17').hide();
		$('#qty18').hide();
		$('#qty20').show();
		$('#qty19').hide();
	}else
	{
		$('#qty16').hide();
		$('#qty17').hide();
		$('#qty19').hide();
		$('#qty18').show();
		$('#qty20').hide();
		
	}
}

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

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

function openTabs(evt, tbName) {
  var i, tabcontent, tablinks;
  tabcontent = document.getElementsByClassName("tabcontent");
  for (i = 0; i < tabcontent.length; i++) {
    tabcontent[i].style.display = "none";
  }
  tablinks = document.getElementsByClassName("tablinks");
  for (i = 0; i < tablinks.length; i++) {
    tablinks[i].className = tablinks[i].className.replace(" active", "");
  }
  document.getElementById(tbName).style.display = "block";
  evt.currentTarget.className += " active";
}

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 existing = '<?php echo @$dptId; ?>';
  
  var p_div = document.getElementById("packing_type_div");
  if(existing == '2' && x == '3'){
  	p_div.style.display = "block";
    document.getElementById("packaging_qt").required = true;
    
  }
/*else if(existing == '15' && x == '11'){
  	$("#packaging_label").html('<b>No. of Bags</b>');
  	p_div.style.display = "block";
    document.getElementById("packaging_qt").required = true;
  }*/
  else{
  	p_div.style.display = "none";
  	document.getElementById("packaging_qt").required = false;
  }
}

function onchangeDepartList(){
	var userid = '<?php echo @$userid; ?>';
	var username = '<?php echo @$name; ?>';
	console.log('userID',userid);
	location.href = "../view/admin_view.php?username="+username+"&dptId="+$('#dptList').val()+"&userid="+userid;
}

</script>
</html>