  <?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;
  }
  ?>


<main>

<head>
  <meta charset="UTF-8">
  <title>Invoice List</title>
  <link rel="stylesheet" href="style.css">
</head>

  <div class="container">
    <h2>Senarai Invois</h2>
    <form method="post" action="">
      <div class="form-group">
        <div>
          <label for="clientregno">No Pendaftaran Pelanggan:</label>
          <select id="clientregno" name="clientregno">
            <?php
        // Retrieve list of available account numbers
            $query = "SELECT clientregno, custname FROM customer";
            $result = mysqli_query($conn, $query);
            while ($row = mysqli_fetch_assoc($result)) {
              echo "<option value='" . $row['clientregno'] . "'>" . $row['custname'] . "</option>";
            }
            ?>
          </select>
        </div>
      </div>
       <div>
        <input type="submit" name="submit" value="Senaraikan">
      </div>
    </form>
  </div>







<body>

<?php
  // Query the database for the list of clientregno
  $query = "SELECT DISTINCT clientregno FROM invhead ORDER BY clientregno ASC";
  $result = mysqli_query($conn, $query);

  // Check if the form has been submitted
  if (isset($_POST['submit'])) {
    $clientregno = $_POST['clientregno'];

$query01 = "SELECT custname FROM customer WHERE clientregno = '$clientregno'";
$result01 = mysqli_query($conn, $query01);
if ($result01) {
    $row = mysqli_fetch_assoc($result01);
    $custname = $row['custname'];
}
    
    // Query the database for the list of invoices
    $query = "SELECT invno, invduedate, totamt, totrec FROM invhead WHERE clientregno = '$clientregno' ORDER BY invduedate ASC";
    $result = mysqli_query($conn, $query);

    // Check if any invoices are found
    if (mysqli_num_rows($result) > 0) {
      // Display the invoices in a table
      echo "<h2>Invoice List for ClientRegNo: $custname</h2>";
      echo "<table>";
      echo "<tr><th>Invoice No.</th><th>Invoice Due Date</th><th>Total Amount</th><th>Total Received</th><th>Status</th></tr>";
      $owing = 0;
      while ($row = mysqli_fetch_assoc($result)) {
        echo "<tr>";
        echo "<td>" . $row['invno'] . "</td>";
        echo "<td>" . $row['invduedate'] . "</td>";
        echo "<td>" . number_format($row['totamt'], 2, '.', ',') . "</td>";
        echo "<td>" . $row['totrec'] . "</td>";
        if ($row['totamt'] - $row['totrec'] == 0) {
          echo "<td>Closed</td>";
        } else {
          echo "<td>Open</td>";
          $owing += $row['totamt'] - $row['totrec'];
        }
        echo "</tr>";
      }
      echo "</table>";
      echo "<p>Amount owing by $custname: " . number_format($owing, 2, '.', ',') ."</p>";
    } else {
      // If no invoices are found, display a message
      echo "<p>No invoices found for ClientRegNo: $custname</p>";
    }
  }
?>

</body>

</main>
