<?php
/**
 * Admin.
 *
 * @package Noesia_Commerce_Suite
 */

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

// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- View templates receive local variables from their renderer.
$settings             = isset( $settings ) && is_array( $settings ) ? $settings : array();
$passport_rows        = isset( $passport_rows ) && is_array( $passport_rows ) ? $passport_rows : array();
$qr_settings          = isset( $qr_settings ) && is_array( $qr_settings ) ? $qr_settings : array();
$hub_status           = isset( $hub_status ) && is_array( $hub_status ) ? $hub_status : array();
$smart_links          = isset( $smart_links ) && is_array( $smart_links ) ? $smart_links : array();
$latest_live_hub_url  = '';
foreach ( $passport_rows as $preview_row ) {
	if ( empty( $preview_row['has_token'] ) || empty( $preview_row['review_ready'] ) || empty( $preview_row['open_url'] ) ) {
		continue;
	}
	$latest_live_hub_url = $preview_row['open_url'];
	break;
}
if ( ! $latest_live_hub_url ) {
	foreach ( $passport_rows as $preview_row ) {
		if ( empty( $preview_row['has_token'] ) || empty( $preview_row['open_url'] ) ) {
			continue;
		}
		$latest_live_hub_url = $preview_row['open_url'];
		break;
	}
}
$active_section = isset( $_GET['noesia_cs_hub_section'] ) ? sanitize_key( wp_unslash( $_GET['noesia_cs_hub_section'] ) ) : 'overview'; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
$sections = array(
	'overview'  => __( 'Overview', 'noesia-commerce-suite' ),
	'core'      => __( 'Core Actions', 'noesia-commerce-suite' ),
	'smart'     => __( 'Smart Links', 'noesia-commerce-suite' ),
	'access'    => __( 'Access & QR', 'noesia-commerce-suite' ),
	'design'    => __( 'Design & Display', 'noesia-commerce-suite' ),
	'security'  => __( 'Security & Visibility', 'noesia-commerce-suite' ),
);
if ( ! isset( $sections[ $active_section ] ) ) {
	$active_section = 'overview';
}

$hub_url = function ( $section ) use ( $admin ) {
	return add_query_arg( 'noesia_cs_hub_section', sanitize_key( $section ), $admin->tab_url( 'order-care-hub' ) );
};

$render_status = function ( $label, $active, $detail = '' ) {
	?>
	<div class="noesia-cs-hub-status-card <?php echo $active ? 'is-on' : 'is-off'; ?>">
		<span><?php echo esc_html( $label ); ?></span>
		<strong><?php echo $active ? esc_html__( 'On', 'noesia-commerce-suite' ) : esc_html__( 'Off', 'noesia-commerce-suite' ); ?></strong>
		<?php if ( $detail ) : ?><small><?php echo esc_html( $detail ); ?></small><?php endif; ?>
	</div>
	<?php
};

$render_hub_card = function ( $icon, $title, $description, $fields, $tone = 'default', $badge = '' ) use ( $admin, $settings ) {
	$tone = sanitize_html_class( $tone );
	?>
	<section class="noesia-cs-hub-smart-card noesia-cs-hub-smart-card--<?php echo esc_attr( $tone ); ?>">
		<div class="noesia-cs-hub-smart-card__head">
			<span class="noesia-cs-hub-smart-card__icon" aria-hidden="true"><?php echo esc_html( $icon ); ?></span>
			<div>
				<h3><?php echo esc_html( $title ); ?></h3>
				<p><?php echo esc_html( $description ); ?></p>
			</div>
			<?php if ( $badge ) : ?><em class="is-active"><?php echo esc_html( $badge ); ?></em><?php endif; ?>
		</div>
		<div class="noesia-cs-hub-smart-card__fields">
			<?php foreach ( $fields as $field ) : ?>
				<?php $admin->render_field( $field, $settings[ $field ] ?? '' ); ?>
			<?php endforeach; ?>
		</div>
	</section>
	<?php
};

