<?php

/**
 * A1-WooBay - eBay Listings Page
 *
 * @package A1_WooBay
 * @since   0.3.6
 */
if ( !defined( 'ABSPATH' ) ) {
    exit;
}
// --- Server-side bulk handler (local-only actions only) ---
$bulk_message = '';
$bulk_error = '';
if ( isset( $_SERVER['REQUEST_METHOD'] ) && 'POST' === $_SERVER['REQUEST_METHOD'] && isset( $_POST['a1wb_listings_nonce'] ) && wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['a1wb_listings_nonce'] ) ), 'a1wb_listings_bulk' ) && current_user_can( 'manage_woocommerce' ) ) {
    $action = sanitize_text_field( wp_unslash( $_POST['bulk_action'] ?? '' ) );
    $product_ids = array_map( 'intval', (array) wp_unslash( $_POST['product_ids'] ?? array() ) );
    if ( $action && $product_ids ) {
        $count = count( $product_ids );
        switch ( $action ) {
            case 'move_to_archive':
                foreach ( $product_ids as $pid ) {
                    A1WooBay_Product_Meta::set_listing_archived( $pid );
                }
                /* translators: %d: number of listings archived */
                $bulk_message = sprintf( _n(
                    '%d listing archived.',
                    '%d listings archived.',
                    $count,
                    'a1-marketplace-sync-with-ebay-for-woocommerce'
                ), $count );
                break;
            case 'reset_to_prepared':
                foreach ( $product_ids as $pid ) {
                    A1WooBay_Product_Meta::reset_to_prepared( $pid );
                }
                /* translators: %d: number of items reset */
                $bulk_message = sprintf( _n(
                    '%d item reset to Prepared.',
                    '%d items reset to Prepared.',
                    $count,
                    'a1-marketplace-sync-with-ebay-for-woocommerce'
                ), $count );
                break;
        }
    }
}
// --- Status filter ---
$status_filter = ( isset( $_GET['status'] ) ? sanitize_text_field( wp_unslash( $_GET['status'] ) ) : 'all' );
// --- Query products with a profile assigned OR an eBay item ID (adopted listings) ---
// Membership criteria: has profile OR has eBay item ID
$membership = array(
    'relation' => 'OR',
    array(
        'key'     => A1WooBay_Product_Meta::META_PROFILE_ID,
        'value'   => '0',
        'compare' => '>',
        'type'    => 'NUMERIC',
    ),
    array(
        'key'     => A1WooBay_Product_Meta::META_EBAY_ITEM_ID,
        'compare' => 'EXISTS',
    ),
);
// Status filter must be AND'd with membership, not OR'd
if ( 'needs_revision' === $status_filter ) {
    $meta_query = array(
        'relation' => 'AND',
        $membership,
        array(
            'key'   => A1WooBay_Product_Meta::META_NEEDS_REVISION,
            'value' => '1',
        ),
    );
} elseif ( 'all' !== $status_filter ) {
    $meta_query = array(
        'relation' => 'AND',
        $membership,
        array(
            'key'   => A1WooBay_Product_Meta::META_EBAY_STATUS,
            'value' => $status_filter,
        ),
    );
} else {
    $meta_query = $membership;
}
$query = new WP_Query(array(
    'post_type'      => 'product',
    'post_status'    => 'publish',
    'posts_per_page' => 200,
    'meta_query'     => $meta_query,
));
$products = $query->posts;
// --- Status tab counts ---
$all_statuses = array(
    A1WooBay_Product_Meta::STATUS_NOT_LISTED,
    A1WooBay_Product_Meta::STATUS_VERIFIED,
    A1WooBay_Product_Meta::STATUS_ACTIVE,
    A1WooBay_Product_Meta::STATUS_ENDED,
    A1WooBay_Product_Meta::STATUS_SOLD,
    A1WooBay_Product_Meta::STATUS_ARCHIVED,
    A1WooBay_Product_Meta::STATUS_ERROR
);
$tab_counts = array(
    'all' => 0,
);
foreach ( $all_statuses as $s ) {
    $tab_counts[$s] = 0;
}
$all_products_query = new WP_Query(array(
    'post_type'      => 'product',
    'post_status'    => 'publish',
    'posts_per_page' => 500,
    'meta_query'     => array(
        'relation' => 'OR',
        array(
            'key'     => A1WooBay_Product_Meta::META_PROFILE_ID,
            'value'   => '0',
            'compare' => '>',
            'type'    => 'NUMERIC',
        ),
        array(
            'key'     => A1WooBay_Product_Meta::META_EBAY_ITEM_ID,
            'compare' => 'EXISTS',
        ),
    ),
));
foreach ( $all_products_query->posts as $p ) {
    $s = A1WooBay_Product_Meta::get_status( $p->ID );
    $tab_counts['all']++;
    if ( isset( $tab_counts[$s] ) ) {
        $tab_counts[$s]++;
    }
}
// Count needs-revision flag separately (it's a flag, not a status)
$needs_revision_query = new WP_Query(array(
    'post_type'      => 'product',
    'post_status'    => 'publish',
    'posts_per_page' => 500,
    'fields'         => 'ids',
    'meta_query'     => array(
        'relation' => 'AND',
        array(
            'relation' => 'OR',
            array(
                'key'     => A1WooBay_Product_Meta::META_PROFILE_ID,
                'value'   => '0',
                'compare' => '>',
                'type'    => 'NUMERIC',
            ),
            array(
                'key'     => A1WooBay_Product_Meta::META_EBAY_ITEM_ID,
                'compare' => 'EXISTS',
            ),
        ),
        array(
            'key'   => A1WooBay_Product_Meta::META_NEEDS_REVISION,
            'value' => '1',
        ),
    ),
));
$tab_counts['needs_revision'] = $needs_revision_query->found_posts;
$tabs = array(
    'all' => 'All',
);
foreach ( $all_statuses as $s ) {
    $tabs[$s] = A1WooBay_Product_Meta::get_status_label( $s );
}
$tabs['needs_revision'] = 'Needs Verification';
?>
<div class="wrap">
	<h1 style="display:flex;align-items:center;gap:12px;">
		<?php 
