<?php
session_start();
include("header.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;
}

// Retrieve the available pono values from the sup_invhead table
$sql = "SELECT pono FROM sup_invhead ORDER BY pono ASC";
$result = $conn->query($sql);

// Check if there are any pono values available
if ($result->num_rows > 0) {
  // Display the form to select an pono
  ?>
  <main>
    <form method="post">
      <label for="pono">Pilih No PO:</label>
      <select name="pono" id="pono">
        <?php
        // Display the available pono values as options in the dropdown menu
        while ($row = $result->fetch_assoc()) {
          echo '<option value="' . $row['pono'] . '">' . $row['pono'] . '</option>';
        }
        ?>
      </select>
      <button type="submit">View PO</button>
    </form>
  </main>
  <?php
} else {
  echo "No results found.";
}

// Check if a specific pono has been selected
if (isset($_POST['pono'])) {
  // Retrieve the data for the selected pono from the sup_invhead and sup_invdet tables
  $pono = $_POST['pono'];
  $sql = "SELECT sup_invhead.pono, sup_invhead.supregno, sup_invhead.cfno, sup_invhead.jobno, sup_invhead.podate, sup_invhead.poduedate, sup_invhead.taxamt AS totaltax, sup_invhead.totamt, sup_invhead.totpay, sup_invdet.expcode, sup_invdet.detail, sup_invdet.amount, sup_invdet.tax, sup_invdet.taxamt 
  FROM sup_invhead 
  LEFT JOIN sup_invdet ON sup_invhead.pono = sup_invdet.pono 
  WHERE sup_invhead.pono = '$pono'
  ORDER BY sup_invhead.pono ASC";
  $result = $conn->query($sql);


  // Display the sup_invhead data for the selected invoice
  if ($result->num_rows > 0) {
    $row = $result->fetch_assoc();
    ?>
    <main>
      <h2>No PO: <?php echo $row['pono']; ?></h2>
      <table>
        <tr>
          <td>Supplier Reg No:</td>
          <td><?php echo $row['supregno']; ?></td>
        </tr>
        <tr>
          <td>CF No:</td>
          <td><?php echo $row['cfno']; ?></td>
        </tr>
        <tr>
          <td>Job No:</td>
          <td><?php echo $row['jobno']; ?></td>
        </tr>        
        <tr>
          <td>Inv Date:</td>
          <td><?php echo $row['podate']; ?></td>
        </tr>
        <tr>
          <td>Inv Due Date:</td>
          <td><?php echo $row['poduedate']; ?></td>
        </tr>
        <tr>
          <td>Tax Amt:</td>
          <td><?php echo number_format($row['totaltax'], 2, '.', ','); ?></td>

        </tr>
        <tr>
          <td>Tot Amt:</td>
          <td><?php echo number_format($row['totamt'], 2, '.', ','); ?></td>

        </tr>
        <tr>
          <td>Tot Pay:</td>
          <td><?php echo number_format($row['totrec'], 2, '.', ','); ?></td>

        </tr>
      </table>
      <h3>Invoice Details</h3>
      <table>
      <tr>
        <th>Code</th>
        <th>Detail</th>
        <th>Amount</th>
        <th>Tax</th>
        <th>Tax Amount</th>
      </tr>
      <?php
      // Display the sup_invdet data for the selected invoice
      $result->data_seek(0);
      while ($row = $result->fetch_assoc()) {
        ?>
        <tr>
          <td><?php echo $row['expcode']; ?></td>
          <td><?php echo $row['detail']; ?></td>
          <td><?php echo number_format($row['amount'], 2, '.', ','); ?></td>
          <td><input type="checkbox" name="tax[]" value="<?php echo $row['tax']; ?>"<?php if ($row['tax'] == 1) echo "checked"; ?>></td>
          <td><?php echo number_format($row['taxamt'], 2, '.', ','); ?></td>

        </tr>
        <?php
      }
      ?>
    </table>
<style>
  .form-container {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 10px;
  }

  .form-container button {
    margin-left: 10px;
  }
</style>

<div class="form-container">


  <form method="post" onsubmit="return confirmDelete()">
    <input type="hidden" name="pono" value="<?php echo $pono; ?>">
    <button type="submit" name="delete">Delete This Invoice</button>
  </form>

  <button type="submit" name="print" onclick="confirmPrint()">Print</button>
</div>
<script>
function confirmDelete() {
  return confirm("Are you sure you want to delete this invoice no <?php echo $pono; ?>?");
}

function confirmPrint() {
  if (confirm('Are you sure you want to print invoice <?php echo $pono; ?>?')) {
    window.open('/TCPDF/examples/purchase_order.php?pono=<?php echo $pono; ?>', '_blank');
  }
}

</script>
  </main>
  <?php
  } else {
    echo "No results found.";
  }
}
if (isset($_POST['delete'])) {
  // Get the selected pono
  $pono = $_POST['pono'];

  // DELETE data FROM sup_invhead table
  $sql = "DELETE FROM sup_invhead WHERE pono = '$pono'";
  
  $result = $conn->query($sql);

    // Check for errors
  if (!$result) {
    echo '<script>alert("Error deleting payment header.")</script>';
    exit;
  }

  // Insert data into sup_invdet table
  $sql = "DELETE FROM sup_invdet WHERE pono = '$pono'";
  $result = $conn->query($sql);

  // Check if the data was successfully inserted into both tables
  if ($result) {
    
    echo '<script>alert("Data DELETED successfully.")</script>';
    header("Refresh:0");
    exit();
  } else {
    echo "Error: " . $sql . "<br>" . $conn->error;
  }
}

if (isset($_POST['print'])) {
  // Get the selected pono
  $pono = $_POST['pono'];

  // Redirect to the print.php file, passing the pono as a parameter in the URL
  header("Location: TCPDF/examples/purchase_order.php?pono='$pono'");
  exit;
}








// Include footer file
include 'footer.php';

?>