<?php
/* 
* Theme: PREMIUMPRESS CORE FRAMEWORK FILE
* Url: www.premiumpress.com
* Author: Mark Fail
*
* THIS FILE WILL BE UPDATED WITH EVERY UPDATE
* IF YOU WANT TO MODIFY THIS FILE, CREATE A CHILD THEME
*
* http://codex.wordpress.org/Child_Themes
*/
if (!defined('THEME_VERSION')) {	header('HTTP/1.0 403 Forbidden'); exit; }

global $CORE, $CORE_UI, $LAYOUT, $wpdb, $wp_query;

$randID = rand(0,99999);
$count = 1;
$selected_categories = array();
$cats = get_terms( 'country', array( 'hide_empty' => 1, 'parent' => 0  ));
 
///////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////// 
 
?> 


 
<div <?php if(!is_admin() && isset($_POST['action'])){ ?>style="min-height:200px;"<?php } ?>>

 <?php if(isset($_POST['sidebar'])){ ?>
	 
<?php  if(!empty($cats)){ $taxonomy = "country"; ?>
<div class="tax_wrapperxxxxxxxx">
	<?php foreach($cats as $cat){  ?>


		<div class="tax-div tax-val-<?php echo $cat->term_id; ?>">

<label class="custom-control custom-<?php if($cat->parent == 0){ echo "radio"; }else{ echo "checkbox"; } ?> f-<?php echo $taxonomy."-".$cat->term_id; ?>" onclick="showsubcats('<?php echo $cat->term_id; ?>');">	
    <input 
    type="<?php if($cat->parent == 0){ echo "radio"; }else{ echo "checkbox"; } ?>" <?php if(isset($GLOBALS['flag-taxonomy-id']) && $GLOBALS['flag-taxonomy-id'] == $cat->term_id ){  ?>disabled<?php } ?> 
    value="<?php echo $cat->term_id; ?>" 
    name="catid[]" 
    class="custom-control-input customfilter" 
    data-type="checkbox" 
    onclick="_hidesubcats(<?php echo $cat->term_id; ?>);" 
    data-key="taxonomy" 
    data-value="<?php echo $taxonomy; ?>-<?php echo $cat->term_id; ?>"
        <?php if(isset($_GET['tax-'.$taxonomy]) && $_GET['tax-'.$taxonomy] == $cat->term_id){ echo "checked=checked"; } ?>
    >

    <div class="custom-control-label" data-countkey="<?php echo $taxonomy; ?>-<?php echo $cat->term_id; ?>"> 
      <div class="d-flex justify-content-between">      
          
	  		<div class="filtertxt"><span class="flag <?php echo $CORE->GEO("get_flagicon_from_name", array($cat->name, $GLOBALS['core_country_list_new']));  ?>"></span> 
		  	<?php echo $CORE->GEO("translation_tax_value", array($cat->term_id, $cat->name));  ?>

			 

		</div>
          
          <div class="addcount" data-countkey="<?php echo $taxonomy; ?>-<?php echo $cat->term_id; ?>">
			<?php if($cat->count > 0){ ?>
				<span class="badge defaultvalue badge-primary text-light"><?php echo $cat->count; ?></span>

				<?php /*$i=0; while($i < $cat->count ){ ?>
					<div class="addondata <?php echo $taxonomy; ?>-<?php echo $cat->term_id; ?> remove-default"></div>
				<?php $i++; }*/  ?>
				 

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

</div>

<?php $subcats = get_terms( 'country', array( 'hide_empty' => 1, 'parent' => $cat->term_id  )); if(!empty($subcats)){  ?>
	<div class="tax_wrapperxxxxxxxx subcats-<?php echo $cat->term_id; ?>" style="display:none;">
	<?php foreach($subcats as $subcat){  ?>


		<div class="tax-div tax-val-<?php echo $subcat->term_id; ?>">

<label class="custom-control custom-radio f-<?php echo $taxonomy."-".$subcat->term_id; ?> parent-cat-<?php echo $cat->term_id; ?>">	
    <input 
    type="radio" <?php if(isset($GLOBALS['flag-taxonomy-id']) && $GLOBALS['flag-taxonomy-id'] == $subcat->term_id ){  ?>disabled<?php } ?> 
    value="<?php echo $subcat->term_id; ?>" 
    name="catid[]" 
    class="custom-control-input customfilter" 
    data-type="checkbox" 
    onclick="_filter_update()" 
    data-key="taxonomy" 
    data-value="<?php echo $taxonomy; ?>-<?php echo $subcat->term_id; ?>"
        <?php if(isset($_GET['tax-'.$taxonomy]) && $_GET['tax-'.$taxonomy] == $subcat->term_id){ echo "checked=checked"; } ?>
    >

    <div class="custom-control-label" data-countkey="<?php echo $taxonomy; ?>-<?php echo $subcat->term_id; ?>"> 
      <div class="d-flex justify-content-between">      
          <div class="filtertxt"> <?php echo $CORE->GEO("translation_tax_value", array($subcat->term_id, $subcat->name));  ?> </div>
          
          <div class="addcount" data-countkey="<?php echo $taxonomy; ?>-<?php echo $subcat->term_id; ?>">
			<?php if($subcat->count > 0){ ?>
				<span class="badge defaultvalue badge-primary text-light"><?php echo $subcat->count; ?></span>
			<?php } ?>
		</div>
          
      </div>
    </div>
</label>

</div>

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






	<?php } ?>

</div>


<?php } ?>

<script>
function _hidesubcats(pcid){
	 
	if(jQuery('[data-val="country-'+pcid+'"]').length > 0){

		jQuery(".parent-cat-"+pcid+"").each(function(){
			var h = jQuery(this).find("input").val();			
			if(jQuery('[data-val="country-'+h+'"]').length > 0){
				jQuery(this).find("input").trigger('click'); 
				jQuery(this).find("input").prop('checked',false); 
			} 
		
		});
		
	}

	_filter_update();
}
</script>


<?php }else{  
	
///////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////// 

?>
 

<select id="parent_category_list<?php echo $randID; ?>" class="form-control <?php if(!is_admin() && isset($_POST['action'])){ ?>fs-5<?php } ?> mb-3" onchange="UpdateCategoryList<?php echo $randID; ?>()" data-type="text" data-key="taxonomy" name="catid[]">
<option value=""><?php echo __("Any","premiumpress"); ?></option>
<?php

if(!empty($cats)){

	foreach($cats as $cat){ 
	if($cat->parent != 0){ continue; } 

?>
<option value="<?php echo $cat->term_id; ?>" <?php if(in_array($cat->term_id, $selected_categories) ){ echo "selected=selected";  }  ?>>
	<?php echo $CORE->GEO("translation_tax", array($cat->term_id, $cat->name)); ?>
</option>

<?php $count++; } } ?>
</select>
 
<div id="subcategory_list<?php echo $randID; ?>" style="display:none;">
<select  onchange="UpdateCategoryListSub<?php echo $randID; ?>()" class="form-control <?php if(!is_admin() && isset($_POST['action'])){ ?>fs-5<?php } ?> mb-3"  id="category_subs<?php echo $randID; ?>" data-type="text" data-key="taxonomy">
<option value=""><?php echo __("Any","premiumpress"); ?></option>
</select>
</div>

<div id="sub_subcategory_list<?php echo $randID; ?>" style="display:none;">
<select class="form-control <?php if(!is_admin() && isset($_POST['action'])){ ?>fs-5<?php } ?> mb-3"  id="category_subs_sub<?php echo $randID; ?>">
<option value=""><?php echo __("Any","premiumpress"); ?></option>
</select>
</div>
</div>
<div <?php if(!is_admin() && isset($_POST['action'])){ ?>class="text-center"<?php } ?>>
<button data-ppt-btn class="btn-primary" type="button" onclick="UpdatecountryData<?php echo $randID; ?>()"><?php echo __("Update","premiumpress"); ?></button>


<?php } ?>


</div>
  
  
<script>
function showsubcats(cid){	 

	if(jQuery("[data-value='country-"+cid+"']").prop("checked") == true){
		jQuery('.subcats-'+cid).show(100);
	}else{
		jQuery('.subcats-'+cid).hide(100);
	} 

}
</script>

<?php 

///////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////// 


if(!isset($_POST['sidebar'])){ ?>
         
<script>


function UpdatecountryData<?php echo $randID; ?>(){

var i = jQuery("#parent_category_list<?php echo $randID; ?>"); 
if(i.val() != "" && i.val() !== null){

jQuery("body").append('<input type="hidden" name="catid[]" id="lfilter1"  custom-data-value="" class="customfilter" data-key="taxonomy" data-value="" data-custom-text=""  data-type="custom-text" value="">');

jQuery("#lfilter1").val("country-"+i.val());
jQuery("#lfilter1").attr("data-value", "country-"+i.val());
jQuery("#lfilter1").attr("custom-data-value", "country-"+i.val());
jQuery("#lfilter1").attr("data-custom-text", i.find(":selected").text());
jQuery("#lfilter1").attr("value", "country-"+i.val());
jQuery("#lfilter1").addClass("customfilter");
}else{
jQuery("#lfilter1").removeClass("customfilter");
}

var i = jQuery("#category_subs<?php echo $randID; ?>"); 
if(i.val() != "" && i.val() !== null){
jQuery("body").append('<input type="hidden" name="catid[]" id="lfilter2"  custom-data-value="" class="customfilter" data-key="taxonomy" data-value="" data-custom-text=""  data-type="custom-text" value="">');
jQuery("#lfilter2").val("country-"+i.val());
jQuery("#lfilter2").attr("data-value", "country-"+i.val());
jQuery("#lfilter2").attr("custom-data-value", "country-"+i.val());
jQuery("#lfilter2").attr("data-custom-text", i.find(":selected").text());
jQuery("#lfilter2").attr("value", "country-"+i.val());
jQuery("#lfilter2").addClass("customfilter");
}else{
jQuery("#lfilter2").removeClass("customfilter");
}

var i = jQuery("#category_subs_sub<?php echo $randID; ?>"); 
if(i.val() != "" && i.val() !== null){
jQuery("body").append('<input type="hidden" name="catid[]" id="lfilter3"  custom-data-value="" class="customfilter" data-key="taxonomy" data-value="" data-custom-text=""  data-type="custom-text" value="">');
jQuery("#lfilter3").val("country-"+i.val());
jQuery("#lfilter3").attr("data-value", "country-"+i.val());
jQuery("#lfilter3").attr("custom-data-value", "country-"+i.val());
jQuery("#lfilter3").attr("data-custom-text", i.find(":selected").text());
jQuery("#lfilter3").attr("value", "country-"+i.val());
jQuery("#lfilter3").addClass("customfilter");
}else{
jQuery("#lfilter3").removeClass("customfilter");
}



_filter_update();

}

function UpdateCategoryList<?php echo $randID; ?>() { 
 
	jQuery('#category_subs<?php echo $randID; ?>').html("");
	jQuery('#category_subs_sub<?php echo $randID; ?>').html("");
	
	var catids = "";
	
	jQuery("#parent_category_list<?php echo $randID; ?> > option:selected").each(function() {
		catids = catids + this.value+',';
	});
	
	showsubcategories<?php echo $randID; ?>(catids, '#subcategory_list<?php echo $randID; ?>', '#category_subs<?php echo $randID; ?>', 0);
	
	// sub cats
	 setTimeout( function(){ 							 
		UpdateCategoryListSub<?php echo $randID; ?>();		
	 }  , 1000 );	
    
}
function showsubcategories<?php echo $randID; ?>(catids, sel1, sel2, childof){
  	
	// NOW TRY SHOW SUB CATEGORIES
	if(catids == ""){
	return;
	}
	
	// CHILD OF FOR SUB CATS
	if(childof == 0){
	cc = jQuery("#callcatidsbox").val();
	}else{
	cc  = childof;
	}
	 
	jQuery.ajax({
		type: "POST",
		url: '<?php echo home_url(); ?>/',	
		dataType: 'json',	
		data: {
				action: "load_taxonomy_list",
				taxonomy1: "country",
				parent:catids,
				child: cc,
				type:"list",
		},
		success: function(response) {
		 
		 	if(response.total > 0){	
				jQuery(sel1).show();			
				jQuery(sel2).html(response.output);				
			} else{ 
				jQuery(sel1).hide();							
				jQuery(sel2).val("0");	
			} 
				
		},
		error: function(e) {
			console.log(e)
		}
	});

}
function UpdateCategoryListSub<?php echo $randID; ?>(){ 
	
	// SUB SUB CATS
	var catids = "";
	
	// GET FIRST VALUE IF NON ARE SELECTED
	if(jQuery("#category_subs<?php echo $randID; ?> > option:selected").length == 0){
 
	 setTimeout( function(){ 
							 
		catids = jQuery("#category_subs<?php echo $randID; ?> option:first").val()+',';
				
		showsubcategories<?php echo $randID; ?>(catids, '#sub_subcategory_list<?php echo $randID; ?>', '#category_subs_sub<?php echo $randID; ?>', catids);
		
	  }  , 1000 );
			
		  
	}else{
	
		jQuery("#category_subs<?php echo $randID; ?> > option:selected").each(function() {
			catids = catids + this.value+',';
		});
		
		showsubcategories<?php echo $randID; ?>(catids, '#sub_subcategory_list<?php echo $randID; ?>', '#category_subs_sub<?php echo $randID; ?>', catids);
	
	} 
	
}
</script>
<?php } ?>