<h3>Payment History</h3>
<hr>

<style>
table {
  font-family: arial, sans-serif;
  border-collapse: collapse;
  width: 100%;
}

td, th {
  border: 1px solid #dddddd;
  text-align: left;
  padding: 8px;
}

tr:nth-child(even) {
  background-color: #dddddd;
}
</style>

<table class="table table-stripped">

<thead style="background-color:#218838;color:#ffffff;font-size:13px;">
  <!-- <th>S/N</th> -->
  <th>File Name</th>
  <th>Uploaded by </th>
</thead>

<?php

// $sql="SELECT * FROM payment WHERE sname = '$sname'";
// $result = $db-> query($sql);
// if ($result-> num_rows > 0) {
//   while ($row = $result-> fetch_assoc()){
//     echo "<tr><td>".$row["id"]. "</td><td>".$row["pay"]."</td><td>". $row["paymentfiles"]. "</td></tr>";
//   }
//   echo"</table>";
// }
// else{
//   echo"0 result";
// }
// $db-> close();



$payments = mysqli_query($db, "SELECT * FROM payment WHERE sname = '$sname'") ;

if(mysqli_fetch_array($payments)){

    //use foreach to echo the datas
    foreach($payments as $payment){
   
?>
<tr>
<!-- <td><?php echo $payment['payment_id']; ?></td> -->
<td><?php echo $payment['pay']; ?></td>
<td><?php echo $payment['sname']; ?></td>

  <!-- <td><a href="index.php?env=payment-history?payment_id=<?php echo $payment['payment_id']; ?>" target="_blank">Download</a></td> -->
</tr>
<?php

    }
}

if(isset($_GET['payment_id'])) {
  $id = $_GET['payment_id'];

  // fetch file to download from database
  $sql = "SELECT * FROM payment WHERE payment_id=$id";
  $result = mysqli_query($db, $sql);

  $file = mysqli_fetch_assoc($result);
  $filepath = 'upload/' . $file['paymentfiles'];

  if(file_exists($filepath)) {
      header('Content-Description: File Transfer');
      header('Content-Type: application/pdf');
      header('Content-Disposition: inline; filename=' . basename($filepath));
      header('Content-Transfer-Encoding: binary'); 
      header('Expires: 0');
      header('Cache-Control: must-revalidate');
      header('Pragma: public');
      header('Content-Length: ' . filesize('upload/' . $file['paymentfiles']));
      readfile('upload/' . $file['paymentfiles']);

      // Now update downloads count
      $newCount = $file['downloads'] + 1;
      $updateQuery = "UPDATE payment SET downloads=$newCount WHERE payment_id=$id";
      mysqli_query($db, $updateQuery);
      exit;
  }

}
?>
</table>
                    <div class="row" style="margin-top:50px;">
                        <div class="col-md-6">
                            <a href="index.php?env=fees" style="color:#ffffff;text-decoration:none;"> 
                                <button type="button" class="btn btn-primary btn-block"><i class="fa fa-arrow-left"></i> Go back</button>
                            </a>
                        </div>
                        <div class="col-md-6">
                        
                        </div>
                    </div>






<?php
if(isset($_GET['payment_id'])) {
    $id = $_GET['payment_id'];
 
    // fetch file to download from database
    $sql = "SELECT * FROM payment WHERE payment_id=$id";
    $result = mysqli_query($db, $sql);
 
    $file = mysqli_fetch_assoc($result);
    $filepath = 'upload/' . $file['paymentfiles'];
 
    if(file_exists($filepath)) {
        header('Content-Description: File Transfer');
        header('Content-Type: application/pdf');
        header('Content-Disposition: inline; filename=' . basename($filepath));
        header('Content-Transfer-Encoding: binary'); 
        header('Expires: 0');
        header('Cache-Control: must-revalidate');
        header('Pragma: public');
        header('Content-Length: ' . filesize('upload/' . $file['paymentfiles']));
        readfile('upload/' . $file['paymentfiles']);
 
        // Now update downloads count
        $newCount = $file['downloads'] + 1;
        $updateQuery = "UPDATE payment SET downloads=$newCount WHERE payment_id=$id";
        mysqli_query($db, $updateQuery);
        exit;
    }
 
 }


?>