$render_hub_cards_form = function ( $title, $eyebrow, $description, $cards, $section_key, $button_text = '' ) use ( $render_hub_card ) {
	?>
	<form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>" class="noesia-cs-settings-form noesia-cs-hub-form noesia-cs-hub-smart-form">
		<?php wp_nonce_field( 'noesia_cs_save_settings' ); ?>
		<input type="hidden" name="action" value="noesia_cs_save_settings">
		<input type="hidden" name="noesia_cs_tab" value="order-care-hub">
		<input type="hidden" name="noesia_cs_hub_section" value="<?php echo esc_attr( sanitize_key( $section_key ) ); ?>">
		<section class="noesia-cs-panel noesia-cs-hub-smart-intro">
			<div class="noesia-cs-panel__head">
				<h2><?php echo esc_html( $title ); ?></h2>
				<span><?php echo esc_html( $eyebrow ); ?></span>
			</div>
			<p><?php echo esc_html( $description ); ?></p>
		</section>
		<div class="noesia-cs-hub-smart-grid">
			<?php foreach ( $cards as $card ) : ?>
				<?php
				$render_hub_card(
					$card['icon'] ?? '●',
					$card['title'] ?? '',
					$card['description'] ?? '',
					$card['fields'] ?? array(),
					$card['tone'] ?? 'default',
					$card['badge'] ?? ''
				);
				?>
			<?php endforeach; ?>
		</div>
		<div class="noesia-cs-sticky-save"><button class="noesia-cs-primary-button" type="submit"><?php echo esc_html( $button_text ? $button_text : __( 'Save Order Care Hub Settings', 'noesia-commerce-suite' ) ); ?></button></div>
	</form>
	<?php
};


$hub_preview_style         = in_array( $settings['order_care_public_style'] ?? 'clean', array( 'clean', 'compact', 'premium', 'enterprise' ), true ) ? $settings['order_care_public_style'] : 'clean';
$hub_preview_button_style  = in_array( $settings['order_care_button_style'] ?? 'solid', array( 'solid', 'soft', 'outline' ), true ) ? $settings['order_care_button_style'] : 'solid';
$hub_preview_width         = in_array( $settings['order_care_content_width'] ?? 'medium', array( 'small', 'medium', 'full' ), true ) ? $settings['order_care_content_width'] : 'medium';
$hub_preview_title         = ! empty( $settings['care_page_title_text'] ) ? $settings['care_page_title_text'] : __( 'Order Care Hub', 'noesia-commerce-suite' );
$hub_preview_intro         = ! empty( $settings['care_page_intro_text'] ) ? $settings['care_page_intro_text'] : __( 'Your secure post-purchase space for reviews, reorders and support.', 'noesia-commerce-suite' );
$hub_preview_logo          = ! empty( $settings['company_logo'] ) ? esc_url( $settings['company_logo'] ) : '';
$hub_preview_review_text   = ! empty( $settings['review_intro_text'] ) ? $settings['review_intro_text'] : __( 'Share your experience with this purchase.', 'noesia-commerce-suite' );
$hub_preview_support_text  = ! empty( $settings['support_intro_text'] ) ? $settings['support_intro_text'] : __( 'Contact the store team about this order.', 'noesia-commerce-suite' );
$hub_preview_smart_links   = array();
if ( ! empty( $smart_links['warranty'] ) ) {
	$hub_preview_smart_links[] = array(
		'icon'   => '🛡',
		'title'  => $settings['order_care_warranty_title'] ?? __( 'Warranty & Returns', 'noesia-commerce-suite' ),
		'text'   => $settings['order_care_warranty_text'] ?? '',
	);
}
if ( ! empty( $smart_links['documents'] ) ) {
	$hub_preview_smart_links[] = array(
		'icon'   => '📄',
		'title'  => $settings['order_care_documents_title'] ?? __( 'Documents & Manuals', 'noesia-commerce-suite' ),
		'text'   => $settings['order_care_documents_text'] ?? '',
	);
}
if ( ! empty( $smart_links['video'] ) ) {
	$hub_preview_smart_links[] = array(
		'icon'   => '▶',
		'title'  => $settings['order_care_video_title'] ?? __( 'Video Guides', 'noesia-commerce-suite' ),
		'text'   => $settings['order_care_video_text'] ?? '',
	);
}
if ( ! empty( $smart_links['promo'] ) ) {
	$hub_preview_smart_links[] = array(
		'icon'   => '🎁',
		'title'  => $settings['order_care_promo_title'] ?? __( 'Thank you offer', 'noesia-commerce-suite' ),
		'text'   => $settings['order_care_promo_text'] ?? '',
	);
}

