<?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;
}

// Check if there is already a row in the company table
$query = "SELECT * FROM company";
$result = mysqli_query($conn, $query);
$num_rows = mysqli_num_rows($result);

if ($num_rows > 0) {
  // If there is already a row in the table, display the data in a table with an "Edit" button
  $row = mysqli_fetch_assoc($result);
  ?>
  <main>
  <h2>Company Information</h2>
  <table>
    <tr>
      <th>Co. Reg. No.</th>
      <th>Company Name</th>
      <th>Address 1</th>
      <th>Address 2</th>
      <th>Edit</th>
    </tr>
    <tr>
      <td><?php echo $row['coregno']; ?></td>
      <td><?php echo $row['coname']; ?></td>
      <td><?php echo $row['add1']; ?></td>
      <td><?php echo $row['add2']; ?></td>
      <td><a href="editcompany.php?coregno=<?php echo $row['coregno']; ?>">Edit</a></td>
    </tr>
  </table>
</main>
  <?php
} else {
  // If there is no row in the table, display a message and an "Add" button
  ?>
  <main>
  <h2>No data found in company table</h2>
  <p>Please click the button below to add data.</p>
  <button>Add Company</button>
</main>
  <?php
}

// Include footer file
include 'footer.php';
?>
