<?php
$optres['css'][] = $path . 'assets/css/bootstrap_select/bootstrap-select.css';
$optres['js'][] = $path . 'assets/js/bootstrap_select/bootstrap-select.min.js';
$optres['css'][] = $module_css_path . 'amodules/wb_search_top/css/wb_search_top.css';
$optres['js'][] = $path . 'amodules/wb_search_top/js/wb_search_top_default.js';
$wb_advert_codes = '';
if(isset($wb_search_params['wb_adverts'])) {
	$wb_advert_codes = "'" . implode("','", $wb_search_params['wb_adverts']) . "'";
}
// Get rezults locations
if(!isset($block_districts) || intval($block_districts) == 0 || (isset($wb_srch_dcode) && $wb_srch_dcode != "")) {
	$q = "select adv_code, dcode, lcode from advertisment_locations where active='1' and main_location='1' and adv_code in ($wb_advert_codes);";
	//echo $q;
	$r = @mysql_query($q);
	if($r && mysql_num_rows($r) > 0) {
		while ($row = mysql_fetch_assoc($r)) {
			if(!isset($wb_filters_district[$row['dcode']])) {
				$wb_filters_district[$row['dcode']] = 1;
			}
			else {
				$wb_filters_district[$row['dcode']] += 1;
			}
			
			if(!isset($wb_filters_locations[$row['lcode']])) {
				$wb_filters_locations[$row['lcode']] = 1;
			}
			else {
				$wb_filters_locations[$row['lcode']] += 1;
			}
		}
	}
	
	
	/******************************************************************************************************/
	// Get Districts
	$d = array();
	$districts = array();
	$q = "select * from cyprus_districts order by district asc;";
	$r = @mysql_query($q);
	if($r && mysql_num_rows($r) > 0) {
		while ($row = mysql_fetch_assoc($r)) {
			$d [] = array_map('stripslashes', $row);
            $districts [$row['dcode']] = stripslashes($row['district']);
			/*if( isset($wb_filters_district[$row['dcode']]) ) {
				$districts [$row['dcode']] = stripslashes($row['district']);
			}*/
		}
	}
	
	// Get Locations
	$a = array();
	$locations = array();
	if($wb_srch_dcode != "") {
		$q = "select * from cyprus_locations where dcode='$wb_srch_dcode' order by location asc;";
		$r = @mysql_query($q);
		if($r && mysql_num_rows($r) > 0) {
			while ($row = mysql_fetch_assoc($r)) {
				$t_dcode = $row['dcode'];
				$t_lcode = $row['lcode'];
				
				//if( isset($wb_filters_locations[$row['lcode']]) ) {

					if(isset($locations[$t_dcode])) {
						$locations[$t_dcode][$t_lcode] = stripslashes($row['location']);
					}
					else {
						$locations[$t_dcode] = array();
						$locations[$t_dcode][$t_lcode] = stripslashes($row['location']);
					}
				//}
			}
		}
	}
	/******************************************************************************************************/
}
$show_form = true;

$fmains = array();
$fcategs = array();
$felems = array();
$elms = array();


if(!isset($fm_uid) || $fm_uid == "") {
	if(isset($wb_srch_fm_uid) && $wb_srch_fm_uid != "") {
		$fm_uid = $wb_srch_fm_uid;
	}
	elseif (isset($wb_srch_selected_fm_uid) && $wb_srch_selected_fm_uid != "") {
		$fm_uid = $wb_srch_selected_fm_uid;
	}
}

// Get Main Categories
$q = "select * from wb_filters_mains where wb_uid='$wb_uid' order by pos asc;";
$r = @mysql_query($q);
if($r && mysql_num_rows($r) > 0) {
	$fm_selected = (isset($fm_uid) && $fm_uid != "") ? $fm_uid : '';
	$fm_cnt = 1;
	while ($row = mysql_fetch_assoc($r)) {
		
		if($fm_selected == "" && $fm_cnt = 1) {
			//$fm_selected = $row['fm_uid'];
			//$fm_uid = $row['fm_uid'];
		}
		$fmains [$row['fm_uid']] = array(
			"title"		=> trim(stripslashes($row['title'])),
			"results"	=> 0,
			"selected"	=> ($row['fm_uid'] == $fm_selected) ? 1 : 0
		);
		
		$fm_cnt++;
	}
}