$render_smart_link_card = function ( $icon, $title, $description, $fields, $status_label, $status_active, $tone ) use ( $admin, $settings ) {
	$tone = sanitize_html_class( $tone );
	?>
	<section class="noesia-cs-hub-smart-card noesia-cs-hub-smart-card--<?php echo esc_attr( $tone ); ?>">
		<div class="noesia-cs-hub-smart-card__head">
			<span class="noesia-cs-hub-smart-card__icon" aria-hidden="true"><?php echo esc_html( $icon ); ?></span>
			<div>
				<h3><?php echo esc_html( $title ); ?></h3>
				<p><?php echo esc_html( $description ); ?></p>
			</div>
			<em class="<?php echo $status_active ? 'is-active' : 'is-hidden'; ?>"><?php echo esc_html( $status_label ); ?></em>
		</div>
		<div class="noesia-cs-hub-smart-card__fields">
			<?php foreach ( $fields as $field ) : ?>
				<?php $admin->render_field( $field, $settings[ $field ] ?? '' ); ?>
			<?php endforeach; ?>
		</div>
	</section>
	<?php
};
?>
<section class="noesia-cs-feature-hero noesia-cs-feature-hero--order-care-hub">
	<div>
		<span><?php esc_html_e( 'Customer Experience', 'noesia-commerce-suite' ); ?></span>
		<h2><?php esc_html_e( 'Order Care Hub', 'noesia-commerce-suite' ); ?></h2>
		<p><?php esc_html_e( 'A branded post-purchase customer hub with review, reorder and support actions, plus clean global links for warranty, manuals, videos and offers.', 'noesia-commerce-suite' ); ?></p>
	</div>
	<div class="noesia-cs-hero-actions">
		<a class="noesia-cs-primary-button noesia-cs-hub-preview-jump" href="<?php echo esc_url( $hub_url( 'overview' ) ); ?>#noesia-cs-hub-preview" data-noesia-cs-no-ajax="1"><?php esc_html_e( 'Preview Customer Hub', 'noesia-commerce-suite' ); ?></a>
		<?php if ( $latest_live_hub_url ) : ?><a class="noesia-cs-secondary-button noesia-cs-hub-live-link" href="<?php echo esc_url( $latest_live_hub_url ); ?>" target="_blank" rel="noopener noreferrer nofollow"><?php esc_html_e( 'Open Latest Live Hub', 'noesia-commerce-suite' ); ?></a><?php endif; ?>
		<div class="noesia-cs-hero-orb">HUB</div>
	</div>
</section>

<nav class="noesia-cs-hub-subtabs" aria-label="<?php esc_attr_e( 'Order Care Hub sections', 'noesia-commerce-suite' ); ?>">
	<?php foreach ( $sections as $section_key => $section_label ) : ?>
		<a class="<?php echo $active_section === $section_key ? 'is-active' : ''; ?>" href="<?php echo esc_url( $hub_url( $section_key ) ); ?>" data-noesia-cs-no-ajax="1"><?php echo esc_html( $section_label ); ?></a>
	<?php endforeach; ?>
</nav>

