<?php ob_start(); ?>
<!DOCTYPE html>
<html lang="en"> 

<head>
<?php   require_once 'head_link.php'; ?> 

</head>

<body>
 
<?php   require_once 'header1.php'; ?> 

<?php
 
	if(isset($_POST['btn_save_updates']))
	{
		
		$UserId = $_POST['UserId'];
		$Rooms = $_POST['Rooms'];
		$CatName = $_POST['CatName'];
		$AstDetails = $_POST['AstDetails'];
		$BuyQty = $_POST['BuyQty'];
		$DamQty = $_POST['DamQty'];
		$Price = $_POST['Price'];
		$Causes = $_POST['Causes'];
		$Date = $_POST['Date'];
		
		
		if(empty($CatName)){
			$errMSG = "Please Enter  Name.";
		}
		else if(empty($Price)){
			$errMSG = "Please Enter  Quantity.";
		}
		
		
		
		// if no error occured, continue ....
		if(!isset($errMSG)) 
		{
			$stmt = $DB_con->prepare('INSERT INTO assets_details (user_id,rooms,ast_cat,details,buy_qty, quantity,price,causes,entry_date) VALUES(:UserId,:Rooms,:CatName,:AstDetails,:BuyQty,:DamQty,:Price,:Causes,:Date)');
			
			
			$stmt->bindParam(':UserId',$UserId);
			$stmt->bindParam(':Rooms',$Rooms);
			$stmt->bindParam(':CatName',$CatName);
			$stmt->bindParam(':AstDetails',$AstDetails);
			$stmt->bindParam(':BuyQty',$BuyQty);
			$stmt->bindParam(':DamQty',$DamQty);
			$stmt->bindParam(':Price',$Price);
			$stmt->bindParam(':Causes',$Causes);
			$stmt->bindParam(':Date',$Date);
			
			if($stmt->execute())
			{
				?>
              <script>
				 
				</script>
          <?php	
		  
		  }
			else
			{
				$errMSG = "error while inserting....";
			}
		}
	}
?>

