<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}

ob_start();


function elex_mv_get_count_and_category_name() {
	global $wpdb;
		
	$results = $wpdb->get_results(
		"SELECT COUNT( DISTINCT 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

			INNER JOIN {$wpdb->prefix}termmeta as tm ON t.term_id = tm.term_id
			
			WHERE post_type IN ( 'product' )
			
			AND post_status = 'publish'
		
			AND tt.taxonomy = 'product_cat'
			GROUP BY t.term_id" 
	);


	$count_and_name = wp_list_pluck( $results, 'COUNT( DISTINCT ID)', 'term_id' );
	return $count_and_name;
}

	$category_count_all = elex_mv_get_count_and_category_name();



?>
<div class="d-flex flex-wrap justify-content-between align-items-center">
			
		<div class="flex-1">
			<div class="d-flex flex-wrap mb-3 gap-3">
				<div class="elex-multi-vendor-for-woocommerce-front-product-category-nav">
					<ul class="nav nav-pills nav-fill btn-sm p-1 border border-primary border-2 rounded-3 m-0">
						<li class="nav-item mb-0">

						<a id="elex-multi-vendor-for-woocommerce-product-btn" class="nav-link py-1 <?php ( 'product' === get_query_var( 'tab', 'product' ) ) ? esc_html_e( 'active' , 'elex-multi-vendor-for-woocommerce' ) : ''; ?>" aria-current="page" href="<?php echo esc_url( add_query_arg( 'tab', 'product' ) ); ?>">Products</a>
						</li>
						<li class="nav-item mb-0">
							<a id="elex-multi-vendor-for-woocommerce-categories-btn" class="nav-link py-1 <?php ( 'categories' === get_query_var( 'tab', 'product' ) ) ? esc_html_e( 'active' , 'elex-multi-vendor-for-woocommerce' ) : ''; ?>" href="<?php echo esc_url( add_query_arg( 'tab', 'categories' ) ); ?>">Categories</a>
						</li>
					</ul>
				</div>
				<div class="elex-multi-vendor-for-woocommerce-front-product-category-search">
					<div class="input-group h-100 border rounded ">
						<input value="<?php echo esc_attr( $category_search ); ?>" type="text" placeholder="Search Category" class="form-control p-2 h-100 border-0 m-0" id='elex-search-category-vendor' >
						<button class="input-group-text m-0 border-0" id='elex-search-category-vendor-button'><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-search"><circle cx="11" cy="11" r="8"></circle><line x1="21" y1="21" x2="16.65" y2="16.65"></line></svg></button>
					</div>
				</div>
			</div>
		</div>
		

		<div class="toast p-3 position-fixed top-0 start-50 translate-middle-x bg-white mt-3" id="wsdesk_delete_category_toast" style="min-width: 450px;">
			<div class="toast-header border-0 border-bottom p-0 pb-2">
				<strong class="mx-auto">
					<svg xmlns="http://www.w3.org/2000/svg" width="16" height="18" viewBox="0 0 20 22" class="me-2">
						<g id="Icon_feather-trash-2" data-name="Icon feather-trash-2" transform="translate(-3.5 -2)">
							<path id="Path_648" data-name="Path 648" d="M4.5,9h18" transform="translate(0 -2)" fill="none" stroke="#dc3545" stroke-linecap="round" stroke-linejoin="round" stroke-width="2"/>
							<path id="Path_649" data-name="Path 649" d="M21.5,7V21a2,2,0,0,1-2,2H9.5a2,2,0,0,1-2-2V7m3,0V5a2,2,0,0,1,2-2h4a2,2,0,0,1,2,2V7" transform="translate(-1 0)" fill="none" stroke="#dc3545" stroke-linecap="round" stroke-linejoin="round" stroke-width="2"/>
							<path id="Path_650" data-name="Path 650" d="M15,16.5v6" transform="translate(-3.5 -4.5)" fill="none" stroke="#dc3545" stroke-linecap="round" stroke-linejoin="round" stroke-width="2"/>
							<path id="Path_651" data-name="Path 651" d="M21,16.5v6" transform="translate(-5.5 -4.5)" fill="none" stroke="#dc3545" stroke-linecap="round" stroke-linejoin="round" stroke-width="2"/>
						</g>
						</svg>
						Confirm Delete</strong>
				<button type="button" class="btn-close" data-bs-dismiss="toast" ></button>
			</div>
			<div class="toast-body p-0">
				<div class="my-2">
					<div class="fs-6 mb-1 fw-semi-bold">Are you sure,you want to delete the category?</div>
					<div class="fs-6  mb-1">Deleting this category, all the items will move to uncategorized</div>
				</div>
				

				<div class="d-flex gap-3 justify-content-center ">
					<button class="btn px-4 btn-outline-secondary" data-bs-dismiss="toast">Cancel</button>
					<button class="btn px-4 btn-primary" id="elex-multi-vendor-for-woocommerce-delete-cat-confirm-btn">
						Confirm
					</button>
				</div>
				</div>
			 
		</div>
		<?php
		if ( get_post_meta( $vendor_store_id , 'elex_mv_vendor_to_create_category', true ) ) {
			?>
			<div class="col-lg-2 mb-3">
						<button class="btn btn-primary w-100 h-100 elex-multi-vendor-for-woocommerce-add-category-btn">
							<i class="fa-solid fa-plus"></i>
							New Category
						</button>
					</div>

			<?php
		}
		?>
		
		<div class="bg-dark bg-opacity-25  elex-multi-vendor-for-woocommerce-popup row justify-content-end" id="elex-multi-vendor-for-woocommerce-add-category">
		<div class="toast p-2 position-fixed top-0 start-50 translate-middle-x bg-white elex_new_category_toast">
			<div class="toast-header border-0 ">
				<strong class="me-auto">
					<svg xmlns="http://www.w3.org/2000/svg" width="22.415" height="22.026" viewBox="0 0 22.415 22.026" class="me-2">
						<g id="Icon_feather-check-circle" data-name="Icon feather-check-circle" transform="translate(-1.998 -1.979)">
							<path id="Path_646" data-name="Path 646" d="M23,12.076V13a10,10,0,1,1-5.93-9.139" fill="none" stroke="#28a745" stroke-linecap="round" stroke-linejoin="round" stroke-width="2"/>
							<path id="Path_647" data-name="Path 647" d="M26.5,6l-10,10.009-3-3" transform="translate(-3.5 -1.003)" fill="none" stroke="#28a745" stroke-linecap="round" stroke-linejoin="round" stroke-width="2"/>
						</g>
						</svg>
						<span id ="toast_message_cat_mv">
						New Category Added Successfully
						</span>
						</strong>
				<button type="button" class="btn-close" data-bs-dismiss="toast"></button>
			</div>
			 
		</div>
		<div class="col-md-9 col-lg-6 bg-white h-100 ms-auto overflow-auto">
			<div class="py-2 px-4 mt-5 position-relative">
				<div class="fs-5 fw-semi-bold my-1 text-center">Add Category</div>
				<button class="btn rounded-circle position-absolute top-0 end-0 elex-multi-vendor-for-woocommerce-close-cross-category-btn">
					<i class="fa-solid fa-xmark"></i>
				</button>
				<form id="elex-multi-vendor-for-woocommerce-category-detail-form">
					<?php wp_nonce_field( 'elex_mv_add_cat', 'elex-nonce' ); ?>

					<div class="px-5 py-3">
							<div class="form-group mb-3">
								<div class="fs-6 fw-semi-bold text-dark">Category Name</div>
								<input type="text" class="form-control border-2 border-secondary" name="category_name" id="category_name" >
								<div id="elex_mv_show_error_category_name"></div>	
							</div>
							<input value="<?php echo esc_html( get_current_user_id() ); ?>" type="text" class="form-control border-2 border-secondary" name="cat_vendor_id" id="cat_vendor_id" hidden>
							<div class="form-group mb-3">
								<div class="fs-6 fw-semi-bold text-dark">Parent</div>
								
								<select class="form-control border-2 border-secondary" name="category_parent" id="category_parent">
								<option selected="selected" value=''>--Choose-Category--</option>';

								<?php 
					$taxonom      = 'product_cat';
					$orderb       = 'name';  
					$show_count   = 0;      // 1 for yes, 0 for no
					$pad_counts   = 0;      // 1 for yes, 0 for no
					$hierarchical = 0;      // 1 for yes, 0 for no  
					$title_li     = '';  
					$empty        = 0;
					
					$args       = array(
						'taxonomy'     => $taxonom,
						'orderby'      => $orderb,
						'show_count'   => $show_count,
						'pad_counts'   => $pad_counts,
						'hierarchical' => $hierarchical,
						'title_li'     => $title_li,
						'hide_empty'   => $empty,
					);
					$categories = get_categories( $args );
								foreach ( $categories as $category ) :
								
									?>
							<option value="<?php echo esc_attr( $category->term_id ); ?>"><?php echo esc_html( $category->name ); ?></option>
							
							<?php	  


					endforeach; 
								?>
								</select>
								<!-- <input type="text" class="form-control border-2 border-secondary" name="elex_multivendor_category_parent" id="elex_multivendor_category_parent" > -->

								<div id="elex_mv_show_error_category_parent"></div>	
							</div>
							
						
							<div class="d-flex gap-2">
								<button class="btn btn-primary w-100 elex-multi-vendor-for-woocommerce-add-category-wp-btn" type="button">Add Category</button>
							</div>
					</div>

					
				</form>
			</div>
		</div>
		</div>

