<!DOCTYPE html>
<?php
require_once('../Db/dbconfig.php');
@$name=$_GET['username'];
@$userid=$_GET['userid'];
?>
<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>
<script src="https://cdn.datatables.net/plug-ins/1.10.24/api/sum().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;
}

@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-trash fa-xs" style="color: black"></i>
        </span><b>F-39 In</b></h3>
        <input type="hidden" id="username" value="<?php echo @$name;?>">
	</div>
	<div class="card-body child" style="overflow: auto;">
			<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</th>
					<th>Sent From</th>
					<th>Product Name</th>
					<th>Final Product</th>
					<th>Waste Type</th>
					<th>Quantity Type</th>
					<th>Quantity</th>	
					<th>Sent DateTime</th>
					<th>Send To</th>
					<th style="display: none">Quantity Type id</th>
					<th style="display: none">Waste Type id</th>
					<th style="display: none">is Single</th>
					<th style="display: none">final product</th>
					<?php
						if(@$name == 'superadmin'){
					?>
					<th>View</th>
					<?php
						}
					?>
				</tr>
			</thead>
			<tbody>
			<?php 
				$sql = "select is_single,group_concat(if(is_single = 1,concat(id,'-','1'),concat(id,'-','0'))) as ids, finalproductId,finalprodct,sum(wasteqty) as wasteqty,qtytypeId,wasteqt_type_name,wasteId,id,frmdpt,group_concat(distinct productItem_id) as productItem_id,senddttime from (
					select '1' as is_single ,s.finalproductId,s.finalprodct,s.wasteqty,s.qtytypeId,s.wasteqt_type_name,s.wasteId,s.id,s.frmdpt,s.productItem_id,s.senddttime from (select fp.id as finalproductId,fp.item as finalprodct,ad.wasteqty ,
					ad.wtype as qtytypeId,wasteqttype.q_name as wasteqt_type_name ,
					ad.waste as wasteId,ad.id ,frmd.depart as frmdpt,ad.productItem_id,
					ad.created_at as senddttime from admin_dpts ad ,department frmd ,quantity_type wasteqttype,final_product fp 
					where ad.send_from = frmd.id and ad.final_product = fp.id and wasteqttype.id = ad.wtype and ad.wasteqty !=0) as s
					union 
					select '0' as is_single,m.finalproductId,m.finalprodct,m.wasteqty,m.qtytypeId,m.wasteqt_type_name,m.wasteId,m.id,m.frmdpt,m.productItem_id,m.senddttime from (select adm.wasteqty,admrc.storageId,frmd.depart as frmdpt,
					adm.final_product as finalproductId,admrc.water_wt ,adm.waste as wasteId, adm.id,admrc.productItem_id, adm.send_from ,
					adm.send_to, adm.created_at as senddttime ,adm.wtype as qtytypeId,
					fp.item as finalprodct,qt.q_name as wasteqt_type_name  from admin_dpts_mul_raw adm,final_product fp ,quantity_type qt, department frmd, admin_dpts_mul_raw_child admrc 
					where adm.final_product = fp.id and adm.wtype = qt.id and adm.send_from = frmd.id and adm.lotnumber = admrc.lotnumber  and adm.wasteqty !=0 group by adm.id) as m
					) as t group by finalproductId,wasteId,qtytypeId";
				$result = $con->query($sql);

				if ($result->num_rows > 0) 
				{
				    // output data of each row
					$wasteId='';
		    		while($row = $result->fetch_assoc()) 
		    		{
						//echo"<pre>";print_r($row);
						$wasteId = $row['wasteId'];
			?>
			<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
					 $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 
					
				   $sql1="select GROUP_CONCAT(name) as name1 from waster_type p where id in(".$wasteId.")";
				   $res=mysqli_query($con,$sql1);
				   while($row1 = mysqli_fetch_array($res))
				   {
					   echo $row1['name1'];
				   } 
					?></p>
				</td>
				<td>
					<p><?php echo $row['wasteqt_type_name']?></p>
				</td>
				<td>
					<p><?php 

					if($row['qtytypeId'] == '1' or $row['qtytypeId'] == '3'){
						if($row['wasteqty'] != 0){
							echo ($row['wasteqty']/1000);
						}else{
							echo $row['wasteqty'];
						}
					}else{
						echo $row['wasteqty'];
					}
					?>
					</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">
					<p><?php echo $row['qtytypeId']?></p>
				</td>
				<td style="display: none">
					<p><?php echo $row['wasteId']?></p>
				</td>
				<td style="display: none">
					<p><?php echo $row['is_single']?></p>
				</td>
				<td style="display: none">
					<p><?php echo $row['finalproductId']?></p>
				</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 viewbtn" title="View"><i class="ace-icon fa fa-search-plus bigger-130"></i></button>
				</td>
				<?php
					}
				?>
				
			</tr>
			<?php
					}
				}

			?>
			</tbody>
		</table>
				
	</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" id="issendfromsingle" readonly>
		<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="revcdFinalProdct">Received Final Product : </label>
		    <input type="text" class="form-control" id="revcdFinalProdct" name="revcdFinalProdct" disabled>
		</div>
		<div class="form-group">
			<label  style="width:150px;"for="no">Item Category</label>
							
			<select name="item_category" id="item_category" style="width:250px;" class="rou" onchange="getItemDesctp(this.value);" required>
			<option value="" disabled selected hidden>Please Choose an Item...</option>
			<?php 
				$sql1="select * from 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 bg-gradient-primary text-white mr-2 add" 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>
		<div class="form-group" id="pol">
			<label  style="width:150px;"for="no">Item Description</label>
		
			<select name="itemr" id="itemr" style="width:250px;" class="rou" required>
				<option value="" disabled selected hidden>Please Select...</option>
			</select> 
			<button type="button" class="btn bg-gradient-primary text-white mr-2 editinv" data-toggle="tooltip" data-placement="bottom" title="Add Inventory" style="background-color:#b134eb ;"><i class="fas fa-cart-arrow-down"></i></button>			
		</div>
		<div class="form-group">
			<label style="width:150px;" for="item">Product Type</label>
			<select style="width:250px;" class="rou" id="ptype" name="ptype" required>
			<option value="" disabled selected hidden>Please Select...</option>
			<?php 
				$sql1="select * from productype";
				$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 bg-gradient-primary text-white mr-2 productype" data-toggle="tooltip" data-placement="bottom" title="Add product type" style="background-color:#b134eb ;"><i class="ace-icon fa fa-plus bigger-130"></i></button>-->
							
		</div>	
		<div class="form-group">
			 <label style="width:150px;" for="no">Waste Type :</label>
			 <input type="text" class="rounded" style="width:150px;"  id="wtype" name="wtype" disabled>
        </div>	
		
		<div class="form-group">
		    <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 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=4";
				 $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>	
		
	  <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" id="issendfromsingleedit" readonly>
	<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>
            <input type="text" class="form-control" id="editfin_product" name="editfin_product" disabled>
        </div>	
		<div class="form-group">
		<label style="width:150px;" for="qty"><b>Old 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="wqtytype" name="wqtytype" 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="wasteqty" name="wasteqty">
		<div class="form-group" id="qty31" >
			<label style="width:150px;" for="qty"><b>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>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>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>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>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">
			 <label style="width:150px;" for="no"><b>Waste Type :</b></label>
              <select style="width:150px;" id="editwtype" name="editwtype" 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>

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

