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

if (!empty($meta_fields)) :
    foreach ($meta_fields as $iwbvel_custom_field) :
        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', 'image', 'file'])) {
                    continue;
                }
            } else {
                $iwbvel_custom_field['main_type'] = $iwbvel_custom_field['main_type'];
            }
        }

        if (!in_array($iwbvel_custom_field['main_type'], [
            iwbvel\classes\repositories\Meta_Field::TEXTINPUT,
            iwbvel\classes\repositories\Meta_Field::TEXT,
            iwbvel\classes\repositories\Meta_Field::NUMBER,
            iwbvel\classes\repositories\Meta_Field::TEXTAREA,
            iwbvel\classes\repositories\Meta_Field::PASSWORD,
            iwbvel\classes\repositories\Meta_Field::EDITOR,
            iwbvel\classes\repositories\Meta_Field::URL,
            iwbvel\classes\repositories\Meta_Field::EMAIL,
            iwbvel\classes\repositories\Meta_Field::CHECKBOX,
            iwbvel\classes\repositories\Meta_Field::SELECT,
            iwbvel\classes\repositories\Meta_Field::RADIO,
            iwbvel\classes\repositories\Meta_Field::ARRAY_TYPE,
            iwbvel\classes\repositories\Meta_Field::CALENDAR,
            iwbvel\classes\repositories\Meta_Field::DATE_TIME,
            iwbvel\classes\repositories\Meta_Field::TIME,
            iwbvel\classes\repositories\Meta_Field::IMAGE,
            iwbvel\classes\repositories\Meta_Field::FILE,
        ])) {
            continue;
        }

        $iwbvel_field_id = "iwbvel-bulk-edit-form-custom-field-" . esc_attr($iwbvel_custom_field['key']);
?>
        <div class="iwbvel-form-group" data-type="meta_field" data-name="<?php echo esc_attr($iwbvel_custom_field['key']); ?>">
            <div>
                <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::EMAIL,
                        iwbvel\classes\repositories\Meta_Field::TEXT,
                        iwbvel\classes\repositories\Meta_Field::PASSWORD,
                        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/bulk_edit_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 include IWBVEL_VIEWS_DIR . "bulk_edit/bulk_edit_form/variable.php"; ?>
                <?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
                ) : ?>
                    <select title="<?php esc_attr_e('Select Operator', 'ithemeland-bulk-variation-editing-for-woocommerce'); ?>" data-field="operator">
                        <?php include IWBVEL_VIEWS_DIR . "bulk_edit/bulk_edit_form/operators/number.php"; ?>
                    </select>
                    <input type="number" class="iwbvel-input-md" data-field="value" id="<?php echo esc_attr($iwbvel_field_id); ?>" title="<?php echo esc_attr($iwbvel_custom_field['title']); ?>" placeholder="<?php echo esc_attr($iwbvel_custom_field['title']); ?> ...">
                <?php elseif ($iwbvel_custom_field['main_type'] == iwbvel\classes\repositories\Meta_Field::TEXTAREA) : ?>
                    <select title="<?php esc_attr_e('Select Operator', 'ithemeland-bulk-variation-editing-for-woocommerce'); ?>" data-field="operator">
                        <?php include IWBVEL_VIEWS_DIR . "bulk_edit/bulk_edit_form/operators/text.php"; ?>
                    </select>
                    <textarea data-field="value" id="<?php echo esc_attr($iwbvel_field_id); ?>" placeholder="<?php echo esc_attr($iwbvel_custom_field['title']); ?> ..."></textarea>
                <?php elseif ($iwbvel_custom_field['main_type'] == iwbvel\classes\repositories\Meta_Field::CHECKBOX) : ?>
                    <select id="<?php echo esc_attr($iwbvel_field_id); ?>" data-field="value" class="iwbvel-input-md" 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="value" id="<?php echo esc_attr($iwbvel_field_id); ?>" title="<?php echo esc_attr($iwbvel_custom_field['title']); ?>" placeholder="<?php echo esc_attr($iwbvel_custom_field['title']); ?> ...">
                <?php elseif (in_array($iwbvel_custom_field['main_type'], [
                    iwbvel\classes\repositories\Meta_Field::DATE_TIME
                ])) : ?>
                    <input type="text" class="iwbvel-input-md iwbvel-datepicker" data-field="value" id="<?php echo esc_attr($iwbvel_field_id); ?>" title="<?php echo esc_attr($iwbvel_custom_field['title']); ?>" placeholder="<?php echo esc_attr($iwbvel_custom_field['title']); ?> ...">
                <?php elseif (in_array($iwbvel_custom_field['main_type'], [
                    iwbvel\classes\repositories\Meta_Field::TIME
                ])) : ?>
                    <input type="text" class="iwbvel-input-md iwbvel-datepicker" data-field="value" id="<?php echo esc_attr($iwbvel_field_id); ?>" title="<?php echo esc_attr($iwbvel_custom_field['title']); ?>" placeholder="<?php echo esc_attr($iwbvel_custom_field['title']); ?> ...">
                <?php elseif (in_array($iwbvel_custom_field['main_type'], [
                    iwbvel\classes\repositories\Meta_Field::IMAGE
                ])) : ?>
                    <button type="button" data-type="single" class="iwbvel-button iwbvel-button-blue iwbvel-float-left iwbvel-open-uploader" data-target="#iwbvel-bulk-edit-form-custom-field-<?php echo esc_attr($iwbvel_custom_field['key']); ?>">
                        <?php esc_html_e('Choose image', 'ithemeland-bulk-variation-editing-for-woocommerce') ?>
                    </button>
                    <input type="hidden" data-field="value" class="iwbvel-bulk-edit-form-custom-field-<?php echo esc_attr($iwbvel_custom_field['key']); ?>" id="iwbvel-bulk-edit-form-custom-field-<?php echo esc_attr($iwbvel_custom_field['key']); ?>">
                    <div id="iwbvel-bulk-edit-form-custom-field-<?php echo esc_attr($iwbvel_custom_field['key']); ?>-preview" class="iwbvel-bulk-edit-form-item-image-preview"></div>
                <?php elseif (in_array($iwbvel_custom_field['main_type'], [
                    iwbvel\classes\repositories\Meta_Field::FILE
                ])) :
                    include IWBVEL_VIEWS_DIR . 'bulk_edit/custom_field_files.php';
                endif; ?>
            </div>
        </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; ?>