<?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 product 		
		 
		WHERE product.product_id=:uid');
		$stmt_edit->execute(array(':uid'=>$id));
		$edit_row = $stmt_edit->fetch(PDO::FETCH_ASSOC);
		extract($edit_row);
	}
	else
	{
		header("Location: product-list");
	}

	if(isset($_POST['btn_save_updates']))
	{
		$ProductName = $_POST['ProductName'];
		$Details = $_POST['Details'];
		$BuyRate = $_POST['BuyRate']; 
		$ReatilSaleRate = $_POST['ReatilSaleRate'];
		$WholSaleRate = $_POST['WholSaleRate'];  
		
		// if no error occured, continue ....
		if(!isset($errMSG))
		{
			$stmt = $DB_con->prepare('UPDATE product 
									   SET   product_name=:ProductName,
										     product_details=:Details, 
										     buy_rate=:BuyRate, 
										     retail_rate=:ReatilSaleRate,
										     rate=:WholSaleRate 
											 
								       WHERE  product_id = :uid');
			
			$stmt->bindParam(':ProductName',$ProductName);
			$stmt->bindParam(':Details',$Details);
			$stmt->bindParam(':BuyRate',$BuyRate); 
			$stmt->bindParam(':ReatilSaleRate',$ReatilSaleRate); 
			$stmt->bindParam(':WholSaleRate',$WholSaleRate);  
			$stmt->bindParam(':uid',$id);
			
			if($stmt->execute()){
				?>
                <script>
				alert('Update Successful ...');
				window.location.href='courses-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-pencil"></i> Course Edit / Update |  <span> <a href="courses-list">    <i class="bi bi-table"></i> </a> </span> </h1>
    </div> 
	
    <section class="section"> 
      <div class="row">
        <div class="col-lg-8">

          <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">Course Name  </label></td>
     <td><input class="form-control" type="text" id="ProductName" name="ProductName" value="<?php echo $product_name; ?>"  required></td>
  </tr>
  
    <tr>
    	<td><label class="control-label">Duration</label></td>
        <td>
       <input class="form-control" type="text" id="Details"  name="Details"   value="<?php echo $product_details; ?>"  required></td>
    </tr>
	
   
	
	<tr>
    	<td><label class="control-label"> Cost Price</label></td>
        <td><input class="form-control" type="text" id="BuyRate" name="BuyRate" value="<?php echo $buy_rate; ?>" required></td>
    </tr>
	
	<tr>
    	<td><label class="control-label">Sale   Price</label></td>
        <td> <input class="form-control" type="text" id="ReatilSaleRate"  name="ReatilSaleRate" value="<?php echo $retail_rate; ?>"  oninput="myFunction()" required></td>
    </tr>
	
	 
	
	
	<script>
	/*
  function myFunction() {
    var x = Number (document.getElementById("ReatilSaleRate").value);
    var x1 = Number (document.getElementById("ReatilSaleRate").value);
    var y = Number (document.getElementById("Discount").value);
	
	
    var z = Number (x * y / 100);
    var z1 = Number (x1-z);

    document.getElementById("BuyRate").value = z1; 
	                  }  */
  </script>
		
	 <tr style="display:none;">
    	<td><label class="control-label">Whole Sale Rate</label></td>
        <td><input class="form-control" type="text" id="WholSaleRate"  name="WholSaleRate" value="<?php echo $rate; ?>"   oninput="myFunctionTP()" required></td>
    </tr>
	 

  <tr style="display:none;">
    	<td><label class="control-label">Photo </label></td>
        <td><input class="input-group" type="file" name="user_image" accept="image/*" /></td>
    </tr>
    
	<tr style="display:none;">
    	<td><label class="control-label">Date</label></td>
        <td><input class="form-control" type="text" name="Date" placeholder="Date" value="<?php echo date("Y/m/d") ;?>" /></td>
    </tr>
	
    
    </table>
	<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="courses-list"> <span class="bi bi-arrow-left-circle"></span> Back </a>
    
</form>
			  
			  
            </div>
          </div>

          
        </div>

       
    </section>

  </main> 
  



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

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