<?php
/**
 * Product CSV export form (WooCommerce options, zerti layout).
 *
 * @var WC_Product_CSV_Exporter $exporter
 * @var list<int>               $product_ids_to_export
 * @var bool                    $is_exporting_product_ids
 */

if (!defined('ABSPATH')) {
    exit;
}
?>
<div class="woocommerce-exporter-wrapper zerti-inv-ie-export">
    <form class="woocommerce-exporter zerti-inv-ie-form">
        <?php if ($is_exporting_product_ids) : ?>
            <?php
            $clear_url = remove_query_arg('product_ids');
            $count = count($product_ids_to_export);
            ?>
            <div class="alert alert-info py-2 small" role="status">
                <?php
                echo wp_kses_post(
                    sprintf(
                        /* translators: 1: product count, 2: clear selection URL */
                        _n(
                            'در حال خروج داده %1$d محصول هستید. برای خروج داده همه، <a href="%2$s">انتخاب را پاک کنید</a>.',
                            'در حال خروج داده %1$d محصول هستید. برای خروج داده همه، <a href="%2$s">انتخاب را پاک کنید</a>.',
                            $count,
                            'zerti'
                        ),
                        $count,
                        esc_url($clear_url)
                    )
                );
                ?>
            </div>
            <input type="hidden" name="product_ids" value="<?php echo esc_attr(implode(',', $product_ids_to_export)); ?>">
        <?php endif; ?>

        <header class="zerti-inv-ie-form__header">
            <span class="spinner-border spinner-border-sm text-primary zerti-inv-ie-export-spinner d-none" role="status" aria-hidden="true"></span>
            <h3 class="h6 mb-2"><?php esc_html_e('Export products to CSV file', 'zerti'); ?></h3>
            <p class="text-body-secondary small mb-0">
                <?php
                if ($is_exporting_product_ids) {
                    esc_html_e('A CSV file containing selected products will be generated and downloaded.', 'zerti');
                } else {
                    esc_html_e('A CSV file containing the products list (with filters below) will be generated and downloaded.', 'zerti');
                }
                ?>
            </p>
        </header>

        <section class="zerti-inv-ie-form__section">
            <table class="form-table woocommerce-exporter-options zerti-inv-ie-options">
                <tbody>
                    <tr>
                        <th scope="row">
                            <span id="woocommerce-exporter-columns-label"><?php esc_html_e('Which columns should be included in export?', 'zerti'); ?></span>
                        </th>
                        <td>
                            <?php
                            $export_column_options = $exporter->get_default_column_names();
                            $export_column_options['downloads'] = __('Downloads', 'zerti');
                            $export_column_options['attributes'] = __('Attributes', 'zerti');
                            ?>
                            <div
                                id="zerti-exporter-columns-picker"
                                class="zerti-inv-ie-checkbox-picker"
                                data-sync-select=".woocommerce-exporter-columns"
                                role="group"
                                aria-labelledby="woocommerce-exporter-columns-label"
                            >
                                <p class="zerti-inv-ie-checkbox-picker__hint text-body-secondary small mb-2">
                                    <?php esc_html_e('If none selected = all columns in CSV. Check items to limit export.', 'zerti'); ?>
                                </p>
                                <div class="zerti-inv-ie-checkbox-picker__toolbar" role="toolbar" aria-label="<?php esc_attr_e('Select columns', 'zerti'); ?>">
                                    <button type="button" class="btn btn-link btn-sm p-0 zerti-inv-ie-checkbox-picker__select-all">
                                        <?php esc_html_e('Select all', 'zerti'); ?>
                                    </button>
                                    <span class="zerti-inv-ie-checkbox-picker__toolbar-sep" aria-hidden="true">·</span>
                                    <button type="button" class="btn btn-link btn-sm p-0 zerti-inv-ie-checkbox-picker__clear-all">
                                        <?php esc_html_e('Clear selection', 'zerti'); ?>
                                    </button>
                                </div>
                                <div class="zerti-inv-ie-checkbox-picker__list">
                                    <?php foreach ($export_column_options as $column_id => $column_name) : ?>
                                        <label class="form-check zerti-inv-ie-checkbox-picker__item">
                                            <input
                                                type="checkbox"
                                                class="form-check-input zerti-inv-ie-checkbox-picker__check"
                                                value="<?php echo esc_attr((string) $column_id); ?>"
                                            >
                                            <span class="form-check-label"><?php echo esc_html((string) $column_name); ?></span>
                                        </label>
                                    <?php endforeach; ?>
                                </div>
                            </div>
                            <select
                                class="woocommerce-exporter-columns zerti-inv-ie-picker-sync"
                                multiple
                                tabindex="-1"
                                aria-hidden="true"
                            >
                                <?php foreach ($export_column_options as $column_id => $column_name) : ?>
                                    <option value="<?php echo esc_attr((string) $column_id); ?>"><?php echo esc_html((string) $column_name); ?></option>
                                <?php endforeach; ?>
                            </select>
                        </td>
                    </tr>
                    <?php if (!$is_exporting_product_ids) : ?>
                        <?php
                        $export_product_types = WC_Admin_Exporters::get_product_types();
                        $export_categories = get_categories([
                            'taxonomy' => 'product_cat',
                            'hide_empty' => false,
                        ]);
                        ?>
                        <tr class="zerti-inv-ie-export-types-row">
                            <th scope="row">
                                <span id="woocommerce-exporter-types-label"><?php esc_html_e('Product type', 'zerti'); ?></span>
                            </th>
                            <td>
                                <div
                                    id="zerti-exporter-types-picker"
                                    class="zerti-inv-ie-checkbox-picker"
                                    data-sync-select=".woocommerce-exporter-types"
                                    role="group"
                                    aria-labelledby="woocommerce-exporter-types-label"
                                >
                                    <p class="zerti-inv-ie-checkbox-picker__hint text-body-secondary small mb-2">
                                        <?php esc_html_e('If none selected = all product types. Check items to limit export.', 'zerti'); ?>
                                    </p>
                                    <div class="zerti-inv-ie-checkbox-picker__toolbar" role="toolbar" aria-label="<?php esc_attr_e('Select product type', 'zerti'); ?>">
                                        <button type="button" class="btn btn-link btn-sm p-0 zerti-inv-ie-checkbox-picker__select-all">
                                            <?php esc_html_e('Select all', 'zerti'); ?>
                                        </button>
                                        <span class="zerti-inv-ie-checkbox-picker__toolbar-sep" aria-hidden="true">·</span>
                                        <button type="button" class="btn btn-link btn-sm p-0 zerti-inv-ie-checkbox-picker__clear-all">
                                            <?php esc_html_e('Clear selection', 'zerti'); ?>
                                        </button>
                                    </div>
                                    <div class="zerti-inv-ie-checkbox-picker__list">
                                        <?php foreach ($export_product_types as $type_value => $type_label) : ?>
                                            <label class="form-check zerti-inv-ie-checkbox-picker__item">
                                                <input
                                                    type="checkbox"
                                                    class="form-check-input zerti-inv-ie-checkbox-picker__check"
                                                    value="<?php echo esc_attr((string) $type_value); ?>"
                                                >
                                                <span class="form-check-label"><?php echo esc_html((string) $type_label); ?></span>
                                            </label>
                                        <?php endforeach; ?>
                                    </div>
                                </div>
                                <select
                                    class="woocommerce-exporter-types zerti-inv-ie-picker-sync"
                                    multiple
                                    tabindex="-1"
                                    aria-hidden="true"
                                >
                                    <?php foreach ($export_product_types as $type_value => $type_label) : ?>
                                        <option value="<?php echo esc_attr((string) $type_value); ?>"><?php echo esc_html((string) $type_label); ?></option>
                                    <?php endforeach; ?>
                                </select>
                            </td>
                        </tr>
                        <tr class="zerti-inv-ie-export-category-row">
                            <th scope="row">
                                <span id="woocommerce-exporter-category-label"><?php esc_html_e('Product category', 'zerti'); ?></span>
                            </th>
                            <td>
                                <div
                                    id="zerti-exporter-category-picker"
                                    class="zerti-inv-ie-checkbox-picker zerti-inv-ie-checkbox-picker--scroll"
                                    data-sync-select=".woocommerce-exporter-category"
                                    role="group"
                                    aria-labelledby="woocommerce-exporter-category-label"
                                >
                                    <p class="zerti-inv-ie-checkbox-picker__hint text-body-secondary small mb-2">
                                        <?php esc_html_e('If none selected = all categories. Check items to limit export.', 'zerti'); ?>
                                    </p>
                                    <div class="zerti-inv-ie-checkbox-picker__toolbar" role="toolbar" aria-label="<?php esc_attr_e('Select product category', 'zerti'); ?>">
                                        <button type="button" class="btn btn-link btn-sm p-0 zerti-inv-ie-checkbox-picker__select-all">
                                            <?php esc_html_e('Select all', 'zerti'); ?>
                                        </button>
                                        <span class="zerti-inv-ie-checkbox-picker__toolbar-sep" aria-hidden="true">·</span>
                                        <button type="button" class="btn btn-link btn-sm p-0 zerti-inv-ie-checkbox-picker__clear-all">
                                            <?php esc_html_e('Clear selection', 'zerti'); ?>
                                        </button>
                                    </div>
                                    <div class="zerti-inv-ie-checkbox-picker__list">
                                        <?php foreach ($export_categories as $category) : ?>
                                            <label class="form-check zerti-inv-ie-checkbox-picker__item">
                                                <input
                                                    type="checkbox"
                                                    class="form-check-input zerti-inv-ie-checkbox-picker__check"
                                                    value="<?php echo esc_attr($category->slug); ?>"
                                                >
                                                <span class="form-check-label"><?php echo esc_html($category->name); ?></span>
                                            </label>
                                        <?php endforeach; ?>
                                    </div>
                                </div>
                                <select
                                    class="woocommerce-exporter-category zerti-inv-ie-picker-sync"
                                    multiple
                                    tabindex="-1"
                                    aria-hidden="true"
                                >
                                    <?php foreach ($export_categories as $category) : ?>
                                        <option value="<?php echo esc_attr($category->slug); ?>"><?php echo esc_html($category->name); ?></option>
                                    <?php endforeach; ?>
                                </select>
                            </td>
                        </tr>
                    <?php endif; ?>
                    <tr>
                        <th scope="row">
                            <label for="woocommerce-exporter-meta"><?php esc_html_e('Custom meta', 'zerti'); ?></label>
                        </th>
                        <td>
                            <div class="form-check">
                                <input class="form-check-input" type="checkbox" id="woocommerce-exporter-meta" value="1">
                                <label class="form-check-label small" for="woocommerce-exporter-meta"><?php esc_html_e('Yes, include all custom meta in export', 'zerti'); ?></label>
                            </div>
                        </td>
                    </tr>
                    <?php do_action('woocommerce_product_export_row'); ?>
                </tbody>
            </table>
            <progress class="woocommerce-exporter-progress w-100 mt-2" max="100" value="0"></progress>
        </section>

        <div class="wc-actions zerti-inv-ie-actions zerti-inv-ie-actions--export">
            <button type="submit" class="btn btn-sm btn-primary woocommerce-exporter-button" value="<?php esc_attr_e('Generate CSV', 'zerti'); ?>">
                <?php esc_html_e('Generate CSV', 'zerti'); ?>
            </button>
        </div>
    </form>
</div>
