<?php
/**
 * Plugin settings page (Free version)
 *
 * @package AfriRegions
 */

defined( 'ABSPATH' ) or die( 'Forbidden' );

class SCPWOO_Page_Settings {

    /** @return void */
    public static function render(): void {
        $settings = wp_parse_args( SCPWOO_States_Places::get_json_option( SCPWOO_States_Places::OPTION_SETTINGS ), array(
            'address_validation_enabled' => 'yes',
        ) );

        $fields = array(
            'address_validation_enabled' => array(
                'label' => __( 'Address Validation', 'afriregions-city-neighborhood-for-woocommerce' ),
                'desc'  => __( 'Checks that the selected neighborhood belongs to the chosen city before placing an order. Prevents address errors.', 'afriregions-city-neighborhood-for-woocommerce' ),
            ),
        );
        ?>
        <div class="scpwoo-settings-page">

        <div class="scpwoo-row32">

            <!-- Left column: settings cards -->
            <div class="scpwoo-col">

                <div class="scpwoo-settings-card">
                    <div class="scpwoo-settings-card-header"><?php esc_html_e( 'Checkout', 'afriregions-city-neighborhood-for-woocommerce' ); ?></div>
                    <?php foreach ( $fields as $key => $field ) :
                        $is_on = $settings[ $key ] === 'yes';
                    ?>
                    <div class="scpwoo-settings-row">
                        <div class="scpwoo-settings-row-toggle">
                            <label class="scpwoo-toggle">
                                <input type="checkbox" id="scpwoo-<?php echo esc_attr( $key ); ?>" name="<?php echo esc_attr( $key ); ?>" value="yes" <?php checked( $is_on ); ?> />
                                <span class="scpwoo-toggle-slider"></span>
                            </label>
                        </div>
                        <label class="scpwoo-settings-row-label" for="scpwoo-<?php echo esc_attr( $key ); ?>"><?php echo esc_html( $field['label'] ); ?></label>
                        <div class="scpwoo-settings-row-desc"><?php echo wp_kses_post( $field['desc'] ); ?></div>
                    </div>
                    <?php endforeach; ?>
                </div>

                <div class="scpwoo-btn-row">
                    <button type="button" id="scpwoo-save-settings" class="button button-primary">
                        <?php esc_html_e( 'Save settings', 'afriregions-city-neighborhood-for-woocommerce' ); ?>
                    </button>
                </div>

            </div><!-- /.scpwoo-col -->

            <!-- Right column: info + upsell -->
            <div class="scpwoo-col">


                <div class="scpwoo-upsell">
                    <div class="scpwoo-upsell-icon"><span class="dashicons dashicons-star-filled"></span></div>
                    <div class="scpwoo-upsell-title"><?php esc_html_e( 'Upgrade to Pro', 'afriregions-city-neighborhood-for-woocommerce' ); ?></div>
                    <div class="scpwoo-upsell-desc"><?php esc_html_e( 'Smart Search, REST API, CSV exports, and more.', 'afriregions-city-neighborhood-for-woocommerce' ); ?></div>
                    <a href="<?php echo esc_url( admin_url( 'admin.php?page=scpwoo&tab=upgrade' ) ); ?>" class="button button-primary" style="width:100%;text-align:center;">
                        <?php esc_html_e( 'See Pro plans', 'afriregions-city-neighborhood-for-woocommerce' ); ?>
                    </a>
                </div>

            </div><!-- /.scpwoo-col -->

        </div><!-- /.scpwoo-row32 -->

        </div>
        <?php
    }

    /** @return void */
    public static function ajax_save_settings(): void {
        check_ajax_referer( SCPWOO_States_Places::NONCE_ADMIN_WRITE, 'nonce' );

        if ( ! current_user_can( 'manage_woocommerce' ) ) {
            wp_send_json_error( array( 'message' => __( 'Permission denied.', 'afriregions-city-neighborhood-for-woocommerce' ) ) );
        }

        $toggle_keys = array( 'address_validation_enabled' );
        $settings = array();
        foreach ( $toggle_keys as $key ) {
            $settings[ $key ] = isset( $_POST[ $key ] ) && 'yes' === wp_unslash( $_POST[ $key ] ) ? 'yes' : 'no';
        }

        SCPWOO_States_Places::update_json_option( SCPWOO_States_Places::OPTION_SETTINGS, $settings );

        wp_send_json_success( array( 'message' => __( 'Settings saved.', 'afriregions-city-neighborhood-for-woocommerce' ) ) );
    }
}
