<?php

namespace auctionforge\inc\controllers;

use DateTimeZone;
use auctionforge\inc\Debug;
use auctionforge\inc\helpers\SettingsHelper;
use auctionforge\inc\helpers\View;
use WP_Post;

class Settings
{
    const SLUG_PAGE = 'bidspirit_options';
    const SLUG_OPTION = 'bidspirit_settings';
    const SLUG_SECTION_ID = 'section_id';
    const FIELD_DESCRIPTION = 'description';
    const FIELD_PLACEHOLDER = 'placeholder';
    const VIEW_TEXT = 'admin.view.fields.text';
    const VIEW_TEXTAREA = 'admin.view.fields.textarea';
    const VIEW_SELECT = 'admin.view.fields.select';
    const VIEW_NUMBER = 'admin.view.fields.number';

    const MULTI_LANG_NAME = 'ml_full';
    const SYNC_FORCE_NAME = 'sync_force';
    const BIDS_IN_CATALOG = 'bids_in_catalog';
    const ONLY_FOR_USER_AUTH = 'only_for_user_auth';
    const EXTENDED_SORTING_OPTION = 'extended_sorting_option';
    const LOT_SLIDER_TEMPLATE = 'lot_slider_template';
    const LAYOUT_STYLE = 'layout_style';
    const CATALOG_LAYOUT = 'catalog_layout';
    const OLDEST_DAYS_NAME = 'oldest_days';
    const HIDE_UNSOLD_ITEMS = 'hide_unsold_items';
    const HIDE_EMPTY_AUCTION = 'hide_empty_auction';
    const AUTO_COMMIT_DISABLED = 'auto_commit_disabled';
    const POST_PARENT_USE = 'post_parent_use';
    const MIGRATE_SYNC = 'migrate_sync_disabled';
    const BIDDEN_NUMBER_IN_PROFILE = 'bidden_number_in_profile';
    const ENABLE_RECENTLY_VIEWED = 'enable_recently_viewed';
    const ENABLE_LOT_ANALYTICS = 'enable_lot_analytics';
    const SHOW_LOT_VIEW_COUNTER = 'show_lot_view_counter';
    const SHOW_BID_COUNT = 'show_bid_count';
    const SHOW_POPULAR_BADGE = 'show_popular_badge';
    const SHOW_APP_NOTIFICATION = 'show_app_notification';
    const ENABLE_BILLING_TAB = 'enable_billing_tab';
    const SHOW_TIMED_AUCTION_BADGE = 'show_timed_auction_badge';
    const ENABLE_TIMED_AUCTION_CLOSING_TEXT = 'enable_timed_auction_closing_text';
    const ENABLE_REFUND_POLICY = 'enable_refund_policy';
    const ENABLE_CHARGEBACK_WAIVER = 'enable_chargeback_waiver';
    const OPTION_TABLE_PER_OBJECT = "table_per_object";
    const OPTIONS_GROUP = "auctionforge_options";
    const OPTIONS_TIMEZONES = "time_zone_name";

    const OPTIONS_LANG = "lang";
    const OPTIONS_SECOND_LANG = "second_lang";
    const OPTIONS_OTHER_LANG = "other_lang_content";

    private $checkboxField = array();
    private $checkboxDefault = array();
    private $checkboxLabels = array();

    private $languages = array(
        "en" => "English",
        "he" => "עברית",
        "ru" => "Русский",
        "pt" => "Português",
        "it" => "Italiano",
        "ar" => "العَرَبِيَّة",
        "es" => "Español",
        "el" => "ελληνικά",
        "ro" => "Română",
        "zh" => "Chinese",
        "de" => "Deutsch",
        "fr" => "French",
    );

    /**
     * Settings constructor.
     */
    public function __construct()
    {
        // Initialize arrays without translations first
        $this->setCheckboxField(array());
        $this->setCheckboxLabels(array());
        $this->setCheckboxDefault(array(
            'tax_number_required' => 'off',
            'use_pagination' => 'off',
            'delete_hidden' => 'off',
            self::BIDS_IN_CATALOG => 'off',
            self::ONLY_FOR_USER_AUTH => 'off',
            self::MULTI_LANG_NAME => 'off',
            self::HIDE_UNSOLD_ITEMS => 'off',
            self::HIDE_EMPTY_AUCTION => 'off',
            self::AUTO_COMMIT_DISABLED => 'off',
//            self::MIGRATE_SYNC => 'off',
            self::BIDDEN_NUMBER_IN_PROFILE => 'off',
            self::EXTENDED_SORTING_OPTION => 'off',
            self::ENABLE_RECENTLY_VIEWED => 'off',
            self::ENABLE_LOT_ANALYTICS => 'on',
            self::SHOW_LOT_VIEW_COUNTER => 'off',
            self::SHOW_BID_COUNT => 'off',
            self::SHOW_POPULAR_BADGE => 'off',
            self::SHOW_APP_NOTIFICATION => 'off',
            self::ENABLE_BILLING_TAB => 'on',
            self::SHOW_TIMED_AUCTION_BADGE => 'off',
            self::ENABLE_TIMED_AUCTION_CLOSING_TEXT => 'off',
            self::ENABLE_REFUND_POLICY => 'off',
            self::ENABLE_CHARGEBACK_WAIVER => 'off',
        ));
    }

