<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}
	$cat_args = array(
		'hide_empty'   => 0,
		'taxonomy'     => 'product_cat',
		'hierarchical' => 1,
		'orderby'      => 'name',
		'order'        => 'ASC',
		'child_of'     => 0,
	);

	$cat_hierarchy           = elex_fpf_get_cat_hierarchy( 0, $cat_args );
	$category_products_count = get_count_and_category_name();

	$cat_rows = elex_fpf_category_rows( $cat_hierarchy, 0, 'elex_cat_filter', $category_products_count );
	?>
<div id="settings_map_category" >
	<div style="margin: 7px 1px;">
		<div class="elex-fpf-steps-navigator">
			<a href="javascript:void(0);" id ="elex_fpf_sub_map_category" class="elex-select-category elex-select-category-active">
				<?php esc_html_e( 'Map Category', 'elex-product-feed' ); ?>
			</a> |
			<a href="javascript:void(0);" id ="elex_fpf_sub_map_product" class="elex-select-category">
				<?php esc_html_e( 'Map Product', 'elex-product-feed' ); ?>
			</a>   
			<p id="elex_fpf_map_category_edit_text" style="display: none;"><i><?php esc_html_e( 'If you change the existing Google Category, the mapped Google attributes in the feed may be modified or removed based on Google Category.', 'elex-product-feed' ); ?></i></p>         
		</div>
	</div>
	<div id="settings_map_category_1" class="postbox elex-fpf-table-box elex-fpf-table-box-main ">
		<table>
			<tr>
				<td>
					<h1><?php esc_html_e( 'Map Category', 'elex-product-feed' ); ?></h1>
				</td>
				<td>
					<span class='woocommerce-help-tip tooltip' data-tooltip='<?php esc_html_e( 'Map your WooCommerce product categories with corresponding Google categories. Start typing in the Google category field to get options to map. Please make sure to enable mapping by clicking the corresponding checkbox.', 'elex-product-feed' ); ?>'></span>
				</td>
			</tr>
		</table>

		<table id="elex_cat_table" class="widefat">

			<thead>
				<tr>
					<th class="elex-fpf-catmap-checkbox check-column" style="padding-left: inherit;"><input type="checkbox" /></th>
					<th class="elex-fpf-settings-table-cat-map-left"> <b> <?php esc_html_e( 'Product Category', 'elex-product-feed' ); ?> </b> </th>
					<th class="elex-fpf-settings-table-cat-map-middle"> <b> <?php esc_html_e( 'Google Category', 'elex-product-feed' ); ?> </b> </th>
				</tr>
			</thead>

		<?php
		foreach ( $cat_rows as $cat_slug => $cat_name ) {
			?>
				<tr>
			
			<td class="elex-fpf-catmap-checkbox check-column"><input value="<?php echo esc_attr( $cat_name['id'] ); ?>" type="checkbox" /></td>
			<td class="elex-fpf-settings-table-cat-map-left"> <?php echo wp_kses_post( $cat_name['slug'] ); ?></td>
			<td class="elex-fpf-settings-table-cat-map-middle"><div class="elex_google_cats_auto"><input class="typeahead" id="elex_google_cats_<?php echo esc_attr( $cat_name['id'] ); ?>" style="width: 100%;" type="text" placeholder="Google Categories"></div> </td>                
		</tr>
			<?php
		}
		?>

		</table>
		<div style="margin-top: 1%;">
			<button id="category_back_button" class="botton button-large button-primary" ><?php esc_html_e( 'Back', 'elex-product-feed' ); ?></button>
			<button id="save_settings_cat_map" class="botton button-large button-primary" style="float: right;"><?php esc_html_e( 'Continue', 'elex-product-feed' ); ?></button>
			<button id="reset_settings_cat_map" class="botton button-large button-primary"><?php esc_html_e( 'Reset google categories', 'elex-product-feed' ); ?></button>            

		</div>

	</div>
	<div id="settings_map_product" class="postbox elex-fpf-table-box elex-fpf-table-box-main ">

		<table>
			<tr>
				<td>
					<h1><?php esc_html_e( 'Map Product', 'elex-product-feed' ); ?></h1>
				</td>
				<td>
					<span class='woocommerce-help-tip tooltip' data-tooltip='<?php esc_html_e( 'Map your WooCommerce products with corresponding Google categories. Start typing in the Product Field and Google category field to get options to map.', 'elex-product-feed' ); ?>'></span>
				</td>
			</tr>
		</table>
		<input type="hidden" name="" id="elex_map_product_count_id" value="<?php echo 1; ?>">
		<table id="elex_cat_table_product" class=" elex_gfd_table_lines elex_fpf_product_table widefat" style="table-layout: fixed;">

			<thead>
				<tr>                   
					<th class="elex-fpf-settings-table-cat-map-left" style="width: 55%;"> <b> <?php esc_html_e( 'Product ', 'elex-product-feed' ); ?> </b> </th>
					<th class="elex-fpf-settings-table-cat-map-middle" style="width: 40%;"> <b> <?php esc_html_e( 'Google Category', 'elex-product-feed' ); ?> </b> </th>
					<th class="elex-fpf-settings-table-cat-map-right" style="width: 5%; text-align:center !important;"> </th>
				</tr>
			</thead>

			<?php
				$row = '                    
                    <td class="elex-fpf-settings-table-cat-map-left" style="width: 55%;">
                        <select class="wc-product-search elex_fpf_include_products" multiple="multiple" style="width: 100%;height:30px" name="elex_fpf_include_products[]" data-placeholder="' . esc_attr__( 'Search for a product&hellip;', 'elex-product-feed' ) . '" data-action="woocommerce_json_search_products_and_variations"></select>
                    </td>
                    
                    <td class="elex-fpf-settings-table-cat-map-middle elex_google_cats_auto " style="width: 40%;">                        
                        <div class="elex_google_cats_auto" >    
                            <input class="typeahead  elex_fpf_product_google_cat" name="elex_fpf_product_google_cats[]"  style="width: 153%;" type="text" placeholder="Google Categories">                        
                        </div>
                    </td>
                    <td class="elex-fpf-settings-table-cat-map-right" style="width: 5%; text-align:center !important ;"> 
                        <a href="javascript:void(0);"> 
                            <span class="elex-fpf-icon elex-fpf-minus-icon elex-fpf-map-product-remove" title="Remove" style="display: inline-block;" >
                            </span>
                        </a> 
                    </td>
                ';
			?>
			<tbody id="elex_map_product_tbody_id" data-row="<?php echo esc_attr( $row ); ?>">
			   
			</tbody>
