<?php

add_filter('zcpri/get-condition-type-group-cart_items_subtotal', 'zc_pri_get_condition_type_cart_items_subtotal_exc_tax_products', 10, 2);
if (!function_exists('zc_pri_get_condition_type_cart_items_subtotal_exc_tax_products')) {

    function zc_pri_get_condition_type_cart_items_subtotal_exc_tax_products($list = array(), $args) {
        $list['cart_items_subtotal_exc_tax_products'] = __('Products Subtotal', 'zc-woo-pricely');
        return $list;
    }

}

add_filter('zcpri/get-condition-type-fields', 'zc_pri_get_condition_type_cart_items_subtotal_exc_tax_products_fields', 10, 2);

if (!function_exists('zc_pri_get_condition_type_cart_items_subtotal_exc_tax_products_fields')) {

    function zc_pri_get_condition_type_cart_items_subtotal_exc_tax_products_fields($fields, $args) {

        $fields['cart_items_subtotal_exc_tax_products'] = array(
            array(
                'id' => 'product_ids',
                'type' => 'select2',
                'multiple' => true,
                'minimum_input_length' => 2,
                'placeholder' => 'Search products...',
                'allow_clear' => true,
                'minimum_results_forsearch' => 10,
                'data' => array(
                    'source' => 'wc:products',
                    'ajax' => true,
                    'value_col' => 'id',
                    'value_col_pre' => '#',
                    'show_value' => true,
                ),
                'width' => '99%',
                'box_width' => '38%',
            ),
            array(
                'id' => 'compare',
                'type' => 'select2',
                'default' => '>',
                'options' => array(
                    '>=' => __('More than or equal to', 'zc-woo-pricely'),
                    '>' => __('More than', 'zc-woo-pricely'),
                    '<=' => __('Less than or equal to', 'zc-woo-pricely'),
                    '<' => __('Less than', 'zc-woo-pricely'),
                    '==' => __('Equal to', 'zc-woo-pricely'),
                    '!=' => __('Not equal to', 'zc-woo-pricely'),
                ),
                'width' => '98%',
                'box_width' => '31%',
            ),
            array(
                'id' => 'subtotal_exc_tax',
                'type' => 'textbox',
                'input_type' => 'number',
                'default' => '0.00',
                'placeholder' => __('0.00', 'zc-woo-pricely'),
                'width' => '100%',
                'box_width' => '12%',
                'attributes' => array(
                    'min' => '0',
                    'step' => '0.01',
                ),
            ),
        );
        return $fields;
    }

}

add_filter('zcpri/validate-condition-cart_items_subtotal_exc_tax_products', 'zc_pri_validate_condition_cart_items_subtotal_exc_tax_products', 10, 3);
if (!function_exists('zc_pri_validate_condition_cart_items_subtotal_exc_tax_products')) {

    function zc_pri_validate_condition_cart_items_subtotal_exc_tax_products($rule, $args) {
      if(!is_array($rule['product_ids'])){
          return false;
      }
        $subtotals = 0;
        foreach (WooPricely::get_products_from_cart() as $product) {
            if (WooPricely_Validation_Util::validate_value_list($product['id'], $rule['product_ids'], 'in_list') != true) {
                continue;
            }
            if (isset($product['cart_price'])) {
                $qty = 1;
                if (isset($product['quantity'])) {
                    $qty = $product['quantity'];
                }
                $subtotals += ((float) ($product['cart_price'] * $qty));
            }
        }


        return WooPricely_Validation_Util::validate_value($rule['compare'], $subtotals, $rule['subtotal_exc_tax'], 'no');
    }

}