esc_html_e( 'eBay Listings', 'a1-marketplace-sync-with-ebay-for-woocommerce' );
?>
	</h1>

	<?php 
if ( $bulk_message ) {
    ?>
		<div class="notice notice-success is-dismissible"><p><?php 
    echo esc_html( $bulk_message );
    ?></p></div>
	<?php 
}
?>
	<?php 
if ( $bulk_error ) {
    ?>
		<div class="notice notice-warning is-dismissible"><p><?php 
    echo esc_html( $bulk_error );
    ?></p></div>
	<?php 
}
?>

	<!-- Status tabs -->
	<ul class="subsubsub" style="margin-bottom:8px;">
		<?php 
foreach ( $tabs as $key => $label ) {
    ?>
			<li>
				<a href="<?php 
    echo esc_url( add_query_arg( array(
        'page'   => 'a1woobay-listings',
        'status' => $key,
    ), admin_url( 'admin.php' ) ) );
    ?>"
				   <?php 
    if ( $status_filter === $key ) {
        echo 'class="current" style="font-weight:700;"';
    }
    ?>>
					<?php 
    echo esc_html( $label );
    ?>
					<span class="count">(<?php 
    echo esc_html( $tab_counts[$key] ?? 0 );
    ?>)</span>
				</a>
				<?php 
    if ( array_key_last( $tabs ) !== $key ) {
        echo ' | ';
    }
    ?>
			</li>
		<?php 
}
?>
	</ul>

	<?php 
