<?php
/**
 * Product CSV import — column mapping (zerti markup).
 *
 * @var Zerti_Product_CSV_Importer_Controller $this
 * @var array                               $headers
 * @var array                               $mapped_items
 * @var array                               $sample
 * @var array                               $args
 */

if (!defined('ABSPATH')) {
    exit;
}
?>
<form class="wc-progress-form-content woocommerce-importer zerti-inv-ie-form" method="post" action="<?php echo esc_url($this->get_next_step_link()); ?>">
    <header class="zerti-inv-ie-form__header">
        <h3 class="h6 mb-2"><?php esc_html_e('Map CSV fields to product', 'zerti'); ?></h3>
        <p class="text-body-secondary small mb-0">
            <?php esc_html_e('Map CSV columns to product fields, or leave blank to ignore.', 'zerti'); ?>
        </p>
    </header>
    <section class="wc-importer-mapping-table-wrapper zerti-inv-ie-mapping-scroll">
        <table class="widefat wc-importer-mapping-table table table-sm mb-0 zerti-inv-ie-mapping-table">
            <thead>
                <tr>
                    <th><?php esc_html_e('Column name', 'zerti'); ?></th>
                    <th><?php esc_html_e('Map to field', 'zerti'); ?></th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($headers as $index => $name) : ?>
                    <?php $mapped_value = $mapped_items[$index]; ?>
                    <tr>
                        <td class="wc-importer-mapping-table-name">
                            <?php echo esc_html($name); ?>
                            <?php if (!empty($sample[$index])) : ?>
                                <span class="description d-block small text-body-secondary mt-1">
                                    <?php esc_html_e('Example:', 'zerti'); ?>
                                    <code dir="ltr"><?php echo esc_html((string) $sample[$index]); ?></code>
                                </span>
                            <?php endif; ?>
                        </td>
                        <td class="wc-importer-mapping-table-field">
                            <input type="hidden" name="map_from[<?php echo esc_attr((string) $index); ?>]" value="<?php echo esc_attr($name); ?>">
                            <select class="zerti-admin-select" name="map_to[<?php echo esc_attr((string) $index); ?>]">
                                <option value=""><?php esc_html_e('Do not import', 'zerti'); ?></option>
                                <option value="">--------------</option>
                                <?php foreach ($this->get_mapping_options($mapped_value) as $key => $value) : ?>
                                    <?php if (is_array($value)) : ?>
                                        <optgroup label="<?php echo esc_attr((string) $value['name']); ?>">
                                            <?php foreach ($value['options'] as $sub_key => $sub_value) : ?>
                                                <option value="<?php echo esc_attr((string) $sub_key); ?>" <?php selected($mapped_value, $sub_key); ?>><?php echo esc_html((string) $sub_value); ?></option>
                                            <?php endforeach; ?>
                                        </optgroup>
                                    <?php else : ?>
                                        <option value="<?php echo esc_attr((string) $key); ?>" <?php selected($mapped_value, $key); ?>><?php echo esc_html((string) $value); ?></option>
                                    <?php endif; ?>
                                <?php endforeach; ?>
                            </select>
                        </td>
                    </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </section>
    <div class="wc-actions zerti-inv-ie-actions zerti-inv-ie-actions--import">
        <button type="submit" class="btn btn-sm btn-primary button-next" name="save_step" value="1"><?php esc_html_e('Run import', 'zerti'); ?></button>
        <input type="hidden" name="file" value="<?php echo esc_attr($this->file); ?>">
        <input type="hidden" name="delimiter" value="<?php echo esc_attr($this->delimiter); ?>">
        <input type="hidden" name="update_existing" value="<?php echo (int) $this->update_existing; ?>">
        <?php if (!empty($args['character_encoding'])) : ?>
            <input type="hidden" name="character_encoding" value="<?php echo esc_attr((string) $args['character_encoding']); ?>">
        <?php endif; ?>
        <?php wp_nonce_field('woocommerce-csv-importer'); ?>
    </div>
</form>
