<?php
session_start();
include("header.php");
include("footer.php");
include("nav.php");
include("connect.php");

// Check if the user is logged in
if (!isset($_SESSION['emailadd'])) {
  header("Location: index.php");
  exit;
}
include 'function.php';

// Check if the user has permission to view this page
if (!user_has_permission('clerk')) {
  header("Location: main.php");
  exit;
}


?>

<main>
  <h2>Sejarah Pembelian Mengikut Kod</h2>
  <form method="post" action="show_select_expcode.php">
    <div>
      <label for="expcode">Kod Pembelian :</label>
    <select id="expcode" name="expcode">
            <?php
        // Retrieve list of available account numbers
            $query = "SELECT expcode, detail FROM exptype";
            $result = mysqli_query($conn, $query);
            while ($row = mysqli_fetch_assoc($result)) {
              echo "<option value='" . $row['expcode'] . "'>" . $row['detail'] . "</option>";
            }
            ?>
          </select>
    </div>

    <button type="submit">Submit</button>

  </form>



<?php





if ($_SERVER['REQUEST_METHOD'] == 'POST') {
  $expcode = $_POST['expcode'];

// Query the database for the required data
$sql = "SELECT sup_invhead.supregno, sup_invhead.pono, sup_invhead.podate, sup_invhead.cfno
        FROM sup_invhead
        JOIN sup_invdet ON sup_invhead.pono = sup_invdet.pono
        WHERE sup_invdet.expcode = '$expcode'";

$result = mysqli_query($conn, $sql);

// Display the data in an HTML table
echo "<table>
        <tr>
            <th>Supplier Registration Number</th>
            <th>Purchase Order Number</th>
            <th>Purchase Order Date</th>
            <th>CF Number</th>
        </tr>";

while ($row = mysqli_fetch_assoc($result)) {
  echo "<tr>
            <td>".$row['supregno']."</td>
            <td>".$row['pono']."</td>
            <td>".$row['podate']."</td>
            <td>".$row['cfno']."</td>
        </tr>";
}

echo "</table>";


}
?>


</main>