<?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($_GET['update']) && !empty($_GET['update']))
	{
		$id = $_GET['update'];
		$stmt_edit = $DB_con->prepare('SELECT * FROM assets 
		left join asset_cat on asset_cat.ast_id=assets.ast_cat
		left join asset_rooms on asset_rooms.rom_id=assets.rooms
		WHERE assets.id =:uid');
		$stmt_edit->execute(array(':uid'=>$id));
		$edit_row = $stmt_edit->fetch(PDO::FETCH_ASSOC);
		extract($edit_row);
	}
	else
	{
		header("Location: asset-list");
	}
	
	
	
	if(isset($_POST['btn_save_updates']))
	{
		
		$Rooms = $_POST['Rooms'];
		$CatName = $_POST['CatName'];
		$AstDetails = $_POST['AstDetails'];
		$Quantity = $_POST['Quantity'];
		$Price = $_POST['Price'];
		$TotalPrice = $_POST['TotalPrice'];
		$BuyFrom = $_POST['BuyFrom'];
		$Date = $_POST['Date'];
		
		// if no error occured, continue ....
		if(!isset($errMSG))
		{
			$stmt = $DB_con->prepare('UPDATE assets 
									     SET 
										     rooms=:Rooms, 
										     ast_cat=:CatName, 
										     details=:AstDetails, 
										     quantity=:Quantity, 
										     price=:Price, 
										     total_price=:TotalPrice, 
										     buy_from=:BuyFrom,
										     entry_date=:Date 
											 
								       WHERE  assets.id = :uid');
			
		
			
			
			$stmt->bindParam(':Rooms',$Rooms);
			$stmt->bindParam(':CatName',$CatName);
			$stmt->bindParam(':AstDetails',$AstDetails);
			$stmt->bindParam(':Quantity',$Quantity);
			$stmt->bindParam(':Price',$Price);
			$stmt->bindParam(':TotalPrice',$TotalPrice);
			$stmt->bindParam(':BuyFrom',$BuyFrom);
			$stmt->bindParam(':Date',$Date);
			
			$stmt->bindParam(':uid',$id);
			
			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>Assets Edit / Update</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>
    	<td><label class="control-label">Rooms Name </label>  </td>
        <td> 
		<select style="width:100%;" class="form-select chosen-select1" Id="Rooms" name="Rooms" required>
		<option value="<?php echo $rooms; ?>"><?php echo $room_name; ?></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">Category Name </label>  </td>
        <td> 
		<select style="width:100%;" class="form-control chosen-select" Id="CatName" name="CatName" required>
		<option value="<?php echo $ast_cat; ?>"><?php echo $ast_name; ?></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"> Details</label></td>
		<td><input class="form-control" type="text" name="AstDetails" value="<?php echo $details ;?>" required></td>
    </tr>
	
	<tr>
    	<td><label class="control-label"> Quantity</label></td>
		<td><input class="form-control" type="text" name="Quantity"  id="Quantity" value="<?php echo $quantity ;?>" oninput="myFunction()"  required> </td>
    </tr>
	
		
	<tr>
    	<td><label class="control-label">Price </label></td>
        <td><input class="form-control" type="text" name="Price"  id="Price" value="<?php echo $price ;?>" oninput="myFunction()" required></td>
    </tr>
	
	<tr>
    	<td><label class="control-label">Total Price </label></td>
        <td><input class="form-control" type="text" name="TotalPrice" id="TotalPrice"  value="<?php echo $total_price ;?>" required></td>
    </tr>
	
	<script>
  function myFunction() {
    var x = Number (document.getElementById("Quantity").value);
    var y = Number (document.getElementById("Price").value);
	
	
    var z = Number (x * y);
 
    document.getElementById("TotalPrice").value = z;
	                  }
  </script>
	
	
	<tr>
    	<td><label class="control-label">Buy From </label></td>
		<td><input class="form-control" type="text" name="BuyFrom" placeholder="Buy From" value="<?php echo $buy_from ;?>" required></td>
    </tr>
	
	<tr>
    	<td><label class="control-label">Date </label></td>
		<td><input class="form-control" type="text" name="Date"  value="<?php echo $entry_date ;?>" ></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="glyphicon glyphicon-save"></span> Update
        </button>
        
        <a class="btn btn-danger" href="asset-list"> <span class="glyphicon glyphicon-backward"></span> Cancel </a>
        
        </td>
    </tr>
    
</form>
			  
			  
            </div>
          </div>

          
        </div>

       
    </section>

  </main> 

    <?php  require_once 'footer1.php'; ?>
	
<script src = "js/chosen.js"></script>
<script type = "text/javascript">
	$('.chosen-select').chosen({width: "100%"});
</script>