<?php 
	
	if(isset($_POST['btn_save_updates']))
	{
		
		 
		$CatName = $_POST['CatName']; 
		$RecQty = $_POST['RecQty'];
		
		// if no error occured, continue ....
		if(!isset($errMSG))
		{
			$stmt = $DB_con->prepare('UPDATE assets 
									     SET  quantity=:RecQty
											 
								       WHERE  ast_cat = :CatName ');
			
		
			
			
			 
			$stmt->bindParam(':CatName',$CatName); 
			$stmt->bindParam(':RecQty',$RecQty); 
						
			if($stmt->execute()){
				?>
                <script>
				alert('Update Successful...');
				window.location.href='asset-list';
				</script>
                <?php
			}
			else{
				$errMSG = "Sorry Data Could Not Updated !";
			}
		
		}
		
						
	}
	
?>

<?php  require_once 'sidebar.php'; ?>

  <main id="main" class="main">

    <div class="pagetitle bg-light rounded p-2">
      <h1><i class="bi bi-box-seam"></i> Buy / Sale / Damage    Assets</h1>  
    </div> 
	
    <section class="section">
      <div class="row">
        <div class="col-lg-6">

          <div class="card">
            <div class="card-body">
              <h5 class="card-title">
			  <?php
	if(isset($errMSG)){
			?>
            <div class="alert alert-danger">
            	<span class="glyphicon glyphicon-info-sign"></span> <strong><?php echo $errMSG; ?></strong>
            </div>
            <?php
	}
	else if(isset($successMSG)){
		?>
        <div class="alert alert-success">
              <strong><span class="glyphicon glyphicon-info-sign"></span> <?php echo $successMSG; ?></strong>
        </div>
        <?php
	}
	?>  
			  
</h5>
			  

<form method="post" enctype="multipart/form-data" class="form-horizontal">
	
    
    <?php
	if(isset($errMSG)){
		?>
        <div class="alert alert-danger">
          <span class="glyphicon glyphicon-info-sign"></span> &nbsp; <?php echo $errMSG; ?>
        </div>
        <?php
	}
	?>
   
    
	<table class="table table-hover table-responsive">
	<tr>
    	
		<?php   
				   $pq=mysqli_query($con,"select * from user where userid='".$_SESSION['id']."'");
				   while($pqrow=mysqli_fetch_array($pq)){
				?>
        
        <input class="form-control" type="hidden" name="UserId"  value="<?php echo $pqrow['userid']; ?>" />
		<?php }?>
    </tr>
	 
	 
	 <tr>
    	<td><label class="control-label">Rooms Name </label>  </td>
        <td> 
		<select style="width:100%;" class="form-select chosen-select1" Id="RomId" name="RomId" >
		<option value="" >Select Room</option>
		
				      	<?php 
				      	$sql = "SELECT rom_id,room_name FROM asset_rooms where  user_id='".$_SESSION['id']."'";
								$result = $con->query($sql);

								while($row = $result->fetch_array()) {
									echo "<option value='".$row[0]."'>".$row[1]."</option>";
								} 
				      	?>
	   </select> 
     </tr>
	 
	<tr>
    	<td><label class="control-label">Assets Name </label>  </td>
        <td> 
		<select style="width:100%;" class="form-control chosen-select" Id="CatName" name="CatName" >
		<option value="" >Select Assets </option>		
				      	<?php 
				      	$sql = "SELECT ast_id,ast_name FROM asset_cat where  user_id='".$_SESSION['id']."'";
								$result = $con->query($sql);

								while($row = $result->fetch_array()) {
									echo "<option value='".$row[0]."'>".$row[1]."</option>";
								} 
				      	?>
	   </select> 
     </tr>
	
	<tr>
    	<td><label class="control-label"> Rooms</label></td>
		<td>
		   <input class="form-control" type="text" name="RoomsName" id="RoomsName"  readonly> 
		   <input class="form-control" type="hidden" name="Rooms" id="Rooms"  readonly>
		   <input class="form-control" type="hidden" name="RomId1" id="RomId1" value="2" readonly>
		</td>
    </tr>
	 
	
	<tr>
    	<td><label class="control-label"> Details</label></td>
		<td><input class="form-control" type="text" name="AstDetails"  id="AstDetails"   required readonly></td>
    </tr>
	
	<tr>
    	<td><label class="control-label"> Quantity</label></td>
		<td><input class="form-control" type="text" name="Quantity"  id="Quantity" oninput="myFunction()"  required readonly> </td>
    </tr>
	
	<tr>
    	<td><label class="control-label"> Buy Qty </label></td>
		<td><input class="form-control" type="text" name="BuyQty"  id="BuyQty"  value="0" oninput="myFunction()" required> </td>
    </tr>
	
	<tr>
    	<td><label class="control-label"> Damage / Sale Qty </label></td>
		<td><input class="form-control" type="text" name="DamQty"  id="DamQty" value="0" oninput="myFunction()" required> </td>
    </tr>
	
	<tr>
    	<td><label class="control-label"> Recent Quantity</label></td>
		<td><input class="form-control" type="text" name="RecQty"  id="RecQty"  required readonly> </td>
    </tr>	
	
	<script>
  function myFunction() {
    var x = Number (document.getElementById("Quantity").value);
    var x1 = Number (document.getElementById("BuyQty").value);
    var y = Number (document.getElementById("DamQty").value);
	
	
    var z = Number (x + x1 - y);

    document.getElementById("RecQty").value = z; 
	                  }
  </script>
	
		
	<tr>
    	<td><label class="control-label">Price </label></td>
        <td><input class="form-control" type="text" name="Price" id="Price"  required></td>
    </tr>
	
	
	<tr>
    	<td><label class="control-label">Comments </label></td>
		<td><input class="form-control" type="text" name="Causes" placeholder="Buy / Sale / Damage"  required></td>
    </tr>
	
	<tr>
    	<td><label class="control-label">Date </label></td>
		<td><input class="form-control" type="date" name="Date"  value="<?php date_default_timezone_set("Asia/Dhaka"); echo date("Y-m-d") ;?>"> </td>
    </tr>
    
	 <tr><td></td><td></td></tr>
    
    </table>
	
	<tr>
        <td colspan="2"><button type="submit" name="btn_save_updates" class="btn btn-primary">
        <span class="bi bi-check-circle"></span> Update
        </button>
        
        <a class="btn btn-danger" href="asset-dam-sale-list"> <span class="bi bi-arrow-left-circle"></span> Cancel </a>
        
        </td>
    </tr>
    
</form>
			  
			  
            </div>
          </div>

          
        </div>

       
    </section>

  </main> 

    <?php  require_once 'footer1.php'; ?>
	
	<script>
	$("#CatName").on("change", function(){
		
		var customerID = $("#CatName").val();
		if(customerID == "")
		{
			alert("Please enter a valid customer ID!");
		}
		else{
			// $.ajax({url: "ajax_c_load_assets.php?c=" + customerID, success: function(result){
			 $.ajax({url: "ajax_c_load_assets.php?r=" + $("#RomId").val() + " & c=" + customerID, success: function(result){
			var customer = JSON.parse(result);
				$("#RoomsName").val(customer.RomNam);
				$("#Rooms").val(customer.Room);
				$("#AstDetails").val(customer.Detail);
				$("#Quantity").val(customer.Qty);
				$("#Price").val(customer.Cost);
				 
			}});
		}
	});
</script>
	
<script src = "js/chosen.js"></script>
<script type = "text/javascript">
	$('.chosen-select').chosen({width: "100%"});
</script>