<?php

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

if (!isset($_SESSION['role']) || $_SESSION['role'] !== 'admin') {
    header("Location: " . BASE_URL . "dashboard.php");
    exit();
}

$sql = "SELECT um.*, COUNT(ue.id) AS equipment_count
        FROM unit_master um
        LEFT JOIN unit_equipment ue ON ue.unit_id = um.id
        GROUP BY um.id
        ORDER BY um.display_order ASC, um.unit_name ASC";

$units = mysqli_query($conn, $sql);

if (!$units) {
    die("Database Error : " . mysqli_error($conn));
}

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

<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-building"></i>
                Unit Management
            </h5>

            <button type="button" class="btn btn-light btn-sm" id="addUnitButton">
                <i class="fa-solid fa-plus"></i>
                Add Unit
            </button>

        </div>

        <div class="card-body">

            <div class="alert alert-info">
                <strong>Note:</strong>
                Computer Lab and Office are fixed sections in every inventory.
                All other locations must be added through Other Units.
            </div>

            <div class="row mb-3">

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

                    <input
                    type="text"
                    id="searchUnit"
                    class="form-control"
                    placeholder="Search unit...">

                </div>

            </div>

            <div class="table-responsive">

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

                    <thead class="table-dark">
                        <tr>
                            <th width="60">#</th>
                            <th>Unit Name</th>
                            <th>Description</th>
                            <th width="90">Order</th>
                            <th width="110">Required</th>
                            <th width="110">Status</th>
                            <th width="110">Equipment</th>
                            <th width="130">Action</th>
                        </tr>
                    </thead>

                    <tbody>
                    <?php $number = 1; while ($unit = mysqli_fetch_assoc($units)) { ?>
                        <tr>
                            <td><?= $number++ ?></td>
                            <td><?= htmlspecialchars($unit['unit_name']) ?></td>
                            <td><?= htmlspecialchars($unit['description'] ?? '') ?></td>
                            <td><?= (int)$unit['display_order'] ?></td>
                            <td>
                                <span class="badge bg-<?= $unit['is_required'] === 'NO' ? 'primary' : 'secondary' ?>">
                                    <?= htmlspecialchars($unit['is_required']) ?>
                                </span>
                            </td>
                            <td>
                                <span class="badge bg-<?= $unit['status'] === 'Active' ? 'success' : 'danger' ?>">
                                    <?= htmlspecialchars($unit['status']) ?>
                                </span>
                            </td>
                            <td><?= (int)$unit['equipment_count'] ?></td>
                            <td>
                                <button type="button" class="btn btn-warning btn-sm edit-unit"
                                    data-id="<?= (int)$unit['id'] ?>"
                                    data-name="<?= htmlspecialchars($unit['unit_name'], ENT_QUOTES) ?>"
                                    data-description="<?= htmlspecialchars($unit['description'] ?? '', ENT_QUOTES) ?>"
                                    data-order="<?= (int)$unit['display_order'] ?>"
                                    data-required="<?= htmlspecialchars($unit['is_required'], ENT_QUOTES) ?>"
                                    data-status="<?= htmlspecialchars($unit['status'], ENT_QUOTES) ?>">
                                    <i class="fa-solid fa-pen-to-square"></i>
                                </button>
                                <button type="button" class="btn btn-danger btn-sm delete-unit" data-id="<?= (int)$unit['id'] ?>" 
                                    data-name="<?= htmlspecialchars($unit['unit_name'], ENT_QUOTES) ?>">
                                    <i class="fa-solid fa-trash"></i>
                                </button>
                            </td>
                        </tr>
                    <?php } ?>
                    </tbody>

                </table>

            </div>

        </div>

    </div>

</main>

<div class="modal fade" id="unitModal" tabindex="-1">
    <div class="modal-dialog">
        <form class="modal-content" id="unitForm">
            <div class="modal-header bg-primary text-white">
                <h5 class="modal-title" id="unitModalTitle">Add Unit</h5>
                <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal"></button>
            </div>
            <div class="modal-body">
                <input type="hidden" name="id" id="unit_id">
                <input type="hidden" name="csrf_token" value="<?= htmlspecialchars($_SESSION['csrf_token']) ?>">
                <div class="mb-3">
                    <label class="form-label">Unit Name</label>
                    <input type="text" class="form-control" name="unit_name" id="unit_name" maxlength="100" required>
                </div>
                <div class="mb-3">
                    <label class="form-label">Description</label>
                    <input type="text" class="form-control" name="description" id="description" maxlength="255">
                </div>
                <div class="row">
                    <div class="col-md-4 mb-3">
                        <label class="form-label">Order</label>
                        <input type="number" class="form-control" name="display_order" id="display_order" min="1" value="1" required>
                    </div>
                    <div class="col-md-4 mb-3">
                        <label class="form-label">Required</label>
                        <select class="form-select" name="is_required" id="is_required">
                            <option value="Yes">Yes</option><option value="No">No</option>
                        </select>
                    </div>
                    <div class="col-md-4 mb-3">
                        <label class="form-label">Status</label>
                        <select class="form-select" name="status" id="status">
                            <option value="Active">Active</option>
                            <option value="Inactive">Inactive</option>
                        </select>
                    </div>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
                <button class="btn btn-success">
                    <i class="fa-solid fa-floppy-disk"></i> 
                    Save Unit
                </button>
            </div>
        </form>
    </div>
</div>

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

<script src="<?= BASE_URL ?>assets/js/units.js?v=<?= SYSTEM_VERSION ?>"></script>
