<?php

if ( !defined( 'ABSPATH' ) ) {
    exit;
}

if ( !class_exists( 'Reon' ) ) {
    return;
}

if ( !class_exists( 'Pricely_Admin_On_Sale_Query_Settings' ) ) {

    class Pricely_Admin_On_Sale_Query_Settings {

        public function __construct() {

            add_filter( 'pricely_admin_get-settings-section-panels', array( $this, 'get_panel' ), 30 );
        }

        public function get_panel( $in_fields ) {

            $in_fields[] = array(
                'id' => 'any_id',
                'type' => 'panel',
                'last' => true,
                'white_panel' => false,
                'panel_size' => 'smaller',
                'width' => '100%',
                'field_css_class' => array( 'pricely_setting_panel' ),
                'css_class' => array( 'rn-round-small' ),
                'merge_fields' => false,
                'fields' => $this->get_panel_fields( array() ),
            );

            return $in_fields;
        }

        private function get_panel_fields( $in_fields ) {

            $allowed_html = array(
                'a' => array(
                    'id' => true,
                    'href' => true,
                    'title' => true,
                    'class' => true,
                    'target' => true,
                ),
            );

            $in_fields[] = array(
                'id' => 'any_id',
                'type' => 'paneltitle',
                'full_width' => true,
                'center_head' => true,
                'title' => esc_html__( 'On-Sale - Products Query', 'pricely-dynamic-pricing-cart-discounts' ),
                'desc' => wp_kses( __( 'Use these settings to configure on-sale products query, use <a href="https://docs.woocommerce.com/document/woocommerce-shortcodes/" target="_blank">WooCommerce shortcodes</a> to display the products', 'pricely-dynamic-pricing-cart-discounts' ), $allowed_html ),
            );

            $in_fields[] = array(
                'id' => 'on_sale_product_query',
                'type' => 'columns-field',
                'columns' => 7,
                'merge_fields' => true,
                'fields' => $this->get_fields(),
            );


            return $in_fields;
        }

        private function get_fields() {

            $in_fields = array();

            $in_fields[] = array(
                'id' => 'enable',
                'type' => 'select2',
                'column_size' => 1,
                'column_title' => esc_html__( 'Enable', 'pricely-dynamic-pricing-cart-discounts' ),
                'tooltip' => esc_html__( 'Choose whether the on-sale products query should be enabled', 'pricely-dynamic-pricing-cart-discounts' ),
                'default' => array( 'no' ),
                'options' => array(
                    'no' => esc_html__( 'No', 'pricely-dynamic-pricing-cart-discounts' ),
                    'yes' => esc_html__( 'Yes', 'pricely-dynamic-pricing-cart-discounts' ),
                ),
                'width' => '100%',
                'fold_id' => 'enable_on_sale',
            );

            $in_fields[] = array(
                'id' => 'query_limit',
                'type' => 'textbox',
                'input_type' => 'number',
                'column_size' => 2,
                'column_title' => esc_html__( 'Number of Products', 'pricely-dynamic-pricing-cart-discounts' ),
                'tooltip' => esc_html__( 'Set the maximum total number of products for the query result', 'pricely-dynamic-pricing-cart-discounts' ),
                'default' => '50',
                'placeholder' => esc_html__( '0.00', 'pricely-dynamic-pricing-cart-discounts' ),
                'width' => '100%',
                'attributes' => array(
                    'min' => '0',
                    'step' => '1',
                ),
                'fold' => array(
                    'target' => 'enable_on_sale',
                    'attribute' => 'value',
                    'value' => 'yes',
                    'oparator' => 'eq',
                    'clear' => false,
                ),
            );

            $in_fields[] = array(
                'id' => 'cache_duration',
                'type' => 'textbox',
                'input_type' => 'number',
                'column_size' => 2,
                'column_title' => esc_html__( 'Cache Duration', 'pricely-dynamic-pricing-cart-discounts' ),
                'tooltip' => esc_html__( 'Set the cache duration for the query result in minutes', 'pricely-dynamic-pricing-cart-discounts' ),
                'default' => '30',
                'placeholder' => esc_html__( '0.00', 'pricely-dynamic-pricing-cart-discounts' ),
                'width' => '100%',
                'attributes' => array(
                    'min' => '0',
                    'step' => '1',
                ),
                'fold' => array(
                    'target' => 'enable_on_sale',
                    'attribute' => 'value',
                    'value' => 'yes',
                    'oparator' => 'eq',
                    'clear' => false,
                ),
            );

            return $in_fields;
        }

    }

    new Pricely_Admin_On_Sale_Query_Settings();
}