    private function initializeTranslations()
    {
        $this->setCheckboxField(array(
            'cdn_images' => __('Enable CDN for pictures', 'auctionforge'),
            'browser_time' => __('Show user browser time', 'auctionforge'),
            'polylang_time' => __('Display time relative to the user locale', 'auctionforge'),
            'force_show_estimated_price' => __('Show estimated price for all lots', 'auctionforge'),
            'sync_cheduled' => __('Use wp-cron', 'auctionforge'),
            self::AUTO_COMMIT_DISABLED => __('Auto commit mysql disabled', 'auctionforge'),
            'view_pdf' => __('Use auctions as a pdf catalog', 'auctionforge'),
            'accept_privacy_data' => __('Display "Accept personal data processing" in the form of registration',
                'auctionforge'),
            self::BIDS_IN_CATALOG => __('Bids in catalog', 'auctionforge'),
            self::ONLY_FOR_USER_AUTH => __('Features only for authorized users', 'auctionforge'),
            self::EXTENDED_SORTING_OPTION => __('Extended sorting option', 'auctionforge'),
            'display_vat_field' => __('Display VAT field', 'auctionforge'),
            'tax_number_required' => __('Tax number is required', 'auctionforge'),
            self::HIDE_UNSOLD_ITEMS => __('Hide unsold lots', 'auctionforge'),
            self::HIDE_EMPTY_AUCTION => __('Hide empty auctions', 'auctionforge'),
            'use_pagination' => __('Enable pagination', 'auctionforge'),
            'delete_hidden' => __('Delete hidden lots', 'auctionforge'),
            'sync_title' => __('Sync title', 'auctionforge'),
            'sync_desc' => __('Sync Description', 'auctionforge'),
            'sync_images' => __('Sync Images', 'auctionforge'),
//            self::MIGRATE_SYNC => __('Migrate sync off', 'auctionforge'),
            self::BIDDEN_NUMBER_IN_PROFILE => __('Bidder number', 'auctionforge'),
            self::ENABLE_RECENTLY_VIEWED => __('Enable user viewing history in profile page', 'auctionforge'),
            self::ENABLE_LOT_ANALYTICS => __('Enable lot analytics', 'auctionforge'),
            self::SHOW_LOT_VIEW_COUNTER => __('Show view counter on lots', 'auctionforge'),
            self::SHOW_BID_COUNT => __('Show bid count on lots', 'auctionforge'),
            self::SHOW_POPULAR_BADGE => __('Show Popular badge on lots', 'auctionforge'),
            self::SHOW_APP_NOTIFICATION => __('Show app notification checkbox', 'auctionforge'),
            self::ENABLE_BILLING_TAB => __('Enable My Invoices tab', 'auctionforge'),
            self::SHOW_TIMED_AUCTION_BADGE => __('Show Timed Auction badges', 'auctionforge'),
            self::ENABLE_TIMED_AUCTION_CLOSING_TEXT => __('Change text to "Auction will close in"', 'auctionforge'),
            self::ENABLE_REFUND_POLICY => __('Enable Refund & Return Policy', 'auctionforge'),
            self::ENABLE_CHARGEBACK_WAIVER => __('Enable Chargeback Waiver', 'auctionforge'),
        ));

        $this->setCheckboxLabels(array(
            'cdn_images' => __('Use CDN for images of lots (recommended)', 'auctionforge'),
            'browser_time' => __('Display the time of auctions using the system time the user\'s computer (option "Custom format display time" will be overridden)', 'auctionforge'),
            'polylang_time' => __('The language of the site selected by the user is taken into account, if not, then we take it from the browser settings', 'auctionforge'),
            'force_show_estimated_price' => __('Works in tandem with Bidspirit option "Show estimated price in items list"',
                'auctionforge'),
            'sync_cheduled' => __('Synchronize auction using wp-cron (use only as a last resort)', 'auctionforge'),
            self::BIDS_IN_CATALOG => __('Possibility to bid on the catalog page', 'auctionforge'),
            self::ONLY_FOR_USER_AUTH => __('Feature set will only be available to authorized users', 'auctionforge'),
            self::EXTENDED_SORTING_OPTION => __('Add more variations for sorting', 'auctionforge'),
            self::AUTO_COMMIT_DISABLED => __('Use only if sync is configured on the server side', 'auctionforge'),
            'view_pdf' => __('Replace auction to pdf catalog', 'auctionforge'),
            'accept_privacy_data' => __('I consent and accept the conditions on the processing of personal data', 'auctionforge'),
            'display_vat_field' => __('Display VAT fields on auctions', 'auctionforge'),
            'tax_number_required' => __('Display Tax Number fields on profile', 'auctionforge'),
            'use_pagination' => __('Enable pagination for auction page', 'auctionforge'),
            self::HIDE_UNSOLD_ITEMS => __('Hide unsold lots', 'auctionforge'),
            self::HIDE_EMPTY_AUCTION => __('Hide empty auctions on the auctions page', 'auctionforge'),
            'delete_hidden' => __('Hidden lots in bidspirit will be deleted', 'auctionforge'),
            'sync_title' => __('Determines whether lot headers will be synchronized with the bidspirit system', 'auctionforge'),
            'sync_desc' => __('Determines whether lot descriptions will be synchronized with the bidspirit system', 'auctionforge'),
            'sync_images' => __('Determines whether pictures will be synchronized with the bidspirit system', 'auctionforge'),
//            self::MIGRATE_SYNC => __('Disable migrate sync', 'auctionforge'),
            self::BIDDEN_NUMBER_IN_PROFILE => __('Show bidder number in profile page', 'auctionforge'),
            self::ENABLE_RECENTLY_VIEWED => __('Show a Recently Viewed Items tab on the profile page listing the last 20 viewed lots', 'auctionforge'),
            self::ENABLE_LOT_ANALYTICS => __('Track lot views and favorites for analytics, shortcodes, and admin dashboard', 'auctionforge'),
            self::SHOW_LOT_VIEW_COUNTER => __('Display a public view count on each lot page (requires lot analytics to be enabled)', 'auctionforge'),
            self::SHOW_BID_COUNT => __('Display a public bid count on lot pages and catalog cards (requires lot analytics to be enabled)', 'auctionforge'),
            self::SHOW_POPULAR_BADGE => __('Display a Popular badge on lot cards in the catalog when view count exceeds the hot lot threshold (requires lot analytics to be enabled)', 'auctionforge'),
            self::SHOW_APP_NOTIFICATION => __('Show the app notification opt-in checkbox on auction and lot pages (for future mobile app integration)', 'auctionforge'),
            self::ENABLE_BILLING_TAB => __('Show an Invoices tab on the profile page listing paid and unpaid invoices', 'auctionforge'),
            self::SHOW_TIMED_AUCTION_BADGE => __('Display a Timed Auction badge over automatic/online auction cards in the catalog', 'auctionforge'),
            self::ENABLE_TIMED_AUCTION_CLOSING_TEXT => __('For timed auctions, change the countdown text from "The auction will start in..." to "The auction will close in..."', 'auctionforge'),
            self::ENABLE_REFUND_POLICY => __('Require bidders to read and accept the Refund & Return Policy during auction approval (manage content in Legal Agreements page)', 'auctionforge'),
            self::ENABLE_CHARGEBACK_WAIVER => __('Require bidders to read and accept the Chargeback Waiver during auction approval (manage content in Legal Agreements page)', 'auctionforge'),
        ));
    }


    public function init()
    {
        $this->initializeTranslations();
        
        add_action('admin_menu', array($this, 'auction_register_admin_page'));
        add_action('admin_init', array($this, 'bidspirit_options_settings'));
    }

    public function auction_register_admin_page() {
        $pluginInfo = auctionforge_plugin_info();
        add_submenu_page(
            'edit.php?post_type=lot',
            __( 'Setting for page with auctions (' . $pluginInfo['ver'] . ')' ),
            __( 'Settings' ),
            'administrator',
            self::SLUG_PAGE,
            array($this, self::SLUG_PAGE)
        );
    }

    public function bidspirit_options() {
        echo View::render('admin.view.settings', array());
    }

