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

$success = '';
$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("INSERT INTO ingredients (name, unit_id, default_unit_id) VALUES (?, ?, ?)");
        if (!$stmt) die("Σφάλμα: " . $db->error);
        $stmt->bind_param("sii", $name, $unit_id, $unit_id);
        if (!$stmt->execute()) die("Σφάλμα: " . $stmt->error);
        $ingredient_id = $stmt->insert_id;

        $stmt2 = $db->prepare("INSERT INTO ingredient_prices (ingredient_id, price, date) VALUES (?, ?, NOW())");
        if (!$stmt2) die("Σφάλμα: " . $db->error);
        $stmt2->bind_param("id", $ingredient_id, $price);

        if ($stmt2->execute()) {
            $success = "✅ Επιτυχής καταχώρηση!";
        } else {
            $error = "Σφάλμα καταχώρησης τιμής: " . $stmt2->error;
        }
    }
}
?>

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

<style>
/* Wrapper page */
.add-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 */
.add-ingredient-card {
    max-width: 500px;
    width: 100%;
    background: #fff;
    padding: 25px;
    border-radius: 12px;
    box-shadow: 0 4px 12px rgba(0,0,0,0.08);
}

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

<div class="add-ingredient-page">
    <div class="add-ingredient-card">

        <h3 class="text-center mb-4"><i class="bi bi-plus-circle"></i> Προσθήκη Υλικού</h3>

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

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

        <form method="POST">
            <div class="mb-3">
                <label class="form-label">Όνομα Υλικού:</label>
                <input type="text" name="name" class="form-control" required>
            </div>

            <div class="mb-3">
                <label class="form-label">Μονάδα Μέτρησης:</label>
                <select name="unit_id" class="form-select" required>
                    <option value="">-- Επιλογή --</option>
                    <?php
                    $units = $db->query("SELECT * FROM units ORDER BY name");
                    while ($u = $units->fetch_assoc()):
                    ?>
                        <option value="<?= $u['id'] ?>"><?= htmlspecialchars($u['name']) ?></option>
                    <?php endwhile; ?>
                </select>
            </div>

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

            <div class="d-grid gap-2">
                <button type="submit" class="btn btn-success w-100"><i class="bi bi-check-circle"></i> Καταχώρηση Υλικού</button>
                <a href="ingredient_list.php" class="btn btn-secondary w-100 mt-2"><i class="bi bi-arrow-left"></i> Επιστροφή στη λίστα</a>
            </div>
        </form>

    </div>
</div>

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

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