<?php

add_filter('zcpri/get-product-rule-type-group-products', 'zc_pri_get_product_rule_type_categories', 10, 2);

if (!function_exists('zc_pri_get_product_rule_type_categories')) {

    function zc_pri_get_product_rule_type_categories($list = array(), $args) {
        $list['categories'] = __('Product Categories', 'zc-woo-pricely');
        return $list;
    }

}


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

if (!function_exists('zc_pri_get_product_rule_type_categories_fields')) {

    function zc_pri_get_product_rule_type_categories_fields($fields, $args) {

        $fields['categories'] = array(
            array(
                'id' => 'compare',
                'type' => 'select2',
                'default' => 'in_list',
                'options' => array(
                    'any' => __('Any list?', 'zc-woo-pricely'),
                    'in_list' => __('Any in the list', 'zc-woo-pricely'),
                    'in_all_list' => __('All in the list', 'zc-woo-pricely'),
                    'in_list_only' => __('Only in the list', 'zc-woo-pricely'),
                    'in_all_list_only' => __('Only all in the list', 'zc-woo-pricely'),
                    'none' => __('None in the list', 'zc-woo-pricely'),
                ),
                'fold_id' => 'compare',
                'width' => '98%',
                'box_width' => '25%',
            ),
            array(
                'id' => 'is_any',
                'type' => 'select2',
                'default' => 'in_list',
                'options' => array(
                    'no' => __('No', 'zc-woo-pricely'),
                    'yes' => __('Yes', 'zc-woo-pricely'),
                ),
                'fold' => array(
                    'target' => 'compare',
                    'attribute' => 'value',
                    'value' => 'any',
                    'oparator' => 'eq', //eq, neq, gt_eq, lt_eq, gt, lt 
                    'clear' => true,
                ),
                'width' => '100%',
                'box_width' => '56%',
            ),
            array(
                'id' => 'category_ids',
                'type' => 'select2',
                'multiple' => true,
                'placeholder' => 'Product categories...',
                'data' => 'categories:product_cat',
                'fold' => array(
                    'target' => 'compare',
                    'attribute' => 'value',
                    'value' => 'any',
                    'oparator' => 'neq', //eq, neq, gt_eq, lt_eq, gt, lt 
                    'clear' => true,
                ),
                'width' => '100%',
                'box_width' => '56%',
            )
        );
        return $fields;
    }

}


add_filter('zcpri/validate-product-filter-categories', 'zc_pri_validate_product_filter_categories', 10, 3);
if (!function_exists('zc_pri_validate_product_filter_categories')) {

    function zc_pri_validate_product_filter_categories($product, $rule, $args) {
        if ($rule['compare'] == 'any') {
            return WooPricely_Validation_Util::validate_list_empty($product['categories'], $rule['is_any']);
        } else {
            if (!is_array($rule['category_ids'])) {
                return false;
            }
            return WooPricely_Validation_Util::validate_list_list($product['categories'], $rule['category_ids'], $rule['compare']);
        }
    }

}








add_filter('zcpri/get-products-categories', 'zc_pri_get_products_categories', 10, 3);
if (!function_exists('zc_pri_get_products_categories')) {

    function zc_pri_get_products_categories($result, $rule, $qty) {

        if (!is_array($rule['category_ids'])) {
            return $result;
        }


        $args = array(
            'category' => array($rule['category_ids']),
            'orderby' => 'name',
        );
        if ($rule['compare'] != 'none') {
            return WooPricely::get_products_from_database($result, $args, $qty, false);
        } else {
            return WooPricely::get_products_from_database($result, $args, $qty, true);
        }
    }

}