<?php
namespace Shopglut\layouts;

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

// use Shopglut\layouts\shopLayout\chooseTemplates as ShopLayoutTemplates;
// use Shopglut\layouts\shopLayout\SettingsPage as ShopLayoutEditor;
// use Shopglut\layouts\shopLayout\ShopListTable;
// use Shopglut\layouts\cartPage\chooseTemplates as CartPageTemplates;

// use Shopglut\layouts\orderCompletePage\chooseTemplates as OrdercompleteTemplates;
// use Shopglut\layouts\orderCompletePage\SettingsPage as orderCompletePagePreBuilderEditor;
// use Shopglut\layouts\orderCompletePage\OrdercompleteListTable;


// use Shopglut\layouts\accountPage\AccountPageChooseTemplates;
// use Shopglut\layouts\cartPage\SettingsPage as CartPageEditor;
// use Shopglut\layouts\cartPage\SettingsPage as CartPageBuilderEditor;


// use Shopglut\layouts\accountPage\AccountPageSettingsPage as accountpageEditor;
// use Shopglut\layouts\cartPage\CartPageListTable;
// use Shopglut\layouts\accountPage\AccountPageListTable;
// use Shopglut\BusinessSolutions\AllBusinessSolutions;
// use Shopglut\layouts\cartPage\CartPageEntity;
// use Shopglut\layouts\orderCompletePage\orderCompletePageEntity as ordercompleteEntity;
// use Shopglut\layouts\accountPage\AccountPageEntity;
use Shopglut\layouts\LayoutEntity;


class AllLayouts {

	public $not_implemented;

	public function __construct() {


		add_filter( 'admin_body_class', array( $this, 'shopglutBodyClass' ) );

        $this->not_implemented = true;


	}