    public function bidspirit_options_settings()
    {
        register_setting('option_group', self::SLUG_OPTION, array($this, 'sanitize_bidspirit_callback'));

        add_settings_section(self::SLUG_SECTION_ID, '', '', self::SLUG_PAGE);

        add_settings_field('bidspirit_options_server', __('Server', 'auctionforge') . ' *', array($this, 'fill_bidspirit_options_server'), self::SLUG_PAGE, self::SLUG_SECTION_ID, array(self::FIELD_DESCRIPTION => 'https://my-accaunt.bidspirit.com/'));

        add_settings_field('bidspirit_auth_token', __('Webhook auth token', 'auctionforge'), array($this, 'fill_bidspirit_auth_token'),
            self::SLUG_PAGE, self::SLUG_SECTION_ID, array(self::FIELD_DESCRIPTION => '...', self::FIELD_PLACEHOLDER => (defined('AUCTIONFORGE_API_TOKEN') ?
                AUCTIONFORGE_API_TOKEN : "")));

        add_settings_field('bidspirit_options_currency', __('Currency', 'auctionforge') . ' *', array($this, 'fill_bidspirit_options_currency'), self::SLUG_PAGE, self::SLUG_SECTION_ID, array(self::FIELD_DESCRIPTION => 'Default value $'));
        add_settings_field('bidspirit_options_auction_list', __('Auctions page', 'auctionforge') . '*', array($this, 'fill_bidspirit_options_auction_list'),
            self::SLUG_PAGE, self::SLUG_SECTION_ID, array(self::FIELD_DESCRIPTION => __('Auctions page', 'auctionforge')));
        add_settings_field('bidspirit_options_manual_auction_list', __("Manual auctions page", 'auctionforge') . '*', array($this,
            'fill_bidspirit_options_manual_auction_list'), self::SLUG_PAGE, self::SLUG_SECTION_ID, array(self::FIELD_DESCRIPTION => __('Manual auctions page', 'auctionforge')));
        add_settings_field('bidspirit_options_archive_list', __('Auctions archive page', 'auctionforge') . '*', array($this, 'fill_bidspirit_options_archive_list'), self::SLUG_PAGE, self::SLUG_SECTION_ID, array(self::FIELD_DESCRIPTION => __('Auctions archive page', 'auctionforge')));
        add_settings_field('bidspirit_options_terms', __('Terms of Conditions', 'auctionforge') . '*', array($this, 'fill_bidspirit_options_terms'), self::SLUG_PAGE, self::SLUG_SECTION_ID, array(self::FIELD_DESCRIPTION => __('Terms of Conditions', 'auctionforge')));
        add_settings_field('bidspirit_options_contacts', __('Contact Us', 'auctionforge') . '*', array($this, 'fill_bidspirit_options_contacts'), self::SLUG_PAGE, self::SLUG_SECTION_ID, array(self::FIELD_DESCRIPTION => __('"Contact Us" page', 'auctionforge')));
        add_settings_field('bidspirit_options_profile', __('User profile page', 'auctionforge') . '*', array($this, 'fill_bidspirit_options_profile'), self::SLUG_PAGE, self::SLUG_SECTION_ID, array(self::FIELD_DESCRIPTION => __('User profile page', 'auctionforge')));

        add_settings_field('bidspirit_options_login_page', __('Authorization page', 'auctionforge'), array($this, 'fill_bidspirit_options_login_page'),
            self::SLUG_PAGE, self::SLUG_SECTION_ID, array(self::FIELD_DESCRIPTION => __('A separate user authorization page', 'auctionforge')));
        add_settings_field('bidspirit_options_register_page', __('Registration page', 'auctionforge'), array($this, 'fill_bidspirit_options_register_page'),
            self::SLUG_PAGE, self::SLUG_SECTION_ID, array(self::FIELD_DESCRIPTION => __('A separate user registration page', 'auctionforge')));

        add_settings_field('bidspirit_options_type', __('Sync type', 'auctionforge'), array($this, 'fill_bidspirit_options_type'), self::SLUG_PAGE, self::SLUG_SECTION_ID, array(self::FIELD_DESCRIPTION => __('Type sync', 'auctionforge')));

        add_settings_field('bidspirit_options_lang', __('Default language', 'auctionforge') . '*', array($this, 'fill_bidspirit_options_lang'), self::SLUG_PAGE,
            self::SLUG_SECTION_ID, array(self::FIELD_DESCRIPTION => __('Select default language', 'auctionforge')));
        if (PolylangSupport::isPolylangActive() && PolylangSupport::isFullSupport()) {
            add_settings_field('bidspirit_options_second_lang', __('Second language', 'auctionforge') . '*', array($this, 'fill_bidspirit_options_second_lang'),
                self::SLUG_PAGE, self::SLUG_SECTION_ID, array(self::FIELD_DESCRIPTION => __('Select second language', 'auctionforge')));
            add_settings_field('bidspirit_options_other_lang', __('Content for other languages', 'auctionforge') . '*', array($this, 'fill_bidspirit_options_other_lang'),
                self::SLUG_PAGE, self::SLUG_SECTION_ID, array(self::FIELD_DESCRIPTION => __('Content language for other languages', 'auctionforge')));
        }

        add_settings_field('bidspirit_options_exclude_auction', __('Do not sync auctions', 'auctionforge'), array($this, 'fill_bidspirit_options_exclude_auction'), self::SLUG_PAGE, self::SLUG_SECTION_ID, array(self::FIELD_DESCRIPTION => __('1 title per line', 'auctionforge')));
        add_settings_field('bidspirit_options_hidden_auction', __('Hidden auctions', 'auctionforge'), array($this, 'fill_bidspirit_options_hidden_auction'),
            self::SLUG_PAGE, self::SLUG_SECTION_ID, array(self::FIELD_DESCRIPTION => __('1 term ID per line (Insert term ID)', 'auctionforge')));
        add_settings_field('bidspirit_options_shop_auction', __('Auctions as shop', 'auctionforge'), array($this, 'fill_bidspirit_options_shop_auction'),
            self::SLUG_PAGE, self::SLUG_SECTION_ID, array(self::FIELD_DESCRIPTION => __('1 term ID per line (Insert term ID)', 'auctionforge')));
        add_settings_field('bidspirit_options_auction_categories', __('Auction Categories', 'auctionforge'), array($this, 'fill_bidspirit_options_auction_categories'),
            self::SLUG_PAGE, self::SLUG_SECTION_ID, array(self::FIELD_DESCRIPTION => __('Define categories for auctions. The letter is matched against the first character of the Bidspirit Auction Number field. For manual auctions, select the category in the auction editor.', 'auctionforge')));
        add_settings_field('bidspirit_options_hide_prices', __('Hide prices for x hours after auctions', 'auctionforge'), array($this,
            'fill_bidspirit_options_hide_prices'), self::SLUG_PAGE, self::SLUG_SECTION_ID, array(self::FIELD_DESCRIPTION => __('Hide prices for x hours after ended auction', 'auctionforge')));
        add_settings_field('bidspirit_options_time_zone_label', __('Description on time zone', 'auctionforge'), array($this, 'fill_bidspirit_options_time_zone_label'), self::SLUG_PAGE, self::SLUG_SECTION_ID, array(self::FIELD_DESCRIPTION => __('Explanation of the displayed time', 'auctionforge')));

        add_settings_field('bidspirit_options_time_zone_name', __('Time zone name', 'auctionforge'), array($this, 'fill_bidspirit_options_time_zone_name'),
            self::SLUG_PAGE, self::SLUG_SECTION_ID, array(self::FIELD_DESCRIPTION => __('Default ...', 'auctionforge')));

        if (SettingsHelper::getOption('showcase_enable', false, 'file')) {
            add_settings_field('bidspirit_options_showcase_auto_close', 'Auto close showcase', array($this, 'fill_bidspirit_options_showcase_auto_close'),
                self::SLUG_PAGE, self::SLUG_SECTION_ID, array(self::FIELD_DESCRIPTION => 'Number of hours to hide buy now button before auction'));
        }

        add_settings_field('bidspirit_options_format_display_time', __('Format display time', 'auctionforge'), array($this, 'fill_bidspirit_options_format_display_time'), self::SLUG_PAGE, self::SLUG_SECTION_ID, array(self::FIELD_DESCRIPTION => __('Format display time begin auction', 'auctionforge')));
        add_settings_field('bidspirit_options_custom_format_display_time', __('Custom format display time', 'auctionforge'), array($this,
            'fill_bidspirit_options_custom_format_display_time'), self::SLUG_PAGE, self::SLUG_SECTION_ID, array(self::FIELD_DESCRIPTION
        => __('Custom format display time begin auction (format list: https://www.php.net/manual/en/datetime.format.php). Example: d.m.Y, l, H:s',
                'auctionforge')));
        add_settings_field('bidspirit_options_oldest_days', __('Automatic exclusion of auctions', 'auctionforge'), array($this, 'fill_bidspirit_options_oldest_days'), self::SLUG_PAGE, self::SLUG_SECTION_ID, array(self::FIELD_DESCRIPTION => __('Do not sync auctions that ended later than the number of days (default value 30)', 'auctionforge')));
        add_settings_field('bidspirit_options_sync_mode', __('Sync mode', 'auctionforge'), array($this, 'fill_bidspirit_options_sync_mode'), self::SLUG_PAGE, self::SLUG_SECTION_ID, array(self::FIELD_DESCRIPTION => __('Ignore the "Last update of the lot" timestamp when synchronizing.', 'auctionforge')));
        add_settings_field('bidspirit_options_lot_slider_template', __('Template for lot slider on', 'auctionforge'), array
        ($this, 'fill_bidspirit_options_lot_slider_template'), self::SLUG_PAGE, self::SLUG_SECTION_ID, array
        (self::FIELD_DESCRIPTION => __('On single lot page (default as "Slider")', 'auctionforge')));
        add_settings_field('bidspirit_options_layout_style', __('Layout Style', 'auctionforge'), array($this, 'fill_bidspirit_options_layout_style'), self::SLUG_PAGE, self::SLUG_SECTION_ID, array(self::FIELD_DESCRIPTION => __('Choose between Classic (with banner) or Minimal (flat header) page layouts.', 'auctionforge')));
        add_settings_field('bidspirit_options_catalog_layout', __('Catalog Layout', 'auctionforge'), array($this, 'fill_bidspirit_options_catalog_layout'), self::SLUG_PAGE, self::SLUG_SECTION_ID, array(self::FIELD_DESCRIPTION => __('Choose between Standard (top dropdowns) or Side Categories (sidebar with category list) for catalog pages.', 'auctionforge')));

        add_settings_field('bidspirit_minimal_section_heading', '<h3 style="margin:0">' . __('Minimal Template Settings', 'auctionforge') . '</h3>', '__return_false', self::SLUG_PAGE, self::SLUG_SECTION_ID);
        add_settings_field('bidspirit_options_minimal_header_bg', __('Header Background Color', 'auctionforge'), array($this, 'fill_minimal_header_bg'), self::SLUG_PAGE, self::SLUG_SECTION_ID, array(self::FIELD_DESCRIPTION => __('Background color for the minimal header section', 'auctionforge')));
        add_settings_field('bidspirit_options_minimal_title_color', __('Auction Title Text Color', 'auctionforge'), array($this, 'fill_minimal_title_color'), self::SLUG_PAGE, self::SLUG_SECTION_ID, array(self::FIELD_DESCRIPTION => __('Text color for the auction title in minimal header', 'auctionforge')));
        add_settings_field('bidspirit_options_minimal_datetime_color', __('Date/Time Text Color', 'auctionforge'), array($this, 'fill_minimal_datetime_color'), self::SLUG_PAGE, self::SLUG_SECTION_ID, array(self::FIELD_DESCRIPTION => __('Text color for date and time in minimal header', 'auctionforge')));
        add_settings_field('bidspirit_options_minimal_btn_bg', __('Catalog Button Background', 'auctionforge'), array($this, 'fill_minimal_btn_bg'), self::SLUG_PAGE, self::SLUG_SECTION_ID, array(self::FIELD_DESCRIPTION => __('Background color of the Catalog button', 'auctionforge')));
        add_settings_field('bidspirit_options_minimal_btn_text', __('Catalog Button Text Color', 'auctionforge'), array($this, 'fill_minimal_btn_text'), self::SLUG_PAGE, self::SLUG_SECTION_ID, array(self::FIELD_DESCRIPTION => __('Text color of the Catalog button', 'auctionforge')));
        add_settings_field('bidspirit_options_hide_profile_honorific', __('Hide Honorific Field', 'auctionforge'), array($this, 'fill_hide_profile_honorific'), self::SLUG_PAGE, self::SLUG_SECTION_ID, array(self::FIELD_DESCRIPTION => __('Hide the Honorific field on the profile page', 'auctionforge')));
        add_settings_field('bidspirit_options_hide_profile_id', __('Hide ID Field', 'auctionforge'), array($this, 'fill_hide_profile_id'), self::SLUG_PAGE, self::SLUG_SECTION_ID, array(self::FIELD_DESCRIPTION => __('Hide the ID field on the profile page', 'auctionforge')));

        add_settings_field('bidspirit_options_auction_house_state', __('Auction House State', 'auctionforge'), array($this, 'fill_bidspirit_options_auction_house_state'), self::SLUG_PAGE, self::SLUG_SECTION_ID, array(self::FIELD_DESCRIPTION => __('US state where the auction house is located. Used for sales tax calculations.', 'auctionforge')));

        foreach ($this->getCheckboxField() as $key => $item) {
            $labels = $this->getCheckboxLabels();
            add_settings_field(self::SLUG_PAGE . '_' . $key, $item, array($this, 'render_checkbox'), self::SLUG_PAGE, self::SLUG_SECTION_ID, array(
                'id' => $key,
                self::FIELD_DESCRIPTION => $labels[$key]
            ));
        }
    }

