<?php
session_start();
if (!isset($_SESSION['role_id']) || !in_array($_SESSION['role_id'], [1,4])) {
    header("Location: ../../index.php");
    exit();
}
include('../../includes/config.php');
include('../../includes/header.php');

$product_id = $_GET['id'] ?? 0;
if (!is_numeric($product_id) || $product_id <= 0) {
    echo "<p>Invalid product ID.</p>";
    include('../../includes/footer.php');
    exit();
}

// Fetch product details
$stmt = $conn->prepare("SELECT product_name FROM products WHERE id = ?");
$stmt->bind_param("i", $product_id);
$stmt->execute();
$product = $stmt->get_result()->fetch_assoc();
$stmt->close();

if (!$product) {
    echo "<p>Product not found.</p>";
    include('../../includes/footer.php');
    exit();
}
$product_name = $product['product_name'];

// Fetch sales items for this product (joined with sales_transactions for date, status)
$stmt = $conn->prepare("
    SELECT s.transaction_id, s.quantity, s.unit_price, s.total_price, s.created_at AS sale_created_at, t.created_at AS transaction_created_at, t.status
    FROM sales_items s
    JOIN sales_transactions t ON s.transaction_id = t.id
    WHERE s.product_id = ? AND t.status = 'completed'
    ORDER BY t.created_at DESC
");
$stmt->bind_param("i", $product_id);
$stmt->execute();
$sales = $stmt->get_result()->fetch_all(MYSQLI_ASSOC);
$stmt->close();

$page_title = "Product Sales History";
?>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title><?= htmlspecialchars($page_title) ?></title>
<link rel="icon" type="image/png" href="../../images/logo.png" />
<style>
body {
    margin: 0;
    background: url('../../images/profile.jpg') no-repeat center center fixed;
    background-size: cover;
    font-family: 'Segoe UI', sans-serif;
    color: #eee;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
}

.content {
    max-width: 1200px;
    margin: 2rem auto 3rem;
    padding: 40px 50px;
    background: rgba(255, 255, 255, 0.08);
    border-radius: 25px;
    box-shadow: 0 8px 32px rgba(255, 165, 0, 0.15);
    backdrop-filter: blur(18px);
    -webkit-backdrop-filter: blur(18px);
    border: 1.5px solid rgba(255, 165, 0, 0.35);
}

.page-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 2rem;
}

h1.page-title {
    font-weight: 700;
    font-size: 2.3rem;
    color: #ffa500;
    letter-spacing: 1.1px;
    margin: 0;
    user-select: none;
    display: flex;
    align-items: center;
    gap: 12px;
}

h1.page-title img {
    height: 40px;
    width: auto;
}

a.back-link {
    color: #ffa500;
    text-decoration: none;
    font-weight: 600;
    border: 1.5px solid #ffa500;
    padding: 8px 16px;
    border-radius: 8px;
    transition: background-color 0.3s ease, color 0.3s ease;
    white-space: nowrap;
}
a.back-link:hover {
    background-color: #ffa500;
    color: #222;
}

h2 {
    font-weight: 600;
    font-size: 1.8rem;
    color: #ffa500;
    margin-bottom: 1rem;
    user-select: none;
}

table {
    width: 100%;
    border-collapse: separate;
    border-spacing: 0 12px;
    font-size: 15px;
    user-select: none;
}

thead tr th {
    background: rgba(255, 165, 0, 0.8);
    color: #222;
    font-weight: 700;
    padding: 14px 18px;
    border-radius: 12px;
    text-align: left;
    letter-spacing: 0.7px;
}

tbody tr {
    background: rgba(255, 255, 255, 0.12);
    border-radius: 18px;
    transition: background-color 0.3s ease;
}

tbody tr:hover {
    background: rgba(255, 165, 0, 0.18);
}

tbody tr td {
    padding: 14px 18px;
    color: #eee;
    vertical-align: middle;
}

tbody tr td:first-child {
    font-weight: 600;
    color: #ffa500;
}

tbody tr td:nth-child(2),
tbody tr td:nth-child(3),
tbody tr td:nth-child(4) {
    text-align: right;
    font-variant-numeric: tabular-nums;
}

/* Responsive */
@media (max-width: 900px) {
    .content {
        margin: 1.5rem 1rem 2rem;
        padding: 25px 20px;
    }
    h1.page-title {
        font-size: 1.6rem;
    }
    table thead tr th, tbody tr td {
        padding: 10px 12px;
        font-size: 14px;
    }
}
</style>
</head>
<body>

<div class="content">

    <div class="page-header">
        <h1 class="page-title">
            <img src="../../images/logo.png" alt="Logo" />
            <?= htmlspecialchars($page_title) ?>
        </h1>
        <a href="inventory_valuation.php" class="back-link">&laquo; Back to Inventory Valuation</a>
    </div>

    <h2>Product Sales History for <?= htmlspecialchars($product_name) ?></h2>

    <?php if (empty($sales)): ?>
        <p>No sales found for this product.</p>
    <?php else: ?>
        <table aria-label="Sales history table">
            <thead>
                <tr>
                    <th>Date</th>
                    <th>Quantity</th>
                    <th>Unit Price</th>
                    <th>Total Price</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($sales as $sale): ?>
                <tr>
                    <td><?= htmlspecialchars(date('Y-m-d', strtotime($sale['transaction_created_at']))) ?></td>
                    <td><?= number_format($sale['quantity'], 2) ?></td>
                    <td><?= number_format($sale['unit_price'], 2) ?></td>
                    <td><?= number_format($sale['total_price'], 2) ?></td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    <?php endif; ?>

</div>

<?php include('../../includes/footer.php'); ?>

</body>
</html>