	public function shopglutBodyClass( $classes ) {
		$current_screen = get_current_screen();

		if ( empty( $current_screen ) ) {
			return $classes;
		}
		if ( false !== strpos( $current_screen->id, 'shopglut_' ) ) {
			$classes .= ' shopglut-admin';
		}

		// Only apply editor-specific classes in admin context with proper permissions
		if ( ! is_admin() || ! current_user_can( 'manage_options' ) ) {
			return $classes;
		}

		// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Safe admin page parameter check for body class only
		if ( isset( $_GET['page'] ) && 'shopglut_layouts' === sanitize_text_field( wp_unslash( $_GET['page'] ) ) && isset( $_GET['editor'] ) ) {
			$classes .= '-shopglut-editor-collapse ';
		}

		// PHPCS: Input var ok. Body class filter context doesn't require nonce verification.
		if ( isset( $_GET['page'] ) && 'shopglut_layouts' === sanitize_text_field( wp_unslash( $_GET['page'] ) ) && isset( $_GET['editor'] ) && 'shop' === sanitize_text_field( wp_unslash( $_GET['editor'] ) ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
			$classes .= '-shopglut-shop-editor ';
		}

		// PHPCS: Input var ok. Body class filter context doesn't require nonce verification.
		if ( isset( $_GET['page'] ) && 'shopglut_layouts' === sanitize_text_field( wp_unslash( $_GET['page'] ) ) && isset( $_GET['editor'] ) && 'productpageglut' === sanitize_text_field( wp_unslash( $_GET['editor'] ) ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
			$classes .= ' shopglut-admin-sg-single-product ';
		}

		// PHPCS: Input var ok. Body class filter context doesn't require nonce verification.
		if ( isset( $_GET['page'] ) && 'shopglut_layouts' === sanitize_text_field( wp_unslash( $_GET['page'] ) ) && isset( $_GET['editor'] ) && 'cartpage' === sanitize_text_field( wp_unslash( $_GET['editor'] ) ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
			$classes .= '-shopglut-cartpage-editor ';
		}

		// PHPCS: Input var ok. Body class filter context doesn't require nonce verification.
		if ( isset( $_GET['page'] ) && 'shopglut_layouts' === sanitize_text_field( wp_unslash( $_GET['page'] ) ) && isset( $_GET['editor'] ) && 'cartpage' === sanitize_text_field( wp_unslash( $_GET['editor'] ) ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
			$classes .= '-shopglut-cartpage-editor ';
		}

		// PHPCS: Input var ok. Body class filter context doesn't require nonce verification.
		if ( isset( $_GET['page'] ) && 'shopglut_layouts' === sanitize_text_field( wp_unslash( $_GET['page'] ) ) && isset( $_GET['editor'] ) && 'ordercomplete' === sanitize_text_field( wp_unslash( $_GET['editor'] ) ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
			$classes .= '-shopglut-ordercomplete-editor ';
		}

		// PHPCS: Input var ok. Body class filter context doesn't require nonce verification.
		if ( isset( $_GET['page'] ) && 'shopglut_layouts' === sanitize_text_field( wp_unslash( $_GET['page'] ) ) && isset( $_GET['editor'] ) && 'accountpage_prebuilt' === sanitize_text_field( wp_unslash( $_GET['editor'] ) ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
			$classes .= '-shopglut-accountpage_prebuilt-editor ';
		}

		return $classes;
	}

	public function renderLayoutsPages() {

		// Only initialize singleProduct editor if ProductPageGlut is NOT active
// 		if ( ! $this->is_productpageglut_active() ) {
// 			if ( class_exists( 'ProductPageGlut\ProductPageGlut\\SettingsPage' ) ) {
// 				$singleProduct_editor = new \Shopglut\layouts\singleProduct\SettingsPage();
// 			} else {
// 				$singleProduct_editor = null;
// 			}
// 		} else {
// 			$singleProduct_editor = null;
// 		}

			// === COMMENTED OUT - MOVED TO STANDALONE PLUGIN ===
			// 		$shopLayout_editor = new ShopLayoutEditor();
			// 		$cartpage_editor = new CartPageEditor();
			// 		$cartpage_editor = new CartPageBuilderEditor();
			// 		$ordercomplete_editor = new orderCompletePagePreBuilderEditor();
			// 		$accountpage_editor = new accountpageEditor();

		// Sanitize and validate input - only in admin context
		$page = '';
		$editor = '';
		$layout_id = 0;
		$view = '';

		if ( is_admin() && current_user_can( 'manage_options' ) ) {
			// Verify nonce if present (for editor actions), skip for basic navigation
			$nonce_verified = true;
			if ( isset( $_GET['_wpnonce'] ) && isset( $_GET['action'] ) && $_GET['action'] === 'delete' && isset( $_GET['layout_id'] ) ) {
				// For delete actions, verify against the specific delete nonce
				$delete_layout_id = absint( $_GET['layout_id'] );
				$nonce_verified = wp_verify_nonce( sanitize_text_field( wp_unslash( $_GET['_wpnonce'] ) ), 'shopglut_delete_layout_' . $delete_layout_id );
			} elseif ( isset( $_GET['_wpnonce'] ) ) {
				// For other actions, verify against the general admin action nonce
				$nonce_verified = wp_verify_nonce( sanitize_text_field( wp_unslash( $_GET['_wpnonce'] ) ), 'shopglut_admin_action' );
			}

			if ( $nonce_verified ) {
				$page = isset( $_GET['page'] ) ? sanitize_text_field( wp_unslash( $_GET['page'] ) ) : '';
				$editor = isset( $_GET['editor'] ) ? sanitize_text_field( wp_unslash( $_GET['editor'] ) ) : '';
				$layout_id = isset( $_GET['layout_id'] ) ? absint( $_GET['layout_id'] ) : 0;
				$view = isset( $_GET['view'] ) ? sanitize_text_field( wp_unslash( $_GET['view'] ) ) : '';
			}
		}


		// Handle productpageglut editor - use productpage-glut's editor if active, otherwise use shopglut's embedded editor
// 		if ( 'shopglut_layouts' === $page && 'productpageglut' === $editor && $layout_id > 0 ) {
			// Add filters to override URLs when used in shopglut context
// 			add_filter( 'productpageglut_editor_back_page_slug', array( $this, 'filter_list_table_page_slug' ) );
// 			add_filter( 'productpageglut_editor_back_view', array( $this, 'filter_list_table_view' ) );
// 			add_filter( 'productpageglut_list_table_page_slug', array( $this, 'filter_list_table_page_slug' ) );
// 			add_filter( 'productpageglut_list_table_editor_slug', array( $this, 'filter_list_table_editor_slug' ) );
// 
// 			if ( $this->is_productpageglut_active() && class_exists( 'Productpageglut\\layouts\\productPage\\SettingsPage' ) ) {
				// Load productpage-glut's editor
// 				$productpageglut_editor = new \Productpageglut\layouts\productPage\SettingsPage();
// 				$productpageglut_editor->loadSingleProductEditor();
// 			} elseif ( $singleProduct_editor !== null ) {
				// Use shopglut's embedded editor
// 				$singleProduct_editor->loadProductPageGlutEditor();
// 			}
			// === COMMENTED OUT - MOVED TO STANDALONE PLUGIN ===
			// 		} elseif ( 'shopglut_layouts' === $page && 'shop' === $editor && $layout_id > 0 ) {
			// 			$shopLayout_editor->loadShopLayoutEditor();
			// 		} elseif ( 'shopglut_layouts' === $page && 'cartpage' === $editor && $layout_id > 0 ) {
			// 			$cartpage_editor->loadCartPageEditor();
			// 		}  elseif ( 'shopglut_layouts' === $page && 'ordercomplete' === $editor && $layout_id > 0 ) {
			// 			$ordercomplete_editor->loadOrdercompleteEditor();
			// 		} elseif ( 'shopglut_layouts' === $page && 'accountpage' === $editor && $layout_id > 0 ) {
			// 			$accountpage_editor->loadAccountPageEditor();
			// 		} elseif ( 'shopglut_layouts' === $page && 'shop_templates' === $view ) {
			// 			$this->ShoplayoutTemplatesPage();
			// === KEEP ACTIVE - productpage-glut templates ===
// 			} elseif ( 'shopglut_layouts' === $page && ( 'productpageglut_templates' === $view || 'product_page_templates' === $view ) ) {
// 				$this->ProductPageLayoutTemplatesPage();
			// 		} elseif ( 'shopglut_layouts' === $page && 'cartpage_templates' === $view ) {
			// 			$this->CartlayoutTemplatesPage();
			// 		} elseif ( 'shopglut_layouts' === $page && 'accountpage_templates' === $view ) {
			// 			$this->AccountTemplatesPage();
			// 		} elseif ( 'shopglut_layouts' === $page && 'ordercomplete_templates' === $view ) {
			// 			$this->ThanklayoutTemplatesPage();
			// 		}
			if ( 'shopglut_layouts' === $page && ! empty( $view ) ) {
				switch ( $view ) {
					case 'shop':
						$this->renderLayoutsTable();
						break;
					case 'productpageglut':
					case 'product_page':

					case product_page_templates:
					case single_product_templates:
						$this->ProductPageLayoutTemplatesPage();
						break;
t				case 'product_page_templates':
					case 'single_product_templates':
						$this->ProductPageLayoutTemplatesPage();
						break;
						$this->renderProductPage();
						break;
					case 'cartpage':
						$this->renderCart();
						break;
					case 'checkout':
						$this->renderCheckout();
						break;
					case 'ordercomplete':
						$this->renderOrderThankyou();
						break;
					case 'accountpage':
						$this->renderMyAccount();
						break;
					// case 'quick_views':
					//     $this->renderQuickView();
					//     break;
					default:
						//$this->renderLayoutsTable();
						break;
				}
			} elseif ( 'shopglut_layouts' === $page ) {
				$this->renderWooCommerceLayouts();
			} elseif ( 'shopg_woocommerce_builder' === $page ) {
				$this->renderWooBuilderPage();
			} else {
				wp_die( esc_html__( 'Sorry, you are not allowed to access this page.', 'shopglut' ) );
			}

	}


	public function settingsPageHeader( $active_menu ) {
		$logo_url = SHOPGLUT_URL . 'global-assets/images/header-logo.svg';
		?>
		<div class="shopglut-page-header">
			<div class="shopglut-page-header-wrap">
				<div class="shopglut-page-header-banner shopglut-pro shopglut-no-submenu">
					<div class="shopglut-page-header-banner__logo">
						<img src="<?php echo esc_url( $logo_url ); ?>" alt="">
					</div>
					<div class="shopglut-page-header-banner__helplinks">
						<span><a rel="noopener"
								href="https://documentation.appglut.com/?utm_source=shoglutplugin-admin&utm_medium=referral&utm_campaign=adminmenu"
								target="_blank">
								<span class="dashicons dashicons-admin-page"></span>
								<?php echo esc_html__( 'Documentation', 'shopglut' ); ?>
							</a></span>
						<span><a class="shopglut-active" rel="noopener"
								href="https://www.appglut.com/plugin/shopglut/?utm_source=shoglutplugin-admin&utm_medium=referral&utm_campaign=upgrade"
								target="_blank">
								<span class="dashicons dashicons-unlock"></span>
								<?php echo esc_html__( 'Unlock Pro Edition', 'shopglut' ); ?>
							</a></span>
						<span><a rel="noopener"
								href="https://www.appglut.com/support/?utm_source=shoglutplugin-admin&utm_medium=referral&utm_campaign=support"
								target="_blank">
								<span class="dashicons dashicons-share-alt"></span>
								<?php echo esc_html__( 'Support', 'shopglut' ); ?>
							</a></span>
					</div>
					<div class="clear"></div>
					<?php $this->settingsPageHeaderMenus( $active_menu ); ?>
				</div>
			</div>
		</div>
		<?php
	}

	public function settingsPageHeaderMenus( $active_menu ) {

		$menus = $this->headerMenuTabs();

		if ( count( $menus ) < 2 ) {
			return;
		}

		?>
		<div class="shopglut-header-menus">
			<nav class="shopglut-nav-tab-wrapper nav-tab-wrapper">
				<?php foreach ( $menus as $menu ) : ?>
					<?php $id = $menu['id'];
					$url = esc_url_raw( ! empty( $menu['url'] ) ? $menu['url'] : '' );
					?>
					<a href="<?php echo esc_url( remove_query_arg( wp_removable_query_args(), $url ) ); ?>"
						class="shopglut-nav-tab nav-tab<?php echo esc_attr( $id ) == esc_attr( $active_menu ) ? ' shopglut-nav-active' : ''; ?>">
						<?php echo esc_html( $menu['label'] ); ?>
					</a>
				<?php endforeach; ?>
			</nav>
		</div>
		<?php
	}

	public function defaultHeaderMenu() {
		return 'all_layouts';
	}

	public function headerMenuTabs() {
		$tabs = [
			1 => [ 'id' => 'all_layouts', 'url' => admin_url( 'admin.php?page=shopglut_layouts' ), 'label' => '📋 ' . esc_html__( 'All Layouts', 'shopglut' ) ],
			30 => [ 'id' => 'shop', 'url' => admin_url( 'admin.php?page=shopglut_layouts&view=shop' ), 'label' => '🏪 ' . esc_html__( 'Shop & Archive Layouts', 'shopglut' ) ],
			5 => [ 'id' => 'productpageglut', 'url' => admin_url( 'admin.php?page=shopglut_layouts&view=product_page' ), 'label' => '📦 ' . esc_html__( 'Product Page', 'shopglut' ) ],
			10 => ['id' => 'cartpage', 'url' => admin_url('admin.php?page=shopglut_layouts&view=cartpage'), 'label' => '🛒 ' . esc_html__('Cart Page', 'shopglut')],
			20 => ['id' => 'ordercomplete', 'url' => admin_url('admin.php?page=shopglut_layouts&view=ordercomplete'), 'label' => '✅ ' . esc_html__('Order Complete', 'shopglut')],
			15 => ['id' => 'checkout', 'url' => admin_url('admin.php?page=shopglut_layouts&view=checkout'), 'label' => '💳 ' . esc_html__('Shop Checkout', 'shopglut')],
            25 => ['id' => 'accountpage', 'url' => admin_url('admin.php?page=shopglut_layouts&view=accountpage'), 'label' => '⭕ ' . esc_html__('My Account', 'shopglut')],
		];

		ksort( $tabs );

		return $tabs;
}

	public function activeMenuTab() {
		// PHPCS: Input var ok. Navigation context doesn't require nonce verification.
		$page = isset( $_GET['page'] ) ? sanitize_text_field( wp_unslash( $_GET['page'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
		// PHPCS: Input var ok. Navigation context doesn't require nonce verification.
		$view = isset( $_GET['view'] ) ? sanitize_text_field( wp_unslash( $_GET['view'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended

		// Check if we're on a shopglut page
		if ( false !== strpos( $page, 'shopglut' ) ) {
			// If no view parameter, we're on the main landing page (all_layouts)
			if ( empty( $view ) && 'shopglut_layouts' === $page ) {
				return 'all_layouts';
			}

			// Map view values to tab IDs to fix highlighting
			$view_to_tab_map = array(
				'product_page' => 'productpageglut',
				'shop' => 'shop',
				'cartpage' => 'cartpage',
				'checkout' => 'checkout',
				'ordercomplete' => 'ordercomplete',
				'accountpage' => 'accountpage',
			);

			// Return mapped tab ID if exists, otherwise return view or default
			if ( ! empty( $view ) && isset( $view_to_tab_map[ $view ] ) ) {
				return $view_to_tab_map[ $view ];
			}

			return ! empty( $view ) ? $view : $this->defaultHeaderMenu();
		}

		return false;
	}

	public function renderLayoutsTable() {
		if ( ! current_user_can( 'manage_options' ) ) {
			wp_die( esc_html__( 'You do not have sufficient permissions to access this page.', 'shopglut' ) );
		}

		$active_menu = $this->activeMenuTab();
		$this->settingsPageHeader( $active_menu );
		?>
		<div class="wrap shopglut-admin-contents">
			<h2><?php echo esc_html__( 'Shop & Archive Layouts', 'shopglut' ); ?></h2>
			<div class="shopglut-feature-not-added" style="max-width: 700px; margin: 40px auto;">
				<div style="background: #fff; border: 1px solid #c3c4c7; border-left: 4px solid #d63638; padding: 40px; text-align: center; border-radius: 8px;">
					<div style="font-size: 64px; margin-bottom: 20px;">
						<span class="dashicons dashicons-store" style="color: #d63638; font-size: 64px; width: 64px; height: 64px;"></span>
					</div>
					<h2 style="color: #1d2327; font-size: 28px; margin: 0 0 15px 0;">
						<?php esc_html_e( 'Feature Not Added', 'shopglut' ); ?>
					</h2>
					<p style="color: #50575e; font-size: 16px; margin: 0 0 20px 0; max-width: 500px; margin-left: auto; margin-right: auto;">
						<?php esc_html_e( 'The shop and archive layouts feature has not been added yet. This feature will be available in a future update.', 'shopglut' ); ?>
					</p>
				</div>
			</div>
		</div>
		<?php
		return;
	}
	public function ShoplayoutTemplatesPage() {

		$active_menu = 'layouts';
		$this->settingsPageHeader( $active_menu );
		$shopLayout_templates = new ShopLayoutTemplates();
		?>
		<div class="wrap shopglut-admin-contents shoplayouts-templates">
			<h1><?php echo esc_html__( 'PreBuilt ShopPage Templates', 'shopglut' ); ?></h1>
			<p class="subheading"><?php echo esc_html__( 'Choose your desired template to customize', 'shopglut' ); ?></p>
		</div>
		<?php $shopLayout_templates->loadShoplayoutTemplates();

	}

	public function ProductPageLayoutTemplatesPage() {
		$active_menu = 'productpageglut';
		$this->settingsPageHeader( $active_menu );

		// Add filters to override URLs when used in shopglut context
		add_filter( 'productpageglut_templates_page_slug', array( $this, 'filter_templates_page_slug' ) );
		add_filter( 'productpageglut_templates_editor_slug', array( $this, 'filter_templates_editor_slug' ) );
		add_filter( 'productpageglut_list_table_page_slug', array( $this, 'filter_list_table_page_slug' ) );
		add_filter( 'productpageglut_list_table_editor_slug', array( $this, 'filter_list_table_editor_slug' ) );

		// Pre-load ProductPageGlut classes if active
		if ( $this->is_productpageglut_active() ) {
			$template_paths = array(
				WP_PLUGIN_DIR . '/productpage-glut/src/layouts/productPage/chooseTemplates.php',
				WP_PLUGIN_DIR . '/productpage-glut/src/productPageGlut/chooseTemplates.php'
			);
			foreach ( $template_paths as $path ) {
				if ( file_exists( $path ) ) {
					require_once $path;
					break;
				}
			}
		}

		// Determine which templates class to use
		$templates_class = null;
		if ( class_exists( 'Productpageglut\\layouts\\productPage\\chooseTemplates' ) ) {
			$templates_class = 'Productpageglut\\layouts\\productPage\\chooseTemplates';
		} elseif ( class_exists( 'ProductPageGlut\\ProductPageGlut\\chooseTemplates' ) ) {
			$templates_class = 'ProductPageGlut\\ProductPageGlut\\chooseTemplates';
		} elseif ( class_exists( 'ProductDetailsGlut\\singleProduct\\chooseTemplates' ) ) {
			$templates_class = 'ProductDetailsGlut\\singleProduct\\chooseTemplates';
		}

		if ( $templates_class === null ) {
			echo '<p>' . esc_html__( 'Product Page templates not available.', 'shopglut' ) . '</p>';
			return;
		}

		$shopLayout_templates = new $templates_class();

		// Show notice if ProductPageGlut or ProductDetailsGlut is active
		$notice_html = '';
		if ( $this->is_productpageglut_active() ) {
			$notice_html = '<div class="notice notice-info" style="margin-bottom: 20px;"><p>';
			$notice_html .= esc_html__( 'Templates are also available in the', 'shopglut' );
			$notice_html .= ' <a href="' . esc_url( admin_url( 'admin.php?page=productpageglut_layouts&view=templates' ) ) . '">';
			$notice_html .= esc_html__( 'ProductPageGlut plugin', 'shopglut' );
			$notice_html .= '</a>.';
			$notice_html .= '</p></div>';
		} elseif ( $this->is_productdetailsglut_active() && ! $this->is_productpageglut_active() ) {
			$notice_html = '<div class="notice notice-info" style="margin-bottom: 20px;"><p>';
			$notice_html .= esc_html__( 'Templates are also available in the', 'shopglut' );
			$notice_html .= ' <a href="' . esc_url( admin_url( 'admin.php?page=productdetailsglut_layouts&view=templates' ) ) . '">';
			$notice_html .= esc_html__( 'ProductDetailsGlut plugin', 'shopglut' );
			$notice_html .= '</a>.';
			$notice_html .= '</p></div>';
		}

		?>
		<div class="wrap shopglut-admin-contents shoplayouts-templates">
			<h1><?php echo esc_html__( 'Prebuilt SingleProduct Templates', 'shopglut' ); ?></h1>
			<p class="subheading"><?php echo esc_html__( 'Choose your desired template to customize', 'shopglut' ); ?></p>
			<?php echo $notice_html; ?>
			<?php $shopLayout_templates->loadSingleProductTemplates(); ?>
		</div>
		<?php
	}

	public function CartlayoutTemplatesPage() {
		$active_menu = 'cartpage';
		$this->settingsPageHeader( $active_menu );
		$cartLayout_templates = new CartPageTemplates();
		?>
		<div class="wrap shopglut-admin-contents shoplayouts-templates">
			<h1><?php echo esc_html__( 'PreBuilt Cart Layout Templates', 'shopglut' ); ?></h1>
			<p class="subheading"><?php echo esc_html__( 'Choose your desired template to customize', 'shopglut' ); ?></p>
		</div>
		<?php $cartLayout_templates->loadCartPageTemplates();
	}
	
	public function ThanklayoutTemplatesPage() {
		$active_menu = 'ordercomplete';
		$this->settingsPageHeader( $active_menu );
		$thankLayout_templates = new OrdercompleteTemplates();
		?>
		<div class="wrap shopglut-admin-contents shoplayouts-templates">
			<h1><?php echo esc_html__( 'PreBuilt Ordercomplete Layout Templates', 'shopglut' ); ?></h1>
			<p class="subheading"><?php echo esc_html__( 'Choose your desired template to customize', 'shopglut' ); ?></p>
		</div>
		<?php $thankLayout_templates->loadOrdercompleteTemplates();
	}
	
	public function AccountTemplatesPage() {
		$active_menu = 'accountpage';
		$this->settingsPageHeader( $active_menu );
		$thankLayout_templates = new AccountPageChooseTemplates();
		?>
		<div class="wrap shopglut-admin-contents shoplayouts-templates">
			<h1><?php echo esc_html__( 'PreBuilt AccountPage Layout Templates', 'shopglut' ); ?></h1>
			<p class="subheading"><?php echo esc_html__( 'Choose your desired template to customize', 'shopglut' ); ?></p>
		</div>
		<?php $thankLayout_templates->loadProductAccountPageTemplates();
	}

		public function renderCart() {
		if ( ! current_user_can( 'manage_options' ) ) {
			wp_die( esc_html__( 'You do not have sufficient permissions to access this page.', 'shopglut' ) );
		}

		$active_menu = $this->activeMenuTab();
		$this->settingsPageHeader( $active_menu );
		?>
		<div class="wrap shopglut-admin-contents">
			<h2><?php echo esc_html__( 'Cart Page Layouts', 'shopglut' ); ?></h2>
			<div class="shopglut-feature-not-added" style="max-width: 700px; margin: 40px auto;">
				<div style="background: #fff; border: 1px solid #c3c4c7; border-left: 4px solid #d63638; padding: 40px; text-align: center; border-radius: 8px;">
					<div style="font-size: 64px; margin-bottom: 20px;">
						<span class="dashicons dashicons-cart" style="color: #d63638; font-size: 64px; width: 64px; height: 64px;"></span>
					</div>
					<h2 style="color: #1d2327; font-size: 28px; margin: 0 0 15px 0;">
						<?php esc_html_e( 'Feature Not Added', 'shopglut' ); ?>
					</h2>
					<p style="color: #50575e; font-size: 16px; margin: 0 0 20px 0; max-width: 500px; margin-left: auto; margin-right: auto;">
						<?php esc_html_e( 'The cart page layouts feature has not been added yet. This feature will be available in a future update.', 'shopglut' ); ?>
					</p>
				</div>
			</div>
		</div>
		<?php
		return;
	}
		public function renderCheckout() {
		if ( ! current_user_can( 'manage_options' ) ) {
			wp_die( esc_html__( 'You do not have sufficient permissions to access this page.', 'shopglut' ) );
		}

		$active_menu = $this->activeMenuTab();
		$this->settingsPageHeader( $active_menu );
		?>
		<div class="wrap shopglut-admin-contents">
			<h2><?php echo esc_html__( 'Checkout Page Layouts', 'shopglut' ); ?></h2>
			<div class="shopglut-feature-not-added" style="max-width: 700px; margin: 40px auto;">
				<div style="background: #fff; border: 1px solid #c3c4c7; border-left: 4px solid #d63638; padding: 40px; text-align: center; border-radius: 8px;">
					<div style="font-size: 64px; margin-bottom: 20px;">
						<span class="dashicons dashicons-admin-home" style="color: #d63638; font-size: 64px; width: 64px; height: 64px;"></span>
					</div>
					<h2 style="color: #1d2327; font-size: 28px; margin: 0 0 15px 0;">
						<?php esc_html_e( 'Feature Not Added', 'shopglut' ); ?>
					</h2>
					<p style="color: #50575e; font-size: 16px; margin: 0 0 20px 0; max-width: 500px; margin-left: auto; margin-right: auto;">
						<?php esc_html_e( 'The checkout page layouts feature has not been added yet. This feature will be available in a future update.', 'shopglut' ); ?>
					</p>
				</div>
			</div>
		</div>
		<?php
		return;
	}
		public function renderOrderThankyou() {
		if ( ! current_user_can( 'manage_options' ) ) {
			wp_die( esc_html__( 'You do not have sufficient permissions to access this page.', 'shopglut' ) );
		}

		$active_menu = $this->activeMenuTab();
		$this->settingsPageHeader( $active_menu );
		?>
		<div class="wrap shopglut-admin-contents">
			<h2><?php echo esc_html__( 'Order Thank You Page Layouts', 'shopglut' ); ?></h2>
			<div class="shopglut-feature-not-added" style="max-width: 700px; margin: 40px auto;">
				<div style="background: #fff; border: 1px solid #c3c4c7; border-left: 4px solid #d63638; padding: 40px; text-align: center; border-radius: 8px;">
					<div style="font-size: 64px; margin-bottom: 20px;">
						<span class="dashicons dashicons-yes-alt" style="color: #d63638; font-size: 64px; width: 64px; height: 64px;"></span>
					</div>
					<h2 style="color: #1d2327; font-size: 28px; margin: 0 0 15px 0;">
						<?php esc_html_e( 'Feature Not Added', 'shopglut' ); ?>
					</h2>
					<p style="color: #50575e; font-size: 16px; margin: 0 0 20px 0; max-width: 500px; margin-left: auto; margin-right: auto;">
						<?php esc_html_e( 'The order thank you page layouts feature has not been added yet. This feature will be available in a future update.', 'shopglut' ); ?>
					</p>
				</div>
			</div>
		</div>
		<?php
		return;
	}

	public function renderMyAccount() {
		if ( ! current_user_can( 'manage_options' ) ) {
			wp_die( esc_html__( 'You do not have sufficient permissions to access this page.', 'shopglut' ) );
		}

		$active_menu = $this->activeMenuTab();
		$this->settingsPageHeader( $active_menu );
		?>
		<div class="wrap shopglut-admin-contents">
			<h2><?php echo esc_html__( 'My Account Page Layouts', 'shopglut' ); ?></h2>
			<div class="shopglut-feature-not-added" style="max-width: 700px; margin: 40px auto;">
				<div style="background: #fff; border: 1px solid #c3c4c7; border-left: 4px solid #d63638; padding: 40px; text-align: center; border-radius: 8px;">
					<div style="font-size: 64px; margin-bottom: 20px;">
						<span class="dashicons dashicons-admin-users" style="color: #d63638; font-size: 64px; width: 64px; height: 64px;"></span>
					</div>
					<h2 style="color: #1d2327; font-size: 28px; margin: 0 0 15px 0;">
						<?php esc_html_e( 'Feature Not Added', 'shopglut' ); ?>
					</h2>
					<p style="color: #50575e; font-size: 16px; margin: 0 0 20px 0; max-width: 500px; margin-left: auto; margin-right: auto;">
						<?php esc_html_e( 'The my account page layouts feature has not been added yet. This feature will be available in a future update.', 'shopglut' ); ?>
					</p>
				</div>
			</div>
		</div>
		<?php
		return;
	}

	public function renderProductPage() {
		 //if($this->not_implemented):
		 //$this->renderNotImplementedMessage();
		 //else: ?>
		<?php
		if ( ! current_user_can( 'manage_options' ) ) {
			wp_die( esc_html__( 'You do not have sufficient permissions to access this page.', 'shopglut' ) );
		}

		// Pre-load ProductDetailsGlut/ProductPageGlut classes if active
		// This ensures classes are available before we check for them
		if ( $this->is_productpageglut_active() ) {
			// Include ProductLayoutEntity first (other classes depend on it)
			$entity_paths = array(
				WP_PLUGIN_DIR . '/productpage-glut/src/layouts/productPage/ProductLayoutEntity.php',
				WP_PLUGIN_DIR . '/productpage-glut/src/productPageGlut/ProductPageGlutLayoutEntity.php'
			);
			foreach ( $entity_paths as $path ) {
				if ( file_exists( $path ) ) {
					require_once $path;
					break;
				}
			}
			// Include ProductPageListTable
			$table_paths = array(
				WP_PLUGIN_DIR . '/productpage-glut/src/layouts/productPage/ProductPageListTable.php',
				WP_PLUGIN_DIR . '/productpage-glut/src/productPageGlut/ProductPageGlutListTable.php'
			);
			foreach ( $table_paths as $path ) {
				if ( file_exists( $path ) ) {
					require_once $path;
					break;
				}
			}
		}

		// Check if productpageglut plugin is available - if not, show integration page
		$plugin_slug = 'productpage-glut/productpage-glut.php';
		$active_plugins = get_option( 'active_plugins', array() );
		$is_active = in_array( $plugin_slug, $active_plugins );
		$plugin_exists = file_exists( WP_PLUGIN_DIR . '/' . $plugin_slug );
		$github_url = 'https://updates.appglut.com/plugins/productpage-glut.zip';
		$activate_url = wp_nonce_url( admin_url( 'plugins.php?action=activate&plugin=' . $plugin_slug ), 'activate-plugin_' . $plugin_slug );

		// If plugin doesn't exist or is not active, show integration page
		if ( ! $is_active ) {
			$active_menu = $this->activeMenuTab();
			$this->settingsPageHeader( $active_menu );
			?>
			<div class="wrap shopglut-admin-contents">
				<?php if ( $plugin_exists && $is_active ) : ?>
				<h2><?php echo esc_html__( 'Product Page Layouts', 'shopglut' ); ?></h2>
				<?php endif; ?>

				<?php if ( $plugin_exists && ! $is_active ) : ?>
					<div class="shopglut-productpageglut-integration">
						<div style="max-width: 700px; margin: 20px auto;">
							<div class="shopglut-productpageglut-activate-notice" style="background: #fff; border: 1px solid #c3c4c7; padding: 40px; text-align: center;">
								<div style="font-size: 64px; margin-bottom: 20px;">
									<span class="dashicons dashicons-products" style="color: #2271b1; font-size: 64px; width: 64px; height: 64px;"></span>
								</div>
								<h1 style="color: #1d2327; font-size: 28px; margin: 0 0 8px 0;">
									<?php esc_html_e( 'ProductPageGlut is Ready to Activate!', 'shopglut' ); ?>
								</h1>
								<p style="color: #50575e; font-size: 16px; margin: 0 0 20px 0; max-width: 500px; margin-left: auto; margin-right: auto;">
									<?php esc_html_e( 'Complete WooCommerce product page builder with custom fields, swatches, badges, wishlist, and comparison features.', 'shopglut' ); ?>
								</p>
								<div style="background: #f0f6fc; border-left: 4px solid #2271b1; padding: 20px; margin-bottom: 25px; text-align: center;">
									<p style="margin: 0 0 15px 0; color: #0a4b78; font-size: 15px; font-weight: 600;">
										<?php esc_html_e( 'The plugin is already installed on your site. Just activate it to unlock all features.', 'shopglut' ); ?>
									</p>
									<a href="<?php echo esc_url( $activate_url ); ?>" class="button button-primary button-hero" style="display: inline-flex; align-items: center; justify-content: center; gap: 8px;">
										<span class="dashicons dashicons-yes-alt" style="margin: 0; font-size: 16px; width: 16px; height: 16px;"></span>
										<span><?php esc_html_e( 'Activate ProductPageGlut', 'shopglut' ); ?></span>
									</a>
								</div>
							</div>
						</div>
					</div>

				<?php else : ?>
					<div class="shopglut-productpageglut-integration">
						<div style="max-width: 700px; margin: 20px auto;">
							<div class="shopglut-productpageglut-download-notice" style="background: #fff; border: 1px solid #e0e0e0; padding: 40px; text-align: center; border-radius: 12px; box-shadow: 0 4px 12px rgba(0,0,0,0.08);">
								<div style="font-size: 64px; margin-bottom: 20px;">
									<span class="dashicons dashicons-download" style="color: #2271b1; font-size: 64px; width: 64px; height: 64px;"></span>
								</div>
								<h2 style="color: #2c3e50; font-size: 28px; margin: 0 0 10px 0;">
									<?php esc_html_e( 'Download ProductPageGlut Plugin', 'shopglut' ); ?>
								</h2>
								<p style="color: #50575e; font-size: 16px; margin: 0 0 30px 0; max-width: 600px; margin-left: auto; margin-right: auto;">
									<?php esc_html_e( 'Complete WooCommerce product page builder with custom fields, swatches, badges, wishlist, comparison, and quick view features.', 'shopglut' ); ?>
								</p>
								<div style="background: #f0f6fc; border-left: 4px solid #2271b1; padding: 25px; margin-bottom: 20px; border-radius: 4px;">
									<p style="margin: 0 0 15px 0; color: #0a4b78; font-size: 16px; font-weight: 600;">
										<?php esc_html_e( 'ProductPageGlut is completely free! Download the plugin to unlock powerful product page building features.', 'shopglut' ); ?>
									</p>
									<a href="<?php echo esc_url( $github_url ); ?>" target="_blank" class="button button-primary button-hero" style="font-size: 16px; padding: 12px 30px;">
										<span class="dashicons dashicons-cloud-download"></span> <?php esc_html_e( 'Download ProductPageGlut', 'shopglut' ); ?>
									</a>
								</div>
								<div style="border-top: 1px solid #c3c4c7; padding-top: 25px; color: #646970; font-size: 14px; text-align: center;">
									<p style="margin: 0 0 10px 0; font-weight: 600;">
										<?php esc_html_e( 'Installation Instructions:', 'shopglut' ); ?>
									</p>
									<ol style="margin: 0; padding-left: 20px; text-align: left; display: inline-block;">
										<li><?php esc_html_e( 'Download ProductPageGlut plugin', 'shopglut' ); ?></li>
										<li><?php esc_html_e( 'Go to Plugins → Add New → Upload Plugin', 'shopglut' ); ?></li>
										<li><?php esc_html_e( 'Upload and activate the ProductPageGlut plugin', 'shopglut' ); ?></li>
									</ol>
								</div>
							</div>
						</div>
					</div>
				<?php endif; ?>
			</div>
			<?php
			return;
		}

		// At this point, plugin is active - proceed with module check
		$module_manager = \Shopglut\ModuleManager::get_instance();
		if ( ! $this->is_productdetailsglut_active() && ! $module_manager->is_module_enabled( 'productpageglut' ) ) {
			$active_menu = $this->activeMenuTab();
			$this->settingsPageHeader( $active_menu );
			$module_manager->render_disabled_module_message( 'productpageglut' );
			return;
		}

		// Handle individual delete action FIRST - before any other actions
		if ( isset( $_GET['action'] ) && $_GET['action'] === 'delete' && isset( $_GET['layout_id'] ) ) {
			$layout_id = absint( $_GET['layout_id'] );

			// Verify nonce
			if ( isset( $_GET['_wpnonce'] ) && wp_verify_nonce( sanitize_text_field( wp_unslash( $_GET['_wpnonce'] ) ), 'shopglut_delete_layout_' . $layout_id ) ) {
				// Delete the layout - check which plugin is active and use appropriate class
				$deleted = false;

				// Try Productpageglut first
				if ( class_exists( 'Productpageglut\\layouts\\productPage\\ProductLayoutEntity' ) ) {
					\Productpageglut\layouts\productPage\ProductLayoutEntity::delete_layout( $layout_id );
					$deleted = true;
				}
				// Then try ProductPageGlut
				elseif ( class_exists( 'ProductPageGlut\\ProductPageGlut\\ProductPageGlutLayoutEntity' ) ) {
					\ProductPageGlut\ProductPageGlut\ProductPageGlutLayoutEntity::delete_layout( $layout_id );
					$deleted = true;
				}
				// Then try ProductDetailsGlut (load autoloader if needed)
				elseif ( $this->is_productpageglut_active() ) {
					$autoloader_paths = array(
						WP_PLUGIN_DIR . '/productpage-glut/autoloader.php',
						WP_PLUGIN_DIR . '/productpage-glut/autoloader.php'
					);
					foreach ( $autoloader_paths as $autoloader_path ) {
						if ( file_exists( $autoloader_path ) ) {
							include_once $autoloader_path;
							break;
						}
					}
					if ( class_exists( 'Productpageglut\\layouts\\productPage\\ProductLayoutEntity' ) ) {
						\Productpageglut\layouts\productPage\ProductLayoutEntity::delete_layout( $layout_id );
						$deleted = true;
					} elseif ( class_exists( 'ProductPageGlut\\ProductPageGlut\\ProductPageGlutLayoutEntity' ) ) {
						\ProductPageGlut\ProductPageGlut\ProductPageGlutLayoutEntity::delete_layout( $layout_id );
						$deleted = true;
					} elseif ( class_exists( 'ProductDetailsGlut\\singleProduct\\ProductPageGlutLayoutEntity' ) ) {
						\ProductDetailsGlut\singleProduct\ProductPageGlutLayoutEntity::delete_layout( $layout_id );
						$deleted = true;
					}
				}
				// Then try ShopGlut's embedded class
				elseif ( class_exists( 'Shopglut\\layouts\\singleProduct\\ProductPageGlutLayoutEntity' ) ) {
					\Shopglut\layouts\singleProduct\ProductPageGlutLayoutEntity::delete_layout( $layout_id );
					$deleted = true;
				}

				// Fallback to direct database deletion if no class was found
				if ( ! $deleted ) {
					global $wpdb;
					$table_name = $wpdb->prefix . 'shopglut_single_product_layout';
					$wpdb->delete( $table_name, array( 'id' => $layout_id ), array( '%d' ) );
					// Also delete meta data
					$meta_table = $wpdb->prefix . 'shopglut_productpageglut_meta';
					$wpdb->delete( $meta_table, array( 'layout_id' => $layout_id ), array( '%d' ) );
				}

				// Redirect to avoid resubmission
				wp_safe_redirect( admin_url( 'admin.php?page=shopglut_layouts&view=product_page&deleted=true' ) );
				exit;
			} else {
				wp_die( esc_html__( 'Security check failed.', 'shopglut' ) );
			}
		}

		// Handle direct creation from "Create From New" button
		if ( isset( $_GET['action'] ) && $_GET['action'] === 'create_new' && isset( $_GET['_wpnonce'] ) ) {
			if ( wp_verify_nonce( sanitize_text_field( wp_unslash( $_GET['_wpnonce'] ) ), 'create_new_layout_nonce' ) ) {
				try {
					// Get next layout ID from productpage-glut's table
					global $wpdb;
					$table_name = $wpdb->prefix . 'shopglut_single_product_layout';
					$layout_id = intval($wpdb->get_var("SELECT MAX(id) FROM {$wpdb->prefix}shopglut_single_product_layout")) + 1 ?: 1;			// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Table existence check with caching, safe table name from internal function

					// Create layout directly with default settings
					$current_time = current_time('mysql');
					$data = array(
						'id' => $layout_id,
						'layout_name' => sanitize_text_field('Layout(#' . $layout_id . ')'),
						'layout_template' => 'template1', // Default template - must be a valid template name
						'layout_settings' => '{}', // Default empty JSON object
						'created_at' => $current_time,
						'updated_at' => $current_time
					);
					
					$format = array('%d', '%s', '%s', '%s', '%s', '%s');
					
					
					$inserted = $wpdb->insert($table_name, $data, $format);			// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Table existence check with caching, safe table name from internal function

					
					if ($inserted !== false) {
						// Redirect to editor on success
						$redirect_url = add_query_arg(
							array(
								'page' => 'shopglut_layouts',
								'editor' => 'productpageglut',
								'layout_id' => $layout_id
							),
							admin_url('admin.php')
						);
						wp_safe_redirect($redirect_url);
						exit;
					} else {
						// Handle database insertion failure
						add_action('admin_notices', function() {
							echo '<div class="error notice"><p>' . esc_html__('Failed to create new layout. Please try again.', 'shopglut') . '</p></div>';
						});
					}
				} catch (Exception $e) {
					// Handle any exceptions
					add_action('admin_notices', function() use ($e) {
						echo '<div class="error notice"><p>' . esc_html__('An error occurred: ', 'shopglut') . esc_html($e->getMessage()) . '</p></div>';
					});
				}
			} else {
				// Handle nonce verification failure
				wp_die( esc_html__( 'Security check failed.', 'shopglut' ) );
			}
		}

		
		// Show success/error messages
		if ( isset( $_GET['deleted'] ) && $_GET['deleted'] === 'true' ) {
			echo '<div class="updated notice"><p>' . esc_html__( 'Layout deleted successfully.', 'shopglut' ) . '</p></div>';
		}
		// Show bulk delete success message
		if ( isset( $_GET['deleted'] ) && $_GET['deleted'] === 'bulk' && isset( $_GET['count'] ) ) {
			$count = absint( $_GET['count'] );
			if ( $count > 0 ) {
				printf(
					'<div class="updated notice"><p>%s</p></div>',
					esc_html( sprintf(
						_n(
							'%d layout deleted successfully.',
							'%d layouts deleted successfully.',
							$count,
							'shopglut'
						),
						$count
					) )
				);
			}
		}

		// Handle bulk delete action (check POST before any output)
		if ( isset( $_POST['action'] ) && $_POST['action'] === 'delete' && isset( $_POST['user'] ) ) {
			check_admin_referer( 'bulk-layouts' );

			$layout_ids = array_map( 'absint', wp_unslash( $_POST['user'] ) );

			if ( ! empty( $layout_ids ) ) {
				$deleted_count = 0;
				foreach ( $layout_ids as $layout_id ) {
					// Determine which class to use for deletion
					$deleted = false;

					// Try Productpageglut first
					if ( class_exists( 'Productpageglut\\layouts\\productPage\\ProductLayoutEntity' ) ) {
						$result = \Productpageglut\layouts\productPage\ProductLayoutEntity::delete_layout( $layout_id );
						$deleted = ( $result !== false );
					}
					// Then try ProductPageGlut
					elseif ( class_exists( 'ProductPageGlut\\ProductPageGlut\\ProductPageGlutLayoutEntity' ) ) {
						$result = \ProductPageGlut\ProductPageGlut\ProductPageGlutLayoutEntity::delete_layout( $layout_id );
						$deleted = ( $result !== false );
					}
					// Then try ProductDetailsGlut (load autoloader if needed)
					elseif ( $this->is_productpageglut_active() ) {
						$autoloader_paths = array(
							WP_PLUGIN_DIR . '/productpage-glut/autoloader.php',
							WP_PLUGIN_DIR . '/productpage-glut/autoloader.php'
						);
						foreach ( $autoloader_paths as $autoloader_path ) {
							if ( file_exists( $autoloader_path ) ) {
								include_once $autoloader_path;
								break;
							}
						}
						if ( class_exists( 'Productpageglut\\layouts\\productPage\\ProductLayoutEntity' ) ) {
							$result = \Productpageglut\layouts\productPage\ProductLayoutEntity::delete_layout( $layout_id );
							$deleted = ( $result !== false );
						} elseif ( class_exists( 'ProductPageGlut\\ProductPageGlut\\ProductPageGlutLayoutEntity' ) ) {
							$result = \ProductPageGlut\ProductPageGlut\ProductPageGlutLayoutEntity::delete_layout( $layout_id );
							$deleted = ( $result !== false );
						} elseif ( class_exists( 'ProductDetailsGlut\\singleProduct\\ProductPageGlutLayoutEntity' ) ) {
							$result = \ProductDetailsGlut\singleProduct\ProductPageGlutLayoutEntity::delete_layout( $layout_id );
							$deleted = ( $result !== false );
						}
					}
					// Then try ShopGlut's embedded class
					elseif ( class_exists( 'Shopglut\\layouts\\singleProduct\\ProductPageGlutLayoutEntity' ) ) {
						$result = \Shopglut\layouts\singleProduct\ProductPageGlutLayoutEntity::delete_layout( $layout_id );
						$deleted = ( $result !== false );
					}

					// Fallback to direct database deletion if no class was found
					if ( ! $deleted ) {
						global $wpdb;
						$table_name = $wpdb->prefix . 'shopglut_single_product_layout';
						$result = $wpdb->delete( $table_name, array( 'id' => $layout_id ), array( '%d' ) );
						// Also delete meta data
						$meta_table = $wpdb->prefix . 'shopglut_productpageglut_meta';
						$wpdb->delete( $meta_table, array( 'layout_id' => $layout_id ), array( '%d' ) );
						$deleted = ( $result !== false );
					}

					if ( $deleted ) {
						$deleted_count++;
					}
				}

				// Redirect to avoid resubmission and show success message
				$redirect_url = add_query_arg(
					array(
						'page' => 'shopglut_layouts',
						'view' => 'product_page',
						'deleted' => 'bulk',
						'count' => $deleted_count
					),
					admin_url( 'admin.php' )
				);
				wp_safe_redirect( $redirect_url );
				exit;
			}
		}

		// Handle bulk delete from bottom dropdown
		if ( isset( $_POST['action2'] ) && $_POST['action2'] === 'delete' && isset( $_POST['user'] ) ) {
			check_admin_referer( 'bulk-layouts' );

			$layout_ids = array_map( 'absint', wp_unslash( $_POST['user'] ) );

			if ( ! empty( $layout_ids ) ) {
				$deleted_count = 0;
				foreach ( $layout_ids as $layout_id ) {
					// Determine which class to use for deletion
					$deleted = false;

					// Try Productpageglut first
					if ( class_exists( 'Productpageglut\\layouts\\productPage\\ProductLayoutEntity' ) ) {
						$result = \Productpageglut\layouts\productPage\ProductLayoutEntity::delete_layout( $layout_id );
						$deleted = ( $result !== false );
					}
					// Then try ProductPageGlut
					elseif ( class_exists( 'ProductPageGlut\\ProductPageGlut\\ProductPageGlutLayoutEntity' ) ) {
						$result = \ProductPageGlut\ProductPageGlut\ProductPageGlutLayoutEntity::delete_layout( $layout_id );
						$deleted = ( $result !== false );
					}
					// Then try ProductDetailsGlut (load autoloader if needed)
					elseif ( $this->is_productpageglut_active() ) {
						$autoloader_paths = array(
							WP_PLUGIN_DIR . '/productpage-glut/autoloader.php',
							WP_PLUGIN_DIR . '/productpage-glut/autoloader.php'
						);
						foreach ( $autoloader_paths as $autoloader_path ) {
							if ( file_exists( $autoloader_path ) ) {
								include_once $autoloader_path;
								break;
							}
						}
						if ( class_exists( 'Productpageglut\\layouts\\productPage\\ProductLayoutEntity' ) ) {
							$result = \Productpageglut\layouts\productPage\ProductLayoutEntity::delete_layout( $layout_id );
							$deleted = ( $result !== false );
						} elseif ( class_exists( 'ProductPageGlut\\ProductPageGlut\\ProductPageGlutLayoutEntity' ) ) {
							$result = \ProductPageGlut\ProductPageGlut\ProductPageGlutLayoutEntity::delete_layout( $layout_id );
							$deleted = ( $result !== false );
						} elseif ( class_exists( 'ProductDetailsGlut\\singleProduct\\ProductPageGlutLayoutEntity' ) ) {
							$result = \ProductDetailsGlut\singleProduct\ProductPageGlutLayoutEntity::delete_layout( $layout_id );
							$deleted = ( $result !== false );
						}
					}
					// Then try ShopGlut's embedded class
					elseif ( class_exists( 'Shopglut\\layouts\\singleProduct\\ProductPageGlutLayoutEntity' ) ) {
						$result = \Shopglut\layouts\singleProduct\ProductPageGlutLayoutEntity::delete_layout( $layout_id );
						$deleted = ( $result !== false );
					}

					// Fallback to direct database deletion if no class was found
					if ( ! $deleted ) {
						global $wpdb;
						$table_name = $wpdb->prefix . 'shopglut_single_product_layout';
						$result = $wpdb->delete( $table_name, array( 'id' => $layout_id ), array( '%d' ) );
						// Also delete meta data
						$meta_table = $wpdb->prefix . 'shopglut_productpageglut_meta';
						$wpdb->delete( $meta_table, array( 'layout_id' => $layout_id ), array( '%d' ) );
						$deleted = ( $result !== false );
					}

					if ( $deleted ) {
						$deleted_count++;
					}
				}

				// Redirect to avoid resubmission and show success message
				$redirect_url = add_query_arg(
					array(
						'page' => 'shopglut_layouts',
						'view' => 'product_page',
						'deleted' => 'bulk',
						'count' => $deleted_count
					),
					admin_url( 'admin.php' )
				);
				wp_safe_redirect( $redirect_url );
				exit;
			}
		}

		$active_menu = $this->activeMenuTab();
		$this->settingsPageHeader( $active_menu );

		// Determine which table class to use (classes should already be loaded from pre-loading above)
		$table_class = null;

		if ( class_exists( 'Productpageglut\\layouts\\productPage\\ProductPageListTable' ) ) {
			$table_class = 'Productpageglut\\layouts\\productPage\\ProductPageListTable';
		} elseif ( class_exists( 'ProductPageGlut\\ProductPageGlut\\ProductPageGlutListTable' ) ) {
			$table_class = 'ProductPageGlut\\ProductPageGlut\\ProductPageGlutListTable';
		} elseif ( class_exists( 'ProductDetailsGlut\\singleProduct\\ProductPageGlutListTable' ) ) {
			$table_class = 'ProductDetailsGlut\\singleProduct\\ProductPageGlutListTable';
		}

		// Check if we can display the table
		if ( $table_class !== null ) {
			// Add filters to override URLs when used in shopglut context
			add_filter( 'productpageglut_list_table_page_slug', array( $this, 'filter_list_table_page_slug' ) );
			add_filter( 'productpageglut_list_table_editor_slug', array( $this, 'filter_list_table_editor_slug' ) );

			// Prepare layouts table
			$layouts_table = new $table_class();
			$layouts_table->prepare_items();

			// Generate URLs for the action buttons
			$templates_url = admin_url( 'admin.php?page=shopglut_layouts&view=product_page_templates' );

			$create_new_url = add_query_arg(
				array(
					'page' => 'shopglut_layouts',
					'view' => 'product_page',
					'action' => 'create_new',
					'_wpnonce' => wp_create_nonce('create_new_layout_nonce')
				),
				admin_url('admin.php')
			);
			?>
			<div class="wrap shopglut-admin-contents">
				<h2><?php echo esc_html__( 'Product Page Layouts', 'shopglut' ); ?>
					<a href="<?php echo esc_url( $templates_url ); ?>">
						<span class="add-new-h2"><?php echo esc_html__( 'Create From Templates', 'shopglut' ); ?></span>
					</a>

					<!-- <a href="<?php echo esc_url( $create_new_url ); ?>">
						<span class="add-new-h2"><?php echo esc_html__( 'Create From New', 'shopglut' ); ?></span>
					</a> -->
				</h2>

				<?php if ( $this->is_productpageglut_active() ) : ?>
				<div class="notice notice-info" style="margin-bottom: 20px;">
					<p>
						<?php
						echo esc_html__( 'Product Page layout functionality is also available in the', 'shopglut' );
						echo ' <a href="' . esc_url( admin_url( 'admin.php?page=productpageglut_layouts' ) ) . '">';
						echo esc_html__( 'ProductPageGlut plugin', 'shopglut' );
						echo '</a>.';
						?>
					</p>
				</div>
				<?php elseif ( $this->is_productdetailsglut_active() && ! $this->is_productpageglut_active() ) : ?>
				<div class="notice notice-info" style="margin-bottom: 20px;">
					<p>
						<?php
						echo esc_html__( 'Product Page layout functionality is also available in the', 'shopglut' );
						echo ' <a href="' . esc_url( admin_url( 'admin.php?page=productdetailsglut_layouts' ) ) . '">';
						echo esc_html__( 'ProductDetailsGlut plugin', 'shopglut' );
						echo '</a>.';
						?>
					</p>
				</div>
				<?php endif; ?>

				<form method="post" id="shopglut-layouts-form">
					<?php $layouts_table->display(); ?>
				</form>
			</div>
			<?php
		} else {
			// Show productpageglut integration page when plugin is not active
			$plugin_slug = 'productpage-glut/shopglut.php';
			$active_plugins = get_option( 'active_plugins', array() );
			$is_active = in_array( $plugin_slug, $active_plugins );
			$plugin_exists = file_exists( WP_PLUGIN_DIR . '/' . $plugin_slug );
			$github_url = 'https://updates.appglut.com/plugins/productpage-glut.zip';
			$activate_url = wp_nonce_url( admin_url( 'plugins.php?action=activate&plugin=' . $plugin_slug ), 'activate-plugin_' . $plugin_slug );
			?>
			<div class="wrap shopglut-admin-contents">
				<h2><?php echo esc_html__( 'Product Page Layouts', 'shopglut' ); ?></h2>

				<?php if ( $plugin_exists && ! $is_active ) : ?>
					<div class="shopglut-productpageglut-integration">
						<div style="max-width: 700px; margin: 20px auto;">
							<div class="shopglut-productpageglut-activate-notice" style="background: #fff; border: 1px solid #c3c4c7; padding: 40px; text-align: center;">
								<div style="font-size: 64px; margin-bottom: 20px;">
									<span class="dashicons dashicons-products" style="color: #2271b1; font-size: 64px; width: 64px; height: 64px;"></span>
								</div>
								<h1 style="color: #1d2327; font-size: 28px; margin: 0 0 8px 0;">
									<?php esc_html_e( 'ProductPageGlut is Ready to Activate!', 'shopglut' ); ?>
								</h1>
								<p style="color: #50575e; font-size: 16px; margin: 0 0 20px 0; max-width: 500px; margin-left: auto; margin-right: auto;">
									<?php esc_html_e( 'Complete WooCommerce product page builder with custom fields, swatches, badges, wishlist, and comparison features.', 'shopglut' ); ?>
								</p>
								<div style="background: #f0f6fc; border-left: 4px solid #2271b1; padding: 20px; margin-bottom: 25px; text-align: center;">
									<p style="margin: 0 0 15px 0; color: #0a4b78; font-size: 15px; font-weight: 600;">
										<?php esc_html_e( 'The plugin is already installed on your site. Just activate it to unlock all features.', 'shopglut' ); ?>
									</p>
									<a href="<?php echo esc_url( $activate_url ); ?>" class="button button-primary button-hero" style="display: inline-flex; align-items: center; justify-content: center; gap: 8px;">
										<span class="dashicons dashicons-yes-alt" style="margin: 0; font-size: 16px; width: 16px; height: 16px;"></span>
										<span><?php esc_html_e( 'Activate ProductPageGlut', 'shopglut' ); ?></span>
									</a>
								</div>
							</div>
						</div>
					</div>

				<?php else : ?>
					<div class="shopglut-productpageglut-integration">
						<div style="max-width: 700px; margin: 20px auto;">
							<div class="shopglut-productpageglut-download-notice" style="background: #fff; border: 1px solid #e0e0e0; padding: 40px; text-align: center; border-radius: 12px; box-shadow: 0 4px 12px rgba(0,0,0,0.08);">
								<div style="font-size: 64px; margin-bottom: 20px;">
									<span class="dashicons dashicons-download" style="color: #2271b1; font-size: 64px; width: 64px; height: 64px;"></span>
								</div>
								<h2 style="color: #2c3e50; font-size: 28px; margin: 0 0 10px 0;">
									<?php esc_html_e( 'Download ProductPageGlut Plugin', 'shopglut' ); ?>
								</h2>
								<p style="color: #50575e; font-size: 16px; margin: 0 0 30px 0; max-width: 600px; margin-left: auto; margin-right: auto;">
									<?php esc_html_e( 'Complete WooCommerce product page builder with custom fields, swatches, badges, wishlist, comparison, and quick view features.', 'shopglut' ); ?>
								</p>
								<div style="background: #f0f6fc; border-left: 4px solid #2271b1; padding: 25px; margin-bottom: 20px; border-radius: 4px;">
									<p style="margin: 0 0 15px 0; color: #0a4b78; font-size: 16px; font-weight: 600;">
										<?php esc_html_e( 'ProductPageGlut is completely free! Download the plugin to unlock powerful product page building features.', 'shopglut' ); ?>
									</p>
									<a href="<?php echo esc_url( $github_url ); ?>" target="_blank" class="button button-primary button-hero" style="font-size: 16px; padding: 12px 30px;">
										<span class="dashicons dashicons-cloud-download"></span> <?php esc_html_e( 'Download ProductPageGlut', 'shopglut' ); ?>
									</a>
								</div>
								<div style="border-top: 1px solid #c3c4c7; padding-top: 25px; color: #646970; font-size: 14px; text-align: center;">
									<p style="margin: 0 0 10px 0; font-weight: 600;">
										<?php esc_html_e( 'Installation Instructions:', 'shopglut' ); ?>
									</p>
									<ol style="margin: 0; padding-left: 20px; text-align: left; display: inline-block;">
										<li><?php esc_html_e( 'Download ProductPageGlut plugin', 'shopglut' ); ?></li>
										<li><?php esc_html_e( 'Go to Plugins → Add New → Upload Plugin', 'shopglut' ); ?></li>
										<li><?php esc_html_e( 'Upload and activate the ProductPageGlut plugin', 'shopglut' ); ?></li>
									</ol>
								</div>
							</div>
						</div>
					</div>
				<?php endif; ?>
			</div>
			<?php
		}
	}

	public function renderWooBuilderPage() {
		$module_manager = \Shopglut\ModuleManager::get_instance();
		$logo_url = SHOPGLUT_URL . 'global-assets/images/header-logo.svg';

		// Get plugin versions info
		$versions = get_option( 'shopglut_related_plugins_versions', array() );

		?>
		<div class="shopglut-page-header">
			<div class="shopglut-page-header-wrap">
				<div class="shopglut-page-header-banner shopglut-pro shopglut-no-submenu">
					<div class="shopglut-page-header-banner__logo">
						<img src="<?php echo esc_url( $logo_url ); ?>" alt="">
					</div>
					<div class="shopglut-page-header-banner__helplinks">
						<span><a rel="noopener"
								href="https://documentation.appglut.com/?utm_source=shoglutplugin-admin&utm_medium=referral&utm_campaign=adminmenu"
								target="_blank">
								<span class="dashicons dashicons-admin-page"></span>
								<?php echo esc_html__( 'Documentation', 'shopglut' ); ?>
							</a></span>
						<span><a class="shopglut-active" rel="noopener"
								href="https://www.appglut.com/plugin/shopglut/?utm_source=shoglutplugin-admin&utm_medium=referral&utm_campaign=upgrade"
								target="_blank">
								<span class="dashicons dashicons-unlock"></span>
								<?php echo esc_html__( 'Unlock Pro Edition', 'shopglut' ); ?>
							</a></span>
						<span><a rel="noopener"
								href="https://www.appglut.com/support/?utm_source=shoglutplugin-admin&utm_medium=referral&utm_campaign=support"
								target="_blank">
								<span class="dashicons dashicons-share-alt"></span>
								<?php echo esc_html__( 'Support', 'shopglut' ); ?>
							</a></span>
					</div>
					<div class="clear"></div>
				</div>
			</div>
		</div>
		<div class="wrap shopglut-admin-contents shopg-woo-builder">
			<h1><?php echo esc_html__( 'WooCommerce Builder Modules', 'shopglut' ); ?></h1>
			<p class="subheading">
				<?php echo esc_html__( 'Choose your module to build and customize your WooCommerce store.', 'shopglut' ); ?>
			</p>

			<style>
				.shopglut-modules-grid {
					display: grid;
					grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
					gap: 20px;
					margin-top: 20px;
				}
				.shopglut-module-card-new {
					background: #fff;
					border: 1px solid #ddd;
					border-radius: 8px;
					overflow: hidden;
					transition: all 0.3s ease;
					box-shadow: 0 2px 4px rgba(0,0,0,0.05);
					position: relative;
				}
				.shopglut-module-card-new:hover {
					box-shadow: 0 4px 12px rgba(0,0,0,0.1);
					transform: translateY(-2px);
				}
				.shopglut-module-card-new.disabled {
					opacity: 0.7;
				}
				/* Loading overlay */
				.shopglut-module-card-new.loading .module-loading-overlay {
					display: flex;
				}
				.shopglut-module-card-new .module-loading-overlay {
					display: none;
					position: absolute;
					top: 0;
					left: 0;
					right: 0;
					bottom: 0;
					background: rgba(255, 255, 255, 0.85);
					z-index: 100;
					align-items: center;
					justify-content: center;
					flex-direction: column;
				}
				.shopglut-module-card-new .module-loading-spinner {
					width: 40px;
					height: 40px;
					border: 4px solid #f3f3f3;
					border-top: 4px solid #2271b1;
					border-radius: 50%;
					animation: module-spin 1s linear infinite;
				}
				@keyframes module-spin {
					0% { transform: rotate(0deg); }
					100% { transform: rotate(360deg); }
				}
				.shopglut-module-card-new .module-header {
					padding: 20px;
					text-align: center;
					border-bottom: 1px solid #eee;
					position: relative;
				}
				.shopglut-module-card-new .module-icon {
					font-size: 36px;
					margin-bottom: 10px;
					color: #2271b1;
				}
				.shopglut-module-card-new h3 {
					margin: 0 0 8px 0;
					font-size: 18px;
					color: #23282d;
				}
				.shopglut-module-card-new .module-description {
					color: #666;
					margin: 0;
					line-height: 1.5;
					font-size: 14px;
				}
				/* Version Info Styles */
				.shopglut-module-card-new .module-versions {
					display: flex;
					flex-wrap: wrap;
					gap: 10px;
					margin-bottom: 8px;
				}
				.shopglut-module-card-new .version-label {
					font-size: 11px;
					padding: 3px 8px;
					border-radius: 3px;
					background: #f0f0f1;
					color: #646970;
				}
				.shopglut-module-card-new .version-label strong {
					margin-left: 4px;
				}
				.shopglut-module-card-new .version-label.latest-version.update-available {
					background: #edfaef;
					color: #00a32a;
					border: 1px solid #00a32a;
				}
				.shopglut-module-card-new .plugin-update-badge {
					margin-bottom: 8px;
				}
				.shopglut-module-card-new .update-badge {
					display: inline-block;
					background: #d63638;
					color: #fff;
					padding: 3px 8px;
					border-radius: 3px;
					font-size: 11px;
					font-weight: 600;
					animation: pulse 2s infinite;
				}
				@keyframes pulse {
					0%, 100% { opacity: 1; }
					50% { opacity: 0.8; }
				}
				.shopglut-module-card-new .module-body {
					padding: 15px 20px;
					background: #f9f9f9;
				}
				.shopglut-module-card-new .module-features-title {
					font-size: 13px;
					font-weight: 600;
					color: #444;
					margin: 0 0 10px 0;
					text-transform: uppercase;
					letter-spacing: 0.5px;
				}
				.shopglut-module-card-new .module-features {
					list-style: none;
					padding: 0;
					margin: 0;
					display: grid;
					grid-template-columns: repeat(2, 1fr);
					gap: 8px 15px;
				}
				.shopglut-module-card-new .module-features li {
					padding: 4px 0 4px 18px;
					position: relative;
					color: #555;
					font-size: 13px;
					line-height: 1.4;
				}
				.shopglut-module-card-new .module-features li:before {
					content: "✓";
					position: absolute;
					left: 0;
					color: #2271b1;
					font-weight: bold;
					font-size: 12px;
				}
				.shopglut-module-card-new .module-footer {
					padding: 15px 20px;
					text-align: center;
					border-top: 1px solid #eee;
				}
				.shopglut-module-card-new .module-actions {
					display: flex;
					gap: 10px;
					justify-content: center;
					flex-wrap: wrap;
				}
				.shopglut-module-card-new .module-link {
					display: inline-flex;
					align-items: center;
					gap: 5px;
					padding: 10px 20px;
					background: #2271b1;
					color: #fff;
					text-decoration: none;
					border-radius: 4px;
					font-size: 14px;
					font-weight: 500;
					transition: background 0.3s ease;
				}
				.shopglut-module-card-new .module-link:hover {
					background: #135e96;
					color: #fff;
				}
				.shopglut-module-card-new .module-link.secondary {
					background: #fff;
					color: #2271b1;
					border: 1px solid #2271b1;
				}
				.shopglut-module-card-new .module-link.secondary:hover {
					background: #f0f6fc;
				}
				.shopglut-module-card-new.disabled .module-link {
					background: #ccc;
					border-color: #ccc;
					pointer-events: none;
				}
				@media (max-width: 1200px) {
					.shopglut-modules-grid {
						grid-template-columns: 1fr;
					}
				}
			</style>

			<!-- Grid Block Container -->
			<div class="shopglut-modules-grid">

				<?php
				// Get all modules from ModuleManager with their URLs and features
				$all_modules = $module_manager->get_modules();
				$module_urls = $this->getModuleUrls();
				$module_features = $this->getModuleFeatures();
				$module_plugins = $this->getModulePluginInfo();

				foreach ( $all_modules as $module_key => $module_info ) {
					// Skip modules that are marked as coming soon
					$is_coming_soon = isset( $module_info['coming_soon'] ) && $module_info['coming_soon'];
					if ( $is_coming_soon ) {
						continue;
					}

					$is_enabled = $module_manager->is_module_enabled( $module_key );
					$disabled_class = $is_enabled ? '' : 'disabled';
					$module_url = isset( $module_urls[ $module_key ] ) ? $module_urls[ $module_key ] : '#';
					$features = isset( $module_features[ $module_key ] ) ? $module_features[ $module_key ] : [];

					// Get plugin version info
					$plugin_info = isset( $module_plugins[ $module_key ] ) ? $module_plugins[ $module_key ] : null;
					$current_version = null;
					$latest_version = null;
					$has_update = false;
					$version_info = null;

					if ( $plugin_info && isset( $plugin_info['basename'] ) ) {
						$current_version = $this->is_plugin_installed( $plugin_info['basename'] ) ? $this->get_plugin_version( $plugin_info['basename'] ) : null;
						$latest_version = isset( $versions[ $module_key ]['version'] ) ? $versions[ $module_key ]['version'] : null;
						$has_update = $latest_version && $current_version && version_compare( $current_version, $latest_version, '<' );
						$version_info = isset( $versions[ $module_key ] ) ? $versions[ $module_key ] : null;
					}
					?>
					<div class="shopglut-module-card-new <?php echo esc_attr( $disabled_class ); ?>" data-module-card="<?php echo esc_attr( $module_key ); ?>">
						<div class="module-loading-overlay">
							<div class="module-loading-spinner"></div>
						</div>
						<div class="module-header">
							<div class="module-icon">
								<i class="<?php echo esc_attr( $module_info['icon'] ); ?>"></i>
							</div>
							<h3><?php echo esc_html( $module_info['name'] ); ?></h3>
							<p class="module-description"><?php echo esc_html( $module_info['description'] ); ?></p>

							<!-- Version Info -->
							<?php if ( $plugin_info ) : ?>
								<div class="module-versions">
									<?php if ( $current_version ) : ?>
										<span class="version-label current-version">
											<?php esc_html_e( 'Current:', 'shopglut' ); ?>
											<strong><?php echo esc_html( $current_version ); ?></strong>
										</span>
									<?php endif; ?>

									<?php if ( $latest_version ) : ?>
										<span class="version-label latest-version <?php echo $has_update ? 'update-available' : ''; ?>">
											<?php esc_html_e( 'Latest:', 'shopglut' ); ?>
											<strong><?php echo esc_html( $latest_version ); ?></strong>
										</span>
									<?php endif; ?>
								</div>

								<?php if ( $has_update ) : ?>
									<div class="plugin-update-badge">
										<span class="update-badge">
											<?php esc_html_e( 'Update Available!', 'shopglut' ); ?>
										</span>
									</div>
								<?php endif; ?>
							<?php elseif ( isset( $is_coming_soon ) && $is_coming_soon ) : ?>
								<div class="plugin-update-badge">
									<span class="update-badge" style="background: #646970;">
										<?php esc_html_e( 'Coming Soon', 'shopglut' ); ?>
									</span>
								</div>
							<?php endif; ?>

						<?php if ( ! empty( $features ) ) : ?>
						<div class="module-body">
							<h4 class="module-features-title"><?php echo esc_html__( 'Features', 'shopglut' ); ?></h4>
							<ul class="module-features">
								<?php foreach ( $features as $feature ) : ?>
									<li><?php echo esc_html( $feature ); ?></li>
								<?php endforeach; ?>
							</ul>
						</div>
						<?php endif; ?>
						<div class="module-footer">
							<div class="module-actions">
								<?php if ( $has_update && $version_info ) : ?>
									<!-- Update Now button -->
									<button type="button" class="module-link shopglut-update-plugin"
										data-slug="<?php echo esc_attr( $module_key ); ?>"
										data-basename="<?php echo esc_attr( $plugin_info['basename'] ); ?>"
										data-zip="<?php echo esc_url( $version_info['zip_url'] ); ?>"
										data-nonce="<?php echo esc_attr( wp_create_nonce( 'shopglut_update_plugin_' . $module_key ) ); ?>">
										<span class="dashicons dashicons-update-alt"></span>
										<?php esc_html_e( 'Update Now', 'shopglut' ); ?>
									</button>
								<?php endif; ?>

								<?php if ( $is_enabled ) : ?>
									<a href="<?php echo esc_url( $module_url ); ?>" class="module-link">
										<span class="dashicons dashicons-admin-generic"></span>
										<?php esc_html_e( 'Configure', 'shopglut' ); ?>
									</a>
								<?php else : ?>
									<span class="module-link disabled" style="opacity: 0.6;">
										<span class="dashicons dashicons-admin-generic"></span>
										<?php esc_html_e( 'Not Available', 'shopglut' ); ?>
									</span>
								<?php endif; ?>

								<?php if ( $plugin_info && $version_info ) : ?>
									<?php if ( ! $current_version ) : ?>
										<!-- Download Plugin button (not installed) -->
										<a href="<?php echo esc_url( $version_info['zip_url'] ); ?>" target="_blank" class="module-link secondary">
											<span class="dashicons dashicons-download"></span>
											<?php esc_html_e( 'Download Plugin', 'shopglut' ); ?>
										</a>
									<?php elseif ( ! $has_update ) : ?>
										<!-- View Release button (installed, up to date) -->
										<a href="<?php echo esc_url( $version_info['url'] ); ?>" target="_blank" class="module-link secondary">
											<span class="dashicons dashicons-media-text"></span>
											<?php esc_html_e( 'View Release', 'shopglut' ); ?>
										</a>
									<?php endif; ?>
								<?php endif; ?>
							</div>
						</div>
					</div>
					<?php
				}
				?>

			</div>
		</div>

		<script>
		jQuery(document).ready(function($) {
			// Update Plugin button
			$('.shopglut-update-plugin').on('click', function() {
				var $btn = $(this);
				var slug = $btn.data('slug');
				var basename = $btn.data('basename');
				var zipUrl = $btn.data('zip');
				var nonce = $btn.data('nonce');
				var $card = $btn.closest('.shopglut-module-card-new');
				var $progress = $card.find('.module-loading-overlay');

				// Show progress
				$btn.prop('disabled', true);
				$progress.show();
				$progress.find('.progress-text').text('<?php esc_html_e( 'Updating...', 'shopglut' ); ?>');

				$.ajax({
					url: ajaxurl,
					type: 'POST',
					data: {
						action: 'shopglut_update_plugin',
						plugin: basename,
						zip_url: zipUrl,
						slug: slug,
						nonce: nonce
					},
					success: function(response) {
						if (response.success) {
							$progress.find('.progress-text').text('<?php esc_html_e( 'Update complete! Reloading...', 'shopglut' ); ?>');
							setTimeout(function() {
								location.reload();
							}, 1500);
						} else {
							$progress.hide();
							$btn.prop('disabled', false);
							alert('<?php esc_html_e( 'Update failed: ', 'shopglut' ); ?>' + (response.data || 'Unknown error'));
						}
					},
					error: function() {
						$progress.hide();
						$btn.prop('disabled', false);
						alert('<?php esc_html_e( 'Update failed: Server error', 'shopglut' ); ?>');
					}
				});
			});
		});
		</script>
		<?php
	}

	public function renderWooCommerceLayouts() {
		if ( ! current_user_can( 'manage_options' ) ) {
			wp_die( esc_html__( 'You do not have sufficient permissions to access this page.', 'shopglut' ) );
		}

		$active_menu = $this->activeMenuTab();
		$this->settingsPageHeader( $active_menu );
		?>
		<div class="wrap shopglut-admin-contents">
			<h2 style="text-align: center; font-weight: bold;"><?php echo esc_html__( 'WooCommerce Layouts', 'shopglut' ); ?></h2>
			<p class="subheading" style="text-align: center;">
				<?php echo esc_html__( 'Design and customize your WooCommerce store pages with our powerful layout builder', 'shopglut' ); ?>
			</p>
			<div class="shopglut-enhancements-grid" style="display: grid; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); gap: 20px; margin-top: 30px;">
				
				<!-- Product Page -->
				<div class="shopglut-option-card" style="background: #fff; border: 1px solid #ddd; border-radius: 8px; padding: 20px; box-shadow: 0 2px 4px rgba(0,0,0,0.1);">
					<div class="option-header" style="display: flex; align-items: center; margin-bottom: 15px;">
						<i class="fas fa-cube" style="font-size: 24px; color: #667eea; margin-right: 12px;"></i>
						<h3 style="margin: 0; color: #333;"><?php echo esc_html__( 'Product Page', 'shopglut' ); ?></h3>
					</div>
					<p style="color: #666; margin-bottom: 15px;"><?php echo esc_html__( 'Create stunning product detail pages with custom layouts and elements.', 'shopglut' ); ?></p>
					<a href="<?php echo esc_url( admin_url( 'admin.php?page=shopglut_layouts&view=product_page' ) ); ?>" class="button button-primary"><?php echo esc_html__( 'Manage Layouts', 'shopglut' ); ?></a>
				</div>

				<!-- Cart Page -->
				<div class="shopglut-option-card" style="background: #fff; border: 1px solid #ddd; border-radius: 8px; padding: 20px; box-shadow: 0 2px 4px rgba(0,0,0,0.1);">
					<div class="option-header" style="display: flex; align-items: center; margin-bottom: 15px;">
						<i class="fas fa-shopping-cart" style="font-size: 24px; color: #667eea; margin-right: 12px;"></i>
						<h3 style="margin: 0; color: #333;"><?php echo esc_html__( 'Cart Page', 'shopglut' ); ?></h3>
					</div>
					<p style="color: #666; margin-bottom: 15px;"><?php echo esc_html__( 'Customize your shopping cart page layout and enhance user experience.', 'shopglut' ); ?></p>
					<a href="<?php echo esc_url( admin_url( 'admin.php?page=shopglut_layouts&view=cartpage' ) ); ?>" class="button button-primary"><?php echo esc_html__( 'Manage Layouts', 'shopglut' ); ?></a>
				</div>

				<!-- Checkout Page -->
				<div class="shopglut-option-card" style="background: #fff; border: 1px solid #ddd; border-radius: 8px; padding: 20px; box-shadow: 0 2px 4px rgba(0,0,0,0.1);">
					<div class="option-header" style="display: flex; align-items: center; margin-bottom: 15px;">
						<i class="fas fa-credit-card" style="font-size: 24px; color: #667eea; margin-right: 12px;"></i>
						<h3 style="margin: 0; color: #333;"><?php echo esc_html__( 'Checkout Page', 'shopglut' ); ?></h3>
					</div>
					<p style="color: #666; margin-bottom: 15px;"><?php echo esc_html__( 'Optimize your checkout process with custom fields and layouts.', 'shopglut' ); ?></p>
					<a href="<?php echo esc_url( admin_url( 'admin.php?page=shopglut_layouts&view=checkout' ) ); ?>" class="button button-primary"><?php echo esc_html__( 'Manage Layouts', 'shopglut' ); ?></a>
				</div>

				<!-- Order Complete Page -->
				<div class="shopglut-option-card" style="background: #fff; border: 1px solid #ddd; border-radius: 8px; padding: 20px; box-shadow: 0 2px 4px rgba(0,0,0,0.1);">
					<div class="option-header" style="display: flex; align-items: center; margin-bottom: 15px;">
						<i class="fas fa-check-circle" style="font-size: 24px; color: #667eea; margin-right: 12px;"></i>
						<h3 style="margin: 0; color: #333;"><?php echo esc_html__( 'Order Complete Page', 'shopglut' ); ?></h3>
					</div>
					<p style="color: #666; margin-bottom: 15px;"><?php echo esc_html__( 'Create engaging order confirmation pages to delight your customers.', 'shopglut' ); ?></p>
					<a href="<?php echo esc_url( admin_url( 'admin.php?page=shopglut_layouts&view=ordercomplete' ) ); ?>" class="button button-primary"><?php echo esc_html__( 'Manage Layouts', 'shopglut' ); ?></a>
				</div>

				<!-- My Account Page -->
				<div class="shopglut-option-card" style="background: #fff; border: 1px solid #ddd; border-radius: 8px; padding: 20px; box-shadow: 0 2px 4px rgba(0,0,0,0.1);">
					<div class="option-header" style="display: flex; align-items: center; margin-bottom: 15px;">
						<i class="fas fa-user-circle" style="font-size: 24px; color: #667eea; margin-right: 12px;"></i>
						<h3 style="margin: 0; color: #333;"><?php echo esc_html__( 'My Account Page', 'shopglut' ); ?></h3>
					</div>
					<p style="color: #666; margin-bottom: 15px;"><?php echo esc_html__( 'Design personalized customer account pages with custom dashboards.', 'shopglut' ); ?></p>
					<a href="<?php echo esc_url( admin_url( 'admin.php?page=shopglut_layouts&view=accountpage' ) ); ?>" class="button button-primary"><?php echo esc_html__( 'Manage Layouts', 'shopglut' ); ?></a>
				</div>
				<!-- Shop Page -->
				<div class="shopglut-option-card" style="background: #fff; border: 1px solid #ddd; border-radius: 8px; padding: 20px; box-shadow: 0 2px 4px rgba(0,0,0,0.1);">
					<div class="option-header" style="display: flex; align-items: center; margin-bottom: 15px;">
						<i class="fas fa-store" style="font-size: 24px; color: #667eea; margin-right: 12px;"></i>
						<h3 style="margin: 0; color: #333;"><?php echo esc_html__( 'Shop Page', 'shopglut' ); ?></h3>
					</div>
					<p style="color: #666; margin-bottom: 15px;"><?php echo esc_html__( 'Design beautiful shop pages with advanced product grids and filtering.', 'shopglut' ); ?></p>
					<a href="<?php echo esc_url( admin_url( 'admin.php?page=shopglut_layouts&view=shop' ) ); ?>" class="button button-primary"><?php echo esc_html__( 'Manage Layouts', 'shopglut' ); ?></a>
				</div>

			</div>
		</div>
		<?php
	}

	/**
	 * Get URLs for all modules
	 */
	private function getModuleUrls() {
		return [
			// Layout Modules - single_product points to external plugin
			'single_product' => admin_url( 'admin.php?page=productpageglut_layouts' ),
			// Other modules are coming soon - no URLs
		];
	}

	/**
	 * Get features for all modules
	 */
	private function getModuleFeatures() {
		return [
			// Layout Modules
			'single_product' => [
				__( 'Multiple ready-to-use templates', 'shopglut' ),
				__( 'Live preview with demo mode', 'shopglut' ),
				__( 'Product image & gallery styles', 'shopglut' ),
				__( 'Apply to all/specific products', 'shopglut' )
			],
			// Other modules are coming soon - no features listed
		];
	}

	/**
	 * Get plugin information for each module
	 * Maps module keys to their plugin basename and homepage
	 */
	private function getModulePluginInfo() {
		return array(
			// Layout Modules - only single_product has external plugin
			'single_product' => array(
				'basename' => 'productpage-glut/productpage-glut.php',
				'homepage' => 'https://wordpress.org/plugins/productpageglut/'
			),
			// Other modules are coming soon - no plugin info
		);
	}

	/**
	 * Check if plugin is installed
	 */
	private function is_plugin_installed( $basename ) {
		if ( ! function_exists( 'get_plugins' ) ) {
			require_once ABSPATH . 'wp-admin/includes/plugin.php';
		}
		$plugins = get_plugins();
		return isset( $plugins[ $basename ] );
	}

	/**
	 * Get plugin version
	 */
	private function get_plugin_version( $basename ) {
		if ( ! function_exists( 'get_plugins' ) ) {
			require_once ABSPATH . 'wp-admin/includes/plugin.php';
		}
		$plugins = get_plugins();
		if ( isset( $plugins[ $basename ] ) ) {
			return $plugins[ $basename ]['Version'];
		}
		return null;
	}

	/**
	 * Check if ProductPageGlut plugin is active
	 *
	 * @return bool True if ProductPageGlut is active
	 */
	private function is_productpageglut_active() {
		// Check by active plugins list
		$active_plugins = apply_filters( 'active_plugins', get_option( 'active_plugins', array() ) );

		if ( is_multisite() ) {
			// Get network active plugins
			$network_active_plugins = get_site_option( 'active_sitewide_plugins', array() );
			$active_plugins = array_merge( $active_plugins, array_keys( $network_active_plugins ) );
		}

		// Check for various possible plugin file names
		foreach ( $active_plugins as $plugin ) {
			if ( $plugin === 'productpage-glut/productpage-glut.php'
				|| $plugin === 'productpage-glut/productpageglut.php'
				|| $plugin === 'productpage-glut/productpage-glut.php'
				|| strpos( $plugin, 'productpage' ) !== false ) {
				return true;
			}
		}

		// Also check if the main classes exist
		return class_exists( 'Productpageglut\\layouts\\productPage\\ProductLayoutEntity' )
			|| class_exists( 'Productpageglut\\ProductpageglutBase' )
			|| class_exists( 'ProductPageGlut\\ProductPageGlutBase' )
			|| defined( 'PRODUCTPAGEGLUT_VERSION' );
	}

	/**
	 * Check if ProductDetailsGlut plugin is active (legacy support)
	 *
	 * @return bool True if ProductDetailsGlut is active
	 * @deprecated Use is_productpageglut_active() instead
	 */
	private function is_productdetailsglut_active() {
		// Check by active plugins list
		$active_plugins = apply_filters( 'active_plugins', get_option( 'active_plugins', array() ) );

		if ( is_multisite() ) {
			// Get network active plugins
			$network_active_plugins = get_site_option( 'active_sitewide_plugins', array() );
			$active_plugins = array_merge( $active_plugins, array_keys( $network_active_plugins ) );
		}

		// Check for both old and new plugins
		foreach ( $active_plugins as $plugin ) {
			if ( $plugin === 'product-details-glut/product-details-glut.php'
				|| $plugin === 'productpage-glut/productpage-glut.php'
				|| $plugin === 'productpage-glut/productpage-glut.php' ) {
				return true;
			}
		}

		// Also check if the main class exists (both old and new)
		return class_exists( 'ProductDetailsGlut\\ProductDetailsGlutBase' )
			|| class_exists( 'ProductPageGlut\\ProductPageGlutBase' )
			|| class_exists( 'Productpageglut\\ProductpageglutBase' );
	}

	/**
	 * Check if ShopArchivePageGlut plugin is active
	 *
	 * @return bool True if ShopArchivePageGlut is active
	 */
	private function is_shoparchivepageglut_active() {
		// Check by active plugins list
		$active_plugins = apply_filters( 'active_plugins', get_option( 'active_plugins', array() ) );

		if ( is_multisite() ) {
			// Get network active plugins
			$network_active_plugins = get_site_option( 'active_sitewide_plugins', array() );
			$active_plugins = array_merge( $active_plugins, array_keys( $network_active_plugins ) );
		}

		// Check for shop-archivepage-glut plugin
		foreach ( $active_plugins as $plugin ) {
			if ( $plugin === 'shop-archivepage-glut/shop-archivepage-glut.php'
				|| strpos( $plugin, 'shop-archivepage' ) !== false ) {
				return true;
			}
		}

		// Also check if the main class exists
		return class_exists( 'Shoparchivepageglut\\ShoparchivepageglutBase' )
			|| class_exists( 'Shoparchivepageglut\\layouts\\shopLayout\\ShopLayoutEntity' )
			|| defined( 'SHOPARCHIVEPAGEGLUT_VERSION' );
	}

	/**
	 * Filter the list table page slug when used in shopglut context
	 *
	 * @param string $page_slug The default page slug
	 * @return string The filtered page slug
	 */
	public function filter_list_table_page_slug( $page_slug ) {
		return 'shopglut_layouts';
	}

	/**
	 * Filter the list table editor slug when used in shopglut context
	 *
	 * @param string $editor_slug The default editor slug
	 * @return string The filtered editor slug
	 */
	public function filter_list_table_editor_slug( $editor_slug ) {
		return 'productpageglut';
	}

	/**
	 * Filter the list table view when used in shopglut context
	 *
	 * @param string $view The default view
	 * @return string The filtered view
	 */
	public function filter_list_table_view( $view ) {
		return 'product_page';
	}

	/**
	 * Filter the templates page slug when used in shopglut context
	 *
	 * @param string $page_slug The default page slug
	 * @return string The filtered page slug
	 */
	public function filter_templates_page_slug( $page_slug ) {
		return 'shopglut_layouts';
	}

	/**
	 * Filter the templates editor slug when used in shopglut context
	 *
	 * @param string $editor_slug The default editor slug
	 * @return string The filtered editor slug
	 */
	public function filter_templates_editor_slug( $editor_slug ) {
		return 'productpageglut';
	}

	public static function get_instance() {
		static $instance;

		if ( is_null( $instance ) ) {
			$instance = new self();
		}
		return $instance;
	}
}
