<?php
// ---- Default date range ----
$today = date("Y-m-d");
$monthStart = date("Y-m-01");
$dFrom = $_GET['dFrom'] ?? $monthStart;
$dTo   = $_GET['dTo'] ?? $today;
?>

<div class="container mt-4">
  <div class="card shadow-lg p-4 rounded-3">
    <h4 class="mb-3">Maturity Report</h4>
    <form method="get" class="row g-3">
        <div class="col-md-3">
            <label class="form-label">From</label>
            <input value="<?= $dFrom ?>" type="date" name="dFrom" class="form-control" required>
        </div>
        <div class="col-md-3">
            <label class="form-label">To</label>
            <input value="<?= $dTo ?>" type="date" name="dTo" class="form-control" required>
        </div>
        <div class="col-md-3 d-flex align-items-end" style="margin-top: 25px;">
            <button type="submit" class="btn btn-info w-100">Generate Report</button>
        </div>
    </form>
  </div>
</div>

<?php
function maturity_report($dateFrom, $dateTo) {
    echo '<div class="container mt-4"> 
            <div class="alert alert-primary shadow-sm">
              <h5 class="mb-0">Maturity Report: '.$dateFrom." → ".$dateTo.'</h5>
            </div>
          </div>';

    $conn = new mysqli($GLOBALS['host'], $GLOBALS['user'], $GLOBALS['pass'], $GLOBALS['db']);
    if ($conn->connect_error) die("Connection failed: " . $conn->connect_error);

    // ---- Next month maturity filter ----
    $sql = "SELECT * FROM ".$GLOBALS['arif_ac']." WHERE DATE_FORMAT(AA_DATE_MATURE, '%Y-%m-%d') BETWEEN '".$dateFrom."' AND '".$dateTo."'ORDER BY AA_DATE_MATURE ASC";

    $result = $conn->query($sql);

    echo '
    <div class="container mt-3">
    <div class="card shadow-sm rounded-3">
    <div class="card-body">
    <div class="table-responsive">
    <table class="table table-bordered table-hover align-middle">
      <thead class="table-light">
        <tr>
          <th>AC No</th>
          <th>Name</th>
          <th>Type</th>
          <th>Total Installments</th>
          <th>Paid</th>
          <th>Pending</th>
          <th>Mature Value</th>
          <th>Maturity Date</th>
        </tr>
      </thead>
      <tbody>';

    $grandTotal = 0;

    if ($result && $result->num_rows > 0) {
        while($row = $result->fetch_assoc()) {

            // Calculate installments
            $paid_installments = floor($row['AA_BAL'] / $row['AA_INSTALLMENT']);
            $pending_installments = $row['AA_NO_OF_PAYMENT'] - $paid_installments;

            if ($pending_installments <= 0) {
                // Fully matured
                $payout = $row['AA_MATURE_VALUE'];
            } else {
                // Not fully paid, only balance is eligible
                $payout = $row['AA_BAL']; 
            }

            $grandTotal += $row['AA_MATURE_VALUE'];

            echo "
              <tr>
                <td>".$row['AA_ACNO']."</td>
                <td>".$row['AA_NAME']."</td>
                <td>".$row['AA_ACTYPE']."</td>
                <td>".$row['AA_NO_OF_PAYMENT']."</td>
                <td>".$paid_installments."</td>
                <td>".$pending_installments."</td>
                <td>".number_format($payout,2)."</td>
                <td>".$row['AA_DATE_MATURE']."</td>
              </tr>";
        }
    } else {
        echo "<tr><td colspan='8' class='text-center text-muted'>No matured accounts found</td></tr>";
    }

    echo '
      </tbody>
    </table>
    <h5 class="text-end">Grand Total Payout: <b>'.number_format($grandTotal,2).'</b></h5>
    </div>
    </div>
    </div>
    ';

    $conn->close();
}

// ---- Call maturity function ----
maturity_report($dFrom, $dTo);
?>