<?php if ( 'overview' === $active_section ) : ?>
	<section class="noesia-cs-hub-overview-grid">
		<div class="noesia-cs-panel noesia-cs-panel--wide noesia-cs-hub-status-panel">
			<div class="noesia-cs-panel__head"><h2><?php esc_html_e( 'Hub Status', 'noesia-commerce-suite' ); ?></h2><span><?php esc_html_e( 'current setup', 'noesia-commerce-suite' ); ?></span></div>
			<p class="noesia-cs-panel__body-note"><?php esc_html_e( 'The Order Care Hub is the single public customer experience. QR codes, emails and WooCommerce placements are access methods, not separate products.', 'noesia-commerce-suite' ); ?></p>
			<div class="noesia-cs-hub-status-grid">
				<?php $render_status( __( 'Order Care Hub', 'noesia-commerce-suite' ), ! empty( $hub_status['enabled'] ) ); ?>
				<?php $render_status( __( 'Thank You Access', 'noesia-commerce-suite' ), ! empty( $hub_status['thankyou'] ) ); ?>
				<?php $render_status( __( 'Email Access', 'noesia-commerce-suite' ), ! empty( $hub_status['email'] ) ); ?>
				<?php $render_status( __( 'My Account Access', 'noesia-commerce-suite' ), ! empty( $hub_status['my_account'] ) ); ?>
				<?php $render_status( __( 'Package QR', 'noesia-commerce-suite' ), ! empty( $hub_status['package_qr'] ) ); ?>
				<div class="noesia-cs-hub-status-card is-info"><span><?php esc_html_e( 'Active Smart Links', 'noesia-commerce-suite' ); ?></span><strong><?php echo esc_html( number_format_i18n( absint( $hub_status['smart_links'] ?? 0 ) ) ); ?></strong><small><?php esc_html_e( 'global links configured', 'noesia-commerce-suite' ); ?></small></div>
			</div>
		</div>

		<div class="noesia-cs-panel noesia-cs-hub-preview-panel" id="noesia-cs-hub-preview">
			<div class="noesia-cs-panel__head"><h2><?php esc_html_e( 'Public UI Preview', 'noesia-commerce-suite' ); ?></h2><span><?php esc_html_e( 'current settings', 'noesia-commerce-suite' ); ?></span></div><?php if ( $latest_live_hub_url ) : ?><p class="noesia-cs-panel__body-note"><?php esc_html_e( 'Use Open Latest Live Hub to view a real customer hub from a recent order. The preview below mirrors your saved design settings with safe sample order data.', 'noesia-commerce-suite' ); ?></p><?php else : ?><p class="noesia-cs-panel__body-note"><?php esc_html_e( 'The preview below mirrors your saved design settings with safe sample order data. Generate a hub link from Access & QR to open a real order hub.', 'noesia-commerce-suite' ); ?></p><?php endif; ?>
			<div class="noesia-cs-hub-live-preview noesia-cs-hub-live-preview--<?php echo esc_attr( $hub_preview_style ); ?> noesia-cs-hub-live-preview-buttons--<?php echo esc_attr( $hub_preview_button_style ); ?> noesia-cs-hub-live-preview-width--<?php echo esc_attr( $hub_preview_width ); ?>">
				<section class="noesia-cs-hub-live-preview__hero">
					<div class="noesia-cs-hub-live-preview__brand">
						<?php if ( $hub_preview_logo ) : ?><img src="<?php echo esc_url( $hub_preview_logo ); ?>" alt=""><?php else : ?><span>N</span><?php endif; ?>
						<div><em><?php esc_html_e( 'Secure after-sales hub', 'noesia-commerce-suite' ); ?></em><strong><?php echo esc_html( $hub_preview_title ); ?></strong><small><?php echo esc_html( $hub_preview_intro ); ?></small></div>
					</div>
					<div class="noesia-cs-hub-live-preview__badges"><b><?php esc_html_e( 'Order #12345', 'noesia-commerce-suite' ); ?></b><i><?php echo esc_html( $settings['verified_purchase_text'] ?? __( 'Verified Purchase', 'noesia-commerce-suite' ) ); ?></i></div>
				</section>
				<section class="noesia-cs-hub-live-preview__product"><span>▣</span><div><em><?php esc_html_e( 'Purchased item', 'noesia-commerce-suite' ); ?></em><strong><?php esc_html_e( 'Example product', 'noesia-commerce-suite' ); ?></strong><small><?php esc_html_e( 'Quantity: 1', 'noesia-commerce-suite' ); ?></small></div></section>
				<section class="noesia-cs-hub-live-preview__actions">
					<div class="is-review"><b>★</b><strong><?php echo esc_html( $settings['review_button_label'] ?? __( 'Leave a Review', 'noesia-commerce-suite' ) ); ?></strong><small><?php echo esc_html( $hub_preview_review_text ); ?></small></div>
					<div class="is-reorder"><b>↻</b><strong><?php esc_html_e( 'Order Again', 'noesia-commerce-suite' ); ?></strong><small><?php esc_html_e( 'Buy this item again without searching for it.', 'noesia-commerce-suite' ); ?></small></div>
					<div class="is-support"><b>?</b><strong><?php echo esc_html( $settings['support_button_label'] ?? __( 'Need Help?', 'noesia-commerce-suite' ) ); ?></strong><small><?php echo esc_html( $hub_preview_support_text ); ?></small></div>
				</section>
				<?php if ( ! empty( $hub_preview_smart_links ) ) : ?>
					<section class="noesia-cs-hub-live-preview__smart">
						<?php foreach ( $hub_preview_smart_links as $preview_link ) : ?>
							<div><b><?php echo esc_html( $preview_link['icon'] ); ?></b><strong><?php echo esc_html( $preview_link['title'] ); ?></strong><?php if ( ! empty( $preview_link['text'] ) ) : ?><small><?php echo esc_html( $preview_link['text'] ); ?></small><?php endif; ?></div>
						<?php endforeach; ?>
					</section>
				<?php endif; ?>
			</div>
		</div>
	</section>
