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

// Έλεγχος ID
$id = (int) ($_GET['id'] ?? 0);
if ($id <= 0) {
    echo "<div style='color:red; font-weight:bold;'>Μη έγκυρο ID υλικού!</div>";
    include "footer.php";
    exit;
}

// Λήψη στοιχείων υλικού και τελευταίας τιμής
$stmt = $db->prepare("
    SELECT i.id, i.name, i.unit_id, ip.price 
    FROM ingredients i
    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 i.id = ?
");
$stmt->bind_param("i", $id);
$stmt->execute();
$result = $stmt->get_result();
$ingredient = $result->fetch_assoc();

if (!$ingredient) {
    echo "<div style='color:red; font-weight:bold;'>Το υλικό δεν βρέθηκε!</div>";
    include "footer.php";
    exit;
}

// Λήψη όλων των μονάδων
$units_result = $db->query("SELECT * FROM units ORDER BY name");
$units = [];
while ($u = $units_result->fetch_assoc()) {
    $units[$u['id']] = $u['name'];
}

// Αν υποβλήθηκε η φόρμα
$success = false;
$error = '';
if ($_SERVER["REQUEST_METHOD"] === "POST") {
    $name = trim($_POST['name'] ?? '');
    $unit_id = (int) ($_POST['unit_id'] ?? 0);
    $price = (float) ($_POST['price'] ?? 0);

    if (empty($name) || $unit_id <= 0 || $price <= 0) {
        $error = "Παρακαλώ συμπληρώστε όλα τα πεδία σωστά.";
    } else {
        $stmt = $db->prepare("UPDATE ingredients SET name = ?, unit_id = ?, default_unit_id = ? WHERE id = ?");
        $stmt->bind_param("siii", $name, $unit_id, $unit_id, $id);
        $stmt->execute();

        $stmt2 = $db->prepare("INSERT INTO ingredient_prices (ingredient_id, price, date) VALUES (?, ?, NOW())");
        $stmt2->bind_param("id", $id, $price);
        $stmt2->execute();

        $success = true;
        $ingredient['name'] = $name;
        $ingredient['unit_id'] = $unit_id;
        $ingredient['price'] = $price;
    }
}
?>

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css">

<style>
body {
    background: #f9f9f9;
    margin: 0;
}

/* ================= Page Wrapper ================= */
.edit-ingredient-page {
    width: calc(100vw - 250px - 30px);
    margin-left: 250px;
    margin-right: 30px;
    min-height: 100vh;

    display: flex;
    align-items: center;
    justify-content: center;

    padding: 20px;
    box-sizing: border-box;
}


/* ================= Card ================= */
.edit-ingredient-card {
    max-width: 600px;   /* αν θέλεις να μην ξεπερνάει */
    width: 100%;        /* καταλαμβάνει όλο το διαθέσιμο πλάτος έως το max */
    min-width: 300px;   /* θέτει όριο μικρότερου πλάτους */
    background: #fff;
    padding: 25px;
    border-radius: 12px;
    box-shadow: 0 4px 12px rgba(0,0,0,0.08);
}


.alert-success,
.alert-danger {
    text-align: center;
}
</style>

<div class="edit-ingredient-page">

    <div class="edit-ingredient-card">
        <h3 class="text-center mb-4">
            <i class="bi bi-pencil-square"></i> Επεξεργασία Υλικού
        </h3>

        <?php if ($success): ?>
            <div class="alert alert-success">✔ Οι αλλαγές αποθηκεύτηκαν!</div>
        <?php endif; ?>

        <?php if ($error): ?>
            <div class="alert alert-danger"><?= htmlspecialchars($error) ?></div>
        <?php endif; ?>

        <form method="POST">

            <div class="mb-3">
                <label class="form-label">Όνομα Υλικού:</label>
                <p class="form-control bg-light"><?= htmlspecialchars($ingredient['name']) ?></p>
                <input type="hidden" name="name" value="<?= htmlspecialchars($ingredient['name']) ?>">
            </div>

            <div class="mb-3">
                <label class="form-label">Μονάδα Μέτρησης:</label>
                <div class="form-control bg-light">
                    <?= htmlspecialchars($units[$ingredient['unit_id']] ?? '—') ?>
                </div>
                <input type="hidden" name="unit_id" value="<?= $ingredient['unit_id'] ?>">
            </div>

            <div class="mb-3">
                <label class="form-label">Τιμή Αγοράς (€):</label>
                <input
                    type="number"
                    step="0.0001"
                    name="price"
                    class="form-control"
                    value="<?= htmlspecialchars($ingredient['price'] ?? '') ?>"
                    required
                >
            </div>

            <div class="d-grid gap-2">
                <button type="submit" class="btn btn-success">
                    <i class="bi bi-check-circle"></i> Αποθήκευση Αλλαγών
                </button>
                <a href="ingredient_list.php" class="btn btn-secondary">
                    <i class="bi bi-arrow-left"></i> Επιστροφή στη λίστα
                </a>
            </div>

        </form>
    </div>

</div>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>

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