    public function render_checkbox($args)
    {
        echo $this->render_checkbox_field($args['id'], $args[self::FIELD_DESCRIPTION]);
    }

    public function fill_bidspirit_options_auction_house_state($args)
    {
        $id = 'auction_house_state';
        $val = get_option(self::SLUG_OPTION);
        $val = $val ? ($val[$id] ?? '') : '';
        $states = array(
            'AL' => 'Alabama', 'AK' => 'Alaska', 'AZ' => 'Arizona', 'AR' => 'Arkansas',
            'CA' => 'California', 'CO' => 'Colorado', 'CT' => 'Connecticut', 'DE' => 'Delaware',
            'FL' => 'Florida', 'GA' => 'Georgia', 'HI' => 'Hawaii', 'ID' => 'Idaho',
            'IL' => 'Illinois', 'IN' => 'Indiana', 'IA' => 'Iowa', 'KS' => 'Kansas',
            'KY' => 'Kentucky', 'LA' => 'Louisiana', 'ME' => 'Maine', 'MD' => 'Maryland',
            'MA' => 'Massachusetts', 'MI' => 'Michigan', 'MN' => 'Minnesota', 'MS' => 'Mississippi',
            'MO' => 'Missouri', 'MT' => 'Montana', 'NE' => 'Nebraska', 'NV' => 'Nevada',
            'NH' => 'New Hampshire', 'NJ' => 'New Jersey', 'NM' => 'New Mexico', 'NY' => 'New York',
            'NC' => 'North Carolina', 'ND' => 'North Dakota', 'OH' => 'Ohio', 'OK' => 'Oklahoma',
            'OR' => 'Oregon', 'PA' => 'Pennsylvania', 'RI' => 'Rhode Island', 'SC' => 'South Carolina',
            'SD' => 'South Dakota', 'TN' => 'Tennessee', 'TX' => 'Texas', 'UT' => 'Utah',
            'VT' => 'Vermont', 'VA' => 'Virginia', 'WA' => 'Washington', 'WV' => 'West Virginia',
            'WI' => 'Wisconsin', 'WY' => 'Wyoming', 'DC' => 'District of Columbia'
        );
        echo View::render(self::VIEW_SELECT, array(
            'id' => $id,
            'val' => $val,
            'slug' => self::SLUG_OPTION,
            'options' => $states,
            self::FIELD_DESCRIPTION => $args[self::FIELD_DESCRIPTION]
        ));
    }

