{extends file="layout.tpl"}

{block name="content"}
<div class="container-fluid">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <div>
            <h4 class="mb-1">Reguły IP</h4>
            <p class="text-muted mb-0">Zarządzaj whitelist i blacklist adresów IP</p>
        </div>
        <button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#addRuleModal">
            <i class="bi bi-plus-lg me-1"></i>Dodaj regułę
        </button>
    </div>

    <!-- Statystyki -->
    <div class="row g-4 mb-4">
        <div class="col-md-4">
            <div class="card bg-danger bg-opacity-10 border-danger">
                <div class="card-body">
                    <h6 class="text-danger">Zablokowane IP (automatycznie)</h6>
                    <h2 class="mb-0">{$blockedIps|count}</h2>
                </div>
            </div>
        </div>
        <div class="col-md-4">
            <div class="card bg-dark bg-opacity-10">
                <div class="card-body">
                    <h6>Blacklist</h6>
                    <h2 class="mb-0">{$stats.ip_rules.blacklist|default:0}</h2>
                </div>
            </div>
        </div>
        <div class="col-md-4">
            <div class="card bg-success bg-opacity-10 border-success">
                <div class="card-body">
                    <h6 class="text-success">Whitelist</h6>
                    <h2 class="mb-0">{$stats.ip_rules.whitelist|default:0}</h2>
                </div>
            </div>
        </div>
    </div>

    {if $blockedIps|count > 0}
    <div class="card mb-4">
        <div class="card-header bg-danger bg-opacity-10">
            <h5 class="card-title mb-0 text-danger"><i class="bi bi-shield-x me-2"></i>Automatycznie zablokowane IP</h5>
        </div>
        <div class="card-body p-0">
            <div class="table-responsive">
                <table class="table table-hover mb-0">
                    <thead>
                        <tr>
                            <th>IP</th>
                            <th>Próba użytkownika</th>
                            <th>Próby</th>
                            <th>Zablokowane do</th>
                            <th>Akcje</th>
                        </tr>
                    </thead>
                    <tbody>
                        {foreach $blockedIps as $blocked}
                        <tr>
                            <td><code>{$blocked.ip_address}</code></td>
                            <td>{$blocked.username|default:'-'}</td>
                            <td><span class="badge bg-danger">{$blocked.attempts}</span></td>
                            <td>{$blocked.blocked_until|date_format:"%d.%m.%Y %H:%M"}</td>
                            <td>
                                <form method="post" action="{$baseUrl}/security/unblock-ip" style="display:inline;">
                                    <input type="hidden" name="csrf_token" value="{$csrfToken}">
                                    <input type="hidden" name="ip" value="{$blocked.ip_address}">
                                    <button type="submit" class="btn btn-sm btn-outline-success" onclick="return confirm('Odblokować to IP?')">
                                        <i class="bi bi-unlock"></i> Odblokuj
                                    </button>
                                </form>
                            </td>
                        </tr>
                        {/foreach}
                    </tbody>
                </table>
            </div>
        </div>
    </div>
    {/if}

    <div class="card">
        <div class="card-header d-flex justify-content-between align-items-center">
            <h5 class="card-title mb-0"><i class="bi bi-shield-lock me-2"></i>Reguły IP</h5>
            <div class="btn-group btn-group-sm">
                <a href="?type=" class="btn btn-outline-secondary {if !$filterType}active{/if}">Wszystkie</a>
                <a href="?type=blacklist" class="btn btn-outline-secondary {if $filterType == 'blacklist'}active{/if}">Blacklist</a>
                <a href="?type=whitelist" class="btn btn-outline-secondary {if $filterType == 'whitelist'}active{/if}">Whitelist</a>
            </div>
        </div>
        <div class="card-body p-0">
            <div class="table-responsive">
                <table class="table table-hover mb-0">
                    <thead>
                        <tr>
                            <th>IP</th>
                            <th>Typ</th>
                            <th>Powód</th>
                            <th>Wygasa</th>
                            <th>Dodał</th>
                            <th>Data</th>
                            <th>Akcje</th>
                        </tr>
                    </thead>
                    <tbody>
                        {foreach $rules as $rule}
                        <tr>
                            <td><code>{$rule.ip_address}</code></td>
                            <td>
                                {if $rule.rule_type == 'blacklist'}
                                <span class="badge bg-danger">Blacklist</span>
                                {else}
                                <span class="badge bg-success">Whitelist</span>
                                {/if}
                            </td>
                            <td>{$rule.reason|default:'-'|truncate:30}</td>
                            <td>
                                {if $rule.expires_at}
                                {$rule.expires_at|date_format:"%d.%m.%Y"}
                                {else}
                                <span class="text-muted">Nigdy</span>
                                {/if}
                            </td>
                            <td>{$rule.created_by_username|default:'System'}</td>
                            <td>{$rule.created_at|date_format:"%d.%m.%Y"}</td>
                            <td>
                                <form method="post" action="{$baseUrl}/security/ip-rules/delete" style="display:inline;">
                                    <input type="hidden" name="csrf_token" value="{$csrfToken}">
                                    <input type="hidden" name="id" value="{$rule.id}">
                                    <button type="submit" class="btn btn-sm btn-outline-danger" onclick="return confirm('Usunąć tę regułę?')">
                                        <i class="bi bi-trash"></i>
                                    </button>
                                </form>
                            </td>
                        </tr>
                        {foreachelse}
                        <tr>
                            <td colspan="7" class="text-center py-4 text-muted">Brak reguł IP</td>
                        </tr>
                        {/foreach}
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>

<!-- Modal dodawania reguły -->
<div class="modal fade" id="addRuleModal" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content">
            <form method="post" action="{$baseUrl}/security/ip-rules/add">
                <input type="hidden" name="csrf_token" value="{$csrfToken}">
                <div class="modal-header">
                    <h5 class="modal-title">Dodaj regułę IP</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                </div>
                <div class="modal-body">
                    <div class="mb-3">
                        <label class="form-label">Adres IP <span class="text-danger">*</span></label>
                        <input type="text" name="ip_address" class="form-control" required placeholder="np. 192.168.1.100">
                    </div>
                    <div class="mb-3">
                        <label class="form-label">Typ reguły</label>
                        <select name="rule_type" class="form-select">
                            <option value="blacklist">Blacklist (blokuj)</option>
                            <option value="whitelist">Whitelist (zezwól)</option>
                        </select>
                    </div>
                    <div class="mb-3">
                        <label class="form-label">Powód</label>
                        <input type="text" name="reason" class="form-control" placeholder="Opcjonalny powód">
                    </div>
                    <div class="mb-3">
                        <label class="form-label">Wygasa</label>
                        <input type="datetime-local" name="expires_at" class="form-control">
                        <small class="text-muted">Pozostaw puste dla reguły permanentnej</small>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Anuluj</button>
                    <button type="submit" class="btn btn-primary">Dodaj regułę</button>
                </div>
            </form>
        </div>
    </div>
</div>
{/block}
