<?php
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
add_action( 'admin_init', function() {

  // save shipping zones in transient
  //delete_transient('dspilot_shipping_zones');
  if ( false === get_transient('dspilot_shipping_zones') ) {
    if ( class_exists('WC_Shipping_Zones') && function_exists('WC') && WC() && WC()->shipping ) {
        $zones_data = WC_Shipping_Zones::get_zones();
        $zones = [];
        foreach ( $zones_data as $zone_arr ) {
            if ( isset( $zone_arr['zone_id'] ) && is_numeric( $zone_arr['zone_id'] ) ) {
                $zone = new WC_Shipping_Zone( intval( $zone_arr['zone_id'] ) );
                $zones[ $zone->get_id() ] = $zone->get_zone_name();
            }
        }
        // Add default zone manually
        $default_zone = new WC_Shipping_Zone( 0 );
        $zones[ $default_zone->get_id() ] = $default_zone->get_zone_name();

        set_transient( 'dspilot_shipping_zones', $zones, DAY_IN_SECONDS );
    }
  }

  // save wc countries and states in transient
  // delete_transient('dspilot_wc_countries');
  // delete_transient('dspilot_wc_states');
  if ( false === get_transient('dspilot_wc_countries') ) {
    $countries = Dspilot_Helper::get_all_wc_countries_and_states();

    //var_dump($countries);

    if ( ! empty( $countries['countries'] ) ) {
        set_transient( 'dspilot_wc_countries', $countries['countries'], DAY_IN_SECONDS );
        
    }
    if ( ! empty( $countries['states'] ) ) {
        set_transient( 'dspilot_wc_states', $countries['states'], DAY_IN_SECONDS );
    }
  }  
  delete_transient('dspilot_wc_product_variations');
  if ( false === get_transient('dspilot_wc_product_variations') ) {
    $dspilot_wc_product_variations = Dspilot_Helper::get_product_variations();

    if ( ! empty( $dspilot_wc_product_variations ) ) {
        set_transient( 'dspilot_wc_product_variations', $dspilot_wc_product_variations, DAY_IN_SECONDS );
        
    }
  }
});

