<?php
ob_start();
$fal_title = isset($module_params['fal_title']) ? stripslashes(trim($module_params['fal_title'])) : '';
$fal_title_tag = isset($module_params['fal_title_tag']) ? stripslashes(trim($module_params['fal_title_tag'])) : 'span';
$fal_meta_desc = isset($module_params['fal_meta_desc']) ? stripslashes($module_params['fal_meta_desc']) : '';
$fal_meta_keywords = isset($module_params['fal_meta_keywords']) ? stripslashes($module_params['fal_meta_keywords']) : '';

if($fal_title != "") {
	$webpage_title_en = $fal_title;
}

if(trim($fal_meta_desc) != "") $meta['description'] = trim(stripslashes($fal_meta_desc));
if(trim($fal_meta_keywords) != "") $meta['keywords'] = trim(stripslashes($fal_meta_keywords));

if($fal_title != "") {
    ?>
    <header>
        <<?=$fal_title_tag;?>><?php echo $fal_title;?></<?=$fal_title_tag;?>>
    </header>
    <?php
}
?>
<div class="article-body cyprus no-margin">
<?php
$microdata_domain = str_replace("www.", "", $ultra_domainname);
$microdata_host = $_SERVER['HTTP_HOST'];
if(str_replace('www.', '', $microdata_host) != str_replace('www.', '', $ultra_domainname)) $microdata_host .= "/" . $ultra_addonname;
$have_microdata = (isset($microdata_domains) && in_array($microdata_domain, $microdata_domains));

$wb_domainname = str_replace("www.", "", $ultra_domainname);

$is_hotel = false;
if($wb_domainname == "cyprus-hotel.com") {
	$is_hotel = true;
}

// Get domain catdoms
$catdoms = array();
$catdoms [] = $domain_id;
$result_sub = mysql_query ("SELECT * FROM  domaincategories WHERE parent_id='$domain_id';");
if (mysql_num_rows($result_sub) <> 0){			  
	while($line_sub = mysql_fetch_array($result_sub)){ 
		$catdoms [] = intval($line_sub["catdom_id"]);
	}
}

if(!isset($wb_uid)) {

	$q = "select wb.* from wb_categories_domains wb where wb.wb_domainname='$wb_domainname';";
	$r = mysql_query($q) or die("<strong>$q</strong>".mysql_error());
	if($r && mysql_num_rows($r) > 0) {
		$row = mysql_fetch_assoc($r);
		$wb_uid = $row['wb_uid'];
	}
		
}

$optres['css'][] = $path . "amodules/wb_search_results/templates_css/$wb_uid.css";

// Get All Main Categories
$wb_mains = array();
$wb_mains_adverts = array();
$q = "SELECT * FROM `wb_filters_mains` where wb_uid='$wb_uid' and active='1' order by pos asc;";
$r = @mysql_query($q);
if($r && mysql_num_rows($r) > 0) {
	while ($row = mysql_fetch_assoc($r)) {
		$wb_mains [$row['fm_uid']] = array_map('stripslashes', $row);
	}
}

// Get All visible Adverts by wb_uid
$total_adverts = 0;
$wb_uid_adverts = array();
$start_advs = array();
$final_advs = array();
$q = "select * from wb_categories_adverts where wb_uid='$wb_uid' and wb_status='1';";
if(isset($_REQUEST['fal_debug'])) {
	//echo "<br/>$q";
}
$r = @mysql_query($q);
if($r && mysql_num_rows($r) > 0) {
	//$total_adverts = mysql_num_rows($r);
	//$meta['description'] = $total_adverts . ' Cyprus Hotels. ' . $meta['description'];
	while ($row = mysql_fetch_assoc($r)) {
		$wb_uid_adverts [$row['wb_advert_uid']] = "'" . $row['wb_advert_uid'] . "'";
		$start_advs [] = $row['wb_advert_uid'];
	}
}

$json_locations = array();

