<?php
/**
 * New Export Wizard container.
 *
 * @package PrimeLink
 */

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

$export_type = isset( $_GET['export_type'] ) ? sanitize_text_field( wp_unslash( $_GET['export_type'] ) ) : '';

// If no export_type is specified, show the selection page
if ( empty( $export_type ) ) {
    include MT_PRIMELINK_PATH . 'templates/admin/exports/new-export-select.php';
    return;
}

// Get step and export_id from URL or create new
$step = isset( $_GET['step'] ) ? absint( $_GET['step'] ) : 1;
$export_id = isset( $_GET['export_id'] ) ? absint( $_GET['export_id'] ) : 0;

// If no export_id, create new draft
if ( $export_id === 0 ) {
    // Will be created via JavaScript on page load
}

$total = 4; // Total steps for export

$base_url = admin_url( 'admin.php?page=mt-primelink-new-export' );
if ( $export_type ) {
    $base_url = add_query_arg( 'export_type', $export_type, $base_url );
}

// Only show wizard for supported types
if ( ! in_array( $export_type, array( 'products', 'orders', 'customers', 'coupons', 'categories', 'tags', 'attributes', 'variations', 'reviews', 'downloads' ), true ) ) : ?>
    <div class="wrap mt-primelink-wrap mt-primelink-wizard">
        <div class="mt_primelink-import-modal glass" style="max-width:720px;margin:40px auto;">
            <div class="mt_primelink-import-modal__header">
                <h2><?php echo esc_html( ucfirst( $export_type ) . ' Export' ); ?></h2>
            </div>
            <p class="muted"><?php esc_html_e( 'This export type is coming soon. Please check back later.', 'primelink' ); ?></p>
            <div style="margin-top:12px;">
                <a class="button button-primary" href="<?php echo esc_url( admin_url( 'admin.php?page=mt-primelink-manage-export' ) ); ?>">
                    <?php esc_html_e( 'Back to Manage Exports', 'primelink' ); ?>
                </a>
            </div>
        </div>
    </div>