    public function fill_bidspirit_options_auction_list($args)
    {
        echo $this->renderPageList('auction_list', $args);
    }

    public function fill_bidspirit_options_manual_auction_list($args)
    {
        echo $this->renderPageList('manual_auction_list', $args);
    }

    /*public function fill_bidspirit_options_welcome_page_app($args)
    {
        echo $this->renderPageList('welcome_app_page', $args);
    }*/

    public function fill_bidspirit_options_terms($args)
    {
        echo $this->renderPageList('terms', $args);
    }

    public function fill_bidspirit_options_contacts($args)
    {
        echo $this->renderPageList('contacts', $args);
    }

    public function fill_bidspirit_options_profile($args)
    {
        echo $this->renderPageList('profile', $args);
    }

    public function fill_bidspirit_options_login_page($args)
    {
        echo $this->renderPageList('authorization', $args);
    }

    public function fill_bidspirit_options_register_page($args)
    {
        echo $this->renderPageList('registration', $args);
    }

    public function fill_bidspirit_options_archive_list($args)
    {
        echo $this->renderPageList('archive_list', $args);
    }

    private function renderPageList($id, $args)
    {
        $val = get_option(self::SLUG_OPTION);
        $val = $val ? $val[$id] : null;

        $options = $this->getPageList();
        return View::render(self::VIEW_SELECT, array(
            'id' => $id,
            'val' => $val,
            'slug' => self::SLUG_OPTION,
            'options' => $options,
            self::FIELD_DESCRIPTION => $args[self::FIELD_DESCRIPTION]
        ));
    }

    private function getPageList()
    {
        $pageList = array();
        $args = array(
            'post_status' => SettingsHelper::STATUS_PUBLISH,
            'sort_column' => 'ID',
            'sort_order' => 'ASC'
        );

        if (function_exists('pll_default_language')) {
            $lang = pll_default_language();
            $args = array_merge($args, [
                'lang' => $lang
            ]);
        }

        $pages = get_pages($args);
        if (!empty($pages)) {
            /** @var WP_Post $page */
            foreach ($pages as $page) {
                $pageList[$page->ID] = $page->ID . ": " . $page->post_title;
            }
        }
        return $pageList;
    }

    public function fill_bidspirit_options_lang($args)
    {
        $id = self::OPTIONS_LANG;
        $val = get_option(self::SLUG_OPTION);
        $val = $val ? $val[$id] : null;
        $options = $this->languages;
        echo View::render(self::VIEW_SELECT, array(
            'id' => $id,
            'val' => $val,
            'slug' => self::SLUG_OPTION,
            'options' => $options,
            self::FIELD_DESCRIPTION => $args[self::FIELD_DESCRIPTION]
        ));
    }

    public function fill_bidspirit_options_second_lang($args)
    {
        $id = self::OPTIONS_SECOND_LANG;
        $val = get_option(self::SLUG_OPTION);
        $val = ($val && isset($val[$id])) ? $val[$id] : null;
        $options = $this->languages;
        echo View::render(self::VIEW_SELECT, array(
            'id' => $id,
            'val' => $val,
            'slug' => self::SLUG_OPTION,
            'options' => $options,
            self::FIELD_DESCRIPTION => $args[self::FIELD_DESCRIPTION]
        ));
    }

    public function fill_bidspirit_options_other_lang($args)
    {
        $id = self::OPTIONS_OTHER_LANG;
        $val = get_option(self::SLUG_OPTION);
        $val = ($val && isset($val[$id])) ? $val[$id] : null;
        $options = [
            'default' => 'Default',
            'second' => 'Second'
        ];
        echo View::render(self::VIEW_SELECT, array(
            'id' => $id,
            'val' => $val,
            'slug' => self::SLUG_OPTION,
            'options' => $options,
            self::FIELD_DESCRIPTION => $args[self::FIELD_DESCRIPTION]
        ));
    }

    public function fill_bidspirit_options_format_display_time($args)
    {
        $id = 'format_display_time';
        $val = get_option(self::SLUG_OPTION);
        $val = $val ? $val[$id] : 1;
        $options = array(
            1 => __('Monday, 30.12.2020, 12:00', 'auctionforge'),
            2 => __('December 30, 2020, 12:00', 'auctionforge')
        );
        echo View::render(self::VIEW_SELECT, array(
            'id' => $id,
            'val' => $val,
            'slug' => self::SLUG_OPTION,
            'options' => $options,
            self::FIELD_DESCRIPTION => $args[self::FIELD_DESCRIPTION]
        ));
    }

