<?php
include "db.php";
include "header.php";

// Έλεγχος αν δόθηκε το id
$product_id = isset($_GET['id']) ? (int)$_GET['id'] : 0;
if ($product_id <= 0) {
    echo "<div class='alert alert-danger'>Μη έγκυρο προϊόν.</div>";
    include "footer.php";
    exit;
}

// Πάρε όνομα προϊόντος
$stmt = $db->prepare("SELECT * FROM products WHERE id = ?");
$stmt->bind_param("i", $product_id);
$stmt->execute();
$product_result = $stmt->get_result();
$product = $product_result->fetch_assoc();

if (!$product) {
    echo "<div class='alert alert-danger'>Το προϊόν δεν βρέθηκε.</div>";
    include "footer.php";
    exit;
}

// Πάρε πρώτες ύλες και ποσότητες με τελευταία τιμή και code μονάδας
$query = "
SELECT pi.quantity, u.name AS unit_name, u.code AS unit_code, i.name AS ingredient_name, COALESCE(ip.price,0) AS price
FROM product_ingredients pi
JOIN ingredients i ON pi.ingredient_id = i.id
JOIN units u ON pi.unit_id = u.id
LEFT JOIN ingredient_prices ip ON ip.id = (
    SELECT id FROM ingredient_prices 
    WHERE ingredient_id = i.id 
    ORDER BY date DESC 
    LIMIT 1
)
WHERE pi.product_id = ?
ORDER BY i.name
";
$stmt2 = $db->prepare($query);
$stmt2->bind_param("i", $product_id);
$stmt2->execute();
$result = $stmt2->get_result();

$ingredients = [];
$total_cost = 0;

while ($row = $result->fetch_assoc()) {
    $ingredients[] = $row;
    $total_cost += $row['price'] * $row['quantity'];
}
?>

<style>
 body {
        min-height: 100vh;
        display: flex;
        margin: 0;
        background: #f9f9f9;
    }
    
</style>

<!-- Bootstrap + DataTables CSS -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css">
<link rel="stylesheet" href="https://cdn.datatables.net/1.13.6/css/dataTables.bootstrap5.min.css">

<div class="container-fluid mt-5" style="padding-left: 220px; padding-right: 30px;">
    <h2>📦 Προϊόν: <?= htmlspecialchars($product['name']) ?> 
    <small style="color:#555;">[Κωδικός: <?= htmlspecialchars($product['product_code']) ?>]</small>
    </h2>

    <?php if (count($ingredients) > 0): ?>
    <table id="productIngredients" class="table table-striped table-bordered shadow-sm mt-3">
        <thead class="table-dark">
            <tr>
                <th>Υλικό</th>
                <th>Ποσότητα</th>
                <th>Τιμή (€)</th>
                <th>Κόστος (€)</th>
            </tr>
        </thead>
        <tbody>
            <?php foreach ($ingredients as $ing): ?>
            <tr>
                <td><?= htmlspecialchars($ing['ingredient_name']) ?> (<?= htmlspecialchars($ing['unit_name']) ?>)</td>
                <td><?= $ing['quantity'] ?> (<?= htmlspecialchars($ing['unit_code']) ?>)</td>
                <td><?= number_format($ing['price'],2) ?></td>
                <td><?= number_format($ing['price'] * $ing['quantity'],2) ?></td>
            </tr>
            <?php endforeach; ?>
        </tbody>
        <tfoot>
            <tr>
                <th colspan="3">Συνολικό Κόστος</th>
                <th><?= number_format($total_cost,2) ?> €</th>
            </tr>
        </tfoot>
    </table>
    <?php else: ?>
    <div class="alert alert-warning mt-3">Δεν υπάρχουν υλικά καταχωρημένα για αυτό το προϊόν.</div>
    <?php endif; ?>

    <!-- Κουμπιά Εξαγωγής Excel, Επεξεργασίας και Πίσω -->
    <div class="d-flex mt-3 mb-3">
        <a href="product_excel.php?id=<?= $product_id ?>" class="btn btn-warning me-2"><i class="bi bi-arrow-down-circle"></i> Εξαγωγή σε Excel</a>
        <a href="product_edit.php?id=<?= $product_id ?>" class="btn btn-primary me-2"><i class="bi bi-pencil-square"></i> Επεξεργασία Προϊόντος</a>
        <a href="recipes_list.php" class="btn btn-secondary"><i class="bi bi-arrow-left"></i> Πίσω στη λίστα προϊόντων</a>
    </div>
</div>

<!-- jQuery + DataTables JS + Bootstrap JS -->
<script src="https://code.jquery.com/jquery-3.7.0.min.js"></script>
<script src="https://cdn.datatables.net/1.13.6/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/1.13.6/js/dataTables.bootstrap5.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>

<?php if(count($ingredients) > 0): ?>
<script>
$(document).ready(function() {
    $('#productIngredients').DataTable({
        "paging": true,
        "searching": true,
        "ordering": true,
        "info": true,
        "language": {
            "url": "//cdn.datatables.net/plug-ins/1.13.6/i18n/el.json"
        }
    });
});
</script>
<?php endif; ?>

<?php include "footer.php"; ?>
