<?php
if (!defined('ABSPATH')) exit;
use auctionforge\inc\controllers\StyleSettings;
?>
<div class="wrap bs-style-settings">
    <h1><span class="dashicons dashicons-art" style="font-size:28px;margin-right:8px;"></span> Bidspirit Style Settings</h1>
    <p class="description">Customize the appearance of your auction plugin. Changes are applied as CSS overrides — no existing files are modified.</p>

    <div class="bs-style-header-actions">
        <button type="button" class="button button-secondary" id="bs-reset-all">
            <span class="dashicons dashicons-image-rotate" style="margin-top:4px;"></span> Reset All to Defaults
        </button>
        <a href="<?php echo esc_url(home_url('/')); ?>" target="_blank" class="button button-secondary">
            <span class="dashicons dashicons-visibility" style="margin-top:4px;"></span> Preview Site
        </a>
    </div>

    <div class="bs-style-wrap">
        <nav class="bs-style-tabs">
            <?php $first = true; ?>
            <?php foreach ($sections as $sectionKey => $section): ?>
                <a href="#" class="bs-style-tab <?php echo $first ? 'active' : ''; ?>" data-tab="<?php echo esc_attr($sectionKey); ?>">
                    <span class="dashicons <?php echo esc_attr($section['icon']); ?>"></span>
                    <span class="tab-label"><?php echo esc_html($section['label']); ?></span>
                </a>
                <?php $first = false; ?>
            <?php endforeach; ?>
        </nav>

        <div class="bs-style-content">
            <form id="bs-style-form">
                <?php $first = true; ?>
                <?php foreach ($sections as $sectionKey => $section): ?>
                    <div class="bs-style-panel <?php echo $first ? 'active' : ''; ?>" data-panel="<?php echo esc_attr($sectionKey); ?>">
                        <div class="bs-panel-header">
                            <h2><?php echo esc_html($section['label']); ?></h2>
                            <button type="button" class="button button-link bs-reset-section" data-section="<?php echo esc_attr($sectionKey); ?>">
                                <span class="dashicons dashicons-image-rotate" style="margin-top:4px;"></span> Reset Section
                            </button>
                        </div>

                        <table class="form-table bs-style-table">
                            <tbody>
                            <?php foreach ($section['fields'] as $fieldKey => $field): ?>
                                <tr>
                                    <th scope="row">
                                        <label for="bs_<?php echo esc_attr($fieldKey); ?>"><?php echo esc_html($field['label']); ?></label>
                                    </th>
                                    <td>
                                        <?php
                                        $value = isset($styles[$fieldKey]) ? $styles[$fieldKey] : '';
                                        $defaultVal = isset(StyleSettings::getDefaults()[$fieldKey]) ? StyleSettings::getDefaults()[$fieldKey] : '';

                                        switch ($field['type']):
                                            case 'color': ?>
                                                <input type="text"
                                                       id="bs_<?php echo esc_attr($fieldKey); ?>"
                                                       name="styles[<?php echo esc_attr($fieldKey); ?>]"
                                                       value="<?php echo esc_attr($value); ?>"
                                                       class="bs-color-picker"
                                                       data-default-color="<?php echo esc_attr($defaultVal); ?>" />
                                                <?php break;

                                            case 'color_alpha': ?>
                                                <div class="bs-alpha-color-wrap">
                                                    <input type="text"
                                                           id="bs_<?php echo esc_attr($fieldKey); ?>"
                                                           name="styles[<?php echo esc_attr($fieldKey); ?>]"
                                                           value="<?php echo esc_attr($value); ?>"
                                                           class="bs-color-picker-alpha"
                                                           data-default-color="<?php echo esc_attr($defaultVal); ?>" />
                                                    <p class="description">Supports rgba, e.g. <code>rgba(0,0,0,0.16)</code></p>
                                                </div>
                                                <?php break;

                                            case 'font': ?>
                                                <select id="bs_<?php echo esc_attr($fieldKey); ?>"
                                                        name="styles[<?php echo esc_attr($fieldKey); ?>]"
                                                        class="bs-font-select">
                                                    <?php foreach ($fonts as $fontValue => $fontLabel): ?>
                                                        <option value="<?php echo esc_attr($fontValue); ?>"
                                                                style="font-family: <?php echo esc_attr($fontValue); ?>"
                                                                <?php selected($value, $fontValue); ?>>
                                                            <?php echo esc_html($fontLabel); ?>
                                                        </option>
                                                    <?php endforeach; ?>
                                                </select>
                                                <span class="bs-font-preview" style="font-family: <?php echo esc_attr($value); ?>">
                                                    The quick brown fox jumps over the lazy dog
                                                </span>
                                                <?php break;

                                            case 'number': ?>
                                                <input type="number"
                                                       id="bs_<?php echo esc_attr($fieldKey); ?>"
                                                       name="styles[<?php echo esc_attr($fieldKey); ?>]"
                                                       value="<?php echo esc_attr($value); ?>"
                                                       class="small-text"
                                                       min="<?php echo esc_attr($field['min'] ?? 0); ?>"
                                                       max="<?php echo esc_attr($field['max'] ?? 100); ?>" />
                                                <span class="description">px</span>
                                                <?php break;

                                            case 'select': ?>
                                                <select id="bs_<?php echo esc_attr($fieldKey); ?>"
                                                        name="styles[<?php echo esc_attr($fieldKey); ?>]">
                                                    <?php foreach ($field['options'] as $optVal => $optLabel): ?>
                                                        <option value="<?php echo esc_attr($optVal); ?>" <?php selected($value, $optVal); ?>>
                                                            <?php echo esc_html($optLabel); ?>
                                                        </option>
                                                    <?php endforeach; ?>
                                                </select>
                                                <?php break;

                                            case 'shadow': ?>
                                                <select id="bs_<?php echo esc_attr($fieldKey); ?>"
                                                        name="styles[<?php echo esc_attr($fieldKey); ?>]"
                                                        class="bs-shadow-select">
                                                    <?php foreach ($shadows as $sVal => $sLabel): ?>
                                                        <option value="<?php echo esc_attr($sVal); ?>" <?php selected($value, $sVal); ?>>
                                                            <?php echo esc_html($sLabel); ?>
                                                        </option>
                                                    <?php endforeach; ?>
                                                </select>
                                                <div class="bs-shadow-preview">
                                                    <div class="bs-shadow-preview-box" data-shadow-field="<?php echo esc_attr($fieldKey); ?>"></div>
                                                </div>
                                                <?php break;

                                        endswitch; ?>
                                    </td>
                                </tr>
                            <?php endforeach; ?>
                            </tbody>
                        </table>
                    </div>
                    <?php $first = false; ?>
                <?php endforeach; ?>

                <div class="bs-style-footer">
                    <button type="submit" class="button button-primary button-hero" id="bs-save-styles">
                        Save All Styles
                    </button>
                    <span class="bs-save-status"></span>
                </div>
            </form>
        </div>
    </div>
</div>
