<?php
$output = '';
include("connectdb.php");

if(!isset($_GET['ltid'])){
  header('location: history');
}
$labtest_till_id = $_GET['ltid'];

if(isset($_POST['update'])){
  $test_id = mysqli_real_escape_string($con, $_POST['test_id']);
  $qty = mysqli_real_escape_string($con, $_POST['qty']);
  
  $sql = mysqli_query($con, "SELECT * from labtesttill WHERE labtesttill_id = $labtest_till_id ");
  $rhow = mysqli_fetch_array($sql);
  $payment_status = $rhow['payment_status'];
  
  if($payment_status == 0){
	  $query = mysqli_query($con, "UPDATE labtesttill SET test_id = '$test_id' , qty = '$qty' WHERE labtesttill_id = $labtest_till_id ");
	  if($query == true){
		 $output = '
					<div class="alert alert-success alert-dismissible fade in">
						<span class="glyphicon glyphicon-info-sign"></span> Successfully updated transaction 
					</div>
				  ';
	  }
  }else{
	 $output = '
	            <div class="alert alert-danger">
				    <span class="glyphicon glyphicon-info-sign"></span> You cannot update a paid transaction.
                </div>
	          ';   
  }
 
}

function testName($test_id){
	global $con;
	$query = mysqli_query($con, "SELECT * FROM labtest WHERE test_id = $test_id ");
	$row = mysqli_fetch_array($query);
	$test_name =  $row['test_name'];
	
	return $test_name;
}

?>
<?php include("side_nav.php"); ?>
<?php include("top_nav.php"); ?>


        <!-- page content -->
        <div class="right_col" role="main">
          <div class="">
            <div class="page-title">
              <div class="title_left">
                <h3>Update transactions</h3>
			  </div>
            </div>
            <div class="clearfix"></div>
            <div class="row">
              <div class="col-md-12 col-sm-12 col-xs-12">
                <div class="x_panel">
                  <div class="x_title">
                    <ul class="nav navbar-right panel_toolbox">
                      <li><a class="collapse-link"><i class="fa fa-chevron-up"></i></a>
                      </li>
                      <li class="dropdown">
                        <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false"><i class="fa fa-wrench"></i></a>
                        <ul class="dropdown-menu" role="menu">
                          <li><a href="#">Settings 1</a>
                          </li>
                          <li><a href="#">Settings 2</a>
                          </li>
                        </ul>
                      </li>
                      <li><a class="close-link"><i class="fa fa-close"></i></a>
                      </li>
                    </ul>
                    <div class="clearfix"></div>
                  </div>
                  <div class="x_content">  
				    <p class="text-muted font-13 m-b-30">
                      Once a transaction is paid for, it cannot be updated.
                    </p>
                    <br />
                    <form action='<?php echo "update-transaction?ltid=$labtest_till_id"; ?>' method="POST" class="form-horizontal form-label-left">
					    
						<?php $sql = mysqli_query($con, "SELECT * FROM labtesttill WHERE labtesttill_id = $labtest_till_id");
							$rhow = mysqli_fetch_array($sql);
						?>
						
						<div class="form-group">
							<div class="col-md-6 col-sm-6 col-xs-12 col-md-offset-3">
								  <?php echo $output;  ?>
							</div>
            	         </div>
							 
						<div class="form-group">
							<label class="control-label col-md-3 col-sm-3 col-xs-12">Name <span class="required">*</span>
							</label>
							<div class="col-md-6 col-sm-6 col-xs-12">
							  <select name="test_id" id="test_id" class="form-control col-md-7 col-xs-12 select2" >
								  <?php $sql = mysqli_query($con, "SELECT * FROM labtest");
									while($row = mysqli_fetch_array($sql)):
								  ?>
								  <option <?php if($rhow['test_id'] == $row['test_id']){echo 'selected';} ; ?> value="<?php echo $row['test_id'] ?>" ><?php echo testName($row['test_id']); ?></option>
								  <?php endwhile; ?>
							  </select>
							</div>
						</div>
						
						<div class="form-group">
							<label class="control-label col-md-3 col-sm-3 col-xs-12">Qty - Amount <span class="required">*</span>
							</label>
							<div class="col-md-3 col-sm-3 col-xs-12">
							  <input type="number" name="qty" id="qty" required="required" value="<?php echo $rhow['qty'] ?>" class="form-control col-md-7 col-xs-12" >
							</div>
							<div class="col-md-3 col-sm-3 col-xs-12">
							  <input type="text" name="to" required="required" value="<?php echo number_format($rhow['qty'] * $rhow['price']); ?>" class="form-control col-md-7 col-xs-12" disabled>
							</div>
						</div>
						  
						<div class="ln_solid"></div>
						<div class="form-group">
							<div class="col-md-6 col-sm-6 col-xs-12 col-md-offset-3">
							  <button type="submit" name="update" class="btn btn-primary">Update</button>
							</div>
						</div>
                    </form>
                  </div>
                </div>
              </div>
            </div>
			
<?php include("footer.php"); ?>         
<script>
    $('.select2').select2();
</script>