<?php elseif ( 'core' === $active_section ) : ?>
	<?php
	$render_hub_cards_form(
		__( 'Core Actions', 'noesia-commerce-suite' ),
		__( 'customer actions', 'noesia-commerce-suite' ),
		__( 'These controls change the three customer-facing boxes inside the public Order Care Hub. Leave a Review appears when at least one review URL is set and the order is not cancelled, refunded or failed.', 'noesia-commerce-suite' ),
		array(
			array(
				'icon'         => '⭐',
				'title'        => __( 'Leave a Review', 'noesia-commerce-suite' ),
				'description'  => __( 'Controls the public Leave a Review box. Add at least one review URL; otherwise the box stays hidden. The box is hidden only for cancelled, refunded or failed orders to avoid asking unhappy customers for public reviews.', 'noesia-commerce-suite' ),
				'fields'       => array( 'enable_review_block', 'review_button_label', 'review_intro_text', 'google_review_url', 'trustpilot_url', 'store_review_url' ),
				'tone'         => 'promo',
				'badge'        => __( 'Core', 'noesia-commerce-suite' ),
			),
			array(
				'icon'         => '🛍',
				'title'        => __( 'Order Again', 'noesia-commerce-suite' ),
				'description'  => __( 'Controls the public Order Again box. Single-item orders show one large reorder action; multi-item orders show a product list with one Reorder button per item.', 'noesia-commerce-suite' ),
				'fields'       => array( 'show_reorder_button', 'reorder_button_behavior' ),
				'tone'         => 'documents',
				'badge'        => __( 'Core', 'noesia-commerce-suite' ),
			),
			array(
				'icon'         => '💬',
				'title'        => __( 'Need Help?', 'noesia-commerce-suite' ),
				'description'  => __( 'Controls the public Need Help box. It can open a support email with the order number prefilled, or send the customer to a support form URL.', 'noesia-commerce-suite' ),
				'fields'       => array( 'enable_support_block', 'support_button_label', 'support_intro_text', 'support_action_type', 'support_email', 'support_url' ),
				'tone'         => 'warranty',
				'badge'        => __( 'Core', 'noesia-commerce-suite' ),
			),
			array(
				'icon'         => '◎',
				'title'        => __( 'Care Activity', 'noesia-commerce-suite' ),
				'description'  => __( 'Records private admin statistics when customers click Review, Reorder or Need Help in the public hub. It does not expose customer data publicly.', 'noesia-commerce-suite' ),
				'fields'       => array( 'care_action_tracking_enabled' ),
				'tone'         => 'video',
			),
		),
		'core',
		__( 'Save Core Actions', 'noesia-commerce-suite' )
	);
	?>
