<?php
  if(!isset($_SESSION) && empty($_SESSION['user_id'])){
      echo "<script>window.location.href = '/Agent/agent-login'</script>";
      exit;
  }
?>
<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($type, $dt) {
    $dateFrom = $dt;

    if($type!="month" && $type!="day") {
        $dateFrom = strtotime($dt); 
        $dateFrom = date("Y-m-d", $dateFrom);
        $dateTo = strtotime('+1 day', strtotime($type)); 
        $dateTo = date("Y-m-d", $dateTo);
    } else {
        if($type=="month") {
            $dateFrom = strtotime('-1 day', strtotime($dt)); 
            $dateFrom = date("Y-m-d", $dateFrom);
        }
        $dateTo = strtotime('+1 '.$type, strtotime($dt));
        $dateTo = date("Y-m-d", $dateTo);
    }

    echo '<div class="container mt-4"> 
            <div class="alert alert-primary shadow-sm">
              <h5 class="mb-0">Report Period: '.$dateFrom." → ".$dateTo." (Up to)</h5>
            </div>
          </div>";

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

    $totalAmount = 0;

    echo '
    <div class="container mt-3">
    <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>';

    // ----- base query -----
    $sql = "SELECT * FROM `".$GLOBALS['arif_tran']."`INNER JOIN `".$GLOBALS['arif_ac']."`ON `".$GLOBALS['arif_tran']."`.`AT_ACID`=`".$GLOBALS['arif_ac']."`.`AA_ACNO` WHERE `AT_TIMESTAMP` BETWEEN '".$dateFrom." 00:00:00' AND '".$dateTo." 00:00:00'";

    // ----- always agent filter -----
    $sql .= " AND `AT_ADMIN`='".$_SESSION['user_id']."'";

    $sql .= " ORDER BY `".$GLOBALS['arif_tran']."`.`AT_ID` DESC";

    $result = $conn->query($sql);
    if ($result && $result->num_rows > 0) {
        while($row = $result->fetch_assoc()) {
            echo "
              <tr>
                <td>".$row["AT_ID"]."</td>    
                <td>".$row["AT_TIMESTAMP"]."</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['tday']) && $_GET['tday']!="") report_view('day', $_GET['tday']); 
if(isset($_GET['tmonth']) && $_GET['tmonth']!="") report_view('month', $_GET['tmonth']); 
if(isset($_GET['dFrom']) && $_GET['dTo']!="") report_view($_GET['dTo'], $_GET['dFrom']); 
?>