// Count active/published listings missing a profile
$needs_profile_count = 0;
foreach ( $products as $_p ) {
    $_status = A1WooBay_Product_Meta::get_status( $_p->ID );
    $_profile = A1WooBay_Product_Meta::get_profile_id( $_p->ID );
    if ( in_array( $_status, array(A1WooBay_Product_Meta::STATUS_ACTIVE, A1WooBay_Product_Meta::STATUS_VERIFIED), true ) && !$_profile ) {
        $needs_profile_count++;
    }
}
if ( $needs_profile_count > 0 ) {
    ?>
		<div class="notice notice-warning" style="margin:0 0 12px;padding:10px 14px;border-left-color:#f0b849;">
			<p>
				<strong>
					<?php 
    printf( 
        /* translators: %d: number of adopted listings missing a profile */
        esc_html( _n(
            '%d adopted listing needs a profile and template assigned',
            '%d adopted listings need a profile and template assigned',
            $needs_profile_count,
            'a1-marketplace-sync-with-ebay-for-woocommerce'
        ) ),
        (int) $needs_profile_count
     );
    ?>
				</strong>
				&mdash; <?php 
    esc_html_e( 'Without a profile, you cannot revise, end, or relist through A1 Marketplace Sync with eBay for WooCommerce. Edit each product and assign a profile in the A1 Marketplace Sync with eBay for WooCommerce panel.', 'a1-marketplace-sync-with-ebay-for-woocommerce' );
    ?>
			</p>
		</div>
	<?php 
}
?>
	<form method="post" id="a1wb-listings-form">
		<?php 
wp_nonce_field( 'a1wb_listings_bulk', 'a1wb_listings_nonce' );
?>

		<!-- Bulk actions bar -->
		<div class="tablenav top" style="display:flex;align-items:center;gap:8px;margin-bottom:8px;">
			<select name="bulk_action" id="a1wb-bulk-action">
				<option value=""><?php 
esc_html_e( 'Bulk actions', 'a1-marketplace-sync-with-ebay-for-woocommerce' );
?></option>
				<optgroup label="eBay Actions">
					<option value="verify"><?php 
esc_html_e( 'Verify with eBay', 'a1-marketplace-sync-with-ebay-for-woocommerce' );
?></option>
					<option value="publish"><?php 
esc_html_e( 'Publish to eBay', 'a1-marketplace-sync-with-ebay-for-woocommerce' );
?></option>
					<option value="revise"><?php 
esc_html_e( 'Revise listings', 'a1-marketplace-sync-with-ebay-for-woocommerce' );
?></option>
					<option value="end_listings"><?php 
esc_html_e( 'End listings', 'a1-marketplace-sync-with-ebay-for-woocommerce' );
?></option>
					<option value="mark_sold"><?php 
esc_html_e( 'Mark as Sold', 'a1-marketplace-sync-with-ebay-for-woocommerce' );
?></option>
					<option value="relist"><?php 
esc_html_e( 'Re-list ended items', 'a1-marketplace-sync-with-ebay-for-woocommerce' );
?></option>
					<option value="resync"><?php 
esc_html_e( 'Re-sync from eBay', 'a1-marketplace-sync-with-ebay-for-woocommerce' );
?></option>
				</optgroup>
				<optgroup label="Local Actions">
					<option value="move_to_archive"><?php 
esc_html_e( 'Move to archive', 'a1-marketplace-sync-with-ebay-for-woocommerce' );
?></option>
					<option value="reset_to_prepared"><?php 
esc_html_e( 'Reset to Prepared', 'a1-marketplace-sync-with-ebay-for-woocommerce' );
?></option>
				</optgroup>
			</select>
			<button type="button" id="a1wb-bulk-apply" class="button"><?php 
esc_html_e( 'Apply', 'a1-marketplace-sync-with-ebay-for-woocommerce' );
?></button>
		<button type="button" id="a1wb-check-sales-btn" class="button button-secondary" style="margin-left:12px;"><?php 
esc_html_e( 'Check for Sales', 'a1-marketplace-sync-with-ebay-for-woocommerce' );
?></button>

		</div>

		<?php 
