<?php

add_filter('zcpri/get-product-rule-type-group-product_prices', 'zc_pri_get_product_rule_type_product_prices', 10, 2);
if (!function_exists('zc_pri_get_product_rule_type_product_prices')) {

    function zc_pri_get_product_rule_type_product_prices($list = array(), $args) {
        $currency = get_woocommerce_currency_symbol(get_woocommerce_currency());
        $list['regular_price'] = str_replace('[0]', $currency, __('Product Regular Price ([0])', 'zc-woo-pricely'));
        $list['sale_price'] = str_replace('[0]', $currency, __('Product Sales Price ([0])', 'zc-woo-pricely'));
        $list['price'] = str_replace('[0]', $currency, __('Product Price ([0])', 'zc-woo-pricely'));
        return $list;
    }

}

add_filter('zcpri/get-product-rule-type-fields', 'zc_pri_get_product_rule_type_product_prices_fields', 10, 2);

if (!function_exists('zc_pri_get_product_rule_type_product_prices_fields')) {

    function zc_pri_get_product_rule_type_product_prices_fields($fields, $args) {

        $compare = array(
            'id' => 'compare',
            'type' => 'select2',
            'default' => '>=',
            'options' => array(
                'empty' => __('Empty', 'zc-woo-pricely'),
                '>=' => __('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'),
            ),
            'fold_id' => 'regular_price_compare',
            'width' => '99%',
            'box_width' => '44%',
        );
        $fld = array();

        $fld[] = $compare;


        $fld[] = array(
            'id' => 'regular_price',
            'type' => 'textbox',
            'input_type' => 'number',
            'default' => '0.00',
            'placeholder' => __('0.00', 'zc-woo-pricely'),
             'width' => '100%',
            'box_width' => '37%',
            'attributes' => array(
                'min' => '0',
                'step' => '0.01',
            ),
            'fold' => array(
                'target' => 'regular_price_compare',
                'attribute' => 'value',
                'value' => 'empty',
                'oparator' => 'neq', //eq, neq, gt_eq, lt_eq, gt, lt 
            ),
        );

        $fld[] = array(
            'id' => 'yes_no',
            'type' => 'select2',
            'default' => '>=',
            'options' => array(
                'no' => __('No', 'zc-woo-pricely'),
                'yes' => __('Yes', 'zc-woo-pricely'),
            ),
            'fold' => array(
                'target' => 'regular_price_compare',
                'attribute' => 'value',
                'value' => 'empty',
                'oparator' => 'eq', //eq, neq, gt_eq, lt_eq, gt, lt 
                'clear' => true,
            ),
            'width' => '100%',
            'box_width' => '37%',
        );
        $fields['regular_price'] = $fld;


        $compare['fold_id'] = 'sale_price_compare';
        $fld = array();
        $fld[] = $compare;

        $fld[] = array(
            'id' => 'sale_price',
            'type' => 'textbox',
            'input_type' => 'number',
            'default' => '0.00',
            'placeholder' => __('0.00', 'zc-woo-pricely'),
            'width' => '100%',
            'box_width' => '37%',
            'attributes' => array(
                'min' => '0',
                'step' => '0.01',
            ),
            'fold' => array(
                'target' => 'sale_price_compare',
                'attribute' => 'value',
                'value' => 'empty',
                'oparator' => 'neq', //eq, neq, gt_eq, lt_eq, gt, lt 
                'clear' => true,
            ),
        );

        $fld[] = array(
            'id' => 'yes_no',
            'type' => 'select2',
            'default' => '>=',
            'options' => array(
                'no' => __('No', 'zc-woo-pricely'),
                'yes' => __('Yes', 'zc-woo-pricely'),
            ),
            'fold' => array(
                'target' => 'sale_price_compare',
                'attribute' => 'value',
                'value' => 'empty',
                'oparator' => 'eq', //eq, neq, gt_eq, lt_eq, gt, lt 
                'clear' => true,
            ),
            'width' => '100%',
            'box_width' => '37%',
        );
        $fields['sale_price'] = $fld;


        $compare['fold_id'] = 'price_compare';
        $fld = array();
        $fld[] = $compare;

        $fld[] = array(
            'id' => 'price',
            'type' => 'textbox',
            'input_type' => 'number',
            'default' => '0.00',
            'placeholder' => __('0.00', 'zc-woo-pricely'),
            'width' => '100%',
            'box_width' => '37%',
            'attributes' => array(
                'min' => '0',
                'step' => '0.01',
            ),
            'fold' => array(
                'target' => 'price_compare',
                'attribute' => 'value',
                'value' => 'empty',
                'oparator' => 'neq', //eq, neq, gt_eq, lt_eq, gt, lt 
                'clear' => true,
            ),
        );

        $fld[] = array(
            'id' => 'yes_no',
            'type' => 'select2',
            'default' => '>=',
            'options' => array(
                'no' => __('No', 'zc-woo-pricely'),
                'yes' => __('Yes', 'zc-woo-pricely'),
            ),
            'fold' => array(
                'target' => 'price_compare',
                'attribute' => 'value',
                'value' => 'empty',
                'oparator' => 'eq', //eq, neq, gt_eq, lt_eq, gt, lt 
                'clear' => true,
            ),
            'width' => '100%',
            'box_width' => '37%',
        );
        $fields['price'] = $fld;


        return $fields;
    }

}


add_filter('zcpri/validate-product-filter-regular_price', 'zc_pri_validate_product_filter_prices', 10, 3);
add_filter('zcpri/validate-product-filter-sale_price', 'zc_pri_validate_product_filter_prices', 10, 3);
add_filter('zcpri/validate-product-filter-price', 'zc_pri_validate_product_filter_prices', 10, 3);
if (!function_exists('zc_pri_validate_product_filter_prices')) {

    function zc_pri_validate_product_filter_prices($product, $rule, $args) {

        $yes_no = 'no';
        $rule_value = '';
        if (isset($rule['yes_no'])) {
            $yes_no = $rule['yes_no'];
        }
        if (isset($rule['regular_price'])) {
            $rule_value = $rule['regular_price'];
        }

        if (isset($rule['sale_price'])) {
            $rule_value = $rule['sale_price'];
        }
        if (isset($rule['price'])) {
            $rule_value = $rule['price'];
        }


        $price = '';
        if (isset($rule['regular_price'])) {
            $price = wc_get_product($product['id'])->get_regular_price(false);
        }
        if (isset($rule['sale_price'])) {
            $price = wc_get_product($product['id'])->get_sale_price(false);
        }
        if (isset($rule['price'])) {
            $price = wc_get_product($product['id'])->get_price(false);
        }

        return WooPricely_Validation_Util::validate_value($rule['compare'], $price, $rule_value, $yes_no);
    }

}