<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly
}

class WDP_Admin_Options_Page extends WDP_Admin_Abstract_Page {
	public $priority = 40;
	protected $tab = 'options';

	public function __construct() {
		$this->title = __( 'Settings', 'advanced-dynamic-pricing-for-woocommerce' );
	}

	public function action() {
		if ( isset( $_POST['save-options'] ) ) {
			$args = array(
				'show_matched_bulk_table'               => FILTER_VALIDATE_BOOLEAN,
				'product_bulk_table_action'             => FILTER_SANITIZE_STRING,
				'show_category_bulk_table'              => FILTER_VALIDATE_BOOLEAN,
				'category_bulk_table_action'            => FILTER_SANITIZE_STRING,
				'show_striked_prices'                   => FILTER_VALIDATE_BOOLEAN,
				'show_onsale_badge'                     => FILTER_VALIDATE_BOOLEAN,
				'combine_discounts'                     => FILTER_VALIDATE_BOOLEAN,
				'default_discount_name'                 => FILTER_SANITIZE_STRING,
				'combine_fees'                          => FILTER_VALIDATE_BOOLEAN,
				'default_fee_name'                      => FILTER_SANITIZE_STRING,
				'discount_for_onsale'                   => FILTER_SANITIZE_STRING,
				'is_override_cents'                     => FILTER_VALIDATE_BOOLEAN,
				'prices_ends_with'                      => array('filter'    => FILTER_VALIDATE_REGEXP,
				                                                 'options'   => array(
				                                                 	'regexp'   => '/^[0-9]{2}$/',
				                                                    'default'  => 99
				                                                 ),
																),
				'is_show_amount_saved_in_mini_cart'     => FILTER_VALIDATE_BOOLEAN,
				'is_show_amount_saved_in_cart'          => FILTER_VALIDATE_BOOLEAN,
				'is_show_amount_saved_in_checkout_cart' => FILTER_VALIDATE_BOOLEAN,
				'uninstall_remove_data'                 => FILTER_VALIDATE_BOOLEAN,
				'load_in_backend'                       => FILTER_VALIDATE_BOOLEAN,
			);
			WDP_Helpers::set_settings( filter_input_array( INPUT_POST, $args ) );

			wp_redirect( $_SERVER['HTTP_REFERER'] );
		}
	}

	public function render() {
		$options = WDP_Helpers::get_settings();

		$this->render_template(
			WC_ADP_PLUGIN_PATH . 'views/tabs/options.php',
			compact( 'options' )
		);
	}
}