<?php elseif ( 'smart' === $active_section ) : ?>
	<form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>" class="noesia-cs-settings-form noesia-cs-hub-form noesia-cs-hub-smart-form">
		<?php wp_nonce_field( 'noesia_cs_save_settings' ); ?>
		<input type="hidden" name="action" value="noesia_cs_save_settings">
		<input type="hidden" name="noesia_cs_tab" value="order-care-hub">
		<input type="hidden" name="noesia_cs_hub_section" value="smart">
		<section class="noesia-cs-panel noesia-cs-hub-smart-intro">
			<div class="noesia-cs-panel__head">
				<h2><?php esc_html_e( 'Smart Links', 'noesia-commerce-suite' ); ?></h2>
				<span><?php esc_html_e( 'global blocks', 'noesia-commerce-suite' ); ?></span>
			</div>
			<p><?php esc_html_e( 'Add simple store-level links that appear below the three main customer actions. Link to your own warranty, manuals, documents, video or offer pages. Disabled or empty blocks are hidden automatically.', 'noesia-commerce-suite' ); ?></p>
		</section>
		<div class="noesia-cs-hub-smart-grid">
			<?php
			$render_smart_link_card(
				'🛡',
				__( 'Warranty & Returns', 'noesia-commerce-suite' ),
				__( 'Send customers to one clear store warranty, guarantee or returns policy page.', 'noesia-commerce-suite' ),
				array( 'order_care_warranty_enabled', 'order_care_warranty_title', 'order_care_warranty_text', 'order_care_warranty_button_label', 'order_care_warranty_url' ),
				( 'yes' === ( $settings['order_care_warranty_enabled'] ?? 'no' ) && ! empty( $settings['order_care_warranty_url'] ) ) ? __( 'Visible', 'noesia-commerce-suite' ) : __( 'Hidden', 'noesia-commerce-suite' ),
				'yes' === ( $settings['order_care_warranty_enabled'] ?? 'no' ) && ! empty( $settings['order_care_warranty_url'] ),
				'warranty'
			);
			$render_smart_link_card(
				'📄',
				__( 'Documents & Manuals', 'noesia-commerce-suite' ),
				__( 'Link to a categorized manuals, documents or setup guides page on the store website.', 'noesia-commerce-suite' ),
				array( 'order_care_documents_enabled', 'order_care_documents_title', 'order_care_documents_text', 'order_care_documents_button_label', 'order_care_documents_url' ),
				( 'yes' === ( $settings['order_care_documents_enabled'] ?? 'no' ) && ! empty( $settings['order_care_documents_url'] ) ) ? __( 'Visible', 'noesia-commerce-suite' ) : __( 'Hidden', 'noesia-commerce-suite' ),
				'yes' === ( $settings['order_care_documents_enabled'] ?? 'no' ) && ! empty( $settings['order_care_documents_url'] ),
				'documents'
			);
			$render_smart_link_card(
				'▶',
				__( 'Video Guides', 'noesia-commerce-suite' ),
				__( 'Link to one product videos page, playlist or tutorial page. Keep the hub fast and clean.', 'noesia-commerce-suite' ),
				array( 'order_care_video_enabled', 'order_care_video_title', 'order_care_video_text', 'order_care_video_button_label', 'order_care_video_url' ),
				( 'yes' === ( $settings['order_care_video_enabled'] ?? 'no' ) && ! empty( $settings['order_care_video_url'] ) ) ? __( 'Visible', 'noesia-commerce-suite' ) : __( 'Hidden', 'noesia-commerce-suite' ),
				'yes' === ( $settings['order_care_video_enabled'] ?? 'no' ) && ! empty( $settings['order_care_video_url'] ),
				'video'
			);
			$render_smart_link_card(
				'🎁',
				__( 'Promo / Discount', 'noesia-commerce-suite' ),
				__( 'Show one thank-you offer or discount block after purchase.', 'noesia-commerce-suite' ),
				array( 'order_care_promo_enabled', 'order_care_promo_title', 'order_care_promo_text', 'order_care_promo_code', 'order_care_promo_button_label', 'order_care_promo_url' ),
				( 'yes' === ( $settings['order_care_promo_enabled'] ?? 'no' ) && ( ! empty( $settings['order_care_promo_text'] ) || ! empty( $settings['order_care_promo_code'] ) ) ) ? __( 'Visible', 'noesia-commerce-suite' ) : __( 'Hidden', 'noesia-commerce-suite' ),
				'yes' === ( $settings['order_care_promo_enabled'] ?? 'no' ) && ( ! empty( $settings['order_care_promo_text'] ) || ! empty( $settings['order_care_promo_code'] ) ),
				'promo'
			);
			?>
		</div>
		<div class="noesia-cs-sticky-save"><button class="noesia-cs-primary-button" type="submit"><?php esc_html_e( 'Save Smart Links', 'noesia-commerce-suite' ); ?></button></div>
	</form>