<?php return; endif; ?>
<div class="wrap mt-primelink-wrap mt-primelink-wizard">

    <div class="mt-primelink-wizard__header glass">
        <div class="mt_primelink-header-top-right">
            <div class="mt_primelink-wizard-version-inline mt_primelink-wizard-version-inline--top-right">
                <span class="mt_primelink-summary-eyebrow"><?php esc_html_e( 'Version', 'primelink' ); ?></span>
                <strong><?php echo esc_html( MT_PRIMELINK_VERSION ); ?></strong>
            </div>
            <a href="https://docs.malotop.ro/primelink/" target="_blank" rel="noopener" class="mt_primelink-docs-link" title="<?php esc_attr_e( 'Documentation & Support', 'primelink' ); ?>">
                <span class="dashicons dashicons-book-alt"></span>
                <span class="mt_primelink-docs-link-text"><?php esc_html_e( 'Docs', 'primelink' ); ?></span>
            </a>
        </div>
        <div class="mt-primelink-wizard__brand">
            <div class="mt_primelink-brand-head">
                <img src="<?php echo esc_url( MT_PRIMELINK_URL . 'assets/admin/img/logo.png' ); ?>" alt="<?php esc_attr_e( 'PrimeLink', 'primelink' ); ?>" class="mt_primelink-brand-logo" />
            </div>
            <div class="mt_primelink-summary-eyebrow"><?php esc_html_e( 'Guided Setup', 'primelink' ); ?></div>
            <h1><?php esc_html_e( 'New Export', 'primelink' ); ?></h1>
            <p class="muted"><?php esc_html_e( 'Follow the steps to configure your export.', 'primelink' ); ?></p>
        </div>

        <div class="mt-primelink-wizard__steps">
            <?php for ( $i = 1; $i <= $total; $i++ ) :
                $active = ( $i === $step ) ? 'is-active' : '';
                $done   = ( $i < $step ) ? 'is-done' : '';
            ?>
                <a class="mt-primelink-step <?php echo esc_attr( "$active $done" ); ?>"
                   href="<?php echo esc_url( add_query_arg( array( 'step' => $i, 'export_id' => $export_id ), $base_url ) ); ?>">
                    <span class="mt-primelink-step__num"><?php echo (int) $i; ?></span>
                    <span class="mt-primelink-step__label">
                        <?php echo esc_html( sprintf( __( 'Step %d', 'primelink' ), $i ) ); ?>
                    </span>
                </a>
            <?php endfor; ?>
        </div>
    </div>

    <div class="mt-primelink-wizard__body glass">
        <?php
        // Load step template based on export type
        if ( $export_type === 'products' ) {
            $step_file = MT_PRIMELINK_PATH . "templates/admin/exports/products/step-{$step}.php";
        } elseif ( $export_type === 'orders' ) {
            $step_file = MT_PRIMELINK_PATH . "templates/admin/exports/orders/step-{$step}.php";
        } elseif ( $export_type === 'customers' ) {
            $step_file = MT_PRIMELINK_PATH . "templates/admin/exports/customers/step-{$step}.php";
        } elseif ( $export_type === 'coupons' ) {
            $step_file = MT_PRIMELINK_PATH . "templates/admin/exports/coupons/step-{$step}.php";
        } elseif ( $export_type === 'categories' ) {
            $step_file = MT_PRIMELINK_PATH . "templates/admin/exports/categories/step-{$step}.php";
        } elseif ( $export_type === 'tags' ) {
            $step_file = MT_PRIMELINK_PATH . "templates/admin/exports/tags/step-{$step}.php";
        } elseif ( $export_type === 'attributes' ) {
            $step_file = MT_PRIMELINK_PATH . "templates/admin/exports/attributes/step-{$step}.php";
        } elseif ( $export_type === 'variations' ) {
            $step_file = MT_PRIMELINK_PATH . "templates/admin/exports/variations/step-{$step}.php";
        } elseif ( $export_type === 'reviews' ) {
            $step_file = MT_PRIMELINK_PATH . "templates/admin/exports/reviews/step-{$step}.php";
        } elseif ( $export_type === 'downloads' ) {
            $step_file = MT_PRIMELINK_PATH . "templates/admin/exports/downloads/step-{$step}.php";
        } else {
            $step_file = MT_PRIMELINK_PATH . "templates/admin/exports/partials/step-{$step}.php";
        }
        
        if ( file_exists( $step_file ) ) {
            include $step_file;
        } else {
            echo '<p>' . esc_html__( 'Step template missing.', 'primelink' ) . '</p>';
        }
        ?>
    </div>

    <div class="mt-primelink-wizard__footer">
        <?php if ( $step > 1 ) : ?>
            <a href="<?php echo esc_url( add_query_arg( array( 'step' => $step - 1, 'export_id' => $export_id ), $base_url ) ); ?>" class="button">
                <?php esc_html_e( 'Previous', 'primelink' ); ?>
            </a>
        <?php else : ?>
            <a href="<?php echo esc_url( admin_url( 'admin.php?page=mt-primelink-new-export' ) ); ?>" class="button">
                <?php esc_html_e( 'Cancel', 'primelink' ); ?>
            </a>
        <?php endif; ?>

        <?php if ( $step < $total ) : ?>
            <button type="button" class="button button-primary" id="mt-primelink-next-step">
                <?php esc_html_e( 'Next', 'primelink' ); ?>
            </button>
        <?php else : ?>
            <!-- Buttons removed for last step - use buttons in step content instead -->
        <?php endif; ?>
    </div>
</div>

<?php
// Pass export type to JS via data attribute
?>
<div data-export-type="<?php echo esc_attr( $export_type ); ?>" style="display:none;"></div>
