<?php
/**
 * (c) ININOUT & PYXIS. All Rights Reserved.
 * Wszelkie modyfikacje tego pliku wymagaja zgloszenia oraz zgody autorow.
 * Any modifications to this file require notification and consent of the authors.
 */
/**
 * i-BOK 5 - Ustawienia funkcji panelu klienta
 */

$save_msg = '';
$save_type = 'success';

// Wczytaj aktualne ustawienia
$cfg_file = IBOK_CFG_DIR . '/features.cfg';
$features = file_exists($cfg_file) ? (array)@json_decode(file_get_contents($cfg_file), true) : [];

// Zapis
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['save_features'])) {
    if (ibok_is_demo()) {
        $save_msg = __('demo.save_blocked');
        $save_type = 'warning';
    } elseif (!csrf_validate()) {
        $save_msg = __('login.error_csrf');
        $save_type = 'danger';
    } else {
        // sms: nie zmieniaj z GUI, tylko z CLI (cfg/features.cfg)
        $features['hide_speed'] = !empty($_POST['hide_speed']);
        $features['hide_device_ip'] = !empty($_POST['hide_device_ip']);
        $features['speed_from_devices'] = !empty($_POST['speed_from_devices']);
        $features['password_min_length'] = max(4, min(32, (int)($_POST['password_min_length'] ?? 6)));

        $result = ibok_save_features($features);
        if ($result === true) {
            $save_msg = __('features.saved');
            ibok_log("Ustawienia funkcji zapisane", 'info');
        } else {
            $save_msg = $result;
            $save_type = 'danger';
        }
    }
}
?>

<div class="fade-in">
    <h4 class="mb-4">
        <i class="bi bi-toggles me-2"></i> <?= __('features.title') ?>
    </h4>

    <?php if ($save_msg): ?>
        <div class="alert alert-<?= $save_type ?> alert-dismissible fade show">
            <?php if ($save_type === 'danger'): ?>
                <i class="bi bi-exclamation-triangle me-1"></i>
            <?php else: ?>
                <i class="bi bi-check-circle me-1"></i>
            <?php endif; ?>
            <?= e($save_msg) ?>
            <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
        </div>
    <?php endif; ?>

    <form method="post" action="admin.php?page=features">
        <?= csrf_field() ?>
        <input type="hidden" name="save_features" value="1">

        <!-- Panel klienta -->
        <div class="card mb-4">
            <div class="card-header">
                <i class="bi bi-person me-1"></i> <?= __('features.section_client_panel') ?>
            </div>
            <div class="card-body">
                <div class="mb-3">
                    <div class="form-check form-switch">
                        <input class="form-check-input" type="checkbox" id="hide_speed" name="hide_speed"
                               <?= !empty($features['hide_speed']) ? 'checked' : '' ?>>
                        <label class="form-check-label" for="hide_speed">
                            <strong><?= __('features.hide_speed') ?></strong>
                        </label>
                    </div>
                    <small class="text-muted ms-4"><?= __('features.hide_speed_desc') ?></small>
                </div>

                <div class="mb-3">
                    <div class="form-check form-switch">
                        <input class="form-check-input" type="checkbox" id="hide_device_ip" name="hide_device_ip"
                               <?= !empty($features['hide_device_ip']) ? 'checked' : '' ?>>
                        <label class="form-check-label" for="hide_device_ip">
                            <strong><?= __('features.hide_device_ip') ?></strong>
                        </label>
                    </div>
                    <small class="text-muted ms-4"><?= __('features.hide_device_ip_desc') ?></small>
                </div>

                <div class="mb-3">
                    <div class="form-check form-switch">
                        <input class="form-check-input" type="checkbox" id="speed_from_devices" name="speed_from_devices"
                               <?= !empty($features['speed_from_devices']) ? 'checked' : '' ?>>
                        <label class="form-check-label" for="speed_from_devices">
                            <strong><?= __('features.speed_from_devices') ?></strong>
                        </label>
                    </div>
                    <small class="text-muted ms-4"><?= __('features.speed_from_devices_desc') ?></small>
                </div>

                <hr>

                <div class="mb-3">
                    <label for="password_min_length" class="form-label">
                        <strong><?= __('features.password_min_length') ?></strong>
                    </label>
                    <input type="number" class="form-control" id="password_min_length" name="password_min_length"
                           value="<?= (int)($features['password_min_length'] ?? 6) ?>" min="4" max="32" style="max-width: 100px;">
                </div>
            </div>
        </div>

        <button type="submit" class="btn btn-primary">
            <i class="bi bi-check-lg me-1"></i> <?= __('common.save') ?>
        </button>
    </form>
</div>