    public function fill_bidspirit_options_custom_format_display_time($args)
    {
        $id = 'custom_format_display_time';
        $val = get_option(self::SLUG_OPTION);
        $val = $val ? $val[$id] : null;
        echo View::render(self::VIEW_TEXT, array(
            'id' => $id,
            'val' => $val,
            'slug' => self::SLUG_OPTION,
            self::FIELD_DESCRIPTION => $args[self::FIELD_DESCRIPTION],
            'args' => $args
        ));
    }

    public function fill_bidspirit_options_type($args)
    {
        $id = 'type';
        $val = get_option(self::SLUG_OPTION);
        $val = $val ? $val[$id] : null;
        $options = array(
            'auto', 'publish', 'draft'
        );
        echo View::render(self::VIEW_SELECT, array(
            'id' => $id,
            'val' => $val,
            'slug' => self::SLUG_OPTION,
            'options' => $options,
            self::FIELD_DESCRIPTION => $args[self::FIELD_DESCRIPTION]
        ));
    }

    public function fill_bidspirit_options_server($args)
    {
        $id = 'server';
        $val = get_option(self::SLUG_OPTION);
        $val = $val ? $val[$id] : null;
        echo View::render(self::VIEW_TEXT, array(
            'id' => $id,
            'val' => $val,
            'slug' => self::SLUG_OPTION,
            self::FIELD_DESCRIPTION => $args[self::FIELD_DESCRIPTION],
            'args' => $args
        ));
    }

    public function fill_bidspirit_auth_token($args)
    {
        $id = 'auth_token';
        $val = get_option(self::SLUG_OPTION);
        $val = $val ? $val[$id] ?? null : null;
        echo View::render(self::VIEW_TEXT, array(
            'id' => $id,
            'val' => $val,
            'slug' => self::SLUG_OPTION,
            self::FIELD_DESCRIPTION => $args[self::FIELD_DESCRIPTION],
            'args' => $args
        ));

        // --- Token generator widget ---
        ?>
        <div id="af-token-generator" style="margin-top:10px;display:flex;align-items:center;gap:8px;flex-wrap:wrap;">
            <button type="button" id="af-generate-token-btn" class="button button-secondary" style="flex-shrink:0;">
                🔑 Generate Token
            </button>
            <span id="af-generated-token"
                  style="font-family:SFMono-Regular,Consolas,'Liberation Mono',Menlo,monospace;
                         font-size:13px;letter-spacing:.5px;padding:5px 10px;
                         background:#f0f0f1;border:1px solid #c3c4c7;border-radius:4px;
                         user-select:all;min-height:20px;display:inline-block;color:#1d2327;"></span>
            <button type="button" id="af-copy-token-btn"
                    title="Copy to clipboard"
                    style="background:none;border:none;cursor:pointer;padding:4px;display:none;position:relative;flex-shrink:0;">
                <svg id="af-copy-icon" xmlns="http://www.w3.org/2000/svg" width="18" height="18" viewBox="0 0 24 24"
                     fill="none" stroke="#50575e" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
                    <rect x="9" y="9" width="13" height="13" rx="2" ry="2"></rect>
                    <path d="M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1"></path>
                </svg>
                <svg id="af-check-icon" xmlns="http://www.w3.org/2000/svg" width="18" height="18" viewBox="0 0 24 24"
                     fill="none" stroke="#00a32a" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round"
                     style="display:none;">
                    <polyline points="20 6 9 17 4 12"></polyline>
                </svg>
                <span id="af-copy-tooltip"
                      style="display:none;position:absolute;bottom:calc(100% + 4px);left:50%;transform:translateX(-50%);
                             background:#1d2327;color:#fff;font-size:11px;padding:3px 8px;border-radius:3px;
                             white-space:nowrap;pointer-events:none;">Copied!</span>
            </button>
        </div>
        <script>
        (function(){
            var chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
            var genBtn   = document.getElementById('af-generate-token-btn');
            var copyBtn  = document.getElementById('af-copy-token-btn');
            var output   = document.getElementById('af-generated-token');
            var copyIcon = document.getElementById('af-copy-icon');
            var checkIcon= document.getElementById('af-check-icon');
            var tooltip  = document.getElementById('af-copy-tooltip');

            genBtn.addEventListener('click', function(){
                var arr = new Uint8Array(16);
                crypto.getRandomValues(arr);
                var token = '';
                for(var i=0;i<16;i++) token += chars[arr[i] % chars.length];
                output.textContent = token;
                copyBtn.style.display = 'inline-flex';
                // Reset icon state
                copyIcon.style.display = '';
                checkIcon.style.display = 'none';
                tooltip.style.display = 'none';
            });

            copyBtn.addEventListener('click', function(){
                var text = output.textContent;
                if(!text) return;
                navigator.clipboard.writeText(text).then(function(){
                    copyIcon.style.display = 'none';
                    checkIcon.style.display = '';
                    tooltip.style.display = '';
                    setTimeout(function(){
                        copyIcon.style.display = '';
                        checkIcon.style.display = 'none';
                        tooltip.style.display = 'none';
                    }, 1500);
                });
            });
        })();
        </script>
        <?php
    }

    public function fill_bidspirit_options_hide_prices($args)
    {
        $id = 'hide_prices';
        $val = get_option(self::SLUG_OPTION);
        $val = $val ? $val[$id] : null;
        echo View::render(self::VIEW_NUMBER, array(
            'id' => $id,
            'val' => $val,
            'slug' => self::SLUG_OPTION,
            self::FIELD_DESCRIPTION => $args[self::FIELD_DESCRIPTION]
        ));
    }

    public function fill_bidspirit_options_exclude_auction($args)
    {
        $id = 'exclude_auction';
        $val = get_option(self::SLUG_OPTION);
        $val = $val ? $val[$id] : null;
        echo View::render(self::VIEW_TEXTAREA, array(
            'id' => $id,
            'val' => $val,
            'slug' => self::SLUG_OPTION,
            self::FIELD_DESCRIPTION => $args[self::FIELD_DESCRIPTION]
        ));
    }

    public function fill_bidspirit_options_hidden_auction($args)
    {
        $id = 'hidden_auction';
        $val = get_option(self::SLUG_OPTION);
        $val = $val ? $val[$id] : null;
        echo View::render(self::VIEW_TEXTAREA, array(
            'id' => $id,
            'val' => $val,
            'slug' => self::SLUG_OPTION,
            self::FIELD_DESCRIPTION => $args[self::FIELD_DESCRIPTION]
        ));
    }

    public function fill_bidspirit_options_shop_auction($args)
    {
        $id = 'shop_auction';
        $val = get_option(self::SLUG_OPTION);
        $val = $val ? $val[$id] : null;
        echo View::render(self::VIEW_TEXTAREA, array(
            'id' => $id,
            'val' => $val,
            'slug' => self::SLUG_OPTION,
            self::FIELD_DESCRIPTION => $args[self::FIELD_DESCRIPTION]
        ));
    }

