<?php
if ( ! defined( 'ABSPATH' ) ) {
	die( 'Direct access is not allowed.' );
}

if ( ! is_admin() ) {
	return;
}
?>

<!-- Products Sync Modal -->
<div id="products-sync-modal" class="finalpos-modal">
	<div class="finalpos-modal-content">
		<div class="finalpos-modal-header">
			<h2 class="finalpos-modal-title"><?php esc_html_e( 'Configure Products Sync', 'finalpos' ); ?></h2>
			<span class="finalpos-modal-close" data-modal-id="products-sync-modal">&times;</span>
		</div>
		<div class="finalpos-modal-body">
			<p class="finalpos-modal-description">
				<?php esc_html_e( 'We will sync all your product categories, or you can choose specific categories to sync from your WooCommerce store to Final POS.', 'finalpos' ); ?>
			</p>
			
			<div class="finalpos-form-group">
				<div class="finalpos-category-header">
					<label class="finalpos-form-label"><?php esc_html_e( 'Category list:', 'finalpos' ); ?></label>
					<div class="finalpos-search-box">
						<input type="text" id="category-search" class="finalpos-form-input" placeholder="<?php esc_attr_e( 'Search for category', 'finalpos' ); ?>">
					</div>
				</div>
				<div class="finalpos-categories-list">
					<div class="finalpos-category-item">
						<label>
							<input type="checkbox" id="select-all-categories" checked>
							<span class="category-name"><?php esc_html_e( 'All Categories', 'finalpos' ); ?></span>
						</label>
					</div>
					<?php
					// Recursive function to display hierarchical categories
					function display_category_hierarchical( $category, $depth = 0 ) {
						$indentation = 20 + ( $depth * 20 );
						echo '<div class="finalpos-category-item" style="padding-left:' . $indentation . 'px;">';
						echo '<label>';
						echo '<input type="checkbox" name="product_categories[]" value="' . esc_attr( $category->term_id ) . '" 
                            data-parent-id="' . $category->parent . '" class="category-checkbox" checked>';
						echo '<span class="category-name">' . esc_html( $category->name ) . '</span>';
						echo '</label>';
						echo '</div>';

						$child_categories = get_terms(
							array(
								'taxonomy'   => 'product_cat',
								'hide_empty' => false,
								'parent'     => $category->term_id,
							)
						);

						if ( ! empty( $child_categories ) && ! is_wp_error( $child_categories ) ) {
							foreach ( $child_categories as $child_category ) {
								display_category_hierarchical( $child_category, $depth + 1 );
							}
						}
					}

					// Get uncategorized separately to place it at the end
					$uncategorized    = get_term_by( 'slug', 'uncategorized', 'product_cat' );
					$uncategorized_id = $uncategorized ? $uncategorized->term_id : 0;

					// Get all top-level categories except uncategorized
					$top_level_categories = get_terms(
						array(
							'taxonomy'   => 'product_cat',
							'hide_empty' => false,
							'parent'     => 0,
							'exclude'    => $uncategorized_id ? array( $uncategorized_id ) : array(),
						)
					);

					if ( ! is_wp_error( $top_level_categories ) && ! empty( $top_level_categories ) ) {
						foreach ( $top_level_categories as $category ) {
							display_category_hierarchical( $category, 0 );
						}
					}

					// Display uncategorized at the end
					if ( $uncategorized_id ) {
						echo '<div class="finalpos-category-item uncategorized" style="padding-left: 20px;">';
						echo '<label>';
						echo '<input type="checkbox" name="product_categories[]" value="' . esc_attr( $uncategorized_id ) . '" 
                            class="category-checkbox" checked>';
						echo '<span class="category-name">' . esc_html( $uncategorized->name ) . '</span>';
						echo '</label>';
						echo '</div>';
					}

					if ( empty( $top_level_categories ) && ! $uncategorized_id ) {
						esc_html_e( 'No product categories found.', 'finalpos' );
					}
					?>
				</div>
			</div>
			
			<div class="finalpos-form-actions">
				<button type="button" class="finalpos-button finalpos-button-secondary modal-cancel" data-modal-id="products-sync-modal"><?php esc_html_e( 'Cancel', 'finalpos' ); ?></button>
				<button type="button" class="finalpos-button modal-save" data-modal-id="products-sync-modal" data-sync-type="products"><?php esc_html_e( 'Save', 'finalpos' ); ?></button>
			</div>
		</div>
	</div>
</div> 