if(count($wb_uid_adverts) > 0) {
	//echo '<pre>', var_export($wb_uid_adverts, true), '</pre>';
	
	$full_locs = array();
	$districts = array();
	$locations = array();
	$loc_advs = array();
	$loc_adv_codes = implode(", ", $wb_uid_adverts);
	$q = "SELECT a.short_title_en, a.title_tr as friendly_url, l.*, w.fm_uid FROM advertisment a, advertisment_locations l, wb_filters_adverts_categs w where a.adv_code=l.adv_code and a.adv_code in ($loc_adv_codes) and a.adv_code=w.adv_code and w.wb_uid='$wb_uid' and l.main_location='1' and l.active='1' and l.dcode!='' and l.lcode != '' order by l.district asc, l.city asc;";
	if(isset($_REQUEST['fal_debug'])) {
		//echo "<br/>$q";
	}
	$r = mysql_query($q);
	if($r && mysql_num_rows($r) > 0) {
		while ($row = mysql_fetch_assoc($r)) {
			$adv_code = $row['adv_code'];
			$dcode = $row['dcode'];
			$lcode = $row['lcode'];
			$district = $row['district'];
			$city = stripslashes($row['city']);
			$short_title_en = stripslashes(trim($row['short_title_en']));
			$friendly_url = trim($row['friendly_url']);
			
			if(abs($row['lat']) - 0 > 0) {
			
				$json_locations [] = array(
					"lat"		=> $row['lat'],
					"lng"		=> $row['lng'],
					"adv_code"	=> $row['adv_code'],
					"title"		=> stripslashes($row['short_title_en']),
					"fm_uid"	=> $row['fm_uid'],
					"fm_name"	=> isset($wb_mains[$row['fm_uid']]) ? $wb_mains[$row['fm_uid']]['title'] : ''
				);
			}
			
			
			if(!isset($full_locs[$dcode])) {
				$full_locs[$dcode] = array();
				$districts[$dcode] = $district;
			}
			
			if(!isset($full_locs[$dcode][$lcode])) {
				$full_locs[$dcode][$lcode] = array();
				$locations[$lcode] = $city;
			}
			
			$full_locs[$dcode][$lcode][$adv_code] = array(
				"short_title_en"	=> $short_title_en,
				"friendly_url"		=> $friendly_url,
				"adv_code"			=> $adv_code
			);
			
			
			if(!isset($loc_advs [$lcode])) {
				$loc_advs [$lcode] = array();
				$loc_advs [$lcode] [] = $adv_code;
			}
			else {
				$loc_advs [$lcode] [] = $adv_code;
			}
			
		}
	}

	$v_desc = array();
	
	if(count($full_locs) > 0) {
		$adv_index = 1;
		$json_dl = array();
		?>
		<!--{ACCOM_FILTER} -->
		<?php
		
		require_once(dirname(__FILE__) . "/full_adverts_list_map.php");

		$optres['css'][] = $module_css_path . 'amodules/full_adverts_list/css/full_adverts_list_default.css';
		?>
		
		<div id="full-list">
			<?php
			foreach ($full_locs as $dcode=>$d) {
				
				if(!isset($json_dl[$dcode])) {
					$json_dl[$dcode] = array(
						"title"	=> $districts[$dcode],
						"num"	=> 0,
						"locs"	=> array(),
						"types"	=> array()
					);
				}
				
				echo '<h2>' . $districts[$dcode] . ' Accommodation</h2>';
				echo '<div class="h2-list">';
				foreach ($d as $l_code=>$l) {
					
					//echo '<pre>$loc_advs[', $l_code,']= ' . var_export($loc_advs[$l_code]), '</pre>';
					
					// Get Categorisations
					$l_code_adv_codes = "'" . implode("', '", $loc_advs[$l_code]) . "'";
					$wb_adv_categs = array();
					foreach ($wb_mains as $wb_fm_uid=>$fm_row) {
						$wb_adv_categs [$wb_fm_uid] = array();
					}
					
					$ar_class_vars = array(
						"star_rating", "apt_class", "class"
					);
					$class_vars = array(
						"'star_rating'", "'apt_class'", "'class'"
					);
					
					
					$used = array();
					$q = "SELECT a.adv_code, a.fm_uid, a.f_value, e.e_label_display, e.e_db, e.e_name FROM wb_filters_adverts a, wb_filters_elems e where a.adv_code in ($l_code_adv_codes) and e.wb_uid='$wb_uid' and a.fe_uid=e.fe_uid  and e.e_name in (".implode(",", $class_vars).") group by a.adv_code";
					//echo "<br/>$q";
					$r = mysql_query($q) or die("<strong>$q</strong>".mysql_error());
					if($r && mysql_num_rows($r) >0) {
						while($row = mysql_fetch_assoc($r)) {
							$adv_adv_code = $row['adv_code'];
							$adv_fm_uid = $row['fm_uid'];
							$f_value = $row['f_value'];
							$e_label_display = stripslashes($row['e_label_display']);
							$e_db = unserialize(base64_decode($row['e_db']));
							$f_value_name = $e_db[$f_value];
							$e_name = $row['e_name'];
							
							$v_desc [$f_value] = $f_value_name;
							
							$used [] = $adv_adv_code;
							
							if(!isset($wb_adv_categs [$adv_fm_uid][$f_value])) {
								$wb_adv_categs [$adv_fm_uid][$f_value] = array();
								$wb_adv_categs [$adv_fm_uid][$f_value][] = $adv_adv_code;
							}
							else {
								$wb_adv_categs [$adv_fm_uid][$f_value][] = $adv_adv_code;
							}
							
							
							//echo '<pre>', var_export($row, true), '</pre>';
							//echo '<pre>', var_export($e_db, true), '</pre>';
							
						//echo '<pre>', var_export($wb_adv_categs, true), '</pre>';
						}
					}
					
					$diff = array_diff($loc_advs[$l_code], $used);
					if(is_array($diff) && count($diff) > 0) {
						$l_code_adv_codes = "'" . implode("', '", $diff) .  "'";
						//$q = "SELECT a.adv_code, a.fm_uid, a.f_value, e.e_label_display, e.e_db, e.e_name FROM wb_filters_adverts a, wb_filters_elems e where a.adv_code in ($l_code_adv_codes) and e.wb_uid='$wb_uid' and a.fe_uid=e.fe_uid group by a.adv_code";
						$q = "select a.wb_advert_uid as adv_code, f.fm_uid from wb_categories_adverts a, wb_filters_adverts_categs f where a.wb_uid='$wb_uid' and a.wb_status='1'  and a.wb_advert_uid in ($l_code_adv_codes) and a.wb_advert_uid=f.adv_code and f.wb_uid='$wb_uid' group by a.wb_advert_uid;";
						//echo "<br/>$q";
						$r = mysql_query($q) or die("<strong>$q</strong>".mysql_error());
						if($r && mysql_num_rows($r) >0) {
							while($row = mysql_fetch_assoc($r)) {
								$adv_adv_code = $row['adv_code'];
								$adv_fm_uid = $row['fm_uid'];
								$f_value = 'generic';//$row['f_value'];
								
								
								//$v_desc [$f_value] = $f_value_name;
								
								if(!isset($wb_adv_categs [$adv_fm_uid][$f_value])) {
									$wb_adv_categs [$adv_fm_uid][$f_value] = array();
									$wb_adv_categs [$adv_fm_uid][$f_value][] = $adv_adv_code;
								}
								else {
									$wb_adv_categs [$adv_fm_uid][$f_value][] = $adv_adv_code;
								}
								
								
								//echo '<pre>', var_export($row, true), '</pre>';
								//echo '<pre>', var_export($e_db, true), '</pre>';
								
							//echo '<pre>', var_export($e_db, true), '</pre>';
							}
							
							//echo '<pre>', var_export($wb_adv_categs, true), '</pre>';
						}
					}
					
					if(!isset($json_dl[$dcode]['locs'][$l_code])) {
						$json_dl[$dcode]['locs'][$l_code] = array(
							"title"		=> $locations[$l_code],
							"num"		=> 0,
							"types"	=> array()
						);
					}
					
					echo '<h3>' . $locations[$l_code] . '</h3>';
					echo '<div class="h3-list">';
						//echo '<pre>', var_export($wb_adv_categs, true), '</pre>';
						$l_c = '';
						$l_c_cnt = 0;
						foreach ($wb_adv_categs as $fm_uid=>$v) {
							if(count($v) > 0) {
								
								//$json_dl[$dcode]['locs'][$l_code]['num'] += 1;
								//$json_dl[$dcode]['num'] += 1;
								
								if($l_c != $fm_uid) {
									if($l_c_cnt > 0) echo '</div>';
									echo '<h4>', $wb_mains[$fm_uid]['title'], '</h4>';
									echo '<div class="h4-list">';
									$l_c = $fm_uid;
								}
								
								//echo '<pre>', var_export($v, true), '</pre>';
								
								
								$l_v = '';
								$l_v_cnt = 1;
								
								$tmp = array();
								foreach ($v as $val=>$val_advs) {
									if(isset($v_desc[$val])) {
										if(!isset($tmp[$v_desc[$val]])) {
											$tmp[$v_desc[$val]] = array();
											$tmp[$v_desc[$val]] [] = $val_advs;
										}
										else {
											$tmp[$v_desc[$val]] [] = $val_advs;
										}
									}
									else {
										if(!isset($tmp['generic'])) {
											$tmp['generic'] = array();
											$tmp['generic'] [] = $val_advs;
										}
										else {
											$tmp['generic'] [] = $val_advs;
										}
									}
								}
								
								ksort($tmp, SORT_STRING);
								
								if(isset($tmp['generic'])) {
									//echo '<pre>', var_export($tmp, true), '</pre>';
								}
								
								//echo '<pre>', var_export($tmp, true), '</pre>';
								
								
								foreach ($tmp as $acc_type=>$acc_codes) {
									$adv_codes = array();
									if($acc_type != "generic") {
										echo '<h5>', $acc_type, '</h5>';
										$adv_codes = $acc_codes[0];
									}
									else {
										//echo '<pre>', var_export($acc_codes, true), '</pre>';
										//foreach ($acc_codes as $i=>$acc) {
										//	$adv_codes [] = $acc[0];
										//}
										$adv_codes = $acc_codes[0];
									}
									echo '<div class="h5-list">';
									
									
									
									$tmp_advs = array();
									foreach ($adv_codes as $i=>$adv_code) {
										$a = $full_locs[$dcode][$l_code][$adv_code];
										$tmp_advs[$a['short_title_en']] = $a;
										//echo '<a href="', $a['friendly_url'], '" target="_blank" title="', $a['short_title_en'], '">', $a['short_title_en'], '</a>' . '<br/>';								
									}
									
									ksort($tmp_advs);
									foreach ($tmp_advs as $i=>$a) {
										
										$json_dl[$dcode]['locs'][$l_code]['num'] += 1;
										$json_dl[$dcode]['num'] += 1;
										
										if(!isset($json_dl[$dcode]['types'][$fm_uid])) {
											$json_dl[$dcode]['types'][$fm_uid] = 1;
										}
										else {
											$json_dl[$dcode]['types'][$fm_uid] += 1;
										}
										
										if(!isset($json_dl[$dcode]['locs'][$l_code]['types'][$fm_uid])) {
											$json_dl[$dcode]['locs'][$l_code]['types'][$fm_uid] = 1;
										}
										else {
											$json_dl[$dcode]['locs'][$l_code]['types'][$fm_uid] += 1;
										}
										
										
										if(!isset($wb_mains_adverts[$fm_uid])) {
											$wb_mains_adverts[$fm_uid] = 1;
										}
										else {
											$wb_mains_adverts[$fm_uid] += 1;
										}
										
										$final_advs[] = $a['adv_code'];
										//echo '<span style="color:#efefef; font-size: 12px;">'.$adv_index.'.</span> <a href="', $a['friendly_url'], '" target="_blank" title="', $a['short_title_en'], '">', $a['short_title_en'], '</a>' . '<br/>';								
										echo '<a href="', $a['friendly_url'], '" target="_blank" title="', $a['short_title_en'], '">', $a['short_title_en'], '</a>' . '<br/>';
										$total_adverts += 1;
										$adv_index++;
									}
									
									echo '</div>';
								}
								/*
								foreach ($v as $val=>$val_advs) {
									if(isset($v_desc[$val])) {
										if($l_v != $val) {
											//if($l_v != '') echo '</div>';
											echo '<h5>', $v_desc[$val], '</h5>';
											//echo '<div class="h5-list">';
										}
									}
									else {
										if($l_v != $val) {
											//if($l_v != '') echo '</div>';
											//echo '<h5>', $v_desc[$val], '</h5>';
											//echo '<div class="h5-list">';
										}
									}
									
									echo '<div class="h5-list">';
									foreach ($val_advs as $adv_code) {
										$a = $full_locs[$dcode][$l_code][$adv_code];
										echo '<a href="', $a['friendly_url'], '" target="_blank" title="', $a['short_title_en'], '">', $a['short_title_en'], '</a>' . '<br/>';								
									}
									echo '</div>';
									
									$l_v_cnt++;
									//if($l_v_cnt == count($v)) echo '</div>';
									
								}
								*/
								$l_c_cnt++;
							}
						}
						if($l_c_cnt > 0 && $l_c != "") echo '</div>';
						
					echo '</div>';
				}
				echo '</div>';
			}
			?>
		</div>
		<?php
		
		//echo '<pre>', var_export($json_dl, true), '</pre>';
		
		$wb_search_params['wb_adverts'] = $final_advs;
		
		$meta['description'] = $total_adverts . ' Cyprus Hotels. ' . $meta['description'];
		if(isset($_REQUEST['fal_debug'])) {
			echo '<pre>', var_export(array_diff($start_advs, $final_advs), true), '</pre>';
		}

        $wb_types_js = array();
        foreach($wb_mains as $fm_uid_val=>$fm_uid_data) {
            $wb_types_js [$fm_uid_val] = $fm_uid_data['title'];
        }

		?>
        <script>
			var json_dl = <?php echo json_encode($json_dl);?>;
			var wb_types_js = <?php echo json_encode($wb_types_js);?>;
            window.wb_search_map_init = '<?=$path;?>amodules/full_adverts_list/js/full_adverts_list.js';
        </script>
		<?php
		$optres['js'][] = $path . 'amodules/full_adverts_list/js/loader.js';
		
	}
	
}
echo '</div>';
$fal_content = ob_get_contents();
ob_end_clean();


