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

$success = '';
$error = '';

if ($_SERVER["REQUEST_METHOD"] === "POST") {
    $code = trim($_POST['code'] ?? '');
    $name = trim($_POST['name'] ?? '');

    if (empty($code) || empty($name)) {
        $error = "Παρακαλώ συμπληρώστε όλα τα πεδία.";
    } else {
        $stmt = $db->prepare("INSERT INTO units (code, name, created_at) VALUES (?, ?, NOW())");
        $stmt->bind_param("ss", $code, $name);
        if ($stmt->execute()) {
            $success = "✅ Η μονάδα προστέθηκε επιτυχώς!";
        } else {
            $error = "Σφάλμα καταχώρησης: " . $stmt->error;
        }
    }
}
?>

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

<style>
/* Layout με sidebar */
body {
    display: grid;
    grid-template-columns: 250px 1fr; /* sidebar + content */
    min-height: 100vh;
    margin: 0;
}

/* Sidebar */
#sidebar {
    grid-column: 1;
    background: #343a40;
    color: #fff;
    display: flex;
    flex-direction: column;
}

#sidebar ul {
    list-style: none;
    padding: 0;
    margin: 0;
}

#sidebar ul li a {
    display: block;
    color: #f8f9fa;
    text-decoration: none;
    padding: 12px 20px;
}

#sidebar ul li a:hover,
#sidebar ul li.active > a {
    background: #ffc107;
    color: #343a40;
    border-radius: 5px;
}

/* Content */
#content {
    grid-column: 2;
    margin-top: -500px;
    padding: 20px;
    display: flex;
    justify-content: center; /* οριζόντια center */
    align-items: center;     /* κάθετα center */
}

/* Card */
.add-unit-card {
    width: 100%;
    max-width: 500px;
    background: #fff;
    padding: 25px;
    border-radius: 12px;
    box-shadow: 0 4px 12px rgba(0,0,0,0.08);
}

input.form-control {
    width: 100%;
    padding: 6px;
    margin-bottom: 12px;
    border-radius: 5px;
    border: 1px solid #ccc;
}

.alert-success {
    color: green;
    font-weight: bold;
    text-align: center;
    margin-top: 15px;
}

.alert-danger {
    color: red;
    font-weight: bold;
    text-align: center;
    margin-bottom: 15px;
}
</style>

<div id="content">
    <div class="add-unit-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">Code:</label>
                <input type="text" name="code" class="form-control" required>
            </div>

            <div class="mb-3">
                <label class="form-label">Όνομα:</label>
                <input type="text" name="name" class="form-control" required>
            </div>

            <button type="submit" class="btn btn-success w-100"><i class="bi bi-check-circle"></i> Καταχώρηση μονάδας</button>
            <a href="units_list.php" class="btn btn-secondary w-100 mt-2"><i class="bi bi-arrow-left"></i> Επιστροφή στη λίστα</a>
        </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"; ?>
