<?php
/**
 * Product CSV import — completed step.
 *
 * @var int         $imported
 * @var int         $imported_variations
 * @var int         $updated
 * @var int         $failed
 * @var int         $skipped
 * @var string      $file_name
 * @var list<mixed> $errors
 */

if (!defined('ABSPATH')) {
    exit;
}
?>
<div class="wc-progress-form-content woocommerce-importer zerti-inv-ie-form">
    <section class="woocommerce-importer-done">
        <?php
        $results = [];

        if ($imported > 0) {
            $results[] = sprintf(
                /* translators: %s: count */
                _n('%s محصول وارد شد', '%s محصول وارد شد', $imported, 'zerti'),
                '<strong>' . esc_html(zerti_to_persian_digits((string) number_format_i18n($imported))) . '</strong>'
            );
        }

        if ($updated > 0) {
            $results[] = sprintf(
                /* translators: %s: formatted product count */
                _n('%s محصول به‌روز شد', '%s محصول به‌روز شد', $updated, 'zerti'),
                '<strong>' . esc_html(zerti_to_persian_digits((string) number_format_i18n($updated))) . '</strong>'
            );
        }

        if ($imported_variations > 0) {
            $results[] = sprintf(
                /* translators: %s: formatted variation count */
                _n('%s تنوع وارد شد', '%s تنوع وارد شد', $imported_variations, 'zerti'),
                '<strong>' . esc_html(zerti_to_persian_digits((string) number_format_i18n($imported_variations))) . '</strong>'
            );
        }

        if ($skipped > 0) {
            $results[] = sprintf(
                /* translators: %s: formatted skipped product count */
                _n('%s محصول نادیده گرفته شد', '%s محصول نادیده گرفته شد', $skipped, 'zerti'),
                '<strong>' . esc_html(zerti_to_persian_digits((string) number_format_i18n($skipped))) . '</strong>'
            );
        }

        if ($failed > 0) {
            $results[] = sprintf(
                /* translators: %s: formatted failed product count */
                _n('ورود داده %s محصول ناموفق بود', 'ورود داده %s محصول ناموفق بود', $failed, 'zerti'),
                '<strong>' . esc_html(zerti_to_persian_digits((string) number_format_i18n($failed))) . '</strong>'
            );
        }

        if ($failed > 0 || $skipped > 0) {
            $results[] = '<a href="#" class="woocommerce-importer-done-view-errors">' . esc_html__('View import report', 'zerti') . '</a>';
        }

        if ($file_name !== '') {
            $results[] = sprintf(
                /* translators: %s: file name */
                __('Uploaded file: %s', 'zerti'),
                '<strong>' . esc_html($file_name) . '</strong>'
            );
        }

        echo '<p class="mb-0">';
        echo wp_kses_post(__('Import completed!', 'zerti') . ' ' . implode('. ', $results));
        echo '</p>';
        ?>
    </section>
    <section class="wc-importer-error-log mt-3" style="display:none">
        <div class="table-responsive zerti-inv-ie-error-scroll">
            <table class="widefat wc-importer-error-log-table table table-sm table-bordered mb-0">
                <thead>
                    <tr>
                        <th><?php esc_html_e('Product', 'zerti'); ?></th>
                        <th><?php esc_html_e('Error reason', 'zerti'); ?></th>
                    </tr>
                </thead>
                <tbody>
                    <?php
                    if (is_array($errors) && count($errors)) {
                        foreach ($errors as $error) {
                            if (!is_wp_error($error)) {
                                continue;
                            }
                            $error_data = $error->get_error_data();
                            ?>
                            <tr>
                                <th scope="row"><code><?php echo esc_html((string) ($error_data['row'] ?? '')); ?></code></th>
                                <td><?php echo wp_kses_post($error->get_error_message()); ?></td>
                            </tr>
                            <?php
                        }
                    }
                    ?>
                </tbody>
            </table>
        </div>
    </section>
    <div class="wc-actions zerti-inv-ie-actions zerti-inv-ie-actions--import">
        <a class="btn btn-sm btn-primary" href="<?php echo esc_url(zerti_admin_route_url(['tab' => 'inventory', 'inv' => 'warehouse'])); ?>">
            <?php esc_html_e('View products', 'zerti'); ?>
        </a>
        <a class="btn btn-sm btn-outline-secondary" href="<?php echo esc_url(zerti_product_import_export_url(['zerti_ie' => 'import', 'step' => 'upload'])); ?>">
            <?php esc_html_e('New import', 'zerti'); ?>
        </a>
    </div>
</div>