`    
		</table>
		<div style="margin-top: 1%;">
			<button id="category_back_button_product" class="botton button-large button-primary" ><?php esc_html_e( 'Back', 'elex-product-feed' ); ?></button>
			<button  class=" elex-fpf-map-product-add botton button-large button-primary" ><?php esc_html_e( 'Add More Rows', 'elex-product-feed' ); ?></button>
			<button id="save_settings_cat_map_product" class="botton button-large button-primary" style="float: right;"><?php esc_html_e( 'Continue', 'elex-product-feed' ); ?></button>
		</div>

	</div>
</div>
<?php
require_once ELEX_FACEBOOK_FEED_TEMPLATE_PATH . '/elex-settings-frontend-map-attributes.php';

function elex_fpf_get_cat_hierarchy( $parent, $args ) {
	 $cats = get_categories( $args );
	$ret   = new stdClass();
	foreach ( $cats as $cat ) {
		if ( $cat->parent == $parent ) {
			$id                 = $cat->cat_ID;
			$ret->$id           = $cat;
			$ret->$id->children = elex_fpf_get_cat_hierarchy( $id, $args );
		}
	}
	return $ret;
	
}

function elex_fpf_category_rows( $categories, $level, $name, $category_products_count ) {
   $html_code        = array();
	$level_indicator = '';
	for ( $i = 0; $i < $level; $i++ ) {
		$level_indicator .= '- ';
	}
	if ( $categories ) {
		foreach ( $categories as $category ) {  
			$category->count                      = isset( $category_products_count[ $category->term_id ] ) ? $category_products_count[ $category->term_id ] : 0;
			$html_code[ $category->slug ]['slug'] = $level_indicator . $category->name . ' <a target = "_blank" href= ' . home_url() . '/wp-admin/edit.php?product_cat=' . $category->slug . '&post_type=product>(' . $category->count . ')</a>';
			$html_code[ $category->slug ]['id']   = $category->term_id;
			if ( $category->children && count( (array) $category->children ) > 0 ) {
				$html_code = array_merge( $html_code, elex_fpf_category_rows( $category->children, $level + 1, $name, $category_products_count ) );
			}
		}
	} else {
		$html_code = esc_html__( 'No categories found.', 'elex-product-feed' );
	}
	return $html_code;
}

function get_count_and_category_name() {
	global $wpdb;
	
	$results        = $wpdb->get_results(
		"SELECT COUNT(ID), t.term_id
    FROM {$wpdb->prefix}posts
		INNER JOIN {$wpdb->prefix}term_relationships as tr ON ( ID = tr.object_id OR post_parent = tr.object_id )
		INNER JOIN {$wpdb->prefix}term_taxonomy as tt ON tr.term_taxonomy_id = tt.term_taxonomy_id
		INNER JOIN {$wpdb->prefix}terms as t ON tt.term_id = t.term_id
		WHERE post_type IN ( 'product' )
		AND post_status = 'publish'
		AND ID NOT IN (
			SELECT DISTINCT post_parent AS parent_id
			FROM {$wpdb->prefix}posts
			WHERE post_type = 'product_variation'
			AND post_status = 'publish'
		)
		AND tt.taxonomy = 'product_cat'
		GROUP BY t.term_id" 
	);
	$count_and_name = wp_list_pluck( $results, 'COUNT(ID)', 'term_id' );
	return $count_and_name;
}
