<!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>Scrap</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"></th>
					<?php
						if(@$name == 'superadmin'){
					?>
					<th>Delete</th>
					<th>Edit</th>
					<?php
						}
					?>
				</tr>
			</thead>
			<tbody>
			<?php 
				$sql = "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,adm.storageId,frmd.depart as frmdpt,
				adm.final_product as finalproductId,adm.water_wt ,adm.waste as wasteId, adm.id,adm.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 adm,final_product fp ,quantity_type qt, department frmd 
				where adm.final_product = fp.id and adm.wtype = qt.id and adm.send_from = frmd.id and adm.wasteqty !=0) as m";
				$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['id']?></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 name from waster_type p where id =".$row['wasteId'].";";
				   $res=mysqli_query($con,$sql1);
				   while($row1 = mysqli_fetch_array($res))
				   {
					   echo $row1['name'];
				   } 
					?></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>
						<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-edit"></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">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 not in (13)";
				 $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>
</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' }
        
    ]
	} );


	$('#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','.editbtn',function() {
		
		$tr = $(this).closest('tr');

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

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

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

		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');
				$('#editadminId').val(data[0].trim());
				$('#edititem').val(data[3].trim());
				$('#editfin_product').val(data[4].trim());
				$('#oldqty').val(data[7].trim());
				$('#oldqtytype').html(data[6].trim());
				$('#editwtype').val(data[10].trim());

				$('#editsendqty_typ').val(data[9].trim());
				
				changefuncEdit();
	      	  	
	      	  }
	        },
	        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[11].trim();

			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);
				}
			}
		});
	});
});

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";
}
</script>
</html>