<?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 - Kontrola dostępu IP (admin)
 */

require_once __DIR__ . '/../inc/ip_access.php';

$config = ip_access_load();
$msg = '';
$msg_type = '';

// Obsługa zapisu
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['save_access'])) {
    if (ibok_is_demo()) {
        $msg = __('demo.save_blocked');
        $msg_type = 'warning';
    } elseif (!csrf_validate()) {
        $msg = __('login.error_csrf');
        $msg_type = 'danger';
    } else {
        $config['client_enabled'] = !empty($_POST['client_enabled']);
        $config['admin_enabled'] = !empty($_POST['admin_enabled']);

        // Parse IP lists (one per line)
        $config['client_allowed_ips'] = array_values(array_filter(
            array_map('trim', explode("\n", $_POST['client_ips'] ?? '')),
            function($v) { return $v !== ''; }
        ));
        $config['admin_allowed_ips'] = array_values(array_filter(
            array_map('trim', explode("\n", $_POST['admin_ips'] ?? '')),
            function($v) { return $v !== ''; }
        ));

        if (ip_access_save($config)) {
            $msg = __('access_control.save_success');
            $msg_type = 'success';
        } else {
            $msg = __('common.error');
            $msg_type = 'danger';
        }
    }
}

$emergency = ip_access_emergency_active();
$current_ip = $_SERVER['REMOTE_ADDR'] ?? '';
?>

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

    <?= ibok_help_section('admin_access', __('help.show_help'), __('help_admin.access_section_content')) ?>

    <?php if ($msg): ?>
        <div class="alert alert-<?= $msg_type ?> alert-dismissible fade show">
            <?= e($msg) ?>
            <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
        </div>
    <?php endif; ?>

    <?php if ($emergency): ?>
        <div class="alert alert-warning">
            <i class="bi bi-exclamation-triangle me-1"></i>
            <?= __('access_control.emergency_active') ?>
        </div>
    <?php endif; ?>

    <div class="alert alert-info small">
        <i class="bi bi-info-circle me-1"></i>
        <?= __('access_control.current_ip') ?>: <strong class="font-monospace"><?= e($current_ip) ?></strong>
    </div>

    <form method="post">
        <?= csrf_field() ?>
        <input type="hidden" name="save_access" value="1">

        <div class="row g-4">
            <!-- Panel klienta -->
            <div class="col-lg-6">
                <div class="card">
                    <div class="card-header">
                        <i class="bi bi-person me-1"></i> <?= __('access_control.client_panel') ?>
                    </div>
                    <div class="card-body">
                        <div class="form-check form-switch mb-3">
                            <input class="form-check-input" type="checkbox" id="clientEnabled" name="client_enabled" value="1"
                                   <?= $config['client_enabled'] ? 'checked' : '' ?>>
                            <label class="form-check-label" for="clientEnabled">
                                <?= __('access_control.restrictions_enabled') ?>
                            </label>
                        </div>
                        <div class="mb-2">
                            <label class="form-label"><?= __('access_control.allowed_ips') ?></label>
                            <textarea class="form-control font-monospace" name="client_ips" rows="8"
                                      placeholder="<?= __('access_control.placeholder') ?>"><?= e(implode("\n", $config['client_allowed_ips'])) ?></textarea>
                            <small class="form-text text-muted"><?= __('access_control.one_per_line') ?></small>
                        </div>
                    </div>
                </div>
            </div>

            <!-- Panel admina -->
            <div class="col-lg-6">
                <div class="card">
                    <div class="card-header">
                        <i class="bi bi-shield-lock me-1"></i> <?= __('access_control.admin_panel') ?>
                    </div>
                    <div class="card-body">
                        <div class="form-check form-switch mb-3">
                            <input class="form-check-input" type="checkbox" id="adminEnabled" name="admin_enabled" value="1"
                                   <?= $config['admin_enabled'] ? 'checked' : '' ?>>
                            <label class="form-check-label" for="adminEnabled">
                                <?= __('access_control.restrictions_enabled') ?>
                            </label>
                        </div>
                        <div class="mb-2">
                            <label class="form-label"><?= __('access_control.allowed_ips') ?></label>
                            <textarea class="form-control font-monospace" name="admin_ips" rows="8"
                                      placeholder="<?= __('access_control.placeholder') ?>"><?= e(implode("\n", $config['admin_allowed_ips'])) ?></textarea>
                            <small class="form-text text-muted"><?= __('access_control.one_per_line') ?></small>
                        </div>
                    </div>
                </div>
            </div>
        </div>

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

    <!-- Emergency access info -->
    <div class="card mt-4">
        <div class="card-header">
            <i class="bi bi-life-preserver me-1"></i> <?= __('access_control.emergency_title') ?>
        </div>
        <div class="card-body">
            <p class="mb-2"><?= __('access_control.emergency_desc') ?></p>
            <code>touch cfg/.emergency_access</code>
            <p class="mt-2 mb-0 small text-muted"><?= __('access_control.emergency_remove') ?></p>
            <code>rm cfg/.emergency_access</code>
        </div>
    </div>
</div>
