<?php
/**
 * Product CSV import — upload step (zerti markup, WC field names).
 *
 * @var int    $bytes
 * @var string $size
 * @var array  $upload_dir
 */

if (!defined('ABSPATH')) {
    exit;
}
?>
<form class="wc-progress-form-content woocommerce-importer zerti-inv-ie-form" enctype="multipart/form-data" method="post" action="<?php echo esc_url(zerti_product_import_export_url(['zerti_ie' => 'import', 'step' => 'upload'])); ?>">
    <header class="zerti-inv-ie-form__header">
        <h3 class="h6 mb-2"><?php esc_html_e('Import products from CSV file', 'zerti'); ?></h3>
        <p class="text-body-secondary small mb-0">
            <?php esc_html_e('With this tool you can import product data from a CSV or TXT file (or merge with existing products).', 'zerti'); ?>
        </p>
    </header>
    <section class="zerti-inv-ie-form__section">
        <table class="form-table woocommerce-importer-options zerti-inv-ie-options">
            <tbody>
                <tr>
                    <th scope="row">
                        <label for="upload"><?php esc_html_e('Select CSV file from computer:', 'zerti'); ?></label>
                    </th>
                    <td>
                        <?php if (!empty($upload_dir['error'])) : ?>
                            <div class="alert alert-danger py-2 small mb-0">
                                <p class="mb-1"><?php esc_html_e('Fix the following error before uploading:', 'zerti'); ?></p>
                                <p class="mb-0"><strong><?php echo esc_html((string) $upload_dir['error']); ?></strong></p>
                            </div>
                        <?php else : ?>
                            <div class="zerti-file-upload d-flex flex-wrap align-items-center gap-2">
                                <input
                                    type="file"
                                    id="upload"
                                    name="import"
                                    class="visually-hidden zerti-file-upload__input"
                                    accept=".csv,.txt,text/csv,text/plain"
                                    data-zerti-file-empty="<?php echo esc_attr__('No file selected', 'zerti'); ?>"
                                >
                                <label class="btn btn-sm btn-outline-secondary zerti-file-upload__button mb-0" for="upload">
                                    <?php esc_html_e('Select file', 'zerti'); ?>
                                </label>
                                <span class="zerti-file-upload__name text-body-secondary small" data-zerti-file-name>
                                    <?php esc_html_e('No file selected', 'zerti'); ?>
                                </span>
                            </div>
                            <input type="hidden" name="action" value="save">
                            <input type="hidden" name="max_file_size" value="<?php echo esc_attr((string) $bytes); ?>">
                            <p class="small text-body-secondary mt-2 mb-0">
                                <?php
                                printf(
                                    /* translators: %s: maximum upload size */
                                    esc_html__('Maximum size: %s', 'zerti'),
                                    esc_html((string) $size)
                                );
                                ?>
                            </p>
                        <?php endif; ?>
                    </td>
                </tr>
                <tr>
                    <th scope="row">
                        <label for="woocommerce-importer-update-existing"><?php esc_html_e('Update existing products', 'zerti'); ?></label>
                    </th>
                    <td>
                        <div class="form-check">
                            <input type="hidden" name="update_existing" value="0">
                            <input class="form-check-input" type="checkbox" id="woocommerce-importer-update-existing" name="update_existing" value="1">
                            <label class="form-check-label small" for="woocommerce-importer-update-existing">
                                <?php esc_html_e('Products matching ID or SKU will be updated; non-matching rows are ignored.', 'zerti'); ?>
                            </label>
                        </div>
                    </td>
                </tr>
                <tr class="woocommerce-importer-advanced hidden">
                    <th scope="row">
                        <label for="woocommerce-importer-file-url"><?php esc_html_e('Or CSV file path on server:', 'zerti'); ?></label>
                    </th>
                    <td>
                        <label for="woocommerce-importer-file-url" class="woocommerce-importer-file-url-field-wrapper d-block">
                            <code class="user-select-all"><?php echo esc_html(ABSPATH); ?></code>
                            <input type="text" class="form-control form-control-sm mt-1" id="woocommerce-importer-file-url" name="file_url" dir="ltr">
                        </label>
                    </td>
                </tr>
                <tr class="woocommerce-importer-advanced hidden">
                    <th scope="row"><label><?php esc_html_e('CSV delimiter', 'zerti'); ?></label></th>
                    <td><input type="text" class="form-control form-control-sm" name="delimiter" placeholder="," size="2" style="max-width:4rem"></td>
                </tr>
                <tr class="woocommerce-importer-advanced hidden">
                    <th scope="row"><label><?php esc_html_e('Use previous mapping?', 'zerti'); ?></label></th>
                    <td>
                        <div class="form-check">
                            <input class="form-check-input" type="checkbox" id="woocommerce-importer-map-preferences" name="map_preferences" value="1">
                        </div>
                    </td>
                </tr>
                <tr class="woocommerce-importer-advanced hidden">
                    <th scope="row"><label for="woocommerce-importer-character-encoding"><?php esc_html_e('File encryption', 'zerti'); ?></label></th>
                    <td>
                        <select class="zerti-admin-select" id="woocommerce-importer-character-encoding" name="character_encoding">
                            <option value="" selected><?php esc_html_e('Auto-detect', 'zerti'); ?></option>
                            <?php
                            $encodings = mb_list_encodings();
                            sort($encodings, SORT_NATURAL);
                            foreach ($encodings as $encoding) {
                                echo '<option>' . esc_html($encoding) . '</option>';
                            }
                            ?>
                        </select>
                    </td>
                </tr>
            </tbody>
        </table>
    </section>
    <div class="wc-actions zerti-inv-ie-actions zerti-inv-ie-actions--import">
        <a href="#" class="woocommerce-importer-toggle-advanced-options small" data-hidetext="<?php esc_attr_e('Hide advanced options', 'zerti'); ?>" data-showtext="<?php esc_attr_e('Show advanced options', 'zerti'); ?>"><?php esc_html_e('Show advanced options', 'zerti'); ?></a>
        <button type="submit" class="btn btn-sm btn-primary button-next" name="save_step" value="1"><?php esc_html_e('Continue', 'zerti'); ?></button>
        <?php wp_nonce_field('woocommerce-csv-importer'); ?>
    </div>
</form>