    public function fill_bidspirit_options_auction_categories($args)
    {
        $categories = self::getAuctionCategories();
        ?>
        <div id="auctionforge-auction-categories-wrap">
            <p class="description" style="margin-bottom:10px;"><?= esc_html($args[self::FIELD_DESCRIPTION]) ?></p>
            <table class="widefat" style="max-width:500px;">
                <thead>
                    <tr>
                        <th style="width:60%;"><?= __('Category Name', 'auctionforge') ?></th>
                        <th style="width:20%;"><?= __('Letter', 'auctionforge') ?></th>
                        <th style="width:20%;">&nbsp;</th>
                    </tr>
                </thead>
                <tbody id="auctionforge-auction-categories-body">
                    <?php if (!empty($categories)) : ?>
                        <?php foreach ($categories as $i => $cat) : ?>
                            <tr>
                                <td><input type="text" name="<?= self::SLUG_OPTION ?>[auction_categories][<?= $i ?>][name]" value="<?= esc_attr($cat['name']) ?>" style="width:100%;" /></td>
                                <td><input type="text" name="<?= self::SLUG_OPTION ?>[auction_categories][<?= $i ?>][letter]" value="<?= esc_attr($cat['letter']) ?>" maxlength="1" style="width:100%;text-transform:uppercase;" /></td>
                                <td><button type="button" class="button auctionforge-remove-cat-row">&times;</button></td>
                            </tr>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </tbody>
            </table>
            <button type="button" class="button" id="auctionforge-add-cat-row" style="margin-top:8px;"><?= __('+ Add Category', 'auctionforge') ?></button>
        </div>
        <script>
        (function(){
            var body = document.getElementById('auctionforge-auction-categories-body');
            var slug = '<?= self::SLUG_OPTION ?>';
            function reindex() {
                var rows = body.querySelectorAll('tr');
                for (var i = 0; i < rows.length; i++) {
                    rows[i].querySelector('input[type=text]').name = slug + '[auction_categories][' + i + '][name]';
                    rows[i].querySelectorAll('input[type=text]')[1].name = slug + '[auction_categories][' + i + '][letter]';
                }
            }
            document.getElementById('auctionforge-add-cat-row').addEventListener('click', function(){
                var idx = body.querySelectorAll('tr').length;
                var tr = document.createElement('tr');
                tr.innerHTML = '<td><input type="text" name="' + slug + '[auction_categories][' + idx + '][name]" value="" style="width:100%;" /></td>'
                    + '<td><input type="text" name="' + slug + '[auction_categories][' + idx + '][letter]" value="" maxlength="1" style="width:100%;text-transform:uppercase;" /></td>'
                    + '<td><button type="button" class="button auctionforge-remove-cat-row">&times;</button></td>';
                body.appendChild(tr);
            });
            body.addEventListener('click', function(e){
                if (e.target.classList.contains('auctionforge-remove-cat-row')) {
                    e.target.closest('tr').remove();
                    reindex();
                }
            });
        })();
        </script>
        <?php
    }

    public static function getAuctionCategories()
    {
        $options = get_option(self::SLUG_OPTION);
        if ($options && isset($options['auction_categories']) && is_array($options['auction_categories'])) {
            return $options['auction_categories'];
        }
        return array();
    }


    public function fill_bidspirit_options_time_zone_name($args)
    {
        $id = self::OPTIONS_TIMEZONES;
        $val = get_option(self::SLUG_OPTION);
        $val = isset($val[$id]) ? $val[$id] : null;

        $timezones = self::getTimeZoneNames();
        $values = array_combine($timezones, $timezones);

        echo View::render(self::VIEW_SELECT, array(
            'id' => $id,
            'val' => $val,
            'slug' => self::SLUG_OPTION,
            'options' => $values,
            self::FIELD_DESCRIPTION => $args[self::FIELD_DESCRIPTION]
        ));
    }

    public static function getTimeZoneNames()
    {
        static $timezoneIdentifiers = null;
        
        if ($timezoneIdentifiers === null) {
            $cached_timezones = get_transient('auctionforge_timezone_names');
            
            if ($cached_timezones !== false) {
                $timezoneIdentifiers = $cached_timezones;
            } else {
                $allTimezones = DateTimeZone::listIdentifiers();
                
                // Separate timezones by region and reorder them
                $europeTimezones = array_filter($allTimezones, function($timezone) {
                    return strpos($timezone, 'Europe/') === 0;
                });
                
                $americaTimezones = array_filter($allTimezones, function($timezone) {
                    return strpos($timezone, 'America/') === 0;
                });
                
                $asiaTimezones = array_filter($allTimezones, function($timezone) {
                    return strpos($timezone, 'Asia/') === 0;
                });
                
                $africaTimezones = array_filter($allTimezones, function($timezone) {
                    return strpos($timezone, 'Africa/') === 0;
                });
                
                $utcTimezones = array_filter($allTimezones, function($timezone) {
                    return $timezone === 'UTC';
                });
                
                // Merge in desired order: Europe, America, Asia, UTC, Africa
                $timezoneIdentifiers = array_merge(
                    array_values($europeTimezones),
                    array_values($americaTimezones),
                    array_values($asiaTimezones),
                    array_values($africaTimezones),
                    array_values($utcTimezones)
                );
                
                $timezoneIdentifiers = apply_filters("bp_available_time_zones", $timezoneIdentifiers);
                
                set_transient('auctionforge_timezone_names', $timezoneIdentifiers, 86400);
            }
        }
        
        return $timezoneIdentifiers;
    }

    public function fill_bidspirit_options_showcase_auto_close($args)
    {
        $id = 'showcase_auto_close';
        $val = get_option(self::SLUG_OPTION);
        $val = $val ? $val[$id] : null;
        echo View::render(self::VIEW_NUMBER, array(
            'id' => $id,
            'val' => $val,
            'slug' => self::SLUG_OPTION,
            self::FIELD_DESCRIPTION => $args[self::FIELD_DESCRIPTION]
        ));
    }

    public function fill_bidspirit_options_time_zone_label($args)
    {
        $id = 'time_zone_label';
        $val = get_option(self::SLUG_OPTION);
        $val = $val ? $val[$id] : null;
        echo View::render(self::VIEW_TEXT, array(
            'id' => $id,
            'val' => $val,
            'slug' => self::SLUG_OPTION,
            self::FIELD_DESCRIPTION => $args[self::FIELD_DESCRIPTION],
            'args' => $args
        ));
    }

    public function fill_bidspirit_options_currency($args)
    {
        $id = 'currency';
        $val = get_option(self::SLUG_OPTION);
        $val = $val ? $val[$id] : null;
        echo View::render(self::VIEW_TEXT, array(
            'id' => $id,
            'val' => $val,
            'slug' => self::SLUG_OPTION,
            self::FIELD_DESCRIPTION => $args[self::FIELD_DESCRIPTION],
            'args' => $args
        ));
    }

