<?php
if (!defined('ABSPATH')) exit; // Exit if accessed directly
?>

<?php
if (!empty($meta_fields)) :
    foreach ($meta_fields as $iwbvel_custom_field) :
        if (in_array($iwbvel_custom_field['key'], ['file', 'image'])) {
            continue;
        }

        if (!empty($acf_fields)) {
            if (!empty($acf_fields[$iwbvel_custom_field['key']])) {
                $iwbvel_custom_field['main_type'] = (!empty($acf_fields[$iwbvel_custom_field['key']]['field_type'])) ? $acf_fields[$iwbvel_custom_field['key']]['field_type'] : $acf_fields[$iwbvel_custom_field['key']]['type'];
                if (in_array($iwbvel_custom_field['main_type'], ['multi_select'])) {
                    continue;
                }
            } else {
                $iwbvel_custom_field['main_type'] = $iwbvel_custom_field['main_type'];
            }
        }

        $iwbvel_field_id = "iwbvel-filter-form-custom-field-" . esc_attr($iwbvel_custom_field['key']) . "";
        $iwbvel_field_type = ($iwbvel_custom_field['main_type'] == 'textinput' && $iwbvel_custom_field['sub_type'] == 'number') ? 'number' : $iwbvel_custom_field['main_type'];
?>
        <div class="iwbvel-form-group" data-field-type="<?php echo esc_attr($iwbvel_field_type); ?>" data-filter-type="custom_field" data-name="<?php echo esc_attr($iwbvel_custom_field['key']); ?>">
            <label><?php echo esc_html($iwbvel_custom_field['title']); ?></label>
            <?php if (($iwbvel_custom_field['main_type'] == iwbvel\classes\repositories\Meta_Field::TEXTINPUT && $iwbvel_custom_field['sub_type'] == iwbvel\classes\repositories\Meta_Field::STRING_TYPE)
                || in_array($iwbvel_custom_field['main_type'], [
                    iwbvel\classes\repositories\Meta_Field::TEXT,
                    iwbvel\classes\repositories\Meta_Field::EMAIL,
                    iwbvel\classes\repositories\Meta_Field::PASSWORD,
                    iwbvel\classes\repositories\Meta_Field::TEXTAREA,
                    iwbvel\classes\repositories\Meta_Field::EDITOR,
                    iwbvel\classes\repositories\Meta_Field::URL
                ])
            ) : ?>
                <select title="<?php esc_attr_e('Select Operator', 'ithemeland-bulk-variation-editing-for-woocommerce'); ?>" data-field="operator">
                    <?php include IWBVEL_VIEWS_DIR . 'bulk_edit/filter_form/operators/text.php'; ?>
                </select>
                <input type="text" data-field="value" id="<?php echo esc_attr($iwbvel_field_id); ?>" placeholder="<?php echo esc_attr($iwbvel_custom_field['title']); ?> ..." title="<?php echo esc_attr($iwbvel_custom_field['title']); ?>" <?php if ($iwbvel_custom_field['main_type'] == iwbvel\classes\repositories\Meta_Field::CALENDAR) : ?> class="iwbvel-datepicker" <?php endif; ?>>
            <?php elseif (
                ($iwbvel_custom_field['main_type'] == iwbvel\classes\repositories\Meta_Field::TEXTINPUT && $iwbvel_custom_field['sub_type'] == iwbvel\classes\repositories\Meta_Field::NUMBER)
                ||
                $iwbvel_custom_field['main_type'] == iwbvel\classes\repositories\Meta_Field::NUMBER
            ) : ?>
                <input type="number" class="iwbvel-input-md" data-field="from" data-field-type="number" id="<?php echo esc_attr($iwbvel_field_id) . '-from'; ?>" title="<?php echo esc_attr($iwbvel_custom_field['title']); ?> <?php esc_attr_e('From', 'ithemeland-bulk-variation-editing-for-woocommerce'); ?>" placeholder="<?php echo esc_attr($iwbvel_custom_field['title']); ?> <?php esc_attr_e('From ...', 'ithemeland-bulk-variation-editing-for-woocommerce'); ?>">
                <input type="number" class="iwbvel-input-md" id="<?php echo esc_attr($iwbvel_field_id) . '-to'; ?>" data-field-type="number" title="<?php echo esc_attr($iwbvel_custom_field['title']); ?> <?php esc_attr_e('To', 'ithemeland-bulk-variation-editing-for-woocommerce'); ?>" data-field="to" placeholder="<?php echo esc_attr($iwbvel_custom_field['title']); ?> <?php esc_attr_e('To ...', 'ithemeland-bulk-variation-editing-for-woocommerce'); ?>">
            <?php elseif ($iwbvel_custom_field['main_type'] == iwbvel\classes\repositories\Meta_Field::CHECKBOX) : ?>
                <select id="<?php echo esc_attr($iwbvel_field_id); ?>" class="iwbvel-input-md" data-type="checkbox" data-field="value" title="<?php esc_attr_e('Select', 'ithemeland-bulk-variation-editing-for-woocommerce'); ?> <?php echo esc_attr($iwbvel_custom_field['title']); ?>">
                    <option value=""><?php esc_html_e('Select', 'ithemeland-bulk-variation-editing-for-woocommerce'); ?></option>
                    <option value="yes"><?php esc_html_e('Yes', 'ithemeland-bulk-variation-editing-for-woocommerce'); ?></option>
                    <option value="no"><?php esc_html_e('No', 'ithemeland-bulk-variation-editing-for-woocommerce'); ?></option>
                </select>
            <?php elseif (
                in_array($iwbvel_custom_field['main_type'], [
                    iwbvel\classes\repositories\Meta_Field::SELECT,
                    iwbvel\classes\repositories\Meta_Field::RADIO,
                    iwbvel\classes\repositories\Meta_Field::ARRAY_TYPE
                ])
            ) : ?>
                <select id="<?php echo esc_attr($iwbvel_field_id); ?>" class="iwbvel-input-md" data-field="value" title="<?php esc_attr_e('Select', 'ithemeland-bulk-variation-editing-for-woocommerce'); ?> <?php echo esc_attr($iwbvel_custom_field['title']); ?>">
                    <option value=""><?php esc_html_e('Select', 'ithemeland-bulk-variation-editing-for-woocommerce'); ?></option>
                    <?php
                    if (!empty($iwbvel_custom_field['key_value'])) :
                        $iwbvel_options = iwbvel\classes\helpers\Meta_Field::key_value_field_to_array($iwbvel_custom_field['key_value']);
                        if (!empty($iwbvel_options) && is_array($iwbvel_options)) :
                            foreach ($iwbvel_options as $iwbvel_option_key => $iwbvel_option_value) :
                    ?>
                                <option value="<?php echo esc_attr($iwbvel_option_key) ?>"><?php echo esc_html($iwbvel_option_value); ?></option>
                    <?php
                            endforeach;
                        endif;
                    endif;
                    ?>
                </select>
            <?php elseif (in_array($iwbvel_custom_field['main_type'], [
                iwbvel\classes\repositories\Meta_Field::CALENDAR,
                iwbvel\classes\repositories\Meta_Field::DATE
            ])) : ?>
                <input type="text" class="iwbvel-input-md iwbvel-datepicker" data-field="from" data-field-type="date" id="<?php echo esc_attr($iwbvel_field_id) . '-from'; ?>" title="<?php echo esc_attr($iwbvel_custom_field['title']); ?> <?php esc_attr_e('From', 'ithemeland-bulk-variation-editing-for-woocommerce'); ?>" placeholder="<?php echo esc_attr($iwbvel_custom_field['title']); ?> <?php esc_attr_e('From ...', 'ithemeland-bulk-variation-editing-for-woocommerce'); ?>">
                <input type="text" class="iwbvel-input-md iwbvel-datepicker" data-field="to" data-field-type="date" id="<?php echo esc_attr($iwbvel_field_id) . '-to'; ?>" title="<?php echo esc_attr($iwbvel_custom_field['title']); ?> <?php esc_attr_e('To', 'ithemeland-bulk-variation-editing-for-woocommerce'); ?>" placeholder="<?php echo esc_attr($iwbvel_custom_field['title']); ?> <?php esc_attr_e('To ...', 'ithemeland-bulk-variation-editing-for-woocommerce'); ?>">
            <?php elseif ($iwbvel_custom_field['main_type'] == iwbvel\classes\repositories\Meta_Field::DATE_TIME) : ?>
                <input type="text" class="iwbvel-input-md iwbvel-datetimepicker" data-field="from" data-field-type="date" id="<?php echo esc_attr($iwbvel_field_id) . '-from'; ?>" title="<?php echo esc_attr($iwbvel_custom_field['title']); ?> <?php esc_attr_e('From', 'ithemeland-bulk-variation-editing-for-woocommerce'); ?>" placeholder="<?php echo esc_attr($iwbvel_custom_field['title']); ?> <?php esc_attr_e('From ...', 'ithemeland-bulk-variation-editing-for-woocommerce'); ?>">
                <input type="text" class="iwbvel-input-md iwbvel-datetimepicker" data-field="to" data-field-type="date" id="<?php echo esc_attr($iwbvel_field_id) . '-to'; ?>" title="<?php echo esc_attr($iwbvel_custom_field['title']); ?> <?php esc_attr_e('To', 'ithemeland-bulk-variation-editing-for-woocommerce'); ?>" placeholder="<?php echo esc_attr($iwbvel_custom_field['title']); ?> <?php esc_attr_e('To ...', 'ithemeland-bulk-variation-editing-for-woocommerce'); ?>">
            <?php elseif ($iwbvel_custom_field['main_type'] == iwbvel\classes\repositories\Meta_Field::TIME) : ?>
                <input type="text" class="iwbvel-input-md iwbvel-timepicker" data-field="from" data-field-type="date" id="<?php echo esc_attr($iwbvel_field_id) . '-from'; ?>" title="<?php echo esc_attr($iwbvel_custom_field['title']); ?> <?php esc_attr_e('From', 'ithemeland-bulk-variation-editing-for-woocommerce'); ?>" placeholder="<?php echo esc_attr($iwbvel_custom_field['title']); ?> <?php esc_attr_e('From ...', 'ithemeland-bulk-variation-editing-for-woocommerce'); ?>">
                <input type="text" class="iwbvel-input-md iwbvel-timepicker" data-field="to" data-field-type="date" id="<?php echo esc_attr($iwbvel_field_id) . '-to'; ?>" title="<?php echo esc_attr($iwbvel_custom_field['title']); ?> <?php esc_attr_e('To', 'ithemeland-bulk-variation-editing-for-woocommerce'); ?>" placeholder="<?php echo esc_attr($iwbvel_custom_field['title']); ?> <?php esc_attr_e('To ...', 'ithemeland-bulk-variation-editing-for-woocommerce'); ?>">
            <?php endif; ?>
        </div>
    <?php endforeach; ?>
<?php else : ?>
    <div class="iwbvel-alert iwbvel-alert-warning">
        <span><?php esc_html_e('There is not any added Meta Fields, You can add new Meta Fields trough "Meta Fields" tab.', 'ithemeland-bulk-variation-editing-for-woocommerce'); ?></span>
    </div>
<?php endif; ?>