<?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['btnsave']))
	{
		
		$UserId = $_POST['UserId'];
		$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(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 (user_id,rooms,ast_cat,details,quantity,price,total_price,buy_from,entry_date) VALUES(:UserId,:Rooms,:CatName,:AstDetails,:Quantity,:Price,:TotalPrice,:BuyFrom,:Date)');
			
			
			$stmt->bindParam(':UserId',$UserId);
			$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);
			
			if($stmt->execute())
			{
				?>
              <script>
				alert('Asset Data Successfully Add ...');
				window.location.href='asset-list';
				</script>
          <?php	
		  
		  }
			else
			{
				$errMSG = "error while inserting....";
			}
		}
	}
?>
<?php  require_once 'sidebar.php'; ?>

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

    <div class="pagetitle bg-light rounded p-2"> 
	  <h1>New Asset  | <span> <a href="asset-list">   <i class="bi bi-table"></i> </a> </span> </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" >
	    
	<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="Rooms" name="Rooms" required>
		<option value="" required>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">Category Name </label>  </td>
        <td> 
		<select style="width:100%;" class="form-control chosen-select" Id="CatName" name="CatName" required>
		<option value="" required>Select Category</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" placeholder="Details" required></td>
    </tr>
	
	<tr>
    	<td><label class="control-label"> Quantity</label></td>
		<td><input class="form-control" type="text" name="Quantity" id="Quantity" placeholder="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" placeholder="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" placeholder="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="Na" 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>
	
	    
    </table>
	
	<tr>
        <td colspan="2"><button type="submit" name="btnsave" class="btn btn-primary">
        <span class="bi bi-check-circle"></span>  &nbsp; Save
        </button>
        </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>