<?php
session_start();
date_default_timezone_set('Asia/Kolkata'); // Display timezone Kolkata

if (!isset($_SESSION['user_id']) || empty($_SESSION['user_id'])) {
    echo "<script>window.location.href = '/Agent/agent-login'</script>";
    exit;
}
?>

<!-- Generate Report Form -->
<div class="container mt-4">
  <div class="card shadow-lg p-4 rounded-3">
    <h4 class="mb-3">Generate Report</h4>
    <form method="get" class="row g-3">
        <div class="col-md-3">
            <label class="form-label">From</label>
            <input value="<?= $_GET['dFrom'] ?? '' ?>" type="date" name="dFrom" class="form-control" required>
        </div>
        <div class="col-md-3">
            <label class="form-label">To</label>
            <input value="<?= $_GET['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 report_view($fromDate, $toDate) {
    $conn = new mysqli($GLOBALS['host'], $GLOBALS['user'], $GLOBALS['pass'], $GLOBALS['db']);
    if ($conn->connect_error) die("Connection failed: " . $conn->connect_error);

    // Display alert for report period
    echo '<div class="container mt-4"> 
            <div class="alert alert-primary shadow-sm">
              <h5 class="mb-0">Report Period: '.$fromDate.' → '.$toDate.' (Up to)</h5>
            </div>
          </div>';

    $totalAmount = 0;

    echo '
    <div class="container mt-3" style="margin-bottom: 105px;">
    <div class="card shadow-sm rounded-3">
    <div class="card-body">
    <h5 class="mb-3">Transaction Report</h5>
    <div class="table-responsive">
    <table class="table table-bordered table-hover align-middle" id="reportTable" style="font-size: 12px;">
      <thead class="table-light">
        <tr>
          <th>Transaction ID</th>
          <th>Time</th>
          <th>AC No</th>
          <th>Name</th>
          <th>Amount</th>
        </tr>
      </thead>
      <tbody>';

    // Query: UTC in DB, filter by date
    $sql = "SELECT t.*, a.AA_NAME  FROM `".$GLOBALS['arif_tran']."` t INNER JOIN `".$GLOBALS['arif_ac']."` a ON t.AT_ACID = a.AA_ACNO WHERE t.AT_ADMIN = '".$_SESSION['user_id']."'  AND DATE(CONVERT_TZ(t.AT_TIMESTAMP,'+00:00','+05:30')) BETWEEN '$fromDate' AND '$toDate' ORDER BY t.AT_ID DESC";

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

    if ($result && $result->num_rows > 0) {
        while($row = $result->fetch_assoc()) {
            // Convert UTC timestamp to Kolkata time
            $kolkataTime = date("d M Y, h:i A", strtotime($row["AT_TIMESTAMP"] . " +5 hours 30 minutes"));

            echo "
              <tr>
                <td>".$row["AT_ID"]."</td>    
                <td>".$kolkataTime."</td>
                <td>".$row["AT_ACID"]."</td>
                <td>".$row["AA_NAME"]."</td>
                <td>".$row["AT_AMOUNT"]."</td>
              </tr>";

            $totalAmount += $row["AT_AMOUNT"];
        }
    } else {
        echo "<tr><td colspan='5' class='text-center text-muted'>No results found</td></tr>";
    }

    $conn->close();

    echo '
      </tbody>
    </table>
    </div>
    <hr>
    <h5 class="text-end">Total Transaction Amount : <b>'.$totalAmount.'</b></h5>
    </div>
    </div>
    </div>
    ';
}

// ---- Call report function ----
if(isset($_GET['dFrom'], $_GET['dTo']) && !empty($_GET['dFrom']) && !empty($_GET['dTo'])) {
    report_view($_GET['dFrom'], $_GET['dTo']);
}
?>