if(isset($wb_mains_adverts) && count($wb_mains_adverts) > 0) {
	ob_start();
	?>


	<div id="fal-filter-container">
		<!--span class="fal-filter-title">Filter Down By Accommodation Type</span-->

        <div class="row">
            <div class="col-sm-10">
                <form method="post" name="fal_search_frm" id="fal_search_frm" role="form">
                    <input type="hidden" name="wb_search" value="1">
                    <section class="evsrch_container box box-srch">
                        <div class="box-srch-header">
                            <a class="trigger-collapse" data-toggle="collapse" href="#event-search-form" aria-expanded="false" aria-controls="event-search-form">
                                <b><i class="fa fa-search fa-fw fa-lg"></i> Find Hotels</b>
                                <span class="pull-right clickable"><i class="glyphicon glyphicon-chevron-down"></i></span>
                            </a>
                        </div>

                        <div class="box-body collapse" id="event-search-form">
                            <div class="container-fluid">
                                <div class="row">
                                    <div class="col-sm-5">
                                        <div class="form-group">
                                            <label class="control-label sr-only" for="dcode"><?php echo 'District';?></label>
                                            <select name="dcode" id="fal_dcode" class="event-selectbox show-menu-arrow form-control input-sm" data-title="All Districts">
                                                <option value="" selected="selected"><?php echo 'All Districts';?></option>
                                                <?php
                                                foreach ($json_dl as $dcode=>$d) {
                                                    echo '<option value="',$dcode,'">',$d['title'],' (',$d['num'],')</option>';
                                                }
                                                ?>
                                            </select>

                                        </div>
                                    </div>
                                    <div class="col-sm-5">
                                        <div class="form-group sm-no-margin-left">
                                            <label class="control-label sr-only" for="jobs_Location"><?php echo 'Location';?></label>
                                            <select name="lcode" id="fal_lcode" class=" event-selectbox show-menu-arrow form-control input-sm" data-title="Any Location">
                                                <option value="" selected="selected">Any Location</option>
                                            </select>
                                        </div>
                                    </div>
                                </div>

                                <div class="row">
                                    <div class="col-sm-5">
                                        <div class="form-group">
                                            <label class="control-label sr-only" for="ch_fm_uid"><?php echo 'Type';?></label>
                                            <select name="ch_fm_uid" id="fal_type" class=" event-selectbox show-menu-arrow form-control input-sm" data-title="All Types">
                                                <?php
                                                foreach ($wb_mains_adverts as $fal_fm_uid=>$fal_num) {
                                                    echo '<option value="'.$fal_fm_uid.'">' . $wb_mains[$fal_fm_uid]['title'] . ' ('.$fal_num.')' . '</option>';
                                                }
                                                ?>
                                            </select>
                                        </div>
                                    </div>
                                    <div class="col-sm-5">
                                        <div class="form-group sm-no-margin-left">
                                            <label class="control-label sr-only" for="fal_keyword"><?php echo 'Keyword';?></label>
                                            <input type="text" value="" placeholder="Keyword" name="nameContains" id="fal_keyword" class="form-control input-sm">
                                        </div>
                                    </div>
                                </div>
                                <div class="row">
                                    <div class="box-srch-footer text-center col-xs-10">
                                        <button type="submit" class="btn btn-success btn-sm">
                                            <i class="fa fa-search fa-lg"></i> Search
                                        </button>
                                        <br><br>
                                    </div>

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

                    </section>
                </form>
            </div>
        </div>
    </div>
	<?php
	$fal_accom_filter = ob_get_contents();
	ob_end_clean();
	
	$fal_content = str_replace('<!--{ACCOM_FILTER} -->', $fal_accom_filter, $fal_content);
}

echo $fal_content;


$optres['js'][] = $path . "amodules/wb_search_results/js/markerclusterer.js";
//$optres['js'][] = $path . "amodules/wb_search_results/js/infobubble.js";
$optres['css'][] = $path . "assets/plugins/snazzy/snazzy-info-window.css";
$optres['css'][] = $path . "assets/plugins/snazzy/snazzy-custom.css";
$optres['css'][] = $path . "assets/css/bootstrap-dialog.min.css";
$optres['js'][] = $path . "assets/js/bootstrap-dialog.min.js";

if(isset($fal_accom_filter)) {
	$optres['js'][] = $path . "js/jquery.blockUI.js";
    $optres['css'][] = $path . 'assets/css/bootstrap_select/bootstrap-select.css';
    $optres['js'][] = $path . 'assets/js/bootstrap_select/bootstrap-select.min.js';
	$optres['js'][] = $path . "amodules/full_adverts_list/js/full_adverts_list_filter.js";
	$optres['css'][] = $module_css_path . "amodules/full_adverts_list/css/full_adverts_list_filter.css";
}
?>