<?php elseif ( 'access' === $active_section ) : ?>
	<?php
	$render_hub_cards_form(
		__( 'Access & QR', 'noesia-commerce-suite' ),
		__( 'access methods', 'noesia-commerce-suite' ),
		__( 'QR is only an access method for the Order Care Hub. Use these controls for Thank You, emails, My Account, order admin and package inserts.', 'noesia-commerce-suite' ),
		array(
			array(
				'icon'         => '🔗',
				'title'        => __( 'Hub Access', 'noesia-commerce-suite' ),
				'description'  => __( 'Enable secure hub links and choose where customers can open them after purchase.', 'noesia-commerce-suite' ),
				'fields'       => array( 'enable_product_passport_qr', 'thankyou_integration', 'show_qr_thankyou', 'email_integration', 'show_qr_emails' ),
				'tone'         => 'documents',
			),
			array(
				'icon'         => '🧭',
				'title'        => __( 'WooCommerce Areas', 'noesia-commerce-suite' ),
				'description'  => __( 'Control My Account, order admin and the Open Noesia Care button in WooCommerce screens.', 'noesia-commerce-suite' ),
				'fields'       => array( 'my_account_integration', 'show_qr_my_account', 'order_admin_metabox', 'show_qr_order_admin', 'show_woo_care_button' ),
				'tone'         => 'warranty',
			),
			array(
				'icon'         => '✦',
				'title'        => __( 'Customer Access Card', 'noesia-commerce-suite' ),
				'description'  => __( 'Text shown on the WooCommerce Thank You Page and My Account card that opens the customer Order Care Hub.', 'noesia-commerce-suite' ),
				'fields'       => array( 'order_care_access_card_title', 'order_care_access_card_text', 'order_care_access_button_label' ),
				'tone'         => 'video',
			),
			array(
				'icon'         => '▦',
				'title'        => __( 'Package QR', 'noesia-commerce-suite' ),
				'description'  => __( 'Controls printable/package QR output. The QR points to the same secure Order Care Hub link.', 'noesia-commerce-suite' ),
				'fields'       => array( 'allow_printable_qr', 'qr_size', 'qr_foreground_color', 'qr_background_color', 'qr_label_text' ),
				'tone'         => 'promo',
			),
		),
		'access',
		__( 'Save Access & QR', 'noesia-commerce-suite' )
	);
	?>
	<div class="noesia-cs-panel noesia-cs-qr-engine-status">
		<div class="noesia-cs-panel__head"><h2><?php esc_html_e( 'Recent Secure Hub Links', 'noesia-commerce-suite' ); ?></h2><span><?php esc_html_e( 'latest 12 orders', 'noesia-commerce-suite' ); ?></span></div><p class="noesia-cs-panel__body-note"><?php esc_html_e( 'This panel shows only the latest WooCommerce orders for quick access and QR checks. Use Orders View for the full order list, filters and pagination.', 'noesia-commerce-suite' ); ?></p>
		<div class="noesia-cs-table-wrap"><table class="noesia-cs-table"><thead><tr><th><?php esc_html_e( 'Order', 'noesia-commerce-suite' ); ?></th><th><?php esc_html_e( 'Customer', 'noesia-commerce-suite' ); ?></th><th><?php esc_html_e( 'Hub Link', 'noesia-commerce-suite' ); ?></th><th><?php esc_html_e( 'QR', 'noesia-commerce-suite' ); ?></th></tr></thead><tbody>
		<?php foreach ( $passport_rows as $row ) : ?>
			<tr>
				<td><strong><?php echo esc_html( $row['reference'] ?? '' ); ?></strong><small><?php echo esc_html( $row['date'] ?? '' ); ?></small></td>
				<td><?php echo esc_html( $row['customer_label'] ?? '' ); ?></td>
				<td>
					<?php if ( ! empty( $row['has_token'] ) && ! empty( $row['url'] ) ) : ?>
						<button class="noesia-cs-link-button noesia-cs-copy-button" type="button" data-noesia-cs-copy="<?php echo esc_url( $row['url'] ); ?>"><?php esc_html_e( 'Copy Link', 'noesia-commerce-suite' ); ?></button>
						<a class="noesia-cs-link-button" href="<?php echo esc_url( $row['url'] ); ?>" target="_blank" rel="noopener noreferrer nofollow"><?php esc_html_e( 'Open', 'noesia-commerce-suite' ); ?></a>
						<?php if ( ! empty( $qr_settings['printable'] ) && ! empty( $row['print_url'] ) ) : ?><a class="noesia-cs-link-button" href="<?php echo esc_url( $row['print_url'] ); ?>" target="_blank" rel="noopener noreferrer"><?php esc_html_e( 'Print QR', 'noesia-commerce-suite' ); ?></a><?php endif; ?>
					<?php else : ?>
						<a class="noesia-cs-link-button" href="<?php echo esc_url( $row['generate_url'] ?? '' ); ?>"><?php esc_html_e( 'Generate Hub Link', 'noesia-commerce-suite' ); ?></a>
					<?php endif; ?>
				</td>
				<td><?php if ( ! empty( $row['has_token'] ) && ! empty( $row['url'] ) ) : ?><div class="noesia-cs-mini-qr" data-noesia-cs-qr="<?php echo esc_url( $row['url'] ); ?>" data-noesia-cs-qr-size="72" data-noesia-cs-qr-foreground="<?php echo esc_attr( $qr_settings['foreground'] ?? '#111827' ); ?>" data-noesia-cs-qr-background="<?php echo esc_attr( $qr_settings['background'] ?? '#ffffff' ); ?>"></div><?php else : ?><span class="noesia-cs-muted"><?php esc_html_e( 'No QR yet', 'noesia-commerce-suite' ); ?></span><?php endif; ?></td>
			</tr>
		<?php endforeach; ?>
		<?php if ( empty( $passport_rows ) ) : ?><tr><td colspan="4"><?php esc_html_e( 'No WooCommerce orders found yet.', 'noesia-commerce-suite' ); ?></td></tr><?php endif; ?>
		</tbody></table></div>
	</div>
