<?php
/**
 * A1-WooBay - eBay Profiles Admin Page
 *
 * @package A1_WooBay
 * @since   0.3.4
 */

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

$action     = isset( $_GET['a1wb_action'] ) ? sanitize_text_field( wp_unslash( $_GET['a1wb_action'] ) ) : 'list';
$profile_id = isset( $_GET['profile_id'] ) ? absint( wp_unslash( $_GET['profile_id'] ) ) : 0;

// Handle form save
if ( isset( $_SERVER['REQUEST_METHOD'] ) && 'POST' === $_SERVER['REQUEST_METHOD'] && isset( $_POST['a1wb_profiles_nonce'] ) ) {
	if ( wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['a1wb_profiles_nonce'] ) ), 'a1wb_save_profile' ) ) {
		$result = A1WooBay_Profiles::save_profile( array(
			'id'             => absint( wp_unslash( $_POST['profile_id'] ?? 0 ) ),
			'title'          => sanitize_text_field( wp_unslash( $_POST['prf_title']         ?? '' ) ),
			'description'    => sanitize_textarea_field( wp_unslash( $_POST['prf_desc']      ?? '' ) ),
			'listing_type'   => sanitize_text_field( wp_unslash( $_POST['prf_type']          ?? 'FIXED_PRICE' ) ),
			'duration'       => sanitize_text_field( wp_unslash( $_POST['prf_duration']      ?? 'GTC' ) ),
			'condition'      => sanitize_text_field( wp_unslash( $_POST['prf_condition']     ?? '' ) ),
			'condition_desc' => sanitize_textarea_field( wp_unslash( $_POST['prf_cond_desc'] ?? '' ) ),
			'handling_time'  => sanitize_text_field( wp_unslash( $_POST['prf_handling']      ?? '1' ) ),
			'category_id'    => sanitize_text_field( wp_unslash( $_POST['prf_cat_id']        ?? '' ) ),
			'category_name'  => sanitize_text_field( wp_unslash( $_POST['prf_cat_name']      ?? '' ) ),
			'category2_id'   => sanitize_text_field( wp_unslash( $_POST['prf_cat2_id']       ?? '' ) ),
			'category2_name' => sanitize_text_field( wp_unslash( $_POST['prf_cat2_name']     ?? '' ) ),
			'policy_ship'    => sanitize_text_field( wp_unslash( $_POST['prf_policy_ship']   ?? '' ) ),
			'policy_return'  => sanitize_text_field( wp_unslash( $_POST['prf_policy_ret']    ?? '' ) ),
			'policy_pay'     => sanitize_text_field( wp_unslash( $_POST['prf_policy_pay']    ?? '' ) ),
			'location'       => sanitize_text_field( wp_unslash( $_POST['prf_location']      ?? '' ) ),
			'postal_code'    => sanitize_text_field( wp_unslash( $_POST['prf_postal']        ?? '' ) ),
			'country'        => sanitize_text_field( wp_unslash( $_POST['prf_country']       ?? 'US' ) ),
			'currency'       => sanitize_text_field( wp_unslash( $_POST['prf_currency']      ?? 'USD' ) ),
			'price_type'     => sanitize_text_field( wp_unslash( $_POST['prf_price_type']    ?? 'none' ) ),
			'price_value'    => sanitize_text_field( wp_unslash( $_POST['prf_price_value']   ?? '0' ) ),
			'variations'     => sanitize_text_field( wp_unslash( $_POST['prf_variations']    ?? 'single' ) ),
			'free_shipping'  => isset( $_POST['prf_free_ship'] )   ? '1' : '0',
			'global_ship'    => isset( $_POST['prf_global_ship'] ) ? '1' : '0',
			'item_specifics' => json_decode( sanitize_textarea_field( wp_unslash( $_POST['prf_item_specifics'] ?? '[]' ) ), true ) ?: array(),
		) );

		if ( is_wp_error( $result ) ) {
			$save_error = $result->get_error_message();
		} else {
			wp_safe_redirect( add_query_arg( array( 'page' => 'a1woobay-profiles', 'a1wb_msg' => 'saved' ), admin_url( 'admin.php' ) ) );
			exit;
		}
	}
}