<!-- 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 inventory Modal -->
<div id="add_inv" 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 Inventory</h4>
		 <button type="button" onclick="clearForm('add_name')" class="close" data-dismiss="modal">&times;</button>
       
      </div>
      <div class="modal-body">
       
		<form name="add_name" id="add_name">
		
		<table class="table table-bordered" id="dynamic_field">
				
			<tr>
			<div class="form-group">
                <label  style="width:150px;"for="no"><b>Item Category</b></label>
            
				<select name="items" id="items" style="width:250px;" class="rou">
				<?php 
					$sql1="select * from 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>
					   
				<td><input type="text" name="name[]" id="name" placeholder="Enter Item Description" class="form-control name_list"></td>
				<td><button type="button" name="addp" id="addp" class="btn btn-success">Add More</button></td>
			</tr>
		
		</table>
			
		  <b id="mesgInv"></b>			
		  <input type="button" name="submit" id="submitInv" style="background-color:#FF6666" class="btn bg-gradient-primary text-white mr-2" value="Submit"/>
		  <button type="button" class="btn btn-danger" data-dismiss="modal" onclick="clearForm('add_name')">Close</button>
		</form> 

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

  </div>
</div>

<!-- Add 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 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 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>Final Product Name</th>
					<th>Quantity</th>	
					<th>Type</th>
					<th>Sent DateTime</th>
					<th style="display: none;">is single</th>
					<th style="display: none;">waste type</th>
					<th style="display: none;">qauntity type</th>
					<?php
						if(@$name == 'superadmin'){
					?>
					<th>Delete</th>
					<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 userid = '<?php echo @$userid; ?>';
	var t = $('#datatableid').DataTable( {
	rowReorder: true,
	"order": [[ 8, "desc" ]],
    columnDefs: [
        { orderable: true, targets: '_all' }
        
    ]
	} );

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


	$('#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();

		$('#issendfromsingle').val(data[11].trim());

		$('#adminId').val(data[0].trim());
		$('#item').val(data[3].trim());
		$('#revcdFinalProdct').val(data[4].trim());
		$('#quan').val(data[7].trim());
		$('#qty_typ').val(data[6].trim());

		$('#qty_typId').val(data[9].trim());
		$('#wtype').val(data[5].trim());
		
		changeInitialQtyType();
		
	});

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

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

		var wasteId = data[10].trim();

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

		var qtytypeId = data[9].trim();

		var tableEle = t4;

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

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

		var tableEle = t3;

		if(isSingle == '0'){
			url = '../controller/multiple_raw_material_controller.php';
		}*/
		
		url = '../controller/multiple_raw_material_controller.php';

		$.ajax( {
			url:url,
			type: 'POST',
			data: 'qtytypeId='+qtytypeId+'&type=viewF39&finalproductId='+finalproductId+'&wasteId='+wasteId,

			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.wasteqty/1000;
						}else{
							finalqty = item.wasteqty;
						}
						
						var rowNode = tableEle
						.row.add( [item.id,item.items,item.finalprodct,finalqty,item.wasteqt_type_name,item.senddttime,item.is_single,item.wasteId,item.qtytypeId,
						'<button type="button" class="btn btn-primary deletebtn" title="Delete"><i class="ace-icon fa fa-trash-o"></i></button>',
						'<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([6,7,8]).visible(false);

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

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

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

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

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

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

		var isSingle = t4.column(6).data()[rowIndex];

		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='+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{
		      	
		      	$('#issendfromsingleedit').val(isSingle);
		      	$('#editModal').modal('show');
				$('#tableModalS').modal('hide');
				$('#editadminId').val(data[0].trim());
				$('#edititem').val(data[1].trim());
				$('#editfin_product').val(data[2].trim());
				$('#oldqty').val(data[3].trim());
				$('#oldqtytype').html(data[4].trim());
				$('#editwtype').val(t4.column(7).data()[rowIndex]);

				$('#wqtytype').val(t4.column(8).data()[rowIndex]);
				
				changefuncEdit();
	      	  	
	      	  }
	        },
	        error: function(r) {
	          alert('Unable To Edit Product'); 
	        }
	      } );

		
	});

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

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

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

		var adminId = datatb[0].trim();
		if (confirm("Are You Sure?")) {
			var isSingle = t4.column(6).data()[rowIndex];

			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='+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=scrapDelete&adminId='+adminId+'&userId='+userid,
			        
			        success: function(data2){
			          console.log('Msg',data2);
			          if(data2.status == true){
			          	alert('Product deleted Successfully'); 
			          	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'); 
	        }
	      } );
		}
	});

	$("#adminForm").submit(function(e) {
    document.getElementById("adminSend").disabled=true;
    //prevent Default functionality
    e.preventDefault();
    //do your own request an handle the results
   	
	/*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);
	
	formData.append('type','scrapSend');
	formData.append('userId',userid);
	
	/*var sendqty = parseFloat($('#initialqty').val());
	console.log('sendqty data',sendqty);

	if(isNaN($('#initialqty').val()) || $('#initialqty').val() == ''){
		alert('Invalid or Empty Value in Input Quantity');
    document.getElementById("adminSend").disabled=false;
	}else if(sendqty > totalqt){
    	alert('Send 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{*/
    	var url = '';
    	/*if($('#issendfromsingle').val() == '1'){
    		url = '../controller/admin_controller.php';
    	}else{*/
    		url = '../controller/multiple_raw_material_controller.php';
    	/*}*/

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

	        success: function(data){
	          console.log('Msghi',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();
	        }
	      } );
	/*}*/
});

	$("#editForm").submit(function(e) {
    document.getElementById("updateProds").disabled=true;
    //prevent Default functionality
    e.preventDefault();
    //do your own request an handle the results
   	
   	var selectedValue = $('#wqtytype').val();
   	
    if(selectedValue == "1")
	{
		var kilogrm = (parseInt($('#eko').val()) * 1000)+parseInt($('#egr').val());
		$('#wasteqty').val(kilogrm);
	}else if(selectedValue == "3")
	{
		var milliltr = (parseInt($('#elitr').val()) * 1000)+parseInt($('#emltr').val());
		$('#wasteqty').val(milliltr);
	}else if(selectedValue == "2")
	{
		$('#wasteqty').val($('#epiec').val());
	}else if(selectedValue == "4")
	{
		$('#wasteqty').val($('#emtr').val());
		
	}else if(selectedValue == "5")
	{
		$('#wasteqty').val($('#eft').val());
	}
	
	var formData = new FormData(this);
	
	formData.append('type','scrapEdit');
	formData.append('userId',userid);
		var url = '';
    	if($('#issendfromsingleedit').val() == '1'){
    		url = '../controller/admin_controller.php';
    	}else{
    		url = '../controller/multiple_raw_material_controller.php';
    	}
    $.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();
        }
    } );
	
});

	$('#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
					}));
					
					$('#add_dptbtn').modal('hide');
					clearForm('dpt_form');
					$('#single_send').modal('show');

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

	$('.editinv').on('click',function() {
		$('#add_inv').modal('show');

		$('#item_category option').each(function() {
		    if($(this).is(':selected')){
		    	$('#items').val($('#item_category').val());
		    }
		});
	});

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

	$('#add_product').click(function(){
		var data=$('#user_form').serialize()+'&type=add_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);
					$('#item_category').append($('<option>', {
					    value: response.rowId,
					    text: productnm
					}));
					$('#items').append($('<option>', {
					    value: response.rowId,
					    text: productnm
					}));

					$('#ad_product').modal('hide');
					clearForm('user_form');

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

	addMoreInv();
});

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 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 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 changefuncEdit(){
	var selectedValue = $('#wqtytype').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 departmentCheck(){
  var x = document.getElementById("dpt").value;

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

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 getItemDesctp(value){
$.ajax( {
    url: '../controller/purchase_controller.php',
    type: 'POST',
    data: 'value='+value+'&type=getItemDesp',

    success: function(data){
      console.log('Msg',data);
      if(data.status == true){
      	  $('#itemr').empty();
          $('#itemr').append(data.data);
  	  }else{
  	  	$('#itemr').empty();
  	  }
    },
    error: function(r) {
      console.log(r);
      $('#itemr').empty();
    }
  } );
}

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

	if(formname == 'user_form'){
		$('#mesg').text('');
		$('#add_product').show();
	}else if(formname == 'add_name'){
		$('#mesgInv').text('');
		$("#dynamic_field").find("tr:gt(0)").remove();
	}
}

function addMoreInv(){
	var i=1;  
	$('#addp').click(function(){
		i++;
		$('#dynamic_field').append('<tr id="row'+i+'"><td><input type="text" name="name[]" id="name" placeholder="Enter Item Description" class="form-control name_list"></td><td><button name="remove" id="'+i+'" class="btn btn-danger btn_remove">X</button></td></tr>');
	});
	$(document).on('click','.btn_remove',function(){
		var button_id=$(this).attr("id");
		$("#row"+button_id+"").remove();
	});
	
	$('#submitInv').click(function(){

		$.ajax({
			url:"../controller/purchase_controller.php",
			method:"POST",
			data:$('#add_name').serialize()+'&type=validateInventory',
			success:function(data)
			{
				if(data.status == true){
					$.ajax({
						url:"../controller/purchase_controller.php",
						method:"POST",
						data:$('#add_name').serialize()+'&type=addInventory',
						success:function(data)
						{
							alert(data);
							$('#add_name')[0].reset();
							$("#dynamic_field").find("tr:gt(0)").remove();
							$('#add_inv').modal('hide');
							getItemDesctp($("#item_category").val());
							
						}
					});
				}else{
					$('#mesgInv').text(data.msg);
				}
				
			}
		});
		
	});
}

</script>
</html>