<?php

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

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

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

$keyword = trim($_GET['keyword'] ?? '');

$sql = "
SELECT *
FROM equipment_master
WHERE equipment_name LIKE ?
ORDER BY display_order ASC, equipment_name ASC
";

$stmt = mysqli_prepare($conn, $sql);

$search = "%{$keyword}%";

mysqli_stmt_bind_param(
    $stmt,
    "s",
    $search
);

mysqli_stmt_execute($stmt);

$result = mysqli_stmt_get_result($stmt);

$totalRecords = mysqli_num_rows($result);

?>

<main class="container mt-4">

    <div class="card shadow">

        <div class="card-header bg-primary text-white d-flex justify-content-between align-items-center">

            <h5 class="mb-0">
            <i class="fa-solid fa-computer"></i>
            Equipment Management
            </h5>

            <a href="add_equipment.php"
            class="btn btn-light btn-sm">

            <i class="fa-solid fa-plus"></i>
            Add Equipment

            </a>

        </div>

        <div class="card-body">

            <form method="GET">

                <div class="row mb-3">

                    <div class="col-md-10">

                        <input
                        type="text"
                        name="keyword"
                        class="form-control"
                        placeholder="Search Equipment..."
                        value="<?= htmlspecialchars($keyword) ?>">

                    </div>

                    <div class="col-md-2 d-grid">

                        <button
                        class="btn btn-primary">

                        Search

                        </button>

                    </div>

                </div>

            </form>

            <div class="alert alert-info">

                <strong>Total Equipment :</strong>

                <?= $totalRecords ?>

            </div>

            <div class="table-responsive">

                <?php if(isset($_GET['updated'])) { ?>
                    <div class="alert alert-success">
                        Equipment updated successfully.
                    </div>
                    <?php } ?>

                    <?php if(isset($_GET['deleted'])) { ?>
                    <div class="alert alert-success">
                        Equipment deleted successfully.
                    </div>
                    <?php } ?>

                    <?php if(isset($_GET['added'])) { ?>
                    <div class="alert alert-success">
                        Equipment added successfully.
                    </div>
                <?php } ?>

                <table class="table table-bordered table-hover align-middle">

                    <thead class="table-dark">

                        <tr>

                            <th width="60">#</th>

                            <th>Equipment Name</th>

                            <th width="120">Order</th>

                            <th width="120">Status</th>

                            <th width="180">Action</th>

                        </tr>

                    </thead>

                    <tbody>

                        <?php

                        $no = 1;

                        if($totalRecords > 0)
                        {
                            while($row = mysqli_fetch_assoc($result))
                            {
                        ?>

                        <tr>

                            <td><?= $no++ ?></td>

                            <td>

                                <?= htmlspecialchars($row['equipment_name']) ?>

                            </td>

                            <td>

                                <?= $row['display_order'] ?>

                            </td>

                            <td>

                                <?php
                                if($row['status'] == 'Active')
                                {
                                ?>

                                <span class="badge bg-success">

                                Active

                                </span>

                                <?php
                                }
                                else
                                {
                                ?>

                                <span class="badge bg-danger">

                                Inactive

                                </span>

                                <?php
                                }
                                ?>

                            </td>

                            <td>

                                <a
                                href="edit_equipment.php?id=<?= $row['id'] ?>"
                                class="btn btn-warning btn-sm">

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

                                Edit

                                </a>

                                <a href="delete_equipment.php?id=<?= $row['id'] ?>"
                                class="btn btn-danger btn-sm"
                                onclick="return confirm('Are you sure you want to delete this equipment?')">

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

                                    Delete

                                </a>

                            </td>

                        </tr>

                        <?php

                            }
                        }
                        else
                        {
                        ?>

                        <tr>

                            <td colspan="5" class="text-center text-danger">

                            No equipment found.

                            </td>

                        </tr>

                        <?php
                        }
                        ?>

                    </tbody>

                </table>

            </div>

        </div>

    </div>

</main>

<?php

    mysqli_stmt_close($stmt);

    include '../includes/footer.php';

?>