<?php
if (!defined('ABSPATH')) exit;

/**
 * Free Auction settings
 */
function stakmv_auction_settings_fields() {

    return [

        [
            'name' => 'Auction Rules',
            'type' => 'title',
            'id'   => 'stakmv_auction_rules',
        ],

        [
            'name'    => 'Payment Timeout (hours)',
            'id'      => 'stakmv_auction_payment_timeout',
            'type'    => 'number',
            'default' => 48,
        ],

        [
            'name'    => 'If Winner Fails to Pay',
            'id'      => 'stakmv_auction_failure_action',
            'type'    => 'select',
            'options' => [
                'next_bidder' => 'Next Bidder',
                'reopen'      => 'Reopen Auction',
            ],
        ],

        [
            'type' => 'sectionend',
            'id'   => 'stakmv_auction_rules',
        ],
    ];
}

/**
 * Render Bidding tab (FREE)
 */
add_action('stakmv_auctions_render_tab_bidding', function () {

    if (isset($_POST['save_bidding'])) {
        check_admin_referer('stakmv_save_bidding', 'stakmv_save_bidding_nonce');
        woocommerce_update_options(stakmv_auction_settings_fields());
        /**
         * Extension point for add-ons to persist
         * extra bidding fields without changing free plugin behavior.
         */
        $bidding_post_data = isset($_POST) && is_array($_POST)
            ? map_deep(wp_unslash($_POST), 'sanitize_text_field')
            : array();
        do_action('stakmv_auctions_save_bidding', $bidding_post_data);
        echo '<div class="updated"><p>Settings saved.</p></div>';
    }
    ?>

    <form method="post">
        <?php wp_nonce_field('stakmv_save_bidding', 'stakmv_save_bidding_nonce'); ?>

        <?php woocommerce_admin_fields(stakmv_auction_settings_fields()); ?>
        <?php do_action('stakmv_auctions_render_bidding_fields'); ?>

        <p>
            <button type="submit"
                    name="save_bidding"
                    class="button-primary">
                Save Changes
            </button>
        </p>

    </form>
<?php
});
