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

<div class="iwbvel-schedule-job-edit-items-section">
    <table class="iwbvel-modal-schedule-job-edit-items-table">
        <thead>
            <tr>
                <th><?php esc_html_e('Column Name', 'ithemeland-bulk-variation-editing-for-woocommerce'); ?></th>
                <th><?php esc_html_e('Operator', 'ithemeland-bulk-variation-editing-for-woocommerce'); ?></th>
                <th><?php esc_html_e('Value', 'ithemeland-bulk-variation-editing-for-woocommerce'); ?></th>
            </tr>
        </thead>
        <tbody>
            <?php
            if (!empty($job->edit_items)):
                foreach ($job->edit_items as $iwbvel_item):
                    if (!isset($iwbvel_item['name']) || !isset($iwbvel_item['value'])) {
                        continue;
                    }

                    $iwbvel_name = (!empty($edit_columns[$iwbvel_item['name']]) && !empty($edit_columns[$iwbvel_item['name']]['label'])) ? $edit_columns[$iwbvel_item['name']]['label'] : $iwbvel_item['name'];
                    $iwbvel_value = '';
                    if (is_array($iwbvel_item['value'])) {
                        if (isset($iwbvel_item['type']) && $iwbvel_item['type'] == 'taxonomy') {
                            $iwbvel_name = (!empty($taxonomies[$iwbvel_item['name']]) && !empty($taxonomies[$iwbvel_item['name']]['label'])) ? $taxonomies[$iwbvel_item['name']]['label'] : $iwbvel_item['name'];
                            foreach ($iwbvel_item['value'] as $iwbvel_term_id) {
                                $iwbvel_term = get_term_by('term_id', intval($iwbvel_term_id), $iwbvel_item['name']);
                                if (!($iwbvel_term instanceof \WP_Term)) {
                                    continue;
                                }
                                if (!empty($iwbvel_value)) {
                                    $iwbvel_value .= ', ';
                                }
                                $iwbvel_value .= $iwbvel_term->name;
                            }
                        } else {
                            if (isset($iwbvel_item['value']['from']) && isset($iwbvel_item['value']['to'])) {
                                $iwbvel_value = 'From: ' . $iwbvel_item['value']['from'] . ' | To: ' . $iwbvel_item['value']['to'];
                            } else {
                                $iwbvel_value = implode(', ', $iwbvel_item['value']);
                            }
                        }
                    } else {
                        $iwbvel_value = $iwbvel_item['value'];
                    }
            ?>
                    <tr>
                        <td><?php echo esc_html($iwbvel_name); ?></td>
                        <?php if (!empty($iwbvel_item['operator'])): ?>
                            <td><?php echo (!empty($operators[$iwbvel_item['operator']])) ? esc_html($operators[$iwbvel_item['operator']]) : esc_html($iwbvel_item['operator']); ?></td>
                        <?php else: ?>
                            <td> </td>
                        <?php endif; ?>
                        <td><?php echo esc_html($iwbvel_value); ?></td>
                    </tr>
            <?php
                endforeach;
            endif;
            ?>
        </tbody>
    </table>
</div>

<div class="iwbvel-schedule-job-edit-items-section">
    <?php if (isset($job->filter_items['product_ids'])): ?>
        <h3><?php esc_html_e('Selected Products', 'ithemeland-bulk-variation-editing-for-woocommerce'); ?></h3>
        <?php
        foreach ($job->filter_items['product_ids'] as $iwbvel_product_id) {
            $iwbvel_product = wc_get_product(intval($iwbvel_product_id));
            if (!($iwbvel_product instanceof \WC_Product)) {
                continue;
            }
            echo '<div class="iwbvel-schedule-job-edit-items-selected-product-item">#' . esc_html($iwbvel_product->get_id()) . ' - ' . esc_html($iwbvel_product->get_title()) . '</div>';
        }
        ?>
    <?php else: ?>
        <h3><?php esc_html_e('Filter Items', 'ithemeland-bulk-variation-editing-for-woocommerce'); ?></h3>
        <?php if (empty($job->filter_items['fields'])): ?>
            <span style="font-size: 14px"><?php esc_html_e('All Products', 'ithemeland-bulk-variation-editing-for-woocommerce'); ?></span>
        <?php else: ?>
            <table class="iwbvel-modal-schedule-job-edit-items-table">
                <thead>
                    <tr>
                        <th><?php esc_html_e('Filter Name', 'ithemeland-bulk-variation-editing-for-woocommerce'); ?></th>
                        <th><?php esc_html_e('Operator', 'ithemeland-bulk-variation-editing-for-woocommerce'); ?></th>
                        <th><?php esc_html_e('Value', 'ithemeland-bulk-variation-editing-for-woocommerce'); ?></th>
                    </tr>
                </thead>
                <tbody>
                    <?php
                    foreach ($job->filter_items['fields'] as $iwbvel_filter_item) :
                        if (!isset($iwbvel_filter_item['name']) || !isset($iwbvel_filter_item['value'])) {
                            continue;
                        }

                        $iwbvel_value = '';
                        if (is_array($iwbvel_filter_item['value'])) {
                            if (isset($iwbvel_filter_item['value']['from']) && isset($iwbvel_filter_item['value']['to'])) {
                                $iwbvel_value = 'From: ' . $iwbvel_filter_item['value']['from'] . ' | To: ' . $iwbvel_filter_item['value']['to'];
                            } else {
                                $iwbvel_value = implode(', ', $iwbvel_filter_item['value']);
                            }
                        } else {
                            $iwbvel_value = $iwbvel_filter_item['value'];
                        }

                        $iwbvel_name = (!empty($filter_columns[$iwbvel_filter_item['name']]) && !empty($filter_columns[$iwbvel_filter_item['name']]['label'])) ? $filter_columns[$iwbvel_filter_item['name']]['label'] : $iwbvel_filter_item['name'];
                    ?>
                        <tr>
                            <td><?php echo esc_html($iwbvel_name); ?></td>
                            <?php if (!empty($iwbvel_filter_item['operator'])): ?>
                                <td><?php echo (!empty($operators[$iwbvel_filter_item['operator']])) ? esc_html($operators[$iwbvel_filter_item['operator']]) : esc_html($iwbvel_filter_item['operator']); ?></td>
                            <?php else: ?>
                                <td> </td>
                            <?php endif; ?>
                            <td><?php echo esc_html($iwbvel_value); ?></td>
                        </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        <?php endif; ?>
    <?php endif; ?>
</div>