if(isset($_REQUEST['debugwb'])) {
	echo '<br/>$fm_uid = ', $fm_uid;
	echo '<br/>$fm_selected = ', $fm_selected;
}

if(isset($fm_uid) && $fm_uid != '') {
	$q = "select * from wb_filters_categs where wb_uid='$wb_uid' and fm_uid='$fm_uid' order by pos asc;";
	//echo $q;
	$r = @mysql_query($q);
	if($r && mysql_num_rows($r) > 0) {
		while ($row = mysql_fetch_assoc($r)) {
			$fcategs [$row['fc_uid']] = array_map('stripslashes', $row);
		}
	}


	$q = "select * from wb_filters_elems where wb_uid='$wb_uid' and fm_uid='$fm_uid' and searchable='1' order by fc_uid asc, pos asc;";
	$r = @mysql_query($q);
	if($r && mysql_num_rows($r) > 0) {
		while ($row = mysql_fetch_assoc($r)) {
			$elms[$row['e_name']] = array(
				'wb_uid'	=> $row['wb_uid'],
				'fc_uid'	=> $row['fc_uid'],
				'fe_uid'	=> $row['fe_uid'],
			);
			
			if(!isset($felems [$row['fc_uid']])) {
				$felems [$row['fc_uid']] = array();
				$felems [$row['fc_uid']][] = array_map('stripslashes', $row);
			}
			else {
				$felems [$row['fc_uid']][] = array_map('stripslashes', $row);
			}
		}
	}
}



if(!isset($wb_search_params['filter'])) $wb_search_params['filter'] = array();
$wb_search_params['feature'] = array();
$wb_search_params['adverts'] = array();

//$q = "select * from wb_filters_adverts where wb_uid='$wb_uid' and fm_uid='$fm_uid' and adv_code IN ($wb_advert_codes);";
//echo $q;

// Get already existing values
/*
$fvalues = array();
$q = "select * from wb_filters_adverts where wb_uid='$wb_uid' and fm_uid='$fm_uid' and adv_code IN ($wb_advert_codes);";
echo $q;
$r = @mysql_query($q);
if($r && mysql_num_rows($r) > 0) {
	while ($row = mysql_fetch_assoc($r)) {
		if(isset($fvalues [$row['fe_uid']])) {
			$fvalues [$row['fe_uid']][] = stripslashes(trim($row['f_value']));
			$wb_search_params['adverts'][$row['fe_uid']][] = $row['adv_code'];
			if(isset($wb_search_params['feature'][$row['fe_uid']][$row['f_value']])) {
				$wb_search_params['feature'][$row['fe_uid']][$row['f_value']] += 1;
				
			}
			else {
				$wb_search_params['feature'][$row['fe_uid']][$row['f_value']] = 1;
			}
		}
		else {
			$fvalues [$row['fe_uid']] = array();
			$fvalues [$row['fe_uid']][] = stripslashes(trim($row['f_value']));
			
			$wb_search_params['adverts'][$row['fe_uid']]= array();
			$wb_search_params['adverts'][$row['fe_uid']][] = $row['adv_code'];
			
			$wb_search_params['feature'][$row['fe_uid']] = array();
			if(isset($wb_search_params['feature'][$row['fe_uid']][$row['f_value']])) {
				$wb_search_params['feature'][$row['fe_uid']][$row['f_value']] += 1;
			}
			else {
				$wb_search_params['feature'][$row['fe_uid']][$row['f_value']] = 1;
			}
		}
	}
}
*/
//echo '<pre>', var_export($fvalues, true), '</pre>';

$wb_form_action = $protocol . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
if(isset($rel_page) && intval($rel_page) >= 1) {
	$wb_page_replace = ".p$rel_page";
	$wb_form_action = str_replace($wb_page_replace, '', $wb_form_action);
	//echo '<pre>', var_export($_SERVER, true), '</pre>';
}
else {
	$rel_page = 1;
}