if ( empty( $products ) ) {
    ?>
			<p style="color:#888;">
				<?php 
    if ( 'all' === $status_filter ) {
        esc_html_e( 'No products have an eBay profile assigned yet. Edit a product and assign a profile in the A1 Marketplace Sync with eBay for WooCommerce panel.', 'a1-marketplace-sync-with-ebay-for-woocommerce' );
    } else {
        esc_html_e( 'No listings with this status.', 'a1-marketplace-sync-with-ebay-for-woocommerce' );
    }
    ?>
			</p>
		<?php 
} else {
    ?>
			<table class="wp-list-table widefat striped" style="margin-top:0;">
				<thead>
					<tr>
						<th style="width:30px;white-space:nowrap;"><input type="checkbox" id="a1wb-check-all"></th>
						<th style="width:44px;white-space:nowrap;"><?php 
    esc_html_e( 'Image', 'a1-marketplace-sync-with-ebay-for-woocommerce' );
    ?></th>
						<th class="a1wb-sort-th" data-sort-col="2" data-sort-type="text" style="white-space:nowrap;cursor:pointer;"><?php 
    esc_html_e( 'SKU', 'a1-marketplace-sync-with-ebay-for-woocommerce' );
    ?> <span class="a1wb-sort-ind" style="color:#aaa;font-size:10px;"></span></th>
						<th class="a1wb-sort-th" data-sort-col="3" data-sort-type="text" style="white-space:nowrap;cursor:pointer;"><?php 
    esc_html_e( 'Title', 'a1-marketplace-sync-with-ebay-for-woocommerce' );
    ?> <span class="a1wb-sort-ind" style="color:#aaa;font-size:10px;"></span></th>
						<th class="a1wb-sort-th" data-sort-col="4" data-sort-type="num" style="white-space:nowrap;cursor:pointer;"><?php 
    esc_html_e( 'Price', 'a1-marketplace-sync-with-ebay-for-woocommerce' );
    ?> <span class="a1wb-sort-ind" style="color:#aaa;font-size:10px;"></span></th>
						<th class="a1wb-sort-th" data-sort-col="5" data-sort-type="num" style="text-align:center;white-space:nowrap;cursor:pointer;"><?php 
    esc_html_e( 'Qty', 'a1-marketplace-sync-with-ebay-for-woocommerce' );
    ?> <span class="a1wb-sort-ind" style="color:#aaa;font-size:10px;"></span></th>
						<th class="a1wb-sort-th" data-sort-col="6" data-sort-type="num" style="text-align:center;white-space:nowrap;cursor:pointer;"><?php 
    esc_html_e( 'Sold', 'a1-marketplace-sync-with-ebay-for-woocommerce' );
    ?> <span class="a1wb-sort-ind" style="color:#aaa;font-size:10px;"></span></th>
						<th class="a1wb-sort-th" data-sort-col="7" data-sort-type="num" style="text-align:center;white-space:nowrap;cursor:pointer;" title="Run Re-sync to refresh. Click to sort."><?php 
    esc_html_e( 'Watchers', 'a1-marketplace-sync-with-ebay-for-woocommerce' );
    ?> <span class="a1wb-sort-ind" style="color:#aaa;font-size:10px;"></span></th>
						<th class="a1wb-sort-th" data-sort-col="8" data-sort-type="text" style="white-space:nowrap;cursor:pointer;"><?php 
    esc_html_e( 'Profile', 'a1-marketplace-sync-with-ebay-for-woocommerce' );
    ?> <span class="a1wb-sort-ind" style="color:#aaa;font-size:10px;"></span></th>
						<th class="a1wb-sort-th" data-sort-col="9" data-sort-type="text" style="white-space:nowrap;cursor:pointer;"><?php 
    esc_html_e( 'Template', 'a1-marketplace-sync-with-ebay-for-woocommerce' );
    ?> <span class="a1wb-sort-ind" style="color:#aaa;font-size:10px;"></span></th>
						<th class="a1wb-sort-th" data-sort-col="10" data-sort-type="text" style="white-space:nowrap;cursor:pointer;"><?php 
    esc_html_e( 'eBay ID', 'a1-marketplace-sync-with-ebay-for-woocommerce' );
    ?> <span class="a1wb-sort-ind" style="color:#aaa;font-size:10px;"></span></th>
						<th class="a1wb-sort-th" data-sort-col="11" data-sort-type="text" style="white-space:nowrap;cursor:pointer;"><?php 
    esc_html_e( 'Status', 'a1-marketplace-sync-with-ebay-for-woocommerce' );
    ?> <span class="a1wb-sort-ind" style="color:#aaa;font-size:10px;"></span></th>
					</tr>
				</thead>
				<tbody id="a1wb-listings-tbody">
					<?php 
    foreach ( $products as $post ) {
        ?>
						<?php 
        $product = wc_get_product( $post->ID );
        if ( !$product ) {
            continue;
        }
        $pid = $post->ID;
        $status = A1WooBay_Product_Meta::get_status( $pid );
        $label = A1WooBay_Product_Meta::get_status_label( $status );
        $color = A1WooBay_Product_Meta::get_status_color( $status );
        $item_id = A1WooBay_Product_Meta::get_ebay_item_id( $pid );
        $profile_id = A1WooBay_Product_Meta::get_profile_id( $pid );
        $template_id = A1WooBay_Product_Meta::get_template_id( $pid );
        $profile = ( $profile_id ? get_post( $profile_id ) : null );
        // Description Templates are a Pro-only feature (WordPress.org
        // Guideline 5) - nothing to show on the free build, see
        // includes/class-a1woobay-templates__premium_only.php's docblock.
        $template = null;
        $edit_url = get_edit_post_link( $pid );
        $thumb = get_the_post_thumbnail( $pid, 'thumbnail', array(
            'style'  => 'width:36px;height:36px;object-fit:cover;display:block;',
            'width'  => '36',
            'height' => '36',
        ) );
        $price = $product->get_price();
        $sku = $product->get_sku();
        $ebay_error = get_post_meta( $pid, A1WooBay_Product_Meta::META_EBAY_ERROR, true );
        $needs_rev = A1WooBay_Product_Meta::needs_revision( $pid );
        $qty = $product->get_stock_quantity();
        $sold = (int) $product->get_total_sales();
        $watch_count = get_post_meta( $pid, A1WooBay_Product_Meta::META_WATCH_COUNT, true );
        $display_title = $post->post_title;
        if ( mb_strlen( $display_title ) > 80 ) {
            $trimmed = mb_substr( $display_title, 0, 80 );
            $last_space = mb_strrpos( $trimmed, ' ' );
            if ( $last_space > 50 ) {
                $trimmed = mb_substr( $trimmed, 0, $last_space );
            }
            $display_title = rtrim( $trimmed );
        }
        ?>
						<tr id="a1wb-row-<?php 
        echo esc_attr( $pid );
        ?>">
							<td><input type="checkbox" name="product_ids[]" value="<?php 
        echo esc_attr( $pid );
        ?>"></td>
							<td style="width:44px;padding:6px 4px;">
								<?php 
        if ( $thumb ) {
            ?>
									<a href="<?php 
            echo esc_url( $edit_url );
            ?>" style="display:block;width:36px;height:36px;overflow:hidden;border-radius:3px;"><?php 
            echo wp_kses_post( $thumb );
            ?></a>
								<?php 
        } else {
            ?>
									<span style="display:block;width:36px;height:36px;background:#eee;border-radius:3px;"></span>
								<?php 
        }
        ?>
							</td>
							<td style="font-size:12px;overflow:hidden;white-space:nowrap;text-overflow:ellipsis;" data-sort-val="<?php 
        echo esc_attr( ( $sku ?: '' ) );
        ?>" title="<?php 
        echo esc_attr( $sku );
        ?>"><?php 
        echo esc_html( ( $sku ?: '--' ) );
        ?></td>
							<td data-sort-val="<?php 
        echo esc_attr( $post->post_title );
        ?>">
								<strong><a href="<?php 
        echo esc_url( $edit_url );
        ?>"><?php 
        echo esc_html( $display_title );
        ?></a></strong>
								<?php 
        if ( $needs_rev && A1WooBay_Product_Meta::STATUS_ACTIVE === $status ) {
            ?>
									<br><small style="color:#d97706;">&#9998; Verification needed</small>
								<?php 
        }
        ?>
								<?php 
        if ( $ebay_error && A1WooBay_Product_Meta::STATUS_ERROR === $status ) {
            ?>
									<br><small style="color:#dc3232;"><?php 
            echo esc_html( $ebay_error );
            ?></small>
								<?php 
        }
        ?>
								<?php 
        $row_actions = array();
        $row_actions[] = '<a href="' . esc_url( $edit_url ) . '">Edit</a>';
        if ( $item_id ) {
            $row_actions[] = '<a href="https://www.ebay.com/itm/' . esc_attr( $item_id ) . '" target="_blank" rel="noopener">View on eBay</a>';
        }
        $row_actions[] = '<a href="#" class="a1wb-row-action" data-action="verify" data-pid="' . esc_attr( $pid ) . '">Verify</a>';
        if ( A1WooBay_Product_Meta::STATUS_VERIFIED === $status ) {
            $row_actions[] = '<a href="#" class="a1wb-row-action" data-action="publish" data-pid="' . esc_attr( $pid ) . '">Publish</a>';
        }
        if ( A1WooBay_Product_Meta::STATUS_ACTIVE === $status ) {
            $row_actions[] = '<a href="#" class="a1wb-row-action" data-action="revise" data-pid="' . esc_attr( $pid ) . '">Revise</a>';
            $row_actions[] = '<a href="#" class="a1wb-row-action" style="color:#dc3232;" data-action="end_listings" data-pid="' . esc_attr( $pid ) . '">End Listing</a>';
        }
        if ( in_array( $status, array(A1WooBay_Product_Meta::STATUS_ENDED, A1WooBay_Product_Meta::STATUS_SOLD), true ) ) {
            $row_actions[] = '<a href="#" class="a1wb-row-action" data-action="relist" data-pid="' . esc_attr( $pid ) . '">Re-list</a>';
        }
        if ( $item_id ) {
            $row_actions[] = '<a href="#" class="a1wb-row-action" data-action="resync" data-pid="' . esc_attr( $pid ) . '">Re-sync</a>';
        }
        ?>
								<div style="margin-top:3px;font-size:11px;color:#2271b1;"><?php 
        echo wp_kses_post( implode( ' <span style="color:#ccc;">|</span> ', $row_actions ) );
        ?></div>
							</td>
							<td style="font-size:13px;" data-sort-val="<?php 
        echo esc_attr( ( $price ?: 0 ) );
        ?>"><?php 
        echo ( $price ? wp_kses_post( wc_price( $price ) ) : '--' );
        ?></td>
							<td style="font-size:12px;text-align:center;" data-sort-val="<?php 
        echo esc_attr( ( null !== $qty ? $qty : -1 ) );
        ?>"><?php 
        echo ( null !== $qty ? esc_html( $qty ) : '--' );
        ?></td>
							<td style="font-size:12px;text-align:center;" data-sort-val="<?php 
        echo esc_attr( $sold );
        ?>"><?php 
        echo ( $sold > 0 ? esc_html( $sold ) : '--' );
        ?></td>
							<td class="a1wb-watch-cell" style="font-size:12px;text-align:center;" data-sort-val="<?php 
        echo esc_attr( ( '' !== $watch_count ? $watch_count : -1 ) );
        ?>"><?php 
        echo ( '' !== $watch_count ? esc_html( $watch_count ) : '--' );
        ?></td>
							<td style="font-size:12px;overflow:hidden;white-space:nowrap;text-overflow:ellipsis;">
								<?php 
        if ( $profile ) {
            ?>
									<a href="<?php 
            echo esc_url( add_query_arg( array(
                'page'        => 'a1woobay-profiles',
                'a1wb_action' => 'edit',
                'profile_id'  => $profile_id,
            ), admin_url( 'admin.php' ) ) );
            ?>"><?php 
            echo esc_html( $profile->post_title );
            ?></a>
								<?php 
        } elseif ( in_array( $status, array(A1WooBay_Product_Meta::STATUS_ACTIVE, A1WooBay_Product_Meta::STATUS_VERIFIED), true ) ) {
            ?>
									<a href="<?php 
            echo esc_url( $edit_url );
            ?>" style="color:#a07000;font-weight:600;">&#9888; Profile needed</a>
								<?php 
        } else {
            ?>
									<span style="color:#888;">--</span>
								<?php 
        }
        ?>
							</td>
							<td style="font-size:12px;overflow:hidden;white-space:nowrap;text-overflow:ellipsis;">
								<?php 
        if ( $template ) {
            ?>
									<a href="<?php 
            echo esc_url( add_query_arg( array(
                'page'        => 'a1woobay-templates',
                'a1wb_action' => 'edit',
                'template_id' => $template_id,
            ), admin_url( 'admin.php' ) ) );
            ?>"><?php 
            echo esc_html( $template->post_title );
            ?></a>
								<?php 
        } else {
            ?>
									<span style="color:#888;">--</span>
								<?php 
        }
        ?>
							</td>
							<td style="font-size:12px;overflow:hidden;white-space:nowrap;text-overflow:ellipsis;">
								<?php 
        if ( $item_id ) {
            ?>
									<a href="https://www.ebay.com/itm/<?php 
            echo esc_attr( $item_id );
            ?>" target="_blank" rel="noopener" title="<?php 
            echo esc_attr( $item_id );
            ?>"><?php 
            echo esc_html( $item_id );
            ?></a>
								<?php 
        } else {
            ?>
									<span style="color:#888;">--</span>
								<?php 
        }
        ?>
							</td>
							<td>
								<span class="a1wb-status-badge" style="display:inline-block;padding:3px 8px;border-radius:3px;font-size:11px;font-weight:600;color:#fff;background:<?php 
        echo esc_attr( $color );
        ?>;">
									<?php 
        echo esc_html( strtoupper( $label ) );
        ?>
								</span>
							</td>
						</tr>
					<?php 
    }
    ?>
				</tbody>
			</table>
		<?php 
}
?>
	</form>
</div>

<!-- Bulk progress modal -->
<div id="a1wb-bulk-modal" style="display:none;position:fixed;top:0;left:0;right:0;bottom:0;background:rgba(0,0,0,0.5);z-index:99999;align-items:center;justify-content:center;">
	<div style="background:#fff;border-radius:6px;width:520px;max-width:95vw;max-height:80vh;display:flex;flex-direction:column;box-shadow:0 8px 32px rgba(0,0,0,0.3);">
		<div style="padding:16px 20px;border-bottom:1px solid #eee;display:flex;align-items:center;justify-content:space-between;">
			<strong id="a1wb-modal-title" style="font-size:15px;">Processing...</strong>
			<span id="a1wb-modal-progress" style="font-size:13px;color:#888;"></span>
		</div>
		<div id="a1wb-modal-log" style="flex:1;overflow-y:auto;padding:12px 20px;font-size:13px;font-family:monospace;line-height:1.8;"></div>
		<div style="padding:12px 20px;border-top:1px solid #eee;text-align:right;">
			<button type="button" id="a1wb-modal-close" class="button button-primary" disabled>Done</button>
		</div>
	</div>
</div>