<?php elseif ( 'design' === $active_section ) : ?>
	<?php
	$care_experience_url = add_query_arg(
		array(
			'noesia_cs_from' => 'design_display',
		),
		$admin->tab_url( 'care-experience-pro' )
	);
	?>
	<div class="noesia-cs-cross-panel noesia-cs-cross-panel--care" aria-label="<?php esc_attr_e( 'Care Hub template shortcut', 'noesia-commerce-suite' ); ?>">
		<div>
			<strong><?php esc_html_e( 'Design controls and Pro templates work together.', 'noesia-commerce-suite' ); ?></strong>
			<p><?php esc_html_e( 'Use this page for core public Care Hub styling, width, buttons and customer-facing display. Use Care Experience Pro for premium visual templates, warranty rules and after-sales branding presets.', 'noesia-commerce-suite' ); ?></p>
		</div>
		<a class="noesia-cs-cross-panel__button" href="<?php echo esc_url( $care_experience_url ); ?>"><?php esc_html_e( 'Open Care Experience Pro', 'noesia-commerce-suite' ); ?></a>
	</div>
	<?php
	$render_hub_cards_form(
		__( 'Design & Display', 'noesia-commerce-suite' ),
		__( 'public look', 'noesia-commerce-suite' ),
		__( 'Control the public hub style, brand colors, customer-facing copy and Thank You card foundation.', 'noesia-commerce-suite' ),
		array(
			array(
				'icon'         => '◐',
				'title'        => __( 'Public Style', 'noesia-commerce-suite' ),
				'description'  => __( 'Public Hub Style, Public Hub Width and Hub Button Style affect the public Order Care Hub. QR Card Layout Style affects only the Thank You/My Account access card, not customer emails.', 'noesia-commerce-suite' ),
				'fields'       => array( 'order_care_public_style', 'order_care_content_width', 'order_care_button_style', 'button_style', 'qr_card_layout_style' ),
				'tone'         => 'documents',
			),
			array(
				'icon'         => '◆',
				'title'        => __( 'Brand & Colors', 'noesia-commerce-suite' ),
				'description'  => __( 'Scope: public Order Care Hub and customer access cards. Upload or paste a logo URL; recommended transparent PNG/SVG around 512×512 or a wide logo up to 900×300.', 'noesia-commerce-suite' ),
				'fields'       => array( 'company_logo', 'primary_color', 'accent_color' ),
				'tone'         => 'promo',
			),
			array(
				'icon'         => '☰',
				'title'        => __( 'Customer Display', 'noesia-commerce-suite' ),
				'description'  => __( 'Scope: public Order Care Hub page only. Choose which safe order details and copy appear there.', 'noesia-commerce-suite' ),
				'fields'       => array( 'show_product_name', 'show_order_number', 'show_purchase_date', 'show_verified_badge', 'care_page_title_text', 'care_page_intro_text' ),
				'tone'         => 'warranty',
			),
		),
		'design',
		__( 'Save Design & Display', 'noesia-commerce-suite' )
	);
	?>
<?php elseif ( 'security' === $active_section ) : ?>
	<?php
	$render_hub_cards_form(
		__( 'Security & Visibility', 'noesia-commerce-suite' ),
		__( 'privacy-safe access', 'noesia-commerce-suite' ),
		__( 'Keep the public hub token-based, noindexed and privacy-safe. Customer email, phone, address and payment details must remain hidden publicly.', 'noesia-commerce-suite' ),
		array(
			array(
				'icon'         => '🔒',
				'title'        => __( 'Token Access', 'noesia-commerce-suite' ),
				'description'  => __( 'Control secure token access, noindex behavior and optional login. If login is required, only the matching customer account or an authorized admin can view the hub.', 'noesia-commerce-suite' ),
				'fields'       => array( 'enable_care_page', 'privacy_noindex', 'require_login_care_page', 'secure_token_behavior' ),
				'tone'         => 'video',
			),
			array(
				'icon'         => '◌',
				'title'        => __( 'Public Privacy', 'noesia-commerce-suite' ),
				'description'  => __( 'Locked protection. These stay enabled as public safeguards; the public hub never shows full names, addresses, emails, phone numbers or payment details.', 'noesia-commerce-suite' ),
				'fields'       => array( 'privacy_hide_full_name', 'privacy_hide_address', 'privacy_hide_email', 'privacy_hide_phone' ),
				'tone'         => 'warranty',
			),
		),
		'security',
		__( 'Save Security & Visibility', 'noesia-commerce-suite' )
	);
	?>
<?php endif; ?>
