<?php
require_once 'includes/auth.php';
require_role(['admin', 'inspector']);

require_once 'db.php';

$message = '';
$messageType = '';

// Handle New Question
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['add_master_item'])) {
    require_csrf();

    $cat  = trim($_POST['category'] ?? 'LLG');
    $item = trim($_POST['check_item'] ?? '');

    if (!empty($item)) {
        $stmt = $pdo->prepare("INSERT INTO tbl_master_checklists (Category, CheckItem) VALUES (?, ?)");
        $stmt->execute([$cat, $item]);
        $message = "New master check item added!";
        $messageType = "success";
    }
}

// Handle Delete
if (isset($_GET['delete'])) {
    require_csrf();
    $delId = intval($_GET['delete']);
    $stmt = $pdo->prepare("DELETE FROM tbl_master_checklists WHERE MasterCheckID = ?");
    $stmt->execute([$delId]);
    header("Location: master_checklists.php");
    exit;
}

// Fetch all Master Checklist Items
$items = $pdo->query("SELECT * FROM tbl_master_checklists ORDER BY Category, DisplayOrder ASC")->fetchAll();
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Master Checklist Management - MEHOK SYSTEM</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
</head>
<body class="bg-light">

<nav class="navbar navbar-expand-lg navbar-dark bg-dark mb-4">
    <div class="container-fluid">
        <a class="navbar-brand fw-bold" href="index.php"><i class="fa-solid fa-square-check text-warning me-2"></i>MEHOK SYSTEM</a>
        <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#masterChecklistsNav" aria-controls="masterChecklistsNav" aria-expanded="false" aria-label="Toggle navigation">
            <span class="navbar-toggler-icon"></span>
        </button>
        <div class="collapse navbar-collapse" id="masterChecklistsNav">
            <ul class="navbar-nav me-auto">
                <li class="nav-item"><a class="nav-link" href="index.php">Task Bar</a></li>
                <li class="nav-item"><a class="nav-link active" href="master_checklists.php">Master Checklists</a></li>
            </ul>
        </div>
    </div>
</nav>

<div class="container">
    <div class="d-flex justify-content-between align-items-center mb-3">
        <h3 class="fw-bold">Master Checklist Questions Bank</h3>
        <a href="index.php" class="btn btn-outline-secondary">Back to Task Bar</a>
    </div>

    <?php if (!empty($message)): ?>
        <div class="alert alert-<?= $messageType; ?>"><?= $message; ?></div>
    <?php endif; ?>

    <div class="card p-3 mb-4">
        <h5 class="fw-bold mb-3">Add New Standard Check Question</h5>
        <form action="master_checklists.php" method="POST" class="row g-2">
            <input type="hidden" name="add_master_item" value="1">
            <?php csrf_field(); ?>
            <div class="col-md-3">
                <select name="category" class="form-select">
                    <option value="LLG">LLG (Lifting Gear)</option>
                    <option value="Appliance">Appliance</option>
                </select>
            </div>
            <div class="col-md-7">
                <input type="text" name="check_item" class="form-control" placeholder="Standard inspection question description..." required>
            </div>
            <div class="col-md-2">
                <button type="submit" class="btn btn-primary w-100 fw-bold">Add Item</button>
            </div>
        </form>
    </div>

    <div class="card p-3">
        <h5 class="fw-bold mb-3">Existing Master Questions</h5>
        <div class="table-responsive">
        <table class="table table-bordered align-middle">
            <thead class="table-dark">
                <tr>
                    <th>Category</th>
                    <th>Standard Question Description</th>
                    <th class="text-center" style="width: 10%;">Action</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($items as $row): ?>
                    <tr>
                        <td><span class="badge bg-secondary"><?= htmlspecialchars($row['Category']); ?></span></td>
                        <td><?= htmlspecialchars($row['CheckItem']); ?></td>
                        <td class="text-center">
                            <a href="master_checklists.php?delete=<?= $row['MasterCheckID']; ?>&amp;csrf_token=<?= urlencode(csrf_token()); ?>" class="btn btn-sm btn-outline-danger" onclick="return confirm('Delete this standard question?')"><i class="fa-solid fa-trash"></i></a>
                        </td>
                    </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
        </div>
    </div>
</div>

</body>
</html>
