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

$success = false;
$error = '';

$unit_id = (int)($_GET['id'] ?? 0);
if ($unit_id <= 0) {
    echo "<div style='color:red; font-weight:bold;'>Μη έγκυρο ID μονάδας.</div>";
    include "footer.php";
    exit;
}

$stmt = $db->prepare("SELECT * FROM units WHERE id = ?");
$stmt->bind_param("i", $unit_id);
$stmt->execute();
$res = $stmt->get_result();
$unit = $res->fetch_assoc();

if (!$unit) {
    echo "<div style='color:red; font-weight:bold;'>Η μονάδα δεν βρέθηκε.</div>";
    include "footer.php";
    exit;
}

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

    if (empty($code) || empty($name)) {
        $error = "Παρακαλώ συμπληρώστε όλα τα πεδία.";
    } else {
        $stmt2 = $db->prepare("UPDATE units SET code=?, name=?, updated_at=NOW() WHERE id=?");
        $stmt2->bind_param("ssi", $code, $name, $unit_id);
        if ($stmt2->execute()) {
            $success = true;
            $unit['code'] = $code;
            $unit['name'] = $name;
        } else {
            $error = "Σφάλμα ενημέρωσης: " . $stmt2->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 */
.edit-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="edit-unit-card">
        <h3 class="text-center mb-4"><i class="bi bi-pencil-square"></i> Επεξεργασία Μονάδας Μέτρησης</h3>

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

        <form method="POST">
            <label>Code:</label>
            <input type="text" name="code" class="form-control" value="<?= htmlspecialchars($unit['code']) ?>" required>

            <label>Όνομα:</label>
            <input type="text" name="name" class="form-control" value="<?= htmlspecialchars($unit['name']) ?>" required>

            <button type="submit" class="btn btn-success w-100 mt-3"><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>

            <?php if ($success): ?>
                <div id="successAlert" class="alert alert-success mt-3 text-center">✔ Οι αλλαγές αποθηκεύτηκαν!</div>
                <script src="https://code.jquery.com/jquery-3.7.0.min.js"></script>
                <script>
                    setTimeout(function() {
                        $("#successAlert").fadeOut("slow");
                    }, 3000);
                </script>
            <?php endif; ?>
        </form>
    </div>
</div>

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