<?php
///////////////////////////////////////////////////////////////////////////////
// Default filters — used if config.php or $portfolio_filters is absent
///////////////////////////////////////////////////////////////////////////////

$portfolio_filters_default = [
    [
        'label'   => 'Registrar',
        'group'   => 'Registrar',
        'columns' => ['d.registrar']
    ],
    [
        'label'   => 'Nameserver',
        'group'   => 'Nameserver',
        'columns' => ['d.ns1','d.ns2','d.ns3','d.ns4','d.ns5','d.ns6','d.ns7','d.ns8']
    ],
];

$config_file = __DIR__ . '/config.php';
if (is_file($config_file)) require $config_file;

// If filters are explicitly disabled in config, render nothing
if (isset($portfolio_filters_enabled) && $portfolio_filters_enabled === false) return;

$active_filters = isset($portfolio_filters) ? $portfolio_filters : $portfolio_filters_default;

// Strip disabled entries — explicit false or missing key means enabled
$active_filters = array_values(array_filter($active_filters, function($f) {
    return !isset($f['disable']) || $f['disable'] !== true;
}));

// If everything was disabled, fall back to defaults
if (empty($active_filters))
    $active_filters = $portfolio_filters_default;

// Derive type — default to 'text' if not set
foreach ($active_filters as &$f) {
    if (!isset($f['type'])) $f['type'] = 'text';
}
unset($f);

function pf_filter_id($label) {
    return 'pf-filter-' . strtolower(str_replace(' ', '-', $label));
}

// Group filters preserving order
$grouped = [];
foreach ($active_filters as $f) {
    $grouped[$f['group']][] = $f;
}
?>
<div id="domain-portfolio-filters">
    <div class="pf-filter-row">

        <!-- Expiration group — always present -->
        <div class="pf-filter-group">
            <div class="pf-filter-group-label">Expiration</div>
            <div class="pf-filter-group-fields">

                <div class="pf-filter-field">
                    <label class="pf-filter-label">Registry</label>
                    <select id="pf-filter-expiry" class="pf-filter-select">
                        <option value="">———</option>
                        <option value="30">Expires within 30 days</option>
                        <option value="60">Expires within 60 days</option>
                        <option value="90">Expires within 90 days</option>
                        <option value="120">Expires within 120 days</option>
                        <option value="365">Expires within 365 days</option>
                        <option value="expired">Expired</option>
                        <option value="expired_unknown">Expired or Unknown</option>
                        <option value="unknown">Unknown</option>
                    </select>
                </div>

                <div class="pf-filter-field">
                    <label class="pf-filter-label">Registrar</label>
                    <select id="pf-filter-registrar-expiry" class="pf-filter-select">
                        <option value="">———</option>
                        <option value="30">Expires within 30 days</option>
                        <option value="60">Expires within 60 days</option>
                        <option value="90">Expires within 90 days</option>
                        <option value="120">Expires within 120 days</option>
                        <option value="365">Expires within 365 days</option>
                        <option value="expired">Expired</option>
                        <option value="expired_unknown">Expired or Unknown</option>
                        <option value="unknown">Unknown</option>
                    </select>
                </div>

            </div>
        </div>

        <!-- Dynamic filter groups from config -->
        <?php foreach ($grouped as $groupName => $fields): ?>
        <div class="pf-filter-group">
            <div class="pf-filter-group-label"><?php echo htmlspecialchars($groupName); ?></div>
            <div class="pf-filter-group-fields">

                <?php foreach ($fields as $f):
                    $inputId   = pf_filter_id($f['label']);
                    $type      = $f['type'];
                    $showLabel = count($fields) > 1 || in_array($type, ['boolean', 'int', 'float']);
                ?>

                <?php if ($type === 'text'): ?>
                    <div class="pf-filter-field">
                        <?php if ($showLabel): ?>
                        <label class="pf-filter-label" for="<?php echo $inputId; ?>">
                            <?php echo htmlspecialchars($f['label']); ?>
                        </label>
                        <?php endif; ?>
                        <input type="text"
                               id="<?php echo $inputId; ?>"
                               class="pf-filter-input"
                               placeholder="Search…" />
                    </div>

                <?php elseif ($type === 'float' || $type === 'int'): ?>
                    <div class="pf-filter-field pf-filter-field-range">
                        <?php if ($showLabel): ?>
                        <label class="pf-filter-label">
                            <?php echo htmlspecialchars($f['label']); ?>
                        </label>
                        <?php endif; ?>
                        <div class="pf-range-inputs">
                            <input type="number"
                                   id="<?php echo $inputId; ?>-min"
                                   class="pf-filter-input pf-range-input"
                                   placeholder="Min"
                                   <?php if ($type === 'float') echo 'step="0.01"'; ?> />
                            <input type="number"
                                   id="<?php echo $inputId; ?>-max"
                                   class="pf-filter-input pf-range-input"
                                   placeholder="Max"
                                   <?php if ($type === 'float') echo 'step="0.01"'; ?> />
                        </div>
                    </div>

                <?php elseif ($type === 'boolean'): ?>
                    <div class="pf-filter-field">
                        <?php if ($showLabel): ?>
                        <label class="pf-filter-label" for="<?php echo $inputId; ?>">
                            <?php echo htmlspecialchars($f['label']); ?>
                        </label>
                        <?php endif; ?>
                        <select id="<?php echo $inputId; ?>" class="pf-filter-select">
                            <option value="">———</option>
                            <option value="1">Yes</option>
                            <option value="0">No</option>
                        </select>
                    </div>

                <?php endif; ?>

                <?php endforeach; ?>

            </div>
        </div>
        <?php endforeach; ?>

    </div>
</div>

<!-- Emit filter config for JS -->
<script>
var PF_FILTERS = <?php echo json_encode(array_map(function($f) {
    return [
        'id'      => pf_filter_id($f['label']),
        'type'    => $f['type'],
        'columns' => $f['columns']
    ];
}, $active_filters), JSON_UNESCAPED_UNICODE); ?>;
</script>