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

$token = $_GET['token'];

$stmt = $conn->prepare("SELECT * FROM stone_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;'>Stone Calculator History</h3>";

    echo "<table>
        <tr>
            <th>Type</th>
            <th>Sqft</th>
            <th>Lnft</th>
            <th>Material</th>
            <th>Stone Cut Size</th>
            <th>Tons</th>
            <th>Date</th>
            <th>Action</th>
        </tr>
    ";

    while ($row = $result->fetch_assoc()) {
        $date = date("m/d/Y h:i A", strtotime($row['created_at']));

        echo "<tr>
            <td>{$row['calc_type']}</td>
            <td>{$row['sqft']}</td>
            <td>{$row['lnft']}</td>
            <td>{$row['material']}</td>
            <td>{$row['stone_size']}</td>
            <td>{$row['tons']}</td>
            <td>{$date}</td>
            <td><button class='delBtn' data-id='{$row['id']}'>🗑</button></td>
        </tr>";
    }

    echo "</table>";
}

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