<?php
/**
 * Export Wizard - Step 3 (Export Options) - Categories.
 *
 * @package PrimeLink
 */

if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

$export_id = isset( $_GET['export_id'] ) ? absint( $_GET['export_id'] ) : 0;
?>
<div class="mt_primelink-summary-eyebrow"><?php esc_html_e( 'Step 3 · Options', 'primelink' ); ?></div>
<h2><?php esc_html_e( 'Export Options', 'primelink' ); ?></h2>
<input type="hidden" id="mt-primelink-step" value="3">

<div class="mt_primelink-step mt_primelink-export-step-3" data-export-id="<?php echo esc_attr( $export_id ); ?>">

    <!-- Export Format -->
    <section class="mt_primelink-card mt_primelink-card--glass">
        <div class="mt_primelink-card-header">
            <div>
                <h3><?php esc_html_e( 'Export Format', 'primelink' ); ?></h3>
                <p class="description" style="margin:4px 0 0;">
                    <?php esc_html_e( 'Choose the file format for your export and configure format-specific options.', 'primelink' ); ?>
                </p>
            </div>
        </div>
        <div class="mt_primelink-card-body">
            <div class="mt_primelink-form-row">
                <label for="mt_primelink-export-format"><?php esc_html_e( 'File Format', 'primelink' ); ?></label>
                <select id="mt_primelink-export-format" name="format" class="widefat">
                    <option value="csv"><?php esc_html_e( 'CSV (Comma Separated Values)', 'primelink' ); ?></option>
                    <option value="xml"><?php esc_html_e( 'XML (Extensible Markup Language)', 'primelink' ); ?></option>
                    <option value="xlsx"><?php esc_html_e( 'XLSX (Excel Spreadsheet)', 'primelink' ); ?></option>
                </select>
                <p class="description">
                    <?php esc_html_e( 'Select the format for your export file. CSV is recommended for most use cases.', 'primelink' ); ?>
                </p>
            </div>

            <div id="mt_primelink-csv-options" class="mt_primelink-format-options" style="margin-top:16px;padding-top:16px;border-top:1px solid rgba(255,255,255,0.1);">
                <div class="mt_primelink-form-row">
                    <label for="mt_primelink-csv-delimiter"><?php esc_html_e( 'CSV Delimiter', 'primelink' ); ?></label>
                    <select id="mt_primelink-csv-delimiter" name="csv_delimiter" class="widefat">
                        <option value=","><?php esc_html_e( 'Comma (,)', 'primelink' ); ?></option>
                        <option value=";"><?php esc_html_e( 'Semicolon (;)', 'primelink' ); ?></option>
                        <option value="\t"><?php esc_html_e( 'Tab', 'primelink' ); ?></option>
                    </select>
                    <p class="description">
                        <?php esc_html_e( 'Choose the character used to separate values in the CSV file.', 'primelink' ); ?>
                    </p>
                </div>
                <div class="mt_primelink-form-row">
                    <label for="mt_primelink-csv-encoding"><?php esc_html_e( 'Character Encoding', 'primelink' ); ?></label>
                    <select id="mt_primelink-csv-encoding" name="csv_encoding" class="widefat">
                        <option value="UTF-8">UTF-8</option>
                        <option value="UTF-8-BOM">UTF-8 with BOM</option>
                        <option value="ISO-8859-1">ISO-8859-1 (Latin-1)</option>
                    </select>
                    <p class="description">
                        <?php esc_html_e( 'Select the character encoding. UTF-8 is recommended for international characters.', 'primelink' ); ?>
                    </p>
                </div>
            </div>
        </div>
    </section>

    <!-- Category Filters -->
    <section class="mt_primelink-card mt_primelink-card--glass">
        <div class="mt_primelink-card-header">
            <div>
                <h3><?php esc_html_e( 'Category Filters', 'primelink' ); ?></h3>
                <p class="description" style="margin:4px 0 0;">
                    <?php esc_html_e( 'Filter which categories to include in the export based on parent category.', 'primelink' ); ?>
                </p>
            </div>
        </div>
        <div class="mt_primelink-card-body">
            <div class="mt_primelink-form-row">
                <label for="mt_primelink-export-parent"><?php esc_html_e( 'Parent Category', 'primelink' ); ?></label>
                <select id="mt_primelink-export-parent" name="filters[parent]" class="widefat">
                    <option value=""><?php esc_html_e( 'All Categories', 'primelink' ); ?></option>
                    <?php
                    $categories = get_terms( array(
                        'taxonomy'   => 'product_cat',
                        'hide_empty' => false,
                        'parent'     => 0,
                    ) );
                    if ( ! is_wp_error( $categories ) && ! empty( $categories ) ) {
                        foreach ( $categories as $category ) {
                            echo '<option value="' . esc_attr( $category->term_id ) . '">' . esc_html( $category->name ) . '</option>';
                        }
                    }
                    ?>
                </select>
                <p class="description">
                    <?php esc_html_e( 'Select a parent category to export only its subcategories. Leave empty to export all categories.', 'primelink' ); ?>
                </p>
            </div>
        </div>
    </section>

    <!-- Advanced Options -->
    <section class="mt_primelink-card mt_primelink-card--glass">
        <div class="mt_primelink-card-header">
            <div>
                <h3><?php esc_html_e( 'Advanced Options', 'primelink' ); ?></h3>
                <p class="description" style="margin:4px 0 0;">
                    <?php esc_html_e( 'Configure additional export settings and limits.', 'primelink' ); ?>
                </p>
            </div>
        </div>
        <div class="mt_primelink-card-body">
            <div class="mt_primelink-form-row">
                <label class="mt_primelink-check">
                    <input type="checkbox" id="mt_primelink-export-include-headers" name="include_headers" checked />
                    <strong><?php esc_html_e( 'Include Column Headers', 'primelink' ); ?></strong>
                </label>
                <p class="description" style="margin-top:4px;">
                    <?php esc_html_e( 'Include a header row with column names as the first line of the export file.', 'primelink' ); ?>
                </p>
            </div>

            <div class="mt_primelink-form-row" style="margin-top:16px;padding-top:16px;border-top:1px solid rgba(255,255,255,0.1);">
                <label for="mt_primelink-export-limit"><?php esc_html_e( 'Limit Number of Categories', 'primelink' ); ?></label>
                <input type="number" id="mt_primelink-export-limit" name="limit" min="0" value="0" class="widefat" style="max-width:200px;" />
                <p class="description">
                    <?php esc_html_e( 'Set to 0 to export all matching categories. Enter a number to limit the export to that many categories.', 'primelink' ); ?>
                </p>
            </div>

            <div class="mt_primelink-form-row" style="margin-top:16px;padding-top:16px;border-top:1px solid rgba(255,255,255,0.1);">
                <label class="mt_primelink-check">
                    <input type="checkbox" id="mt_primelink-export-speed-optimization" />
                    <?php esc_html_e( 'Export Speed Optimization', 'primelink' ); ?>
                </label>
                <p class="description" style="margin-top:4px;">
                    <?php esc_html_e( 'Enable optimizations to speed up large exports. May use more server resources.', 'primelink' ); ?>
                </p>
            </div>

            <div class="mt_primelink-form-row" style="margin-top:12px;">
                <label class="mt_primelink-check">
                    <input type="checkbox" id="mt_primelink-export-iterative-processing" checked />
                    <?php esc_html_e( 'Iterative, Piece-by-Piece Processing', 'primelink' ); ?>
                </label>
                <p class="description" style="margin-top:4px;">
                    <?php esc_html_e( 'Process export in chunks for better performance and memory management.', 'primelink' ); ?>
                </p>
            </div>

            <div class="mt_primelink-form-row" style="margin-top:12px;">
                <label for="mt_primelink-export-iteration-size"><?php esc_html_e( 'Records per iteration', 'primelink' ); ?></label>
                <input type="number" id="mt_primelink-export-iteration-size" min="1" max="500" value="20" style="max-width:200px;" />
                <p class="description">
                    <?php esc_html_e( 'Default: process 20 records per iteration in background.', 'primelink' ); ?>
                </p>
            </div>
        </div>
    </section>

    <!-- Notifications -->
    <section class="mt_primelink-card mt_primelink-card--glass">
        <div class="mt_primelink-card-header">
            <h3><?php esc_html_e( 'Notifications (this export only)', 'primelink' ); ?></h3>
        </div>
        <div class="mt_primelink-card-body">
            <label class="mt_primelink-check">
                <input type="checkbox" id="mt_primelink-export-notify-override" />
                <?php esc_html_e( 'Use custom notification settings for this export', 'primelink' ); ?>
            </label>
            <div id="mt_primelink-export-notify-override-wrap" style="display:none;margin-top:10px;">
                <div class="mt_primelink-form-row">
                    <label for="mt_primelink-export-notify-email-override"><?php esc_html_e( 'Notify email', 'primelink' ); ?></label>
                    <input type="email" id="mt_primelink-export-notify-email-override" placeholder="<?php esc_attr_e( 'you@example.com', 'primelink' ); ?>" />
                </div>
                <div class="mt_primelink-form-row">
                    <label for="mt_primelink-export-notify-finish-override"><?php esc_html_e( 'Email template (finish)', 'primelink' ); ?></label>
                    <textarea id="mt_primelink-export-notify-finish-override" rows="3" placeholder="<?php esc_attr_e( 'Export #{export_id} finished. Status: {status}. Categories exported: {total}', 'primelink' ); ?>"></textarea>
                    <p class="description"><?php esc_html_e( 'Placeholders: {export_id}, {status}, {total}, {file_url}', 'primelink' ); ?></p>
                </div>
                <div class="mt_primelink-form-row">
                    <label for="mt_primelink-export-notify-error-override"><?php esc_html_e( 'Email template (error)', 'primelink' ); ?></label>
                    <textarea id="mt_primelink-export-notify-error-override" rows="3" placeholder="<?php esc_attr_e( 'Export #{export_id} finished with errors: {errors}', 'primelink' ); ?>"></textarea>
                </div>
                <div class="mt_primelink-form-row">
                    <label class="mt_primelink-check">
                        <input type="checkbox" id="mt_primelink-export-email-attachment" />
                        <?php esc_html_e( 'Attach export file to email', 'primelink' ); ?>
                    </label>
                    <p class="description">
                        <?php esc_html_e( 'Send the exported file as an email attachment when export completes.', 'primelink' ); ?>
                    </p>
                </div>
            </div>
        </div>
    </section>

    <!-- Scheduling -->
    <section class="mt_primelink-card mt_primelink-card--glass">
        <div class="mt_primelink-card-header">
            <h3><?php esc_html_e( 'Automatic Scheduling with Background Export', 'primelink' ); ?></h3>
        </div>

        <div class="mt_primelink-card-body">

            <label class="mt_primelink-check">
                <input type="checkbox" id="mt_primelink-export-enable-schedule" />
                <?php esc_html_e( 'Enable Background Export Schedule', 'primelink' ); ?>
            </label>

            <div id="mt_primelink-export-schedule-wrap" style="display:none;margin-top:10px;">

                <div class="mt_primelink-form-row">
                    <label for="mt_primelink-export-schedule-name"><?php esc_html_e( 'Friendly Name', 'primelink' ); ?></label>
                    <input type="text" id="mt_primelink-export-schedule-name" placeholder="<?php esc_attr_e( 'e.g. Daily Category Export', 'primelink' ); ?>" />
                </div>

                <div class="mt_primelink-form-row">
                    <label for="mt_primelink-export-schedule-interval"><?php esc_html_e( 'Export Interval', 'primelink' ); ?></label>
                    <select id="mt_primelink-export-schedule-interval" class="widefat">
                        <option value="hourly"><?php esc_html_e( 'Hourly', 'primelink' ); ?></option>
                        <option value="twicedaily"><?php esc_html_e( 'Twice Daily', 'primelink' ); ?></option>
                        <option value="daily" selected><?php esc_html_e( 'Daily', 'primelink' ); ?></option>
                        <option value="weekly"><?php esc_html_e( 'Weekly', 'primelink' ); ?></option>
                        <option value="monthly"><?php esc_html_e( 'Monthly', 'primelink' ); ?></option>
                    </select>
                </div>

                <div class="mt_primelink-form-row">
                    <label for="mt_primelink-export-schedule-start"><?php esc_html_e( 'Start Time', 'primelink' ); ?></label>
                    <input type="time" id="mt_primelink-export-schedule-start" value="00:00" />
                    <p class="description">
                        <?php esc_html_e( 'Default: current time if left empty.', 'primelink' ); ?>
                    </p>
                </div>

            </div>
        </div>
    </section>
</div>