// Handle delete
if ( 'delete' === $action && $profile_id && isset( $_GET['_wpnonce'] ) ) {
	if ( wp_verify_nonce( sanitize_text_field( wp_unslash( $_GET['_wpnonce'] ) ), 'a1wb_delete_profile_' . $profile_id ) ) {
		A1WooBay_Profiles::delete_profile( $profile_id );
		wp_safe_redirect( add_query_arg( array( 'page' => 'a1woobay-profiles', 'a1wb_msg' => 'deleted' ), admin_url( 'admin.php' ) ) );
		exit;
	}
}

$editing = in_array( $action, array( 'new', 'edit' ), true );
$prf     = ( 'edit' === $action && $profile_id ) ? A1WooBay_Profiles::get_profile( $profile_id ) : null;
$m       = $prf ? A1WooBay_Profiles::get_meta( $profile_id ) : array();

function a1wb_section( $title ) {
	echo '<h2 style="margin:28px 0 4px;padding-bottom:6px;border-bottom:1px solid #ddd;font-size:14px;text-transform:uppercase;letter-spacing:.05em;color:#555;">' . esc_html( $title ) . '</h2>';
}
?>
<div class="wrap">
	<h1 style="display:flex;align-items:center;gap:12px;">
		<?php esc_html_e( 'A1 Marketplace Sync with eBay for WooCommerce - eBay Profiles', 'a1-marketplace-sync-with-ebay-for-woocommerce' ); ?>
		<?php if ( ! $editing ) : ?>
			<a href="<?php echo esc_url( add_query_arg( array( 'page' => 'a1woobay-profiles', 'a1wb_action' => 'new' ), admin_url( 'admin.php' ) ) ); ?>"
				class="button button-primary"><?php esc_html_e( 'Add New Profile', 'a1-marketplace-sync-with-ebay-for-woocommerce' ); ?></a>
		<?php endif; ?>
	</h1>

	<?php $a1wb_msg = isset( $_GET['a1wb_msg'] ) ? sanitize_text_field( wp_unslash( $_GET['a1wb_msg'] ) ) : ''; ?>
	<?php if ( 'saved' === $a1wb_msg ) : ?><div class="notice notice-success is-dismissible"><p><?php esc_html_e( 'Profile saved.', 'a1-marketplace-sync-with-ebay-for-woocommerce' ); ?></p></div><?php endif; ?>
	<?php if ( 'deleted' === $a1wb_msg ) : ?><div class="notice notice-info is-dismissible"><p><?php esc_html_e( 'Profile deleted.', 'a1-marketplace-sync-with-ebay-for-woocommerce' ); ?></p></div><?php endif; ?>

	<?php if ( $editing ) : ?>
		<?php if ( isset( $save_error ) ) : ?><div class="notice notice-error"><p><?php echo esc_html( $save_error ); ?></p></div><?php endif; ?>

		<form method="post" id="a1wb-profile-form">
			<?php wp_nonce_field( 'a1wb_save_profile', 'a1wb_profiles_nonce' ); ?>
			<input type="hidden" name="profile_id" value="<?php echo esc_attr( $profile_id ); ?>">

			<?php a1wb_section( 'General eBay Settings' ); ?>
			<table class="form-table" role="presentation">
				<tr>
					<th><label for="prf_title"><?php esc_html_e( 'Profile Name', 'a1-marketplace-sync-with-ebay-for-woocommerce' ); ?> *</label></th>
					<td><input type="text" id="prf_title" name="prf_title" class="regular-text" required value="<?php echo $prf ? esc_attr( $prf->post_title ) : ''; ?>"></td>
				</tr>
				<tr>
					<th><label for="prf_desc"><?php esc_html_e( 'Description', 'a1-marketplace-sync-with-ebay-for-woocommerce' ); ?></label></th>
					<td><textarea id="prf_desc" name="prf_desc" rows="2" class="large-text"><?php echo esc_textarea( $m['description'] ?? '' ); ?></textarea></td>
				</tr>
				<tr>
					<th><label for="prf_type"><?php esc_html_e( 'Listing Type', 'a1-marketplace-sync-with-ebay-for-woocommerce' ); ?></label></th>
					<td>
						<select id="prf_type" name="prf_type">
							<option value="FIXED_PRICE" <?php selected( $m['listing_type'] ?? 'FIXED_PRICE', 'FIXED_PRICE' ); ?>><?php esc_html_e( 'Fixed Price', 'a1-marketplace-sync-with-ebay-for-woocommerce' ); ?></option>
						</select>
					</td>
				</tr>
				<tr>
					<th><label for="prf_duration"><?php esc_html_e( 'Duration', 'a1-marketplace-sync-with-ebay-for-woocommerce' ); ?></label></th>
					<td>
						<select id="prf_duration" name="prf_duration">
							<?php foreach ( A1WooBay_Profiles::DURATIONS as $val => $label ) : ?>
								<option value="<?php echo esc_attr( $val ); ?>" <?php selected( $m['duration'] ?? 'GTC', $val ); ?>><?php echo esc_html( $label ); ?></option>
							<?php endforeach; ?>
						</select>
					</td>
				</tr>
				<tr>
					<th><label for="prf_condition"><?php esc_html_e( 'Condition', 'a1-marketplace-sync-with-ebay-for-woocommerce' ); ?></label></th>
					<td>
						<select id="prf_condition" name="prf_condition">
							<?php foreach ( A1WooBay_Profiles::CONDITIONS as $val => $label ) : ?>
								<option value="<?php echo esc_attr( $val ); ?>" <?php selected( $m['condition'] ?? '', $val ); ?>><?php echo esc_html( $label ); ?></option>
							<?php endforeach; ?>
						</select>
					</td>
				</tr>
				<tr>
					<th><label for="prf_cond_desc"><?php esc_html_e( 'Condition Description', 'a1-marketplace-sync-with-ebay-for-woocommerce' ); ?></label></th>
					<td>
						<textarea id="prf_cond_desc" name="prf_cond_desc" rows="2" class="large-text"><?php echo esc_textarea( $m['condition_desc'] ?? '' ); ?></textarea>
					</td>
				</tr>
				<tr>
					<th><label for="prf_handling"><?php esc_html_e( 'Handling Time', 'a1-marketplace-sync-with-ebay-for-woocommerce' ); ?></label></th>
					<td>
						<select id="prf_handling" name="prf_handling">
							<?php foreach ( A1WooBay_Profiles::HANDLING_TIMES as $val => $label ) : ?>
								<option value="<?php echo esc_attr( $val ); ?>" <?php selected( $m['handling_time'] ?? '1', $val ); ?>><?php echo esc_html( $label ); ?></option>
							<?php endforeach; ?>
						</select>
					</td>
				</tr>
			</table>

			<?php a1wb_section( 'eBay Categories' ); ?>
			<table class="form-table" role="presentation">
				<tr>
					<th><?php esc_html_e( 'Primary Category', 'a1-marketplace-sync-with-ebay-for-woocommerce' ); ?></th>
					<td>
						<div style="display:flex;gap:8px;flex-wrap:wrap;margin-bottom:6px;">
							<input type="text" id="prf_cat_id" name="prf_cat_id" style="width:120px;"
								placeholder="<?php esc_attr_e( 'Category ID', 'a1-marketplace-sync-with-ebay-for-woocommerce' ); ?>"
								value="<?php echo esc_attr( $m['category_id'] ?? '' ); ?>">
							<input type="text" id="prf_cat_name" name="prf_cat_name" class="regular-text"
								placeholder="<?php esc_attr_e( 'Category name', 'a1-marketplace-sync-with-ebay-for-woocommerce' ); ?>"
								value="<?php echo esc_attr( $m['category_name'] ?? '' ); ?>">
						</div>
						<div style="display:flex;gap:8px;align-items:center;">
							<input type="text" id="a1wb-cat-search" style="width:200px;" placeholder="<?php esc_attr_e( 'Search eBay categories...', 'a1-marketplace-sync-with-ebay-for-woocommerce' ); ?>">
							<button type="button" id="a1wb-cat-search-btn" class="button"><?php esc_html_e( 'Search eBay', 'a1-marketplace-sync-with-ebay-for-woocommerce' ); ?></button>
						</div>
						<div id="a1wb-cat-results"></div>
					</td>
				</tr>
				<tr>
					<th><?php esc_html_e( 'Secondary Category', 'a1-marketplace-sync-with-ebay-for-woocommerce' ); ?></th>
					<td>
						<div style="display:flex;gap:8px;flex-wrap:wrap;margin-bottom:6px;">
							<input type="text" id="prf_cat2_id" name="prf_cat2_id" style="width:120px;"
								placeholder="<?php esc_attr_e( 'Category ID', 'a1-marketplace-sync-with-ebay-for-woocommerce' ); ?>"
								value="<?php echo esc_attr( $m['category2_id'] ?? '' ); ?>">
							<input type="text" id="prf_cat2_name" name="prf_cat2_name" class="regular-text"
								placeholder="<?php esc_attr_e( 'Category name', 'a1-marketplace-sync-with-ebay-for-woocommerce' ); ?>"
								value="<?php echo esc_attr( $m['category2_name'] ?? '' ); ?>">
						</div>
						<div style="display:flex;gap:8px;align-items:center;">
							<input type="text" id="a1wb-cat2-search" style="width:200px;" placeholder="<?php esc_attr_e( 'Search eBay categories...', 'a1-marketplace-sync-with-ebay-for-woocommerce' ); ?>">
							<button type="button" id="a1wb-cat2-search-btn" class="button"><?php esc_html_e( 'Search eBay', 'a1-marketplace-sync-with-ebay-for-woocommerce' ); ?></button>
						</div>
						<div id="a1wb-cat2-results"></div>
					</td>
				</tr>
			</table>

			<?php a1wb_section( 'Business Policies' ); ?>
			<p>
				<button type="button" id="a1wb-load-policies" class="button">
					&#x1F504; <?php esc_html_e( 'Load Policies from eBay', 'a1-marketplace-sync-with-ebay-for-woocommerce' ); ?>
				</button>
				<span id="a1wb-policy-status" style="margin-left:10px;font-size:12px;"></span>
			</p>
			<table class="form-table" role="presentation">
				<tr>
					<th><label for="prf_policy_ship"><?php esc_html_e( 'Fulfillment (Shipping) Policy', 'a1-marketplace-sync-with-ebay-for-woocommerce' ); ?></label></th>
					<td>
						<input type="text" id="prf_policy_ship" name="prf_policy_ship" class="regular-text"
							placeholder="<?php esc_attr_e( 'Policy ID - click Load to browse', 'a1-marketplace-sync-with-ebay-for-woocommerce' ); ?>"
							value="<?php echo esc_attr( $m['policy_ship'] ?? '' ); ?>">
					</td>
				</tr>
				<tr>
					<th><label for="prf_policy_ret"><?php esc_html_e( 'Return Policy', 'a1-marketplace-sync-with-ebay-for-woocommerce' ); ?></label></th>
					<td>
						<input type="text" id="prf_policy_ret" name="prf_policy_ret" class="regular-text"
							placeholder="<?php esc_attr_e( 'Policy ID - click Load to browse', 'a1-marketplace-sync-with-ebay-for-woocommerce' ); ?>"
							value="<?php echo esc_attr( $m['policy_return'] ?? '' ); ?>">
					</td>
				</tr>
				<tr>
					<th><label for="prf_policy_pay"><?php esc_html_e( 'Payment Policy', 'a1-marketplace-sync-with-ebay-for-woocommerce' ); ?></label></th>
					<td>
						<input type="text" id="prf_policy_pay" name="prf_policy_pay" class="regular-text"
							placeholder="<?php esc_attr_e( 'Policy ID - click Load to browse', 'a1-marketplace-sync-with-ebay-for-woocommerce' ); ?>"
							value="<?php echo esc_attr( $m['policy_pay'] ?? '' ); ?>">
					</td>
				</tr>
				<tr>
					<th><?php esc_html_e( 'Free Shipping', 'a1-marketplace-sync-with-ebay-for-woocommerce' ); ?></th>
					<td><label><input type="checkbox" name="prf_free_ship" value="1" <?php checked( $m['free_shipping'] ?? '0', '1' ); ?>> <?php esc_html_e( 'Enable free shipping', 'a1-marketplace-sync-with-ebay-for-woocommerce' ); ?></label></td>
				</tr>
				<tr>
					<th><?php esc_html_e( 'Global Shipping', 'a1-marketplace-sync-with-ebay-for-woocommerce' ); ?></th>
					<td><label><input type="checkbox" name="prf_global_ship" value="1" <?php checked( $m['global_ship'] ?? '0', '1' ); ?>> <?php esc_html_e( 'Use eBay Global Shipping Program', 'a1-marketplace-sync-with-ebay-for-woocommerce' ); ?></label></td>
				</tr>
			</table>

			<?php a1wb_section( 'Location and Currency' ); ?>
			<table class="form-table" role="presentation">
				<tr>
					<th><label for="prf_location"><?php esc_html_e( 'Item Location', 'a1-marketplace-sync-with-ebay-for-woocommerce' ); ?></label></th>
					<td><input type="text" id="prf_location" name="prf_location" class="regular-text"
						placeholder="<?php esc_attr_e( 'e.g. Leander, TX', 'a1-marketplace-sync-with-ebay-for-woocommerce' ); ?>"
						value="<?php echo esc_attr( $m['location'] ?? '' ); ?>"></td>
				</tr>
				<tr>
					<th><label for="prf_postal"><?php esc_html_e( 'Postal Code', 'a1-marketplace-sync-with-ebay-for-woocommerce' ); ?></label></th>
					<td><input type="text" id="prf_postal" name="prf_postal" style="width:120px;" value="<?php echo esc_attr( $m['postal_code'] ?? '' ); ?>"></td>
				</tr>
				<tr>
					<th><label for="prf_country"><?php esc_html_e( 'Country', 'a1-marketplace-sync-with-ebay-for-woocommerce' ); ?></label></th>
					<td>
						<select id="prf_country" name="prf_country">
							<?php foreach ( A1WooBay_Profiles::COUNTRIES as $code => $name ) : ?>
								<option value="<?php echo esc_attr( $code ); ?>" <?php selected( $m['country'] ?? 'US', $code ); ?>><?php echo esc_html( $name ); ?></option>
							<?php endforeach; ?>
						</select>
					</td>
				</tr>
				<tr>
					<th><label for="prf_currency"><?php esc_html_e( 'Currency', 'a1-marketplace-sync-with-ebay-for-woocommerce' ); ?></label></th>
					<td>
						<select id="prf_currency" name="prf_currency">
							<option value="USD" <?php selected( $m['currency'] ?? 'USD', 'USD' ); ?>>USD</option>
							<option value="CAD" <?php selected( $m['currency'] ?? 'USD', 'CAD' ); ?>>CAD</option>
							<option value="GBP" <?php selected( $m['currency'] ?? 'USD', 'GBP' ); ?>>GBP</option>
							<option value="AUD" <?php selected( $m['currency'] ?? 'USD', 'AUD' ); ?>>AUD</option>
						</select>
					</td>
				</tr>
			</table>

			<?php a1wb_section( 'Price' ); ?>
			<table class="form-table" role="presentation">
				<tr>
					<th><label for="prf_price_type"><?php esc_html_e( 'Price Setting', 'a1-marketplace-sync-with-ebay-for-woocommerce' ); ?></label></th>
					<td>
						<select id="prf_price_type" name="prf_price_type">
							<?php foreach ( A1WooBay_Profiles::PRICE_TYPES as $val => $label ) : ?>
								<option value="<?php echo esc_attr( $val ); ?>" <?php selected( $m['price_type'] ?? 'none', $val ); ?>><?php echo esc_html( $label ); ?></option>
							<?php endforeach; ?>
						</select>
					</td>
				</tr>
				<tr id="a1wb-price-value-row" style="<?php echo ! in_array( $m['price_type'] ?? 'none', array( 'percent', 'fixed' ), true ) ? 'display:none;' : ''; ?>">
					<th><label for="prf_price_value"><?php esc_html_e( 'Price Value', 'a1-marketplace-sync-with-ebay-for-woocommerce' ); ?></label></th>
					<td>
						<input type="text" id="prf_price_value" name="prf_price_value" style="width:120px;"
							value="<?php echo esc_attr( $m['price_value'] ?? '0' ); ?>">
						<p class="description"><?php esc_html_e( 'Percent: 10 = +10%, -5 = -5%. Fixed: enter exact price.', 'a1-marketplace-sync-with-ebay-for-woocommerce' ); ?></p>
					</td>
				</tr>
			</table>

			<?php a1wb_section( 'Item Specifics' ); ?>
			<p class="description"><?php esc_html_e( 'Add key-value pairs that appear on your eBay listing. Use "WC Attribute" to pull the value from a WooCommerce product attribute at publish time.', 'a1-marketplace-sync-with-ebay-for-woocommerce' ); ?></p>
			<table id="a1wb-specifics-table" style="width:100%;border-collapse:collapse;margin:8px 0;">
				<thead>
					<tr style="border-bottom:1px solid #ddd;">
						<th style="text-align:left;padding:6px 8px;width:28%;"><?php esc_html_e( 'Name', 'a1-marketplace-sync-with-ebay-for-woocommerce' ); ?></th>
						<th style="text-align:left;padding:6px 8px;width:22%;"><?php esc_html_e( 'Source', 'a1-marketplace-sync-with-ebay-for-woocommerce' ); ?></th>
						<th style="text-align:left;padding:6px 8px;"><?php esc_html_e( 'Value / Attribute', 'a1-marketplace-sync-with-ebay-for-woocommerce' ); ?></th>
						<th style="width:36px;"></th>
					</tr>
				</thead>
				<tbody id="a1wb-specifics-rows">
					<?php foreach ( ( $m['item_specifics'] ?? array() ) as $spec ) : ?>
					<tr class="a1wb-spec-row" style="border-bottom:1px solid #eee;">
						<td style="padding:4px 8px;"><input type="text" class="a1wb-spec-name" style="width:100%;" placeholder="e.g. Brand" value="<?php echo esc_attr( $spec['name'] ?? '' ); ?>"></td>
						<td style="padding:4px 8px;">
							<select class="a1wb-spec-source" style="width:100%;">
								<option value="custom" <?php selected( $spec['source'] ?? 'custom', 'custom' ); ?>><?php esc_html_e( 'Custom Value', 'a1-marketplace-sync-with-ebay-for-woocommerce' ); ?></option>
								<option value="attribute" <?php selected( $spec['source'] ?? 'custom', 'attribute' ); ?>><?php esc_html_e( 'WC Attribute', 'a1-marketplace-sync-with-ebay-for-woocommerce' ); ?></option>
							</select>
						</td>
						<td style="padding:4px 8px;"><input type="text" class="a1wb-spec-value" style="width:100%;" placeholder="Value or attribute (e.g. pa_brand)" value="<?php echo esc_attr( $spec['value'] ?? '' ); ?>"></td>
						<td style="padding:4px 8px;text-align:center;"><button type="button" class="a1wb-remove-spec button-link" style="color:#dc3232;">&times;</button></td>
					</tr>
					<?php endforeach; ?>
				</tbody>
			</table>
			<button type="button" id="a1wb-add-spec" class="button">+ <?php esc_html_e( 'Add Item Specific', 'a1-marketplace-sync-with-ebay-for-woocommerce' ); ?></button>
			<input type="hidden" name="prf_item_specifics" id="prf_item_specifics" value="">

			<?php a1wb_section( 'Variations' ); ?>
			<table class="form-table" role="presentation">
				<tr>
					<th><label for="prf_variations"><?php esc_html_e( 'Variation Mode', 'a1-marketplace-sync-with-ebay-for-woocommerce' ); ?></label></th>
					<td>
						<select id="prf_variations" name="prf_variations">
							<?php foreach ( A1WooBay_Profiles::VARIATION_MODES as $val => $label ) : ?>
								<option value="<?php echo esc_attr( $val ); ?>" <?php selected( $m['variations'] ?? 'single', $val ); ?>><?php echo esc_html( $label ); ?></option>
							<?php endforeach; ?>
						</select>
					</td>
				</tr>
			</table>

			<p style="margin-top:24px;">
				<?php submit_button( $prf ? __( 'Update Profile', 'a1-marketplace-sync-with-ebay-for-woocommerce' ) : __( 'Save Profile', 'a1-marketplace-sync-with-ebay-for-woocommerce' ), 'primary', 'submit', false ); ?>
				&nbsp;
				<a href="<?php echo esc_url( add_query_arg( 'page', 'a1woobay-profiles', admin_url( 'admin.php' ) ) ); ?>" class="button"><?php esc_html_e( 'Cancel', 'a1-marketplace-sync-with-ebay-for-woocommerce' ); ?></a>
			</p>
		</form>

	<?php else : ?>

		<?php $profiles = A1WooBay_Profiles::get_all(); ?>
		<?php if ( empty( $profiles ) ) : ?>
			<p><?php esc_html_e( 'No profiles yet. Click "Add New Profile" to create your first one.', 'a1-marketplace-sync-with-ebay-for-woocommerce' ); ?></p>
		<?php else : ?>
			<table class="wp-list-table widefat fixed striped">
				<thead>
					<tr>
						<th><?php esc_html_e( 'Profile Name', 'a1-marketplace-sync-with-ebay-for-woocommerce' ); ?></th>
						<th><?php esc_html_e( 'Duration', 'a1-marketplace-sync-with-ebay-for-woocommerce' ); ?></th>
						<th><?php esc_html_e( 'Condition', 'a1-marketplace-sync-with-ebay-for-woocommerce' ); ?></th>
						<th><?php esc_html_e( 'Primary Category', 'a1-marketplace-sync-with-ebay-for-woocommerce' ); ?></th>
						<th><?php esc_html_e( 'Actions', 'a1-marketplace-sync-with-ebay-for-woocommerce' ); ?></th>
					</tr>
				</thead>
				<tbody>
					<?php foreach ( $profiles as $p ) : ?>
						<?php $pm = A1WooBay_Profiles::get_meta( $p->ID ); ?>
						<tr>
							<td>
								<strong><a href="<?php echo esc_url( add_query_arg( array( 'page' => 'a1woobay-profiles', 'a1wb_action' => 'edit', 'profile_id' => $p->ID ), admin_url( 'admin.php' ) ) ); ?>"><?php echo esc_html( $p->post_title ); ?></a></strong>
								<?php if ( $pm['description'] ) : ?><br><small style="color:#888;"><?php echo esc_html( $pm['description'] ); ?></small><?php endif; ?>
							</td>
							<td><?php echo esc_html( A1WooBay_Profiles::DURATIONS[ $pm['duration'] ] ?? $pm['duration'] ); ?></td>
							<td><?php echo esc_html( A1WooBay_Profiles::CONDITIONS[ $pm['condition'] ] ?? '--' ); ?></td>
							<td>
								<?php echo $pm['category_name'] ? esc_html( $pm['category_name'] ) . ( $pm['category_id'] ? '<br><small style="color:#888;">' . esc_html( $pm['category_id'] ) . '</small>' : '' ) : '<span style="color:#888;">--</span>'; ?>
							</td>
							<td>
								<a href="<?php echo esc_url( add_query_arg( array( 'page' => 'a1woobay-profiles', 'a1wb_action' => 'edit', 'profile_id' => $p->ID ), admin_url( 'admin.php' ) ) ); ?>"><?php esc_html_e( 'Edit', 'a1-marketplace-sync-with-ebay-for-woocommerce' ); ?></a>
								&nbsp;|&nbsp;
								<a href="<?php echo esc_url( wp_nonce_url( add_query_arg( array( 'page' => 'a1woobay-profiles', 'a1wb_action' => 'delete', 'profile_id' => $p->ID ), admin_url( 'admin.php' ) ), 'a1wb_delete_profile_' . $p->ID ) ); ?>"
									style="color:#dc3232;"
									onclick="return confirm('<?php esc_attr_e( 'Delete this profile?', 'a1-marketplace-sync-with-ebay-for-woocommerce' ); ?>');">
									<?php esc_html_e( 'Delete', 'a1-marketplace-sync-with-ebay-for-woocommerce' ); ?>
								</a>
							</td>
						</tr>
					<?php endforeach; ?>
				</tbody>
			</table>
		<?php endif; ?>

	<?php endif; ?>
</div>
