<?php
// Prevent direct access.
if (!defined('ABSPATH')) {
    exit;
}

/**
 * HTML template for å vise header på Firewall Rules siden.
 * 
 * Forventer å motta følgende variabler:
 * 
 * $msg = Eventuell melding som skal vises i header, feks tilbake melding om at en regel ble lagret.
 * $ruleList = HTML som settes inn i tabellen for å vise firewall reglene.
 */
?>
<div class="wrap wtb-firewall-admin">
    <h1 class="wp-heading-inline">Firewall Rules</h1>

    <?php echo wp_kses_post($msg); ?>  

    <div class="card">            
        <h2>Add New Rule</h2>

        <form method="post" action="<?php echo esc_url(admin_url('admin-post.php')); ?>">
            <input type="hidden" name="action" value="wtb_firewall_add_rule" />
            <?php wp_nonce_field('wtb_firewall_add_rule', 'wtb_firewall_nonce'); ?>

            <table class="form-table" role="presentation">
                <tbody>                       
                    <tr>
                        <th>
                            <strong>Action</strong>
                        </th>
                        <td>
                            <fieldset>
                                <input type="radio" name="rule_action" value="block" checked />Block
                                <br /><br />
                                <input type="radio" name="rule_action" value="allow" />Allow
                            </fieldset>
                        </td>
                    </tr>

                    <tr>
                        <th>
                            <strong>IP / Range</strong>
                        </th>
                        <td>
                            <input type="text" class="regular-text" name="ip_input" placeholder="See examples below for format." required />                            

                            <details style="margin-top: 8px;">
                                <summary>Examples</summary>
                                <ul style="margin: 8px 0 0 18px;">
                                    <li><strong>Single IP:</strong> <code>192.168.0.1 or fd12:3456:789a::1</code></li>
                                    <li><strong>IPv4 range:</strong> <code>192.168.0.1/24 or 192.168.0.1-192.168.1.50</code></li>
                                    <li><strong>IPv6 range:</strong> <code>fd12:3456:789a::/48</code></li>                                    
                                </ul>
                            </details>
                        </td>
                    </tr>

                    <tr>
                        <th>
                            <strong>Rule Scope</strong>
                        </th>
                        <td>
                            <select name="rule_scope">
                                <option value="permanent">Permanent</option>
                                <option value="temporary">Temporary (auto-expire)</option>
                            </select>
                            <p class="description">
                                Temporary rules can auto-disable after a set time.
                            </p>
                            <br/>
                            Duration (if temporary)<br />
                            <input type="text" class="regular-text" name="rule_duration" placeholder="45m or 12h or 4d"/>
                        </td>
                    </tr>

                    <tr>
                        <th>
                            <strong>Reason / Note</strong>
                        </th>
                        <td>
                            <textarea name="rule_note" class="large-text" rows="2" placeholder="Spam signup attempts, XML-RPC brute force, CEO IP, etc."></textarea>
                        </td>
                    </tr>

                    <tr>
                        <th>
                            <strong>Status</strong>
                        </th>
                        <td>  
                            <input type="checkbox" name="rule_enabled" value="1" checked />
                            Enable this rule now
                        </td>
                    </tr>

                </tbody>
            </table>

            <p>
                <button type="submit" class="button button-primary">
                    Add Rule
                </button>
            </p>

        </form>        
    </div>

    <br><br>

    <!-- Bulk actions bar -->    
    <form id="firewallBulkAction" method="post" action="<?php echo esc_url(admin_url('admin-post.php')); ?>">
        <input type="hidden" name="action" value="wtb_firewall_bulk_action" />
        <?php wp_nonce_field('wtb_firewall_bulk_action', 'wtb_firewall_nonce'); ?>
    </form>

    <div style="margin:16px 0; display:flex; justify-content:space-between; align-items:center; flex-wrap:wrap; gap:8px;">
        <div>
            <select name="bulk_action" form="firewallBulkAction" required>
                <option value="">Bulk actions</option>
                <option value="enable">Enable</option>
                <option value="disable">Disable</option>
                <option value="delete">Delete</option>
            </select>
            <button type="submit" class="button action" form="firewallBulkAction">Apply</button>
        </div>                
    </div>


    <table class="wp-list-table widefat fixed striped">
        <thead>
            <tr>
                <th scope="col" class="manage-column column-cb check-column"><input type="checkbox" /></th>                
                <th scope="col" class="manage-column column-primary column-ip_range">IP / Range</th>
                <th scope="col" class="manage-column column-action">Action</th>
                <th scope="col" class="manage-column column-status">Status</th>
                <th scope="col" class="manage-column column-added">Added</th>
                <th scope="col"class="manage-column column-last_blocked">Last Blocked</th>
                <th scope="col"class="manage-column column-expires">Expires</th>
                <th scope="col"class="manage-column column-note">Note</th>
            </tr>
        </thead>

        <tbody>
            <?php
            $allowed_html = wp_kses_allowed_html('post');

            $allowed_html['input'] = array(
                'form' => true,
                'type' => true,
                'name' => true,
                'value' => true,                                           
            );
            echo wp_kses($ruleList, $allowed_html);
            ?>
        </tbody>
    </table>

</div>