</div>




			<!-- product list table -->
			<div class="table-responsive">
				<table class="table table-borderless align-middle text-dark elex-multi-vendor-for-woocommerce-table">
					<thead class="table-light rounded-2">
						<tr class="">
							<th scope="col" width="200px">Categories</th>
							<th scope="col" class="text-center">No. of items</th>
							<th scope="col" width="120px">Actions</th>
						</tr>
					</thead>
					<tbody>

					<?php
$taxonom      = 'product_cat';
$orderb       = 'id';  
$show_count   = 0;      // 1 for yes, 0 for no
$pad_counts   = 0;      // 1 for yes, 0 for no
$hierarchical = 1;      // 1 for yes, 0 for no  
$title_li     = '';  
$empty        = 0;
$searc        = $category_search;

$args       = array(
	'taxonomy'     => $taxonom,
	'orderby'      => $orderb,
	'order'        => 'DESC',
	'show_count'   => $show_count,
	'pad_counts'   => $pad_counts,
	'hierarchical' => $hierarchical,
	'title_li'     => $title_li,
	'hide_empty'   => $empty,
	'search'       => $searc,
);
$categories = get_categories( $args );

$count_of_mv_categories = count( $categories );
					if ( 0 === $count_of_mv_categories ) {
						wp_add_inline_script( 'elex-mv-frontend-categories', 'alert("No results found");' );
					}

					foreach ( $categories as $category ) :
	
						?>
					<tr class="shadow-sm rounded-3">
							<td>
								<div class="fs-6 mb-1"><b><?php echo esc_html( $category->name ); ?></b></div>
							</td>

							<td class="text-center">
								<b>
								<?php 
								
								echo esc_html( isset( $category_count_all[ $category->term_id ] ) ? $category_count_all[ $category->term_id ] : '0' );

								
								?>
								</b>
							</td>
							<td>
							<?php 

							$has_permission = false;

							$catvendor_id = get_term_meta( $category->term_id, 'created_by_vendor', true );

						

							if ( (string) $catvendor_id === (string) $vendor_store_id ) { 
								

								$has_permission = true;
								
							}
						
							if ( $has_permission ) {
								?>
							 
								<button class="btn text-primary d-flex gap-2 align-items-center elex-multi-vendor-for-woocommerce-rename-open-btn" elex-multi-vendor-for-woocommerce-category-id = "<?php echo esc_attr( $category->term_id ); ?>" onclick="renameCategoryFunc(<?php echo esc_js( $category->term_id ); ?>)">
									<img  alt=""  > 
									<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 18.638 18.695">
									<g id="Icon_feather-edit-3" data-name="Icon feather-edit-3" transform="translate(-3.486 -3.183)">
										<path id="Path_29" data-name="Path 29" d="M17.8,30.363H26.12" transform="translate(-4.996 -9.486)" fill="none" stroke="#3f5980" stroke-linecap="round" stroke-linejoin="round" stroke-width="2"/>
										<path id="Path_30" data-name="Path 30" d="M16.964,4.775a1.92,1.92,0,0,1,2.773,0,2.308,2.308,0,0,1,0,3.067L7.952,19.344,4.486,20.878l.693-3.834Z" transform="translate(0 0)" fill="none" stroke="#3f5980" stroke-linecap="round" stroke-linejoin="round" stroke-width="2"/>
									</g>
									</svg>
									Rename</button>
								<button class="btn text-primary d-flex gap-2 align-items-center elex-multi-vendor-for-woocommerce-delete-open-btn" elex-multi-vendor-for-woocommerce-category-id = "<?php echo esc_attr( $category->term_id ); ?>" onclick="deleteCategoryFunc(<?php echo esc_js( $category->term_id ); ?>)" >
									<img  alt=""  >
									<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 19 19">
										<g id="Icon_feather-trash-2" data-name="Icon feather-trash-2" transform="translate(-3.116 -2.5)">
											<path id="Path_31" data-name="Path 31" d="M4.116,9.082h17" transform="translate(0 -2.582)" fill="none" stroke="#3f5980" stroke-linecap="round" stroke-linejoin="round" stroke-width="2"/>
											<path id="Path_32" data-name="Path 32" d="M21.217,6.5v12a2.156,2.156,0,0,1-2,2h-10a2.156,2.156,0,0,1-2-2V6.5m3,0v-2c0-.944.951-1,2-1h4c1.049,0,2,.056,2,1v2" transform="translate(-1.101)" fill="none" stroke="#3f5980" stroke-linecap="round" stroke-linejoin="round" stroke-width="2"/>
											<path id="Path_33" data-name="Path 33" d="M14.969,16.31v5" transform="translate(-3.853 -5.81)" fill="none" stroke="#3f5980" stroke-linecap="round" stroke-linejoin="round" stroke-width="2"/>
											<path id="Path_34" data-name="Path 34" d="M21.171,16.31v5" transform="translate(-6.055 -5.81)" fill="none" stroke="#3f5980" stroke-linecap="round" stroke-linejoin="round" stroke-width="2"/>
										</g>
										</svg>
									Delete</button>
							<?php } ?>
														
							</td>
						</tr>
					<?php
