<?php
include '../inc/db.php';
if ($conn->connect_error) die("DB Connection failed");

$token = $_GET['token'];
$stmt = $conn->prepare("SELECT * FROM aggregate_calculations WHERE user_token = ? ORDER BY id DESC");
$stmt->bind_param("s", $token);
$stmt->execute();
$result = $stmt->get_result();

if ($result->num_rows > 0) {
  echo "<h3 style='text-align:center; color:#fff; margin-bottom:15px; font-weight:500;'>All Calculations</h3>";
  
  echo "<table>
    <tr>
      <th>Material</th>
      <th>Width</th>
      <th>Length</th>
      <th>Depth (in)</th>
      <th>Cubic Yards</th>
      <th>Tons</th>
      <th>Date</th>
      <th>Action</th>
    </tr>";
    
  while ($row = $result->fetch_assoc()) {
    // Format date to US format (MM/DD/YYYY hh:mm AM/PM)
    $formatted_date = date("m/d/Y h:i A", strtotime($row['created_at']));
    
    echo "<tr>
      <td>{$row['material']}</td>
      <td>{$row['width']}</td>
      <td>{$row['length']}</td>
      <td>{$row['depth_in']}</td>
      <td>{$row['cubic_yards']}</td>
      <td>{$row['tons']}</td>
      <td>{$formatted_date}</td>
      <td><button class='delBtn' data-id='{$row['id']}'>🗑</button></td>
    </tr>";
  }
  echo "</table>";
} 

$stmt->close();
$conn->close();
?>