if(isset($wb_search_post_url) && $wb_search_post_url != "") {
	$wb_form_action = $full_addon_uri . str_replace('.html', '', $wb_search_post_url) . '.html';
}

if($show_form) {
	?>

    <div id="wb-search-top-container" data-class="hidden-sm hidden-md hidden-lg">
        <!--span class="fal-filter-title">Filter Down By Accommodation Type</span-->

        <div class="row">
            <div class="col-sm-10">
                <form method="post" action="<?php echo $wb_form_action;?>" name="wb_search_top_frm" id="wb_search_top_frm" role="form">
                    <input type="hidden" name="rel_page" id="rel_page" value="<?php echo $rel_page;?>"/>
                    <input type="hidden" name="wb_search" value="wb_search"/>
                    <section class="wbsrch_container box wb-box-srch">
                        <div class="wb-box-srch-header">
                            <a class="trigger-collapse" data-toggle="collapse" href="#wb-search-form" aria-expanded="false" aria-controls="wb-search-form">
                                <b>
                                    <i class="fa fa-search fa-fw fa-lg"></i>
                                    <?php
                                    echo (isset($wb_search_form_title) && trim($wb_search_form_title)!= "") ? $wb_search_form_title : 'Refine Search';
                                    ?>
                                </b>
                                <span class="pull-right clickable"><i class="glyphicon glyphicon-chevron-down"></i></span>
                            </a>
                        </div>

                        <div class="box-body collapse" id="wb-search-form">
                            <div class="container-fluid">
                                <div class="row">
                                    <div class="col-sm-10">
                                        <?php
                                        if(!isset($block_districts) || (intval($block_districts) == 0) ) {
                                            ?>

                                                <?php
                                                $all_districts_count = 0;
                                                foreach ($wb_filters_district as $dist_code=>$dist_code_val) {
                                                    $all_districts_count += $dist_code_val;
                                                }
                                                if(!isset($wb_hide_from_filter) || !in_array("dcode", $wb_hide_from_filter)) {
                                                    ?>
                                                    <div class="form-group">
                                                        <label class="control-label" id="label_district" for="wb-dcode">District</label>

                                                        <select name="dcode" id="wb-dcode" rel="clear_search" class="wb-trigger-change wb-selectbox show-menu-arrow form-control input-sm" data-show-tick="true" data-title="All">
                                                            <option value="" selected="selected">All</option>
                                                            <?php
                                                            foreach ($districts as $t_dcode=>$t_district) {
                                                                if( isset($wb_filters_district[$t_dcode]) ) {
                                                                    $r_district .= ' (' . $wb_filters_district[$t_dcode] . ')';
                                                                }
                                                                $selected = "";
                                                                if($wb_srch_dcode == $t_dcode) $selected = 'selected="selected"';
                                                                echo '<option value="', $t_dcode, '" ', $selected, '>', $t_district, '</option>';
                                                            }
                                                            ?>
                                                        </select>
                                                    </div>
                                                    <?php
                                                }
                                                if($wb_srch_dcode != "" && (!isset($wb_hide_from_filter) || !in_array("lcode", $wb_hide_from_filter))) {
                                                    ?>
                                                    <div class="form-group">
                                                        <label class="control-label" id="label_location" for="lcode">Location</label>
                                                        <select name="lcode" id="wb-lcode" rel="clear_search" class="wb-trigger-change wb-selectbox show-menu-arrow form-control input-sm" data-show-tick="true" data-title="All">
                                                            <option value="" selected="selected">Any</option>
                                                            <?php
                                                            if($wb_srch_dcode != "") {
                                                                /*foreach ($locations[$wb_srch_dcode] as $t_lcode=>$t_location) {
                                                                    if(isset($wb_filters_locations[$t_lcode])) {
                                                                        $t_location .= ' (' . $wb_filters_locations[$t_lcode] . ')';
                                                                    }
                                                                    $selected = "";
                                                                    if($wb_srch_lcode == $t_lcode) $selected = 'selected="selected"';
                                                                    echo '<option value="', $t_lcode, '" ', $selected, '>', $t_location, '</option>';
                                                                }*/

                                                                $lcode_opts = array(
                                                                    'num' => array(),
                                                                    'nonum' => array()
                                                                );
                                                                foreach ($locations[$wb_srch_dcode] as $t_lcode=>$t_location) {
                                                                    $has_number = false;
                                                                    if(isset($wb_filters_locations[$t_lcode])) {
                                                                        $has_number = true;
                                                                        $t_location .= ' (' . $wb_filters_locations[$t_lcode] . ')';
                                                                    }
                                                                    $selected = "";
                                                                    if($wb_srch_lcode == $t_lcode) $selected = 'selected="selected"';
                                                                    $opt = '<option value="'. $t_lcode .  '" ' . $selected . '>' .  $t_location . '</option>';
                                                                    $lcode_opts [$has_number ? 'num' : 'nonum'][] = $opt;
                                                                }

                                                                foreach($lcode_opts as $nn=>$opts) {
                                                                    if($opts) {
                                                                        echo implode("\n", $opts);
                                                                    }
                                                                }
                                                            }

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


                                            <?php
                                        }
                                        elseif($wb_srch_dcode != "" && (!isset($wb_hide_from_filter) || !in_array("lcode", $wb_hide_from_filter))) {
                                            ?>
                                            <div class="form-group">
                                                <label class="control-label" id="label_location" for="lcode">Location</label>
                                                <select name="lcode" id="wb-lcode" rel="clear_search" class="wb-trigger-change wb-selectbox show-menu-arrow form-control input-sm" data-show-tick="true" data-title="All">
                                                    <option value="" selected="selected">Any</option>
                                                    <?php
                                                    if($wb_srch_dcode != "") {
                                                        $lcode_opts = array(
                                                            'num' => array(),
                                                            'nonum' => array()
                                                        );
                                                        foreach ($locations[$wb_srch_dcode] as $t_lcode=>$t_location) {
                                                            $has_number = false;
                                                            if(isset($wb_filters_locations[$t_lcode])) {
                                                                $has_number = true;
                                                                $t_location .= ' (' . $wb_filters_locations[$t_lcode] . ')';
                                                            }
                                                            $selected = "";
                                                            if($wb_srch_lcode == $t_lcode) $selected = 'selected="selected"';
                                                            $opt = '<option value="'. $t_lcode .  '" ' . $selected . '>' .  $t_location . '</option>';
                                                            $lcode_opts [$has_number ? 'num' : 'nonum'][] = $opt;
                                                        }

                                                        foreach($lcode_opts as $nn=>$opts) {
                                                            if($opts) {
                                                                echo implode("\n", $opts);
                                                            }
                                                        }
                                                    }



                                                    ?>
                                                </select>
                                            </div>
                                            <?php

                                        }
                                        ?>
                                    </div>
                                </div>

                                <?php
                                if($wb_srch_fm_uid == "") {
                                    $_fm_uid_ajax = '';

                                    //echo '<pre>', var_export($fmains, true), '</pre>';

                                    if(count($fmains) > 0) {
                                        ?>
                                        <div class="form-group">
                                            <label class="control-label">Type</label>
                                            <div class="">
                                                <select name="ch_fm_uid" class="wb-selectbox show-menu-arrow form-control input-sm wb-trigger-change" data-show-tick="true" data-title="">
                                                <?php
                                                ob_start();
                                                $all_subs_count = 0;
                                                $all_subs_selected = 'selected="selected"';
                                                foreach ($fmains as $_fm_uid=>$fm_data) {

                                                    //if($_fm_uid_ajax == '') $_fm_uid_ajax = $_fm_uid;

                                                    $opt_count = 0;
                                                    if(isset($wb_filters_mains[$_fm_uid])) {
                                                        $opt_count = intval($wb_filters_mains[$_fm_uid]['results']);
                                                    }
                                                    $selected = '';
                                                    if($fm_data ['selected'] == 1 ) {
                                                        $selected = 'selected="selected"';
                                                        $all_subs_selected = '';
                                                    }

                                                    $disabled_class = '';

                                                    if($opt_count == 0) {
                                                        $disabled_class = 'disabled';
                                                    }

                                                    $all_subs_count += $opt_count;

                                                    ?>
                                                    <option <?=$disabled_class;?> value="<?=$_fm_uid;?>" <?=$selected;?>>
                                                        <?php
                                                        echo $fm_data['title'] . ' (' . $opt_count . ')';
                                                        ?>
                                                    </option>
                                                    <?php
                                                }
                                                $mains_content = ob_get_contents();
                                                ob_end_clean();

                                                $disabled_elem = '';
                                                $disabled_class = '';

                                                if($all_subs_count == 0) {
                                                    $disabled_class = 'disabled';
                                                }
                                                ?>
                                                <option <?=$disabled_class;?> value="" <?=$all_subs_selected;?>>
                                                    <?php
                                                    echo 'All';
                                                    ?>
                                                </option>
                                                <?php
                                                echo $mains_content;
                                                ?>
                                                </select>
                                            </div>
                                        </div>
                                        <?php
                                    }

                                    if((!isset($_SESSION['_wb_search_results_ajax_call']) || $_SESSION['_wb_search_results_ajax_call'] == false) && isset($_fm_uid_ajax) && $_fm_uid_ajax != '') {
                                        $_SESSION['_wb_search_results_ajax_call'] = true;

                                        $wb_search_again = true;

                                    }

                                }

                                foreach ($fcategs as $fc_uid=>$fc) {
                                    ?>

                                    <?php
                                    if($fc['friendly_url'] != "default") {
                                        ?>
                                        <div><b><?php echo $fc['title'];?></b></div>
                                        <?php
                                    }

                                    if(isset($felems[$fc_uid])) {
                                        foreach ($felems[$fc_uid] as $i=>$fe) {
                                            $req = intval($fe['required']) == 1 ? true : false;
                                            $e_label_admin = $fe['e_label_admin'];
                                            $e_label_search = $fe['e_label_search'];
                                            $e_type = $fe['e_type'];
                                            $e_type_search = trim($fe['e_type_search']) != "" ? $fe['e_type_search'] : $e_type;
                                            $e_name = $fe['e_name'];
                                            $e_id = 'wb-' . $fe['e_id'];
                                            $e_class = $fe['e_class'];
                                            $e_style = $fe['e_style'];
                                            $e_db = $fe['e_db'];
                                            $e_def = $fe['e_def'];
                                            $fe_uid = $fe['fe_uid'];

                                            if( isset($wb_hide_from_filter) && !in_array($fe_uid, $wb_hide_from_filter)) {
                                                ?>
                                                <div class="form-group">
                                                    <label class="control-label"><?php echo $e_label_admin;?></label>

                                                    <div>
                                                        <?php
                                                        require(dirname(__FILE__) . "/wb_search_top_filters_elements.php");
                                                        ?>
                                                    </div>
                                                </div>
                                                <?php
                                            }


                                        }
                                    }

                                }

                                ?>

                                <div class="row">
                                    <div class="col-sm-10">
                                        <div class="form-group">
                                            <label class="control-label" for="nameContains" id="label_keyword">Property Name</label>
                                            <div>
                                                <input type="text" class="form-control" placeholder="" name="nameContains" id="nameContains"  value="<?php echo (isset($nameContains) && $nameContains != "") ? stripslashes($nameContains) : '';?>">
                                            </div>
                                        </div>

                                    </div>

                                </div>

                                <div class="box-footer text-center">
                                    <button class="btn btn-primary wb-search-top-button" type="submit">
                                        <i class="fa fa-search"></i> Search
                                    </button>
                                </div>
                            </div>
                        </div>



                    </section>
                </form>
            </div>
        </div>
    </div>
    <script>
        var wb_search_again = <?php echo (isset($wb_search_again) && $wb_search_again) ? 1 : 0; ?>;
    </script>

	<?php
}