// Control core classes for avoid errors
if( class_exists( 'DSPILOT' ) ) {
    (function() {
    $prefix = 'dspilot_options';
    // Create options
    DSPILOT::createOptions( $prefix, array(

      // framework title
      'framework_title'         => 'DiscountPilot',
      'framework_class'         => '',

      // menu settings
      'menu_title'              => 'DiscountPilot',
      'menu_slug'               => 'discountpilot-dynamic-pricing',
      'menu_type'               => 'menu',
      'menu_capability'         => 'manage_options',
      'menu_icon'               => plugins_url( 'discountpilot-dynamic-pricing/admin/assets/img/dspilot-menu-icon.svg' ), // svg icon url is /wp-content/plugins/discountpilot-dynamic-pricing/admin/assets/img/dspilot-menu-icon.svg
      'menu_position'           => null,
      'menu_hidden'             => false,
      'menu_parent'             => '',

      // menu extras
      'show_bar_menu'           => true,
      'show_sub_menu'           => true,
      'show_in_network'         => true,
      'show_in_customizer'      => false,

      'show_search'             => false,
      'show_reset_all'          => true,
      'show_reset_section'      => false,
      'show_footer'             => false,
      'show_all_options'        => false,
      'show_form_warning'       => true,
      'sticky_header'           => true,
      'save_defaults'           => true,
      'ajax_save'               => true,

      // admin bar menu settings
      'admin_bar_menu_icon'     => '',
      'admin_bar_menu_priority' => 80,

      // footer
      'footer_text'             => '',
      'footer_after'            => '',
      'footer_credit'           => '',

      // database model
      'database'                => '', // options, transient, theme_mod, network
      'transient_time'          => 0,

      // contextual help
      'contextual_help'         => array(),
      'contextual_help_sidebar' => '',

      // typography options
      'enqueue_webfont'         => false,
      'async_webfont'           => false,

      // others
      'output_css'              => true,

      // theme and wrapper classname
      'nav'                     => 'inline',
      'theme'                   => 'light',
      'class'                   => '',

      // external default values
      'defaults'                => array(),

    ) );

    $is_pro_active = false;
    if ( function_exists( 'is_plugin_active' ) ) {
        $is_pro_active = is_plugin_active( 'discountpilot-dynamic-pricing-pro/discountpilot-dynamic-pricing-pro.php' );
    } else {
        if ( ! function_exists( 'get_plugins' ) ) {
            require_once ABSPATH . 'wp-admin/includes/plugin.php';
        }
        $is_pro_active = is_plugin_active( 'discountpilot-dynamic-pricing-pro/discountpilot-dynamic-pricing-pro.php' );
    }
    $pro_label = $is_pro_active ? '' : ' - PRO Version';

    $condition_fields_options = array(
      'Cart' => array(
          'cart_total_weight' => 'Cart total weight',
          'number_of_cart_items' => 'Number of cart items',
          'subtotal_including_tax' => 'Subtotal including tax',
          'subtotal_excluding_tax' => 'Subtotal excluding tax' . $pro_label,
          'quantity_of_cart_items' => 'Quantity of cart items' . $pro_label,
          'coupons_applied' => 'Coupons applied' . $pro_label,
          'max_width_of_cart_items' => 'Maximum width of cart items' . $pro_label,
          'max_height_of_cart_items' => 'Maximum height of cart items' . $pro_label,
          'max_length_of_cart_items' => 'Maximum length of cart items' . $pro_label,
          'min_stock_of_cart_items' => 'Minimum stock quantity of cart items' . $pro_label,
      ),
      'Cart items' => array(
          'products_in_cart' => 'Products in cart' . $pro_label,
          'featured_products_in_cart' => 'Featured products in cart' . $pro_label,
          'onsale_products_in_cart' => 'Onsale products in cart' . $pro_label,
          'product_variations_in_cart' => 'Product variations in cart' . $pro_label,
          'product_categories_in_cart' => 'Product categories in cart' . $pro_label,
          'product_brands_in_cart' => 'Product Brands in cart' . $pro_label,
          'shipping_classes_in_cart' => 'Shipping classes in cart' . $pro_label,
      ),
      'Cart items subtotal' => array(
          'subtotal_of_products_include_tax' => 'Subtotal of products include tax' . $pro_label,
          'subtotal_of_products_exclude_tax' => 'Subtotal of products exclude tax' . $pro_label,
          'subtotal_of_onsale_products_include_tax' => 'Subtotal of onsale products include tax' . $pro_label,
          'subtotal_of_onsale_products_exclude_tax' => 'Subtotal of onsale products exclude tax' . $pro_label,
          'subtotal_of_variations_include_tax' => 'Subtotal of variations include tax' . $pro_label,
          'subtotal_of_variations_exclude_tax' => 'Subtotal of variations exclude tax' . $pro_label,
          'subtotal_of_categories_include_tax' => 'Subtotal of categories include tax' . $pro_label,
          'subtotal_of_categories_exclude_tax' => 'Subtotal of categories exclude tax' . $pro_label,
          'subtotal_of_brands_include_tax' => 'Subtotal of Brands include tax' . $pro_label,
          'subtotal_of_brands_exclude_tax' => 'Subtotal of Brands exclude tax' . $pro_label,
      ),
      'Cart items quantity' => array(
          'quantity_of_products' => 'Quantity of products' . $pro_label,
          'quantity_of_variations' => 'Quantity of variations' . $pro_label,
          'quantity_of_categories' => 'Quantity of categories' . $pro_label,
          'quantity_of_brands' => 'Quantity of Brands' . $pro_label,
      ),
      'Customer' => array(
          'user_roles' => 'Roles' . $pro_label,
          'user_capability' => 'User capability' . $pro_label,
          'user_meta' => 'User meta' . $pro_label,
          'user_email' => 'Email' . $pro_label,
          'is_user_logged_in' => 'Is logged in' . $pro_label,
      ),
      'Customer value' => array(
          'money_spent' => 'Money spent' . $pro_label,
          'number_of_orders' => 'Number of orders' . $pro_label,
          'last_order_amount' => 'Last order amount' . $pro_label,
          'average_money_spent_per_order' => 'Average money spent per order' . $pro_label,
          'last_order_date' => 'Last order date' . $pro_label,
          'number_of_products_reviews' => 'Number of products reviews' . $pro_label,
      ),
      'Purchase history' => array(
          'bought_products' => 'Bought products',
          'bought_featured_products' => 'Bought featured products' . $pro_label,
          'bought_onsale_products' => 'Bought onsale products' . $pro_label,
          'bought_product_categories' => 'Bought categories' . $pro_label,
          'bought_product_brands' => 'Bought Brands' . $pro_label,
          
      ),
      'Purchase history quantity' => array(
          'quantity_of_bought_products' => 'Quantity of bought products' . $pro_label,
          'quantity_of_bought_variations' => 'Quantity of bought variations' . $pro_label,
          'quantity_of_bought_categories' => 'Quantity of bought categories' . $pro_label,
          'quantity_of_bought_brands' => 'Quantity of bought Brands' . $pro_label,
      ),
      'Purchase history amount of Money' => array(
          'amount_of_bought_products' => 'Amount of bought products' . $pro_label,
          'amount_of_bought_variations' => 'Amount of bought variations' . $pro_label,
          'amount_of_bought_categories' => 'Amount of bought categories' . $pro_label,
          'amount_of_bought_brands' => 'Amount of bought Brands' . $pro_label,
      ),

      // TODO: Add more conditions
      'Checkout' => array(
          'payment_method' => 'Payment method' . $pro_label,
          'shipping_method' => 'Shipping method' . $pro_label,
      ),
      'Shipping Address' => array(
          'shipping_country' => 'Shipping country' . $pro_label,
          'shipping_state' => 'Shipping state' . $pro_label,
          'shipping_city' => 'Shipping city' . $pro_label,
          'shipping_postcode' => 'Shipping postcode' . $pro_label,
          'shipping_zone' => 'Shipping zone' . $pro_label,
      ),
      
    );

    $condition_compare_fields_options = array(
      'less_than'        => 'Less than',
      'less_equal_to'    => 'Less equal to',
      'greater_than'     => 'Greater than',
      'greater_equal_to' => 'Greater equal to',
      'equal_to'         => 'Equal to',
      'not_equal_to'     => 'Not equal to',
      'in'               => 'In',
      'not_in'           => 'Not in',
      'yes'           => 'Yes',
      'no'           => 'no',
    );


    $product_filter_options = $data = [
      'Products of' => [
          'categories' => 'Categories',
          'brands' => 'Brands',
          'variations' => 'Variations',
          'attributes' => 'Attributes',
          'is_featured' => 'Is featured',
          'is_onsale' => 'Is on-sale',
          'is_trending' => 'Is Trending (Sold in last 15 days)',
      ],
      'Product Properties' => [
          'regular_price' => 'Regular price',
          'stock_quantity' => 'Stock quantity',
          'meta' => 'Meta field',
      ],
      
    ];

    $product_filter_options = apply_filters( 'dspilot_product_filter_options', $product_filter_options );    
    $product_options = [
      'all_products' => 'All products'
    ];

    $products = Dspilot_Helper::get_all_product();

    $product_options = $product_options + $products;


    // get options
    $options = get_option( $prefix );

    // Create pricing discount section
    DSPILOT::createSection( $prefix, array(
      'title'  => 'Pricing Discounts',
      'fields' => array(
        array(
            'id'     => 'pricing_discounts',
            'type'   => 'group',
            'title'  => 'Repeater',
            'class'  => 'hidden-title',
            'fields' => array(
              array(
                'id'    => 'id',
                'type'  => 'text',
                'title' => 'Name'
              ),
              array(
                'id'    => 'status',
                'type'  => 'switcher',
                'title' => 'Status',
              ),
              array(
                'id'    => 'mode',
                'type'  => 'select',
                'title' => 'Mode',
                'class' => 'discount-mode-select',
                'options' => array(
                  'simple' => 'Simple',
                  'bulk' => 'Bulk' . $pro_label,
                  'bundle' => 'Bundle',
                  'buy_x_get_x' => 'Buy x Get x One',
                  'buy_x_get_y' => 'Buy x Get y One',
                ),
              ),
              array(
                'id'    => 'quantity_based',
                'type'  => 'select',
                'title' => 'Quantity Based',
                'class' => 'quantity-based',
                'options' => array(
                  'Single Product' => [
                    'single_product' => 'Single Product',
                    'single_variation' => 'Single Variation',
                  ],
                  'All Products' => [
                    'category' => 'Sum of Categories of All Products',
                    'all_products' => 'Sum of All Products Quantities',
                  ],
                ),
                'dependency' => array(
                  array('mode', '!=', 'simple'),
                  array('mode', '!=', 'bundle'),
                  array('mode', '!=', 'buy_x_get_x')
                )
              ),
              array(
                'id'    => 'type',
                'type'  => 'select',
                'title' => 'Discount Type',
                'options' => array(
                  'fixed' => 'Fixed Amount',
                  'percentage' => 'Percentage',
                ),
                'dependency' => array(
                  'mode', '==', 'simple'
                ),
              ),
              array(
                'id'    => 'amount',
                'type'  => 'number',
                'title' => 'Discount Amount',
                'dependency' => array(
                  'mode', '==', 'simple'
                ),
              ),
              // repeater for minimum and maximum quanity based discounts
              array(
                'id'    => 'bulk_amounts',
                'type'  => 'repeater',
                'title' => 'Purchase Quantity Based Discounts',
                'desc' => 'You can add multiple quantity based discounts. For example, if you want to give a discount of 10% for 5-10 products and 20% for 11-20 products for your discounted prouducts, you can add two entries here.',
                'button_title' => 'Add Discount',
                'class' => 'dspilot-repeater quantity-based-discounts-repeater',
                'fields' => array(
                  array(
                    'id'    => 'minimum_quantity',
                    'type'  => 'number',
                    'title' => 'Minimum Quantity',
                    'min' => 1,
                  ),
                  array(
                    'id'    => 'maximum_quantity',
                    'type'  => 'number',
                    'title' => 'Maximum Quantity',
                  ),
                  array(
                    'id'    => 'amount',
                    'type'  => 'number',
                    'title' => 'Discount Amount'
                  ),
                  array(
                    'id'    => 'type',
                    'type'  => 'select',
                    'title' => 'Discount Type',
                    'options' => array(
                      'fixed' => 'Fixed Amount',
                      'percentage' => 'Percentage',
                    ),
                  ),
                ),
                'dependency' => array(
                  'mode', '==', 'bulk'
                ),
              ),
              array(
                'id'    => 'bundle_amounts',
                'type'  => 'repeater',
                'title' => 'Bundle Products Based Discounts',
                'desc' => 'You can add multiple bundle products based discounts. For example, if you want to give a discount of 10% for 3 bundled products and 20% for 5 bundled products for your discounted prouducts, you can add two entries here.',
                'button_title' => 'Add Discount',
                'class' => 'dspilot-repeater quantity-based-discounts-repeater',
                'fields' => array(
                  array(
                    'id'    => 'quantity',
                    'type'  => 'number',
                    'title' => 'Quantity',
                    'min' => 1,
                  ),
                  array(
                    'id'    => 'type',
                    'type'  => 'select',
                    'title' => 'Discount Type',
                    'options' => array(
                      'Pirce' => array(
                        'fixed_price_per_bundle' => 'Fixed Price Per Bundle',
                        'fixed_price_per_item' => 'Fixed Price Per Item',
                      ),
                      'Discount' => array(
                        'fixed_discount_per_bundle' => 'Fixed Discount Per Bundle',
                        'fixed_discount_per_item' => 'Fixed Discount Per Item',
                        'percentage_discount_per_bundle' => 'Percentage Discount Per Bundle',
                        'percentage_discount_per_item' => 'Percentage Discount Per Item',
                      )
                    ),
                  ),
                  array(
                    'id'    => 'amount',
                    'type'  => 'number',
                    'title' => 'Discount / Price Amount'
                  ),
                ),
                'dependency' => array(
                  'mode', '==', 'bundle'
                ),
              ),
              array(
                'id'    => 'bogo_y_product',
                'type'  => 'select',
                'class' => 'bogo-y-product-select',
                'title' => 'Bogo Y Product',
                'options'     => $products,
                // 'query_args'  => array(
                //   'post_type' => 'product',
                // ),
                //'default' => $products[0],
                'multiple'  => false,
                'dependency' => array(
                  'mode', '==', 'buy_x_get_y'
                ),
              ),
              array(
                'id'    => 'bogo_y_product_variation_id',
                'type'  => 'select',
                'class' => 'bogo-y-product-variations',
                //'chosen'      => true,
                'title' => 'Variations',
                'multiple'    => false,
                'placeholder' => 'Select a variation',
                'options' => [''],
                'default' => $options['bogo_y_product_variation_id'] ?? '',
                'dependency' => array(
                  'mode', '==', 'buy_x_get_y'
                ),
              ),
              array(
                'id'    => 'bogo_discounts_x_x',
                'type'  => 'repeater',
                'title' => 'Bogo Disocunt Rules',
                'desc' => 'Set Purchase and discount rules here. Here you can set bogo discount for same product purchase. For you set purchase quantity 2 and receive quantity 1, for this rules if user purchase 2 quanty of product then he will get discount for 1 quantity for same product.',
                'button_title' => 'Add Discount',
                'class' => 'dspilot-repeater quantity-based-discounts-repeater',
                'fields' => array(
                  array(
                    'id'    => 'minimum_quantity',
                    'type'  => 'number',
                    'title' => 'Min Purchase Quantity',
                    'min' => 1,
                  ),
                  array(
                    'id'    => 'maximum_quantity',
                    'type'  => 'number',
                    'title' => 'Maximum Purchase Quantity',
                    'min' => 1,
                  ),
                  array(
                    'id'    => 'discount_quantity',
                    'type'  => 'number',
                    'title' => 'Discount Quantity',
                    'min' => 1,
                  ),
                  array(
                    'id'    => 'type',
                    'type'  => 'select',
                    'title' => 'Discount Type',
                    'options' => array(
                      'fixed' => 'Fixed Discount',
                      'percentage' => 'Percentage Discount',
                      'price' => 'Fixed Price',
                    ),
                    'default' => 'percentage'
                  ),
                  array(
                    'id'    => 'amount',
                    'type'  => 'number',
                    'title' => 'Discount Amount',
                    'default' => 100
                  ),
                ),
                'dependency' => array(
                  'mode', '==', 'buy_x_get_x'
                ),
              ),
              array(
                'id'    => 'bogo_discounts_x_y',
                'type'  => 'repeater',
                'title' => 'Bogo Disocunt Rules',
                'desc' => 'Set Purchase and discount rules here. Here you can set bogo discount for same product purchase. For you set purchase quantity 2 and receive quantity 1, for this rules if user purchase 2 quanty of product then he will get discount for 1 quantity for same product.',
                'button_title' => 'Add Discount',
                'class' => 'dspilot-repeater quantity-based-discounts-repeater',
                'fields' => array(
                  array(
                    'id'    => 'minimum_quantity',
                    'type'  => 'number',
                    'title' => 'Min Purchase Quantity',
                    'min' => 1,
                  ),
                  array(
                    'id'    => 'maximum_quantity',
                    'type'  => 'number',
                    'title' => 'Maximum Purchase Quantity',
                    'min' => 1,
                  ),
                  array(
                    'id'    => 'discount_quantity',
                    'type'  => 'number',
                    'title' => 'Discount Quantity',
                    'min' => 1,
                  ),
                  array(
                    'id'    => 'type',
                    'type'  => 'select',
                    'title' => 'Discount Type',
                    'options' => array(
                      'fixed' => 'Fixed Discount',
                      'percentage' => 'Percentage Discount',
                      'price' => 'Fixed Price',
                    ),
                    'default' => 'percentage'
                  ),
                  array(
                    'id'    => 'amount',
                    'type'  => 'number',
                    'title' => 'Discount Amount',
                    'default' => 100
                  ),
                ),
                'dependency' => array(
                  'mode', '==', 'buy_x_get_y'
                ),
              ),
              array(
                'id'    => 'start_date',
                'type'  => 'datetime',
                'title' => 'Start Date',
                'settings' => array(
                  'enableTime' => true,
                ),
              ),
              array(
                'id'    => 'end_date',
                'type'  => 'datetime',
                'title' => 'End Date',
                'settings' => array(
                  'enableTime' => true,
                ),
              ),
              array(
                'id'    => 'message',
                'type'  => 'textarea',
                'title' => 'Message',
              ),
              array(
                    'id'    => 'products',
                    'type'  => 'select',
                    'class' => 'products-select',
                    'title' => 'Products',
                    'options'     => $product_options,
                    // 'query_args'  => array(
                    //   'post_type' => 'product',
                    // ),
                    'default' => 'all_products',
                    'multiple'  => true,
                    //'chosen'      => true,
              ),
              //products groups by filter
              array(
                'id'    => 'products_filter',
                'type'  => 'repeater',
                'title' => 'Products Filter',
                'button_title' => 'Add Filter',
                'class' => 'dspilot-repeater conditions-repeater',
                'desc'  => 'You can add multiple conditions to filter products for this discount.',
                'fields' => array(
                  array(
                    'id'    => 'condition',
                    'type'  => 'select',
                    'class' => 'condition-select',
                    'title' => 'Filter Condition',
                    'options' => $product_filter_options,
                    'multiple'    => false,
                  ),
                  array(
                      'id'    => 'product_meta_key',
                      'type'  => 'text',
                      'class' => 'value-product-meta-keys',
                      'title' => 'Meta Key',
                      'placeholder' => 'Meta Key',
                      'dependency' => ['condition', '==', 'product_meta'],
                  ),
                  array(
                    'id'    => 'condition_compare',
                    'type'  => 'select',
                    'class' => 'compare-select',
                    'options' => $condition_compare_fields_options,
                    'title' => 'Compare',
                    'default' => 'in',
                    'dependency' => array(
                      ['condition', '!=', 'all_products'],
                    ),
                  ),
                  array(
                    'id'    => 'text',
                    'type'  => 'text',
                    'title' => 'Value',
                    'class' => 'value-text',
                  ),
                  array(
                    'id'    => 'number',
                    'type'  => 'number',
                    'title' => 'Value',
                    'class' => 'value-number',
                  ),
                  array(
                    'id'    => 'category_ids',
                    'type'  => 'select',
                    'class' => 'value-categories',
                    //'chosen'      => true,
                    'title' => 'Categories',
                    'multiple'    => true,
                    'placeholder' => 'Select category',
                    'options'     => 'categories',
                    'query_args'  => array(
                      'taxonomy' => 'product_cat',
                    ),
                  ),
                  array(
                    'id'    => 'brand_ids',
                    'type'  => 'select',
                    'class' => 'value-brands',
                    //'chosen'      => true,
                    'title' => 'Brands',
                    'multiple'    => true,
                    'placeholder' => 'Select brand',
                    'options'     => Dspilot_Helper::get_wc_brands(),
                  ),
                  array(
                    'id'    => 'variation_ids',
                    'type'  => 'select',
                    'class' => 'value-variations',
                    //'chosen'      => true,
                    'title' => 'Variations',
                    'multiple'    => true,
                    'placeholder' => 'Select a variation',
                    'options'     => Dspilot_Helper::get_product_variations_cached(),
                    //'options' => [''],
                  ),
                  
                ),
              ),
              
              array(
                'id'    => 'condition_groups',
                'type'  => 'group',
                'title' => 'Conditions Group',
                'class' => 'conditions-group',
                'button_title' => 'Add Conditions Group',
                'accordion_title_number' => true,
                'accordion_title_prefix' => 'Condition Group',
                'fields' => array(
                  array(
                    'id'    => 'conditions',
                    'type'  => 'repeater',
                    'title' => 'Conditions',
                    'button_title' => 'Add Or Condition',
                    'class' => 'dspilot-repeater conditions-repeater',
                    'fields' => array(
                      array(
                        'id'    => 'condition',
                        'type'  => 'select',
                        'class' => 'condition-select',
                        'title' => 'Condition',
                        'options' => $condition_fields_options,
                        'multiple'    => false,
                      ),
                      array(
                        'id'    => 'condition_compare',
                        'type'  => 'select',
                        'class' => 'compare-select',
                        'options' => $condition_compare_fields_options,
                        'title' => 'Compare',
                        'default' => 'equal_to',
                      
                      ),
                      array(
                        'id'    => 'user_meta_key',
                        'type'  => 'select',
                        'class' => 'value-user-meta-keys',
                        'title' => 'User Meta Keys',
                        //'chosen'      => true,
                        'multiple'    => false,
                        'placeholder' => 'Select an option',
                        'options'     => Dspilot_Helper::get_all_user_meta_keys(),
                        'dependency' => ['condition', '==', 'user_meta'],
                      ),
                      
                      array(
                        'id'    => 'text',
                        'type'  => 'text',
                        'title' => 'Value',
                        'class' => 'value-text',
                      ),
                      array(
                        'id'    => 'date',
                        'type'  => 'date',
                        'title' => 'Value',
                        'class' => 'value-date',
                        'settings' => array(
                          'enableTime' => false,
                          'dateFormat' => 'dd-mm-yy'
                        ),
                        'dependency' => array(
                          ['condition', '==', 'last_order_date'],
                        ),
                      ),
                      array(
                        'id'    => 'number',
                        'type'  => 'number',
                        'title' => 'Value',
                        'class' => 'value-number',
                        'default' => 0,
                      ),
                      array(
                        'id'    => 'product_ids',
                        'type'  => 'select',
                        'class' => 'value-products',
                        //'chosen'      => true,
                        'title' => 'Products',
                        'multiple'    => true,
                        'placeholder' => 'Select an option',
                        'options'     => 'posts',
                        'query_args'  => array(
                          'post_type' => 'product',
                        ),
                      ),
                      array(
                        'id'    => 'product_id',
                        'type'  => 'select',
                        'class' => 'value-product',
                        //'chosen'      => true,
                        'title' => 'Product',
                        'multiple'    => true,
                        'placeholder' => 'Select an product',
                        'options'     => 'posts',
                        'query_args'  => array(
                          'post_type' => 'product',
                        ),
                      ),
                      
                      array(
                        'id'    => 'category_ids',
                        'type'  => 'select',
                        'class' => 'value-categories',
                        //'chosen'      => true,
                        'title' => 'Categories',
                        'multiple'    => true,
                        'placeholder' => 'Select an option',
                        'options'     => 'categories',
                        'query_args'  => array(
                          'taxonomy' => 'product_cat',
                        ),
                      ),
                      array(
                        'id'    => 'brand_ids',
                        'type'  => 'select',
                        'class' => 'value-brands',
                        //'chosen'      => true,
                        'title' => 'Brands',
                        'multiple'    => true,
                        'placeholder' => 'Select an option',
                        'placeholder' => 'Select a brand',
                        'options'     => Dspilot_Helper::get_wc_brands(),
                      ),
                      array(
                        'id'    => 'variation_ids',
                        'type'  => 'select',
                        'class' => 'value-variations',
                        //'chosen'      => true,
                        'title' => 'Variations',
                        'multiple'    => true,
                        'placeholder' => 'Select a variation',
                        'options'     => ['']
                      ),
                      array(
                        'id'    => 'shipping_classes',
                        'type'  => 'select',
                        'class' => 'value-shipping-classes',
                        //'chosen'      => true,
                        'title' => 'Shipping Classes',
                        'multiple'    => true,
                        'placeholder' => 'Select an option',
                        'options'     => Dspilot_Helper::get_shipping_classes(),
                        'dependency' => ['condition', '==', 'shipping_classes_in_cart'],
                      ),
                      array(
                        'id'    => 'user_roles',
                        'type'  => 'select',
                        'class' => 'value-user-roles',
                        'title' => 'User Role',
                        //'chosen'      => true,
                        'multiple'    => true,
                        'placeholder' => 'Select an option',
                        'options'     => Dspilot_Helper::get_user_roles(),
                        'dependency' => ['condition', '==', 'user_roles'],
                      ),
                      
                      array(
                        'id'    => 'bool',
                        'type'  => 'select',
                        'class' => 'value-bool',
                        'title' => 'Boolean',
                        //'chosen'      => true,
                        'multiple'    => false,
                        'placeholder' => 'Select an option',
                        'options'     => ['true' => 'True', 'false' => 'False'],
                        'dependency' => ['condition', '==', 'is_user_logged_in'],
                      ),
                      array(
                        'id'    => 'user_capabilities',
                        'type'  => 'select',
                        'class' => 'value-user-capabilities',
                        'title' => 'User Capabilities',
                        //'chosen'      => true,
                        'multiple'    => false,
                        'placeholder' => 'Select an option',
                        'options'     => Dspilot_Helper::get_user_capabilities(),
                        'dependency' => array(
                          ['condition', '==', 'user_capability'],
                        ),
                      ),
                      array(
                        'id'    => 'payment_methods',
                        'type'  => 'select',
                        'class' => 'value-payment-method',
                        'title' => 'Payment Methods',
                        //'chosen'      => true,
                        'multiple'    => true,
                        'placeholder' => 'Select an option',
                        //'options'     => Dspilot_Helper::get_wc_payment_methods(),
                        'options'     => [],
                        'dependency' => array(
                          ['condition', '==', 'payment_method'],
                        ),
                      ),
                      array(
                        'id'    => 'shipping_methods',
                        'type'  => 'select',
                        'class' => 'value-shipping-method',
                        'title' => 'Shipping Method',
                        //'chosen'      => true,
                        'multiple'    => true,
                        'placeholder' => 'Select an option',
                        //'options'     => Dspilot_Helper::get_all_wc_shipping_methods(),
                        'options'     => [],
                        'dependency' => array(
                          ['condition', '==', 'shipping_method'],
                        ),
                      ),
                      array(
                        'id'    => 'shipping_countries',
                        'type'  => 'select',
                        'class' => 'value-shipping-contries',
                        'title' => 'Shipping Countries',
                        //'chosen'      => true,
                        'multiple'    => true,
                        'placeholder' => 'Select an country',
                        'options'     => Dspilot_Helper::get_all_wc_countries_and_states_cached()['countries'],
                        //'options'     => [],
                        'dependency' => array(
                          ['condition', '==', 'shipping_country'],
                        ),
                      ),
                      array(
                        'id'    => 'shipping_states',
                        'type'  => 'select',
                        'class' => 'value-shipping-states',
                        'title' => 'Shipping States',
                        //'chosen'      => true,
                        'multiple'    => true,
                        'placeholder' => 'Select an states',
                        'options'     => Dspilot_Helper::get_all_wc_countries_and_states_cached()['states'],
                        // 'options'     => [],
                        'dependency' => array(
                          ['condition', '==', 'shipping_state'],
                        ),
                      ),
                      array(
                        'id'    => 'shipping_zones',
                        'type'  => 'select',
                        'class' => 'value-shipping-zones',
                        'title' => 'Shipping Zones',
                        //'chosen'      => true,
                        'multiple'    => true,
                        'placeholder' => 'Select an zones',
                        'options'     => Dspilot_Helper::get_all_wc_shipping_zones_cached(),
                        //'options'     => [],
                        'dependency' => array(
                          ['condition', '==', 'shipping_zone'],
                        ),
                      ),
                      
                    ),
                  ),
                ),
              ),
              

            ),
        ),
      )
    ) );

    // Create coupon discount section
    DSPILOT::createSection( $prefix, array(
      'title'  => 'Cart & Coupon Discounts',
      'fields' => array(
        array(
            'id'     => 'coupon_discounts',
            'type'   => 'group',
            'title'  => 'Repeater',
            'class'  => 'hidden-title',
            'fields' => array(
              array(
                'id'    => 'label',
                'type'  => 'text',
                'title' => 'Label'
              ),
              array(
                'id'    => 'id',
                'type'  => 'text',
                'title' => 'Coupon Code'
              ),
              array(
                'id'    => 'status',
                'type'  => 'switcher',
                'title' => 'Status',
              ),
              array(
                'id'    => 'type',
                'type'  => 'select',
                'title' => 'Discount Type',
                'options' => array(
                  'fixed' => 'Fixed Amount',
                  'percentage' => 'Percentage',
                ),
              ),
              array(
                'id'    => 'amount',
                'type'  => 'number',
                'title' => 'Discount Amount',
              ),
              array(
                'id'    => 'auto_apply',
                'type'  => 'switcher',
                'title' => 'Auto Apply',
                'default' => false,
              ),
              array(
                'id'    => 'url_coupon',
                'type'  => 'switcher',
                'title' => 'URL Coupon',
                'desc' => 'Enable this option to generate a URL coupon for this discount. Url coupon like this: <code>https://yoursite.com/cart/?coupon=coupon_code</code>',
              ),
              array(
                'id'    => 'usage_limit',
                'type'  => 'number',
                'title' => 'Usage Limit',
              ),
              array(
                'id'    => 'usage_limit_per_user',
                'type'  => 'number',
                'title' => 'Usage Limit Per User',
              ),
              array(
                'id'    => 'start_date',
                'type'  => 'datetime',
                'title' => 'Start Date',
                'settings' => array(
                  'enableTime' => true,
                ),
              ),
              array(
                'id'    => 'end_date',
                'type'  => 'datetime',
                'title' => 'End Date',
                'settings' => array(
                  'enableTime' => true,
                ),
              ),
              array(
                    'id'    => 'products',
                    'type'  => 'select',
                    'class' => 'products-select',
                    'title' => 'Products',
                    'options'     => $product_options,
                    // 'query_args'  => array(
                    //   'post_type' => 'product',
                    // ),
                    'default' => 'all_products',
                    'multiple'  => true,
                    //'chosen'      => true,
              ),
              //products groups by filter
              array(
                'id'    => 'products_filter',
                'type'  => 'repeater',
                'title' => 'Products Filter',
                'button_title' => 'Add Filter',
                'class' => 'dspilot-repeater conditions-repeater',
                'desc'  => 'You can add multiple conditions to filter products for this discount.',
                'fields' => array(
                  array(
                    'id'    => 'condition',
                    'type'  => 'select',
                    'class' => 'condition-select',
                    'title' => 'Filter Condition',
                    'options' => $product_filter_options,
                    'multiple'    => false,
                  ),
                  array(
                      'id'    => 'product_meta_key',
                      'type'  => 'text',
                      'class' => 'value-product-meta-keys',
                      'title' => 'Meta Key',
                      'placeholder' => 'Meta Key',
                      'dependency' => ['condition', '==', 'product_meta'],
                  ),
                  array(
                    'id'    => 'condition_compare',
                    'type'  => 'select',
                    'class' => 'compare-select',
                    'options' => $condition_compare_fields_options,
                    'title' => 'Compare',
                    'default' => 'in',
                    'dependency' => array(
                      ['condition', '!=', 'all_products'],
                    ),
                  ),
                  array(
                    'id'    => 'text',
                    'type'  => 'text',
                    'title' => 'Value',
                    'class' => 'value-text',
                  ),
                  array(
                    'id'    => 'number',
                    'type'  => 'number',
                    'title' => 'Value',
                    'class' => 'value-number',
                  ),
                  array(
                    'id'    => 'category_ids',
                    'type'  => 'select',
                    'class' => 'value-categories',
                    //'chosen'      => true,
                    'title' => 'Categories',
                    'multiple'    => true,
                    'placeholder' => 'Select category',
                    'options'     => 'categories',
                    'query_args'  => array(
                      'taxonomy' => 'product_cat',
                    ),
                  ),
                  array(
                    'id'    => 'brand_ids',
                    'type'  => 'select',
                    'class' => 'value-brands',
                    //'chosen'      => true,
                    'title' => 'Brands',
                    'multiple'    => true,
                    'placeholder' => 'Select brand',
                    'options'     => Dspilot_Helper::get_wc_brands(),
                  ),
                  array(
                    'id'    => 'variation_ids',
                    'type'  => 'select',
                    'class' => 'value-variations',
                    //'chosen'      => true,
                    'title' => 'Variations',
                    'multiple'    => true,
                    'placeholder' => 'Select a variation',
                    'options'     => Dspilot_Helper::get_product_variations_cached(),
                    //'options' => [''],
                  ),
                  
                ),
              ),
              
              array(
                'id'    => 'condition_groups',
                'type'  => 'group',
                'title' => 'Conditions Group',
                'class' => 'conditions-group',
                'button_title' => 'Add Conditions Group',
                'accordion_title_number' => true,
                'accordion_title_prefix' => 'Condition Group',
                'fields' => array(
                  array(
                    'id'    => 'conditions',
                    'type'  => 'repeater',
                    'title' => 'Conditions',
                    'button_title' => 'Add Or Condition',
                    'class' => 'dspilot-repeater conditions-repeater',
                    'fields' => array(
                      array(
                        'id'    => 'condition',
                        'type'  => 'select',
                        'class' => 'condition-select',
                        'title' => 'Condition',
                        'options' => $condition_fields_options,
                        'multiple'    => false,
                      ),
                      array(
                        'id'    => 'condition_compare',
                        'type'  => 'select',
                        'class' => 'compare-select',
                        'options' => $condition_compare_fields_options,
                        'title' => 'Compare',
                        'default' => 'equal_to',
                      
                      ),
                      array(
                        'id'    => 'user_meta_key',
                        'type'  => 'select',
                        'class' => 'value-user-meta-keys',
                        'title' => 'User Meta Keys',
                        //'chosen'      => true,
                        'multiple'    => false,
                        'placeholder' => 'Select an option',
                        'options'     => Dspilot_Helper::get_all_user_meta_keys(),
                        'dependency' => ['condition', '==', 'user_meta'],
                      ),
                      
                      array(
                        'id'    => 'text',
                        'type'  => 'text',
                        'title' => 'Value',
                        'class' => 'value-text',
                      ),
                      array(
                        'id'    => 'date',
                        'type'  => 'date',
                        'title' => 'Value',
                        'class' => 'value-date',
                        'settings' => array(
                          'enableTime' => false,
                          'dateFormat' => 'dd-mm-yy'
                        ),
                        'dependency' => array(
                          ['condition', '==', 'last_order_date'],
                        ),
                      ),
                      array(
                        'id'    => 'number',
                        'type'  => 'number',
                        'title' => 'Value',
                        'class' => 'value-number',
                        'default' => 0,
                      ),
                      array(
                        'id'    => 'product_ids',
                        'type'  => 'select',
                        'class' => 'value-products',
                        //'chosen'      => true,
                        'title' => 'Products',
                        'multiple'    => true,
                        'placeholder' => 'Select an option',
                        'options'     => 'posts',
                        'query_args'  => array(
                          'post_type' => 'product',
                        ),
                      ),
                      array(
                        'id'    => 'product_id',
                        'type'  => 'select',
                        'class' => 'value-product',
                        //'chosen'      => true,
                        'title' => 'Product',
                        'multiple'    => true,
                        'placeholder' => 'Select an product',
                        'options'     => 'posts',
                        'query_args'  => array(
                          'post_type' => 'product',
                        ),
                      ),
                      
                      array(
                        'id'    => 'category_ids',
                        'type'  => 'select',
                        'class' => 'value-categories',
                        //'chosen'      => true,
                        'title' => 'Categories',
                        'multiple'    => true,
                        'placeholder' => 'Select an option',
                        'options'     => 'categories',
                        'query_args'  => array(
                          'taxonomy' => 'product_cat',
                        ),
                      ),
                      array(
                        'id'    => 'brand_ids',
                        'type'  => 'select',
                        'class' => 'value-brands',
                        //'chosen'      => true,
                        'title' => 'Brands',
                        'multiple'    => true,
                        'placeholder' => 'Select an option',
                        'placeholder' => 'Select a brand',
                        'options'     => Dspilot_Helper::get_wc_brands(),
                      ),
                      array(
                        'id'    => 'variation_ids',
                        'type'  => 'select',
                        'class' => 'value-variations',
                        //'chosen'      => true,
                        'title' => 'Variations',
                        'multiple'    => true,
                        'placeholder' => 'Select a variation',
                        'options'     => ['']
                      ),
                      array(
                        'id'    => 'shipping_classes',
                        'type'  => 'select',
                        'class' => 'value-shipping-classes',
                        //'chosen'      => true,
                        'title' => 'Shipping Classes',
                        'multiple'    => true,
                        'placeholder' => 'Select an option',
                        'options'     => Dspilot_Helper::get_shipping_classes(),
                        'dependency' => ['condition', '==', 'shipping_classes_in_cart'],
                      ),
                      array(
                        'id'    => 'user_roles',
                        'type'  => 'select',
                        'class' => 'value-user-roles',
                        'title' => 'User Role',
                        //'chosen'      => true,
                        'multiple'    => true,
                        'placeholder' => 'Select an option',
                        'options'     => Dspilot_Helper::get_user_roles(),
                        'dependency' => ['condition', '==', 'user_roles'],
                      ),
                      
                      array(
                        'id'    => 'bool',
                        'type'  => 'select',
                        'class' => 'value-bool',
                        'title' => 'Boolean',
                        //'chosen'      => true,
                        'multiple'    => false,
                        'placeholder' => 'Select an option',
                        'options'     => ['true' => 'True', 'false' => 'False'],
                        'dependency' => ['condition', '==', 'is_user_logged_in'],
                      ),
                      array(
                        'id'    => 'user_capabilities',
                        'type'  => 'select',
                        'class' => 'value-user-capabilities',
                        'title' => 'User Capabilities',
                        //'chosen'      => true,
                        'multiple'    => false,
                        'placeholder' => 'Select an option',
                        'options'     => Dspilot_Helper::get_user_capabilities(),
                        'dependency' => array(
                          ['condition', '==', 'user_capability'],
                        ),
                      ),
                      array(
                        'id'    => 'payment_methods',
                        'type'  => 'select',
                        'class' => 'value-payment-method',
                        'title' => 'Payment Methods',
                        //'chosen'      => true,
                        'multiple'    => true,
                        'placeholder' => 'Select an option',
                        //'options'     => Dspilot_Helper::get_wc_payment_methods(),
                        'options'     => [],
                        'dependency' => array(
                          ['condition', '==', 'payment_method'],
                        ),
                      ),
                      array(
                        'id'    => 'shipping_methods',
                        'type'  => 'select',
                        'class' => 'value-shipping-method',
                        'title' => 'Shipping Method',
                        //'chosen'      => true,
                        'multiple'    => true,
                        'placeholder' => 'Select an option',
                        //'options'     => Dspilot_Helper::get_all_wc_shipping_methods(),
                        'options'     => [],
                        'dependency' => array(
                          ['condition', '==', 'shipping_method'],
                        ),
                      ),
                      array(
                        'id'    => 'shipping_countries',
                        'type'  => 'select',
                        'class' => 'value-shipping-contries',
                        'title' => 'Shipping Countries',
                        //'chosen'      => true,
                        'multiple'    => true,
                        'placeholder' => 'Select an country',
                        'options'     => Dspilot_Helper::get_all_wc_countries_and_states_cached()['countries'],
                        //'options'     => [],
                        'dependency' => array(
                          ['condition', '==', 'shipping_country'],
                        ),
                      ),
                      array(
                        'id'    => 'shipping_states',
                        'type'  => 'select',
                        'class' => 'value-shipping-states',
                        'title' => 'Shipping States',
                        //'chosen'      => true,
                        'multiple'    => true,
                        'placeholder' => 'Select an states',
                        'options'     => Dspilot_Helper::get_all_wc_countries_and_states_cached()['states'],
                        // 'options'     => [],
                        'dependency' => array(
                          ['condition', '==', 'shipping_state'],
                        ),
                      ),
                      array(
                        'id'    => 'shipping_zones',
                        'type'  => 'select',
                        'class' => 'value-shipping-zones',
                        'title' => 'Shipping Zones',
                        //'chosen'      => true,
                        'multiple'    => true,
                        'placeholder' => 'Select an zones',
                        'options'     => Dspilot_Helper::get_all_wc_shipping_zones_cached(),
                        //'options'     => [],
                        'dependency' => array(
                          ['condition', '==', 'shipping_zone'],
                        ),
                      ),
                      
                    ),
                  ),
                ),
              ),
              

            ),
        ),
      )
    ) );

    DSPILOT::createSection( $prefix, array(
      'title'  => 'General Settings',
      'fields' => array(
          array(
                'id'    => 'show_discount_table',
                'type'  => 'switcher',
                'title' => 'Show Discount Table on Product Page',
                'default' => true,
                'desc'  => 'Enable this option to show the discount table on the single product page.',
          ),
          array(
                'id'    => 'show_discount_message',
                'type'  => 'switcher',
                'title' => 'Show Discount Message on Product Page',
                'default' => true,
                'desc'  => 'Enable this option to show the discount message on the single product page.',
          ),

        )
      )
    );


    })();
}