<?php
include 'config.php';
checkLogin();

/* =========================
   ADD MODEL
========================= */
if (isset($_POST['save'])) {
    $brand  = clean($_POST['brand']);
    $model  = clean($_POST['model']);
    $prefix = clean($_POST['prefix']);

    if ($brand && $model && $prefix) {
        $stmt = $conn->prepare(
            "INSERT INTO laptop_models (brand, model, prefix)
             VALUES (?,?,?)"
        );
        $stmt->bind_param("sss", $brand, $model, $prefix);
        $stmt->execute();
        $stmt->close();
        $msg = "Model added successfully";
    }
}

/* =========================
   DELETE MODEL (SAFE)
========================= */
if (isset($_GET['delete'])) {
    $id = (int)$_GET['delete'];

    // Check if serials exist
    $check = $conn->query(
        "SELECT COUNT(*) c FROM laptop_serials WHERE model_id = $id"
    )->fetch_assoc()['c'];

    if ($check > 0) {
        $err = "Cannot delete model. Serials already generated.";
    } else {
        $conn->query("DELETE FROM laptop_models WHERE id = $id");
        $msg = "Model deleted successfully";
    }
}

/* =========================
   LOAD MODELS
========================= */
$models = $conn->query("SELECT * FROM laptop_models ORDER BY id DESC");

include 'layout/header.php';
include 'layout/sidebar.php';
?>

<h3 class="mb-4">Devices</h3>

<?php if (isset($msg)) { ?>
<div class="alert alert-success"><?= $msg ?></div>
<?php } ?>

<?php if (isset($err)) { ?>
<div class="alert alert-danger"><?= $err ?></div>
<?php } ?>

<!-- ADD MODEL -->
<div class="card p-4 mb-4">
<form method="post">
    <div class="row g-3">
        <div class="col-md-4">
            <input class="form-control" name="brand" placeholder="Brand (Device Brand)" required>
        </div>
        <div class="col-md-4">
            <input class="form-control" name="model" placeholder="Model (Device Model)" required>
        </div>
        <div class="col-md-4">
            <input class="form-control" name="prefix" placeholder="Prefix (LEN-T14)" required>
        </div>
    </div>
    <button class="btn btn-primary mt-3" name="save">Add Model</button>
</form>
</div>

<!-- MODEL LIST -->
<div class="card p-4">
<table class="table table-bordered table-hover">
    <thead class="table-light">
        <tr>
            <th>ID</th>
            <th>Brand</th>
            <th>Model</th>
            <th>Prefix</th>
            <th>Last Serial</th>
            <th width="160">Action</th>
        </tr>
    </thead>
    <tbody>
    <?php while ($row = $models->fetch_assoc()) { ?>
        <tr>
            <td><?= $row['id'] ?></td>
            <td><?= strtoupper($row['brand']) ?></td>
            <td><?= strtoupper($row['model']) ?></td>
            <td><?= $row['prefix'] ?></td>
            <td><?= $row['last_serial'] ?></td>
            <td>
                <a href="edit_model.php?id=<?= $row['id'] ?>" class="btn btn-sm btn-warning">
                    Edit
                </a>
                <a href="?delete=<?= $row['id'] ?>"
                   onclick="return confirm('Delete this model?')"
                   class="btn btn-sm btn-danger">
                    Delete
                </a>
            </td>
        </tr>
    <?php } ?>
    </tbody>
</table>
</div>

<?php include 'layout/footer.php'; ?>
