<?php
include "db.php";
$type = "detection";

/* ADD */
if(isset($_POST['add'])){
    $name = mysqli_real_escape_string($conn,$_POST['name']);
    mysqli_query($conn,"INSERT INTO masters(type,name,position,status)
    VALUES('$type','$name',0,1)");
    header("Location: admin-detection.php");
    exit;
}

/* UPDATE */
if(isset($_POST['update'])){
    $id = $_POST['id'];
    $name = mysqli_real_escape_string($conn,$_POST['name']);
    mysqli_query($conn,"UPDATE masters SET name='$name' WHERE id='$id'");
    header("Location: admin-detection.php");
    exit;
}

/* DELETE */
if(isset($_GET['del'])){
    mysqli_query($conn,"DELETE FROM masters WHERE id=".$_GET['del']);
    header("Location: admin-detection.php");
    exit;
}

/* STATUS TOGGLE */
if(isset($_GET['status'])){
    $id = $_GET['id'];
    $st = $_GET['status']==1 ? 0 : 1;
    mysqli_query($conn,"UPDATE masters SET status='$st' WHERE id='$id'");
    header("Location: admin-detection.php");
    exit;
}

/* EDIT FETCH */
$editData = null;
if(isset($_GET['edit'])){
    $editData = mysqli_fetch_assoc(
        mysqli_query($conn,"SELECT * FROM masters WHERE id=".$_GET['edit'])
    );
}

/* FETCH */
$data = mysqli_query($conn,"SELECT * FROM masters WHERE type='$type' ORDER BY position ASC");
?>
<!doctype html>
<html>
<head>
<title>Detection Systems</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">

<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
<script src="https://code.jquery.com/ui/1.13.2/jquery-ui.min.js"></script>

<style>
.sortable tr{ cursor:move; }
</style>
</head>
<body>

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

<div class="container mt-4">

<h3>Detection Systems (Drag to Sort)</h3>

<!-- ADD / EDIT FORM -->
<form method="post" class="row g-2 mb-3">
    <input type="hidden" name="id" value="<?= $editData['id'] ?? '' ?>">

    <div class="col-md-4">
        <input name="name" class="form-control" placeholder="Detection system name"
               value="<?= $editData['name'] ?? '' ?>" required>
    </div>

    <div class="col-md-2">
        <?php if(isset($editData)){ ?>
            <button name="update" class="btn btn-warning w-100">Update</button>
            <a href="admin-detection.php" class="btn btn-secondary w-100 mt-1">Cancel</a>
        <?php } else { ?>
            <button name="add" class="btn btn-primary w-100">Add</button>
        <?php } ?>
    </div>
</form>

<table class="table table-bordered table-striped">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Status</th>
<th>Actions</th>
</tr>
</thead>

<tbody class="sortable">
<?php while($r=mysqli_fetch_assoc($data)){ ?>
<tr id="row_<?= $r['id'] ?>">
<td><?= $r['id'] ?></td>
<td><?= $r['name'] ?></td>

<td>
<a href="?status=<?= $r['status'] ?>&id=<?= $r['id'] ?>"
   class="btn btn-sm <?= $r['status']?'btn-success':'btn-secondary' ?>">
   <?= $r['status']?'Active':'Inactive' ?>
</a>
</td>

<td>
<a href="?edit=<?= $r['id'] ?>" class="btn btn-warning btn-sm">Edit</a>
<a href="?del=<?= $r['id'] ?>" class="btn btn-danger btn-sm"
   onclick="return confirm('Delete this detection system?')">Delete</a>
</td>
</tr>
<?php } ?>
</tbody>
</table>

</div>

<script>
$(".sortable").sortable({
    update:function(){
        let ids=[];
        $(".sortable tr").each(function(){
            ids.push($(this).attr("id").replace("row_",""));
        });

        $.post("update-position.php",{
            ids:ids,
            table:"masters"
        });
    }
});
</script>

</body>
</html>
