<?php session_start();
?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
  <title>Genealogy Tree</title>
  <link rel="stylesheet" href="assets/styles/style.css">
  <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>
 <img src="php/skybrand.png" width="250px">
  <div class="body genealogy-body genealogy-scroll">
   <form method="GET" action="">
  <label>Search ID:</label>
  <input type="number" name="parent_id" required>
  <button type="submit">Search</button>
</form>

    <div id="genealogy-tree-container">
    <p><?php
if (isset($_SESSION['total_completed_levels'])) {
    $adjustedLevels = max(0, $_SESSION['total_completed_levels']); // ensures it doesn't go below 0
    echo '<h4>Total Completed Levels: ' . $adjustedLevels . '</h4>';
}
?>
</p>
  <?php include("php/load_tree.php"); ?>
</div>

  </div>
  <script src="assets/script/tree.js"></script>
  <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
  $('#add-member-form').on('submit', function(e) {
    e.preventDefault();

    $.ajax({
      type: 'POST',
      url: 'php/add_member.php',
      data: $(this).serialize(),
      success: function(response) {
        $('#message-box').html(response);

        // Reload the genealogy tree HTML
        $('#genealogy-tree-container').load('php/load_tree.php');
      },
      error: function() {
        $('#message-box').html('<span style="color:red;">Error adding member.</span>');
      }
    });
  });
    
document.getElementById('searchForm').addEventListener('submit', function(e) {
    e.preventDefault();
    const id = document.getElementById('search_id').value;
    
    fetch('php/load_tree.php?parent_id=' + id)
        .then(res => res.text())
        .then(html => {
            document.getElementById('genealogy-tree-container').innerHTML = html;
        })
        .catch(err => console.error('Error loading tree:', err));
});
    
</script>

</body>
</html>