<?php
include 'db.php';

$category_id = intval($_GET['category_id'] ?? 0);
if (!$category_id) exit("Invalid category ID.");

// Load settings
$sql = "SELECT * FROM settings WHERE category_id = $category_id AND is_active = 1 ORDER BY name";
$result = mysqli_query($conn, $sql);
if (!$result) {
    exit("Error loading settings.");
}

// Load select options
$options = [];
$opt_result = mysqli_query($conn, "SELECT * FROM setting_options");
while ($opt = mysqli_fetch_assoc($opt_result)) {
    $options[$opt['setting_id']][] = $opt;
}

// Display settings
while ($setting = mysqli_fetch_assoc($result)):
    $val = $setting['value'] ?? $setting['default_value'];
?>
<div class="col-xxl-4 col-xl-6 col-lg-12 col-md-6 d-flex mb-3">
    <div class="connected-app-card d-flex w-100">
        <ul class="w-100">
            <li class="flex-column align-items-start">
                <div class="d-flex align-items-center justify-content-between w-100">
                    <div class="security-type d-flex align-items-center">
                        <span class="system-app-icon">
                            <img src="assets/img/icons/settings.svg" alt="">
                        </span>
                        <div class="security-title ms-2">
                            <h5><?= htmlspecialchars($setting['name']) ?></h5>
                        </div>
                    </div>

                    <?php if ($setting['type'] === 'boolean'): ?>
                        <div class="status-toggle d-flex align-items-center ms-2">
                            <input type="checkbox"
                                   id="setting_<?= $setting['id'] ?>"
                                   class="check setting-toggle"
                                   data-id="<?= $setting['id'] ?>"
                                   <?= $val == '1' ? 'checked' : '' ?>>
                            <label for="setting_<?= $setting['id'] ?>" class="checktoggle"></label>
                        </div>
                    <?php endif; ?>
                </div>

                <p><?= htmlspecialchars($setting['description']) ?></p>

                <?php if (in_array($setting['type'], ['text', 'number'])): ?>
                    <input type="<?= $setting['type'] ?>" class="form-control mt-2"
                           value="<?= htmlspecialchars($val) ?>" disabled>
                <?php elseif ($setting['type'] === 'select' && isset($options[$setting['id']])): ?>
                    <select class="form-select mt-2" disabled>
                        <?php foreach ($options[$setting['id']] as $opt): ?>
                            <option value="<?= $opt['option_value'] ?>" <?= $opt['option_value'] == $val ? 'selected' : '' ?>>
                                <?= htmlspecialchars($opt['option_label']) ?>
                            </option>
                        <?php endforeach; ?>
                    </select>
                <?php endif; ?>
            </li>
        </ul>
    </div>
</div>
<?php endwhile; ?>