    public function fill_bidspirit_options_oldest_days($args)
    {
        $id = 'oldest_days';
        $val = get_option(self::SLUG_OPTION);
        $val = $val ? $val[$id] : 30;

        echo View::render(self::VIEW_TEXT, array(
            'id' => $id,
            'val' => $val,
            'slug' => self::SLUG_OPTION,
            self::FIELD_DESCRIPTION => $args[self::FIELD_DESCRIPTION],
            'args' => $args
        ));
    }

    public function fill_bidspirit_options_sync_mode($args)
    {
        $id = self::SYNC_FORCE_NAME;
        $val = get_option(self::SLUG_OPTION);
        $val = $val ? $val[$id] : null;
        $options = array(
            "off" => "New and changed data", "on" => "All data"
        );
        echo View::render(self::VIEW_SELECT, array(
            'id' => $id,
            'val' => $val,
            'slug' => self::SLUG_OPTION,
            'options' => $options,
            self::FIELD_DESCRIPTION => $args[self::FIELD_DESCRIPTION]
        ));
    }

    public function fill_bidspirit_options_lot_slider_template($args)
    {
        $id = self::LOT_SLIDER_TEMPLATE;
        $val = get_option(self::SLUG_OPTION);
        $val = isset($val[$id]) ? $val[$id] : null;
        $options = array(
            "slider" => "Slider", "grid" => "Grid"
        );
        echo View::render(self::VIEW_SELECT, array(
            'id' => $id,
            'val' => $val,
            'slug' => self::SLUG_OPTION,
            'options' => $options,
            self::FIELD_DESCRIPTION => $args[self::FIELD_DESCRIPTION]
        ));
    }

    public function fill_bidspirit_options_layout_style($args)
    {
        $id = self::LAYOUT_STYLE;
        $val = get_option(self::SLUG_OPTION);
        $val = isset($val[$id]) ? $val[$id] : null;
        $options = array(
            "classic" => "Classic (Banner)", "minimal" => "Minimal (Flat)"
        );
        echo View::render(self::VIEW_SELECT, array(
            'id' => $id,
            'val' => $val,
            'slug' => self::SLUG_OPTION,
            'options' => $options,
            self::FIELD_DESCRIPTION => $args[self::FIELD_DESCRIPTION]
        ));
    }

    public function fill_bidspirit_options_catalog_layout($args)
    {
        $id = self::CATALOG_LAYOUT;
        $val = get_option(self::SLUG_OPTION);
        $val = ($val && isset($val[$id])) ? $val[$id] : 'standard';
        $options = array(
            'standard' => __('Standard', 'auctionforge'),
            'side_categories' => __('Side Categories', 'auctionforge'),
        );
        echo View::render(self::VIEW_SELECT, array(
            'id' => $id,
            'val' => $val,
            'slug' => self::SLUG_OPTION,
            'options' => $options,
            self::FIELD_DESCRIPTION => $args[self::FIELD_DESCRIPTION]
        ));
    }

    private function render_color_field($id, $default, $args)
    {
        $val = get_option(self::SLUG_OPTION);
        $val = isset($val[$id]) ? $val[$id] : $default;
        echo View::render('admin.view.fields.color', array(
            'id' => $id,
            'val' => $val,
            'slug' => self::SLUG_OPTION,
            'default' => $default,
            self::FIELD_DESCRIPTION => $args[self::FIELD_DESCRIPTION]
        ));
    }

    public function fill_minimal_header_bg($args)
    {
        $this->render_color_field('minimal_header_bg', '#f5f5f5', $args);
    }

    public function fill_minimal_title_color($args)
    {
        $this->render_color_field('minimal_title_color', '#333333', $args);
    }

    public function fill_minimal_datetime_color($args)
    {
        $this->render_color_field('minimal_datetime_color', '#666666', $args);
    }

    public function fill_minimal_btn_bg($args)
    {
        $this->render_color_field('minimal_btn_bg', '#333333', $args);
    }

    public function fill_minimal_btn_text($args)
    {
        $this->render_color_field('minimal_btn_text', '#ffffff', $args);
    }

    public function fill_hide_profile_honorific($args)
    {
        echo $this->render_checkbox_field('hide_profile_honorific', $args[self::FIELD_DESCRIPTION]);
    }

    public function fill_hide_profile_id($args)
    {
        echo $this->render_checkbox_field('hide_profile_id', $args[self::FIELD_DESCRIPTION]);
    }

    /**
     * Render checkbox option
     * @param $id
     * @param $description
     * @return false|string
     */
    private function render_checkbox_field($id, $description)
    {
        $val = get_option(self::SLUG_OPTION);
        $val = wp_parse_args($val, array(
            $id => $this->getCheckboxDefault($id, 'on')
        ));

        return View::render('admin.view.fields.checkbox', array(
            'id' => $id,
            'val' => $val[$id],
            'slug' => self::SLUG_OPTION,
            self::FIELD_DESCRIPTION => $description
        ));
    }

    /**
     * Prepare settings fields
     * @param $options
     * @return array
     */
    function sanitize_bidspirit_callback($options)
    {
        foreach ($options as $k => & $val) {
            if ($k === 'auction_categories') {
                continue;
            }
            $val = strip_tags($val);
        }

        if (isset($options['auction_categories']) && is_array($options['auction_categories'])) {
            $cleaned = array();
            foreach ($options['auction_categories'] as $cat) {
                $name = isset($cat['name']) ? sanitize_text_field($cat['name']) : '';
                $letter = isset($cat['letter']) ? strtoupper(sanitize_text_field($cat['letter'])) : '';
                if ($name !== '' && $letter !== '') {
                    $cleaned[] = array('name' => $name, 'letter' => substr($letter, 0, 1));
                }
            }
            $options['auction_categories'] = $cleaned;
        }

        foreach ($this->getCheckboxField() as $key => $item) {
            if (empty($options[$key])) {
                $options[$key] = 'off';
            }
        }

        return $options;
    }

    /**
     * @return array
     */
    public function getCheckboxField()
    {
        return $this->checkboxField;
    }

    /**
     * @param array $checkboxField
     */
    public function setCheckboxField($checkboxField)
    {
        $this->checkboxField = $checkboxField;
    }

    /**
     * @return array
     */
    public function getCheckboxLabels()
    {
        return $this->checkboxLabels;
    }

    /**
     * @param array $checkboxLabels
     */
    public function setCheckboxLabels($checkboxLabels)
    {
        $this->checkboxLabels = $checkboxLabels;
    }

    /**
     * @param $key
     * @param $default
     * @return string
     */
    public function getCheckboxDefault($key, $default)
    {
        if (!empty($this->checkboxDefault[$key])) {
            return $this->checkboxDefault[$key];
        } else {
            return $default;
        }
    }

    /**
     * @param  array  $checkboxDefault
     */
    public function setCheckboxDefault($checkboxDefault)
    {
        $this->checkboxDefault = $checkboxDefault;
    }
}

add_action('init', function () {
    $settings = new Settings();
    $settings->init();
});
