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

if (!empty($histories)) :
    $iwbvel_i = 1;
    foreach ($histories as $iwbvel_history) :
        $iwbvel_user_data = get_userdata(intval($iwbvel_history->user_id));
        $iwbvel_user_id = get_current_user_id(); // Get the current user ID

        // Get the number of undo operations performed by the user
        $iwbvel_user_undo_count = get_user_meta($iwbvel_user_id, 'iwbvel_undo_count', true);
        $iwbvel_user_undo_count = empty($iwbvel_user_undo_count) ? 0 : intval($iwbvel_user_undo_count);
?>
        <tr>
            <td><?php echo esc_html($iwbvel_i); ?></td>
            <td>
                <span class="iwbvel-history-name iwbvel-fw600">
                    <?php
                    switch ($iwbvel_history->operation_type) {
                        case 'inline':
                            $iwbvel_item = (new iwbvel\classes\repositories\history\History_Main())->get_history_items($iwbvel_history->id);
                            echo (!empty($iwbvel_item[0]->post_title)) ? esc_html($iwbvel_item[0]->post_title) : 'Inline Operation';
                            break;
                        case 'bulk':
                            echo 'Bulk Operation';
                            break;
                    }
                    ?>
                </span>
                <?php
                $iwbvel_fields = '';
                if (is_array(unserialize($iwbvel_history->fields)) && !empty(unserialize($iwbvel_history->fields))) {
                    foreach (unserialize($iwbvel_history->fields) as $iwbvel_field) {
                        if (is_array($iwbvel_field)) {
                            foreach ($iwbvel_field as $iwbvel_field_item) {
                                $iwbvel_field_arr = explode('_-_', $iwbvel_field_item);
                                if (!empty($iwbvel_field_arr[0]) && !empty($iwbvel_field_arr[1])) {
                                    $iwbvel_field_item = esc_html($iwbvel_field_arr[1]);
                                }

                                $iwbvel_fields .= "[" . esc_html($iwbvel_field_item) . "]";
                            }
                        } else {
                            $iwbvel_field_arr = explode('_-_', $iwbvel_field);
                            if (!empty($iwbvel_field_arr[0]) && !empty($iwbvel_field_arr[1])) {
                                $iwbvel_field = esc_html($iwbvel_field_arr[1]);
                            }

                            $iwbvel_fields .= "[" . esc_html($iwbvel_field) . "]";
                        }
                    }
                }
                ?>
                <span class="iwbvel-history-text-sm"><?php echo esc_html($iwbvel_fields); ?></span>
            </td>
            <td class="iwbvel-fw600"><?php echo (!empty($iwbvel_user_data)) ? esc_html($iwbvel_user_data->display_name) : ''; ?></td>
            <td class="iwbvel-fw600"><?php echo esc_html(gmdate('Y / m / d', strtotime($iwbvel_history->operation_date))); ?></td>
            <td>
                <button type="button" class="iwbvel-button iwbvel-button-blue iwbvel-history-revert-item"
                    value="<?php echo esc_attr($iwbvel_history->id); ?>"
                    <?php echo !defined('IWBVE_ACTIVE') || !IWBVE_ACTIVE ? 'disabled="disabled"' : ''; ?>>
                    <i class="iwbvel-icon-rotate-cw"></i>
                    <?php esc_html_e('Revert', 'ithemeland-bulk-variation-editing-for-woocommerce'); ?>
                </button>
                <button type="button" class="iwbvel-button iwbvel-button-red iwbvel-history-delete-item"
                    value="<?php echo esc_attr($iwbvel_history->id); ?>"
                    <?php echo !defined('IWBVE_ACTIVE') || !IWBVE_ACTIVE ? 'disabled="disabled"' : ''; ?>>
                    <i class="iwbvel-icon-trash-2"></i>
                    <?php esc_html_e('Delete', 'ithemeland-bulk-variation-editing-for-woocommerce'); ?>
                </button>
            </td>
        </tr>
<?php
        $iwbvel_i++;
    endforeach;
endif;
