<?php

// ADD DEBTOR
if (isset($_POST['debtors_btn'])) {
    $debtors_name = inject_checker($connection, ucwords($_POST['debtors_name']));
    $debtors_amount = inject_checker($connection, $_POST['debtors_amount']);
    $debtors_phone = inject_checker($connection, $_POST['debtors_phone']);
    $debtors_deadline = inject_checker($connection, $_POST['debtors_deadline']);

    if (empty($debtors_name) || empty($debtors_amount) || empty($debtors_phone) || empty($debtors_deadline)) {
        $_SESSION['msg'] = "No field should be empty.";
        $_SESSION['type'] = "danger";
    } else {
        $query = "INSERT INTO `debtors` (`name`, `amount`, `phone`, `deadline`) VALUES (?, ?, ?, ?)";
        $stmt = $connection->prepare($query);
        $stmt->bind_param("sdss", $debtors_name, $debtors_amount, $debtors_phone, $debtors_deadline);

        if ($stmt->execute()) {
            $_SESSION['msg'] = "Debtor details saved successfully.";
            $_SESSION['type'] = "success";
        } else {
            $_SESSION['msg'] = "Failed to save debtor details.";
            $_SESSION['type'] = "danger";
        }
        $stmt->close();
    }

    header("Location: manage_debtors");
    exit;
}

// DELETE DEBTOR
if (isset($_POST['debtors_delete_btn'])) {
    $debtors_hidden_id = $_POST['debtors_hidden_id'];
    $query = "DELETE FROM `debtors` WHERE `id` = ?";
    $stmt = $connection->prepare($query);
    $stmt->bind_param("i", $debtors_hidden_id);

    if ($stmt->execute()) {
        $_SESSION['msg'] = "Debtor record deleted successfully.";
        $_SESSION['type'] = "success";
    } else {
        $_SESSION['msg'] = "Failed to delete debtor record.";
        $_SESSION['type'] = "danger";
    }
    $stmt->close();

    header("Location: manage_debtors");
    exit;
}
?>


<div class='page-title'>
  <div>
    <h1><i class='fa fa-laptop'></i> Manage Debtors</h1>
    <p>Record Debtors</p>
  </div>
</div>

<div class='card'>
  <div class='panel panel-primary'>
    <div class='panel-heading'>
      <h4 class='text-center'>Record Debtors Details</h4>
    </div>
    <div class='panel-body'>
<form method='POST' action=''>
  <div class='form-group'>
    <label>Debtor Name</label>
    <input type='text' name='debtors_name' class='form-control form-control-sm' placeholder='Enter Debtor Name' required>
  </div>

  <div class='form-group'>
    <label>Amount Owed (₦)</label>
    <input type='number' name='debtors_amount' class='form-control form-control-sm' placeholder='Enter Amount' required>
  </div>

  <div class='form-group'>
    <label>Phone Number</label>
    <input type='number' name='debtors_phone' class='form-control form-control-sm' placeholder='Enter Phone Number' required>
  </div>

  <div class='form-group'>
    <label>Deadline</label>
    <input type='date' name='debtors_deadline' class='form-control form-control-sm' required>
  </div>

  <button type='submit' name='debtors_btn' class='btn btn-sm btn-primary'>Save Debtor</button>
</form>


      <hr>

      <?php
      $query = "SELECT SUM(amount) AS total FROM `debtors`";
      $result = mysqli_query($connection, $query);
      $total_debt_owed = ($result && mysqli_num_rows($result) > 0) ? number_format(mysqli_fetch_assoc($result)['total']) : "0";

      $query = "SELECT * FROM `debtors` ORDER BY id DESC";
      $run_query = mysqli_query($connection, $query);

      if (mysqli_num_rows($run_query) > 0): ?>
        <div class='table-responsive'>
          <h4 class='text-center'>Debtors Record</h4>
          <table class='table table-striped'>
            <thead class='bg-primary text-white'>
              <tr>
                <th>S/N</th>
                <th>Name</th>
                <th>Amount</th>
                <th>Phone</th>
                <th>Deadline</th>
                <th>Action</th>
              </tr>
            </thead>
            <tbody>
              <?php
              $i = 0;
              while ($row = mysqli_fetch_assoc($run_query)):
                $i++;
              ?>
              <tr>
                <td><?= $i ?></td>
                <td><?= $row['name'] ?></td>
                <td>₦<?= number_format($row['amount']) ?></td>
                <td><?= $row['phone'] ?></td>
                <td><?= $row['deadline'] ?></td>
                <td>
                  <form method='POST' action=''>
                    <input type='hidden' name='debtors_hidden_id' value='<?= $row['id'] ?>' />
                    <button type='submit' name='debtors_delete_btn' class='btn btn-danger btn-xs'>Delete</button>
                  </form>
                </td>
              </tr>
              <?php endwhile; ?>
            </tbody>
            <tfoot>
              <tr>
                <th colspan='2'>Total Debts</th>
                <th>₦<?= $total_debt_owed ?></th>
                <td colspan='3'></td>
              </tr>
            </tfoot>
          </table>
        </div>
      <?php else: ?>
        <h4 class='text-danger'><b>No Debtor Record Found</b></h4>
      <?php endif; ?>
    </div>
  </div>
</div>