endforeach;

					?>
					</tbody>
				</table>
			</div>

		<div class="bg-dark bg-opacity-25 elex-multi-vendor-for-woocommerce-popup row justify-content-end w-100" id="elex-multi-vendor-for-woocommerce-add-rename">
		<div class="toast p-2 position-fixed top-0 start-50 translate-middle-x bg-white elex_new_category_toast">
			<div class="toast-header border-0 ">
				<strong class="me-auto">
					<svg xmlns="http://www.w3.org/2000/svg" width="22.415" height="22.026" viewBox="0 0 22.415 22.026" class="me-2">
						<g id="Icon_feather-check-circle" data-name="Icon feather-check-circle" transform="translate(-1.998 -1.979)">
							<path id="Path_646" data-name="Path 646" d="M23,12.076V13a10,10,0,1,1-5.93-9.139" fill="none" stroke="#28a745" stroke-linecap="round" stroke-linejoin="round" stroke-width="2"/>
							<path id="Path_647" data-name="Path 647" d="M26.5,6l-10,10.009-3-3" transform="translate(-3.5 -1.003)" fill="none" stroke="#28a745" stroke-linecap="round" stroke-linejoin="round" stroke-width="2"/>
						</g>
						</svg>
						<span id ="toast_message_cat_mv">
						New Category Added Successfully
						</span>
						</strong>
				<button type="button" class="btn-close" data-bs-dismiss="toast"></button>
			</div>
			 
		</div>
			<div class="col-lg-6 col-md-9 mt-3 bg-white h-100 ms-auto overflow-auto">
				<div class="py-2 px-4 text-center position-relative">
					<div class="fs-5 fw-semi-bold">Rename</div>
					<button class="btn rounded-circle position-absolute top-0 end-0 elex-multi-vendor-for-woocommerce-rename-close-btn">
						<i class="fa-solid fa-xmark"></i>
					</button>
				</div>
				<form id="elex-multi-vendor-for-woocommerce-category-rename-form">
				<?php wp_nonce_field( 'elex_mv_rename_cat', 'elex-nonce' ); ?>

				<div class="px-5 py-3">
					<div class="form-group mb-3">
						<input type="text" name="category-current-name" id="category-current-name" class="form-control" hidden>
						<input type="number" name="category-current-id" id="category-current-id" class="form-control" value="s" hidden>

					</div>

					<div class="form-group mb-3">
						<div class="fs-6 fw-semi-bold text-dark">New name</div>
						<input type="text" class="form-control" name="category-new-name" id="category-new-name">
					</div>
					<button class="btn btn-primary w-100" id="elex-multi-vendor-for-woocommerce-category-rename-save-changes" type="button">Save Changes</button>
				</div>
				</form>
			   
			</div>
		</div>

<?php ob_end_flush(); ?>
