<?php

require_once __DIR__ . '/../includes/auth.php';
require_once __DIR__ . '/../config/db.php';

if ($_SESSION['role'] != 'admin')
{
    die("Access Denied");
}

$id = (int)($_GET['id'] ?? 0);

$stmt = mysqli_prepare(
    $conn,
    "SELECT *
     FROM equipment_master
     WHERE id=?"
);

mysqli_stmt_bind_param($stmt, "i", $id);
mysqli_stmt_execute($stmt);

$result = mysqli_stmt_get_result($stmt);
$row = mysqli_fetch_assoc($result);

mysqli_stmt_close($stmt);

if(!$row)
{
    die("Equipment not found.");
}

include '../includes/header.php';
include '../includes/navbar.php';
?>

<main class="container mt-4">

    <div class="row justify-content-center">

        <div class="col-lg-8">

            <div class="card shadow">

                <div class="card-header bg-warning text-dark">

                    <h5 class="mb-0">

                    <i class="fa-solid fa-pen-to-square"></i>

                    Edit Equipment

                    </h5>

                </div>

                <div class="card-body">

                    <form action="update_equipment.php" method="POST">

                        <input
                        type="hidden"
                        name="id"
                        value="<?= $row['id'] ?>">

                        <div class="row">

                            <div class="col-md-8 mb-3">

                                <label class="form-label">

                                Equipment Name

                                </label>

                                <input
                                type="text"
                                name="equipment_name"
                                class="form-control"
                                value="<?= htmlspecialchars($row['equipment_name']) ?>"
                                required>

                            </div>

                            <div class="col-md-4 mb-3">

                                <label class="form-label">

                                Display Order

                                </label>

                                <input
                                type="number"
                                name="display_order"
                                class="form-control"
                                value="<?= $row['display_order'] ?>"
                                min="1"
                                required>

                            </div>

                        </div>

                        <div class="mb-3">

                            <label class="form-label">

                            Status

                            </label>

                            <select
                            name="status"
                            class="form-select">

                            <option
                            value="Active"
                            <?= $row['status']=='Active' ? 'selected' : '' ?>>

                            Active

                            </option>

                            <option
                            value="Inactive"
                            <?= $row['status']=='Inactive' ? 'selected' : '' ?>>

                            Inactive

                            </option>

                            </select>

                        </div>

                        <hr>

                        <div class="d-flex justify-content-between">

                            <a
                            href="manage_equipment.php"
                            class="btn btn-secondary">

                            <i class="fa-solid fa-arrow-left"></i>

                            Back

                            </a>

                            <button
                            type="reset"
                            class="btn btn-warning">

                            <i class="fa-solid fa-eraser"></i>

                            Reset

                            </button>

                            <button
                            type="submit"
                            class="btn btn-success">

                            <i class="fa-solid fa-floppy-disk"></i>

                            Update Equipment

                            </button>

                        </div>

                    </form>

                </div>

            </div>

        </div>

    </div>

</main>

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