<?php

/**
 * BP Kids oldal sablont megvalósító fájl hagyományos változata
 */
get_header(); ?>
<div id="content">
	<?php
	global $wpdb;

	$table1 = 'institution';
	$table2 = 'institution_type';

	//	$the_inst_query = parse_url(get_query_var('intezmeny'));
	//	$the_inst_id = substr($the_inst_query['path'], strrpos($the_inst_query['path'], '/'));
	//	$the_type_query = parse_url(get_query_var('tipus'));
	//	$the_type_id = substr($the_type_query['path'], strrpos($the_type_query['path'], '/'));

	//	$inst = substr($the_inst_id, 1);
	//	$thetypeid = substr($the_type_id, 1);
	$inst = $_GET['intezmeny'];
	$thetypeid = $_GET['type'];

	if (!empty($inst)) {


		$results = $wpdb->get_results("
			SELECT
			*
			FROM
			$table1
			WHERE
			id = $inst
			");

		if (!empty($results)) {
			foreach ($results as $row) {

				$instname = $row->name;
				$lat = $row->lat; // latitude
				$lng = $row->lng; // longtitude
	?>
				<div id="inner-content" class="wrap cf">
					<main id="main" class="m-all cf" role="main">
						<div class="row">
							<div class="col-md-12">
								<h1><?php echo $row->name; ?></h1>
							</div>
							<div class="col-md-6">
								<h3>Elérhetőségek</h3>

								<table class="info-table table-condensed inst-table">
									<tbody>

										<tr>
											<td>Kerület: </td>
											<td><?php echo $row->district_id; ?>. kerület</td>
										</tr>
										<tr>
											<td>Cím: </td>
											<td><?php echo $row->zipcode; ?> Budapest, <?php echo $row->address; ?> </td>
										</tr>
										<?php if (!empty($row->phone_01)) { ?>
											<tr>
												<td>Telefon (2): </td>
												<td> <?php echo $row->phone_01; ?> </td>
											</tr>
										<?php } ?>
										<?php if (!empty($row->phone_02)) { ?>
											<tr>
												<td>Telefon (2): </td>
												<td> <?php echo $row->phone_02; ?> </td>
											</tr>
										<?php } ?>

										<?php
										if (!empty($row->email)) { ?>
											<tr>
												<td>E-mail: </td>
												<td><a href="mailto:<?php echo $row->email; ?>"><?php echo mb_strimwidth($row->email, 0, 42, " ..."); ?></a></td>
											</tr>
										<?php } ?>

										<?php
										if (!empty($row->web) && $row->web != 'http://') { ?>
											<tr>
												<td>Web: </td>
												<td><a href="<?php echo $row->web; ?>" target="_blank"><?php echo mb_strimwidth($row->web, 0, 42, " ..."); ?></a></td>
											</tr>
										<?php } ?>

										<?php
										if (!empty($row->opening_time)) { ?>
											<tr>
												<td>Nyitvatartás:</td>
												<td><?php echo $row->opening_time; ?></td>
											</tr>
										<?php } ?>


									<?php } ?>

									</tbody>
								</table>

							</div>

							<div class="col-md-6">


								<div id="map" style="width:100%;height: 400px;"></div>


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

			<?php
		}
	} else {


		//if(isset($wp_query->query_vars['type'])) {
		//$sMsdsCat = urldecode($wp_query->query_vars['type']);
		//}

		if (!isset($_GET['type'])) {
			$typeid = 1;
		} else {
			$typeid = $_GET['type'];
		}


		if (!empty($_POST["kerulet"])) {
			$postker = $_POST['kerulet'];
			$results = $wpdb->get_results("
			SELECT
			*, 
			inst.*, 
			instype.`name` as tname, 
			inst.`name` as iname
			FROM
			institution AS inst
			INNER JOIN
			institution_type AS instype
			ON 
				inst.type_id = instype.id
			WHERE
			inst.type_id = $typeid AND
			district_id = $postker
			");
		} else {
			$results = $wpdb->get_results("
			SELECT
			*, 
			inst.*, 
			instype.`name` as tname, 
			inst.`name` as iname
			FROM
			institution AS inst
			INNER JOIN
			institution_type AS instype
			ON 
				inst.type_id = instype.id
			WHERE
			inst.type_id = $typeid
			");
		}

			?>




			<div id="inner-content" class="wrap cf">
				<main id="main" class="m-all cf" role="main">
					<div class="row">


						<?php
						// $size_var = (get_query_var('type')) ? get_query_var('type') : false;
						// if($size_var){
						//    var_dump($size_var);
						// } 

						//if(isset($wp->query_var('inst'))) {echo 'YEPP';}



						?>

						<div class="col-md-12">
							<h1 class="archive-title"><?php echo $results['0']->tname; ?></h1>
						</div>
						<div class="col-md-9" style="padding-right: 0!important">
							<div id="map" style="width:100%;height: 500px;"></div>
						</div>
						<div class="col-md-3 mapKerulet" style="background-color: #e1e1e1;display: block;height: 500px; text-align: center;">
							<h3>Válaszd ki a kerületet!</h3>
							<br style="clear:both" />
							<form method="post" action="?pagename=budapest-kids&type=<?php echo $_GET['type']; ?>">
								<select name="kerulet">
									<option <?php if (!isset($postker)) {
												echo 'selected';
											} ?> disabled>Kerület kiválasztása</option>
									<option <?php if (isset($postker) && $postker == '1') {
												echo 'selected';
											} ?> value="1">Budapest I. kerülete</option>
									<option <?php if (isset($postker) && $postker == '2') {
												echo 'selected';
											} ?> value="2">Budapest II. kerülete</option>
									<option <?php if (isset($postker) && $postker == '3') {
												echo 'selected';
											} ?> value="3">Budapest III. kerülete</option>
									<option <?php if (isset($postker) && $postker == '4') {
												echo 'selected';
											} ?> value="4">Budapest IV. kerülete</option>
									<option <?php if (isset($postker) && $postker == '5') {
												echo 'selected';
											} ?> value="5">Budapest V. kerülete</option>
									<option <?php if (isset($postker) && $postker == '6') {
												echo 'selected';
											} ?> value="6">Budapest VI. kerülete</option>
									<option <?php if (isset($postker) && $postker == '7') {
												echo 'selected';
											} ?> value="7">Budapest VII. kerülete</option>
									<option <?php if (isset($postker) && $postker == '8') {
												echo 'selected';
											} ?> value="8">Budapest VIII. kerülete</option>
									<option <?php if (isset($postker) && $postker == '9') {
												echo 'selected';
											} ?> value="9">Budapest IX. kerülete</option>
									<option <?php if (isset($postker) && $postker == '10') {
												echo 'selected';
											} ?> value="10">Budapest X. kerülete</option>
									<option <?php if (isset($postker) && $postker == '11') {
												echo 'selected';
											} ?> value="11">Budapest XI. kerülete</option>
									<option <?php if (isset($postker) && $postker == '12') {
												echo 'selected';
											} ?> value="12">Budapest XII. kerülete</option>
									<option <?php if (isset($postker) && $postker == '13') {
												echo 'selected';
											} ?> value="13">Budapest XIII. kerülete</option>
									<option <?php if (isset($postker) && $postker == '14') {
												echo 'selected';
											} ?> value="14">Budapest XIV. kerülete</option>
									<option <?php if (isset($postker) && $postker == '15') {
												echo 'selected';
											} ?> value="15">Budapest XV. kerülete</option>
									<option <?php if (isset($postker) && $postker == '16') {
												echo 'selected';
											} ?> value="16">Budapest XVI. kerülete</option>
									<option <?php if (isset($postker) && $postker == '17') {
												echo 'selected';
											} ?> value="17">Budapest XVII. kerülete</option>
									<option <?php if (isset($postker) && $postker == '18') {
												echo 'selected';
											} ?> value="18">Budapest XVIII. kerülete</option>
									<option <?php if (isset($postker) && $postker == '19') {
												echo 'selected';
											} ?> value="19">Budapest XIX. kerülete</option>
									<option <?php if (isset($postker) && $postker == '20') {
												echo 'selected';
											} ?> value="20">Budapest XX. kerülete</option>
									<option <?php if (isset($postker) && $postker == '21') {
												echo 'selected';
											} ?> value="21">Budapest XXI. kerülete</option>
									<option <?php if (isset($postker) && $postker == '22') {
												echo 'selected';
											} ?> value="22">Budapest XXII. kerülete</option>
									<option <?php if (isset($postker) && $postker == '23') {
												echo 'selected';
											} ?> value="23">Budapest XXIII. kerülete</option>
								</select>
								<br style="clear:both" />
								<button type="submit" class="btn btn-default">Mehet</button>
							</form>
							<br style="clear:both" />
							<p>vagy <a href="<?php echo the_permalink(); ?>?type=<?php echo $thetypeid; ?>">alaphelyzet</a></p>

						</div>
						<div class="col-md-12">
							<?php
							if (!empty($results)) {
								foreach ($results as $row) {
									$instname = $row->name;
									$lat = $row->lat; // latitude
									$lng = $row->lng; // longtitude
							?>
									<div class="list-item" id="inst_<?php echo $row->id; ?>" data-instid="<?php echo $row->id; ?>">
										<div class="list-item-content">
											<h3 class="title"><a href="?pagename=budapest-kids&intezmeny=<?php get_permalink();
																											echo $row->id; ?>"><?php echo $row->iname; ?></a></h3>
											<?php echo $row->address; ?>
											<hr>
											<div class="clear"></div>
										</div>
									</div>
							<?php
								}
							}
							?>
							<nav class="pagination new"></nav>
						</div>
					</div>
				</main>

			</div>

</div>

<?php } ?>
<?php get_footer(); ?>


<script>
	$(function() {

		<?php if (!empty($the_inst_id)) { ?>
			var myLatLng = {
				lat: <?php echo $lat; ?>,
				lng: <?php echo $lng; ?>
			};

			var map = new google.maps.Map(document.getElementById('map'), {
				zoom: 15,
				center: new google.maps.LatLng(myLatLng),
				zoomControl: true,
				mapTypeControl: false,
				scaleControl: true,
				streetViewControl: false,
				rotateControl: true,
				fullscreenControl: false,
				styles: [{
					stylers: [{
						saturation: -100
					}]
				}]
			});
		<?php } else { ?>
			var myLatLng = {
				lat: 47.4999,
				lng: 19.0852
			};

			var map = new google.maps.Map(document.getElementById('map'), {
				zoom: 11,
				center: new google.maps.LatLng(myLatLng),
				zoomControl: true,
				mapTypeControl: false,
				scaleControl: true,
				streetViewControl: false,
				rotateControl: true,
				fullscreenControl: false,
				styles: [{
					stylers: [{
						saturation: -100
					}]
				}]
			});
		<?php } ?>
		<?php foreach ($results as $mrow) { ?>
			var content_<?php echo $mrow->id; ?> = '<div class="infowindow" style="padding: 12px 24px 24px 12px;margin:0;">';
			content_<?php echo $mrow->id; ?> += '<strong>';
			content_<?php echo $mrow->id; ?> += '<a href="https://enbudapestem.hu/?pagename=budapest-kids&intezmeny=<?php echo $mrow->id; ?>" target="_blank"><?php echo $mrow->name; ?></a>';
			content_<?php echo $mrow->id; ?> += '</strong><br /><br />';
			content_<?php echo $mrow->id; ?> += '<strong>Cím:</strong> <?php echo $mrow->zipcode; ?> Budapest, <?php echo $mrow->address; ?><br />';
			<?php if (!empty($mrow->phone_01)) { ?>
				content_<?php echo $mrow->id; ?> += '<strong>Telefon (1):</strong> <?php echo $mrow->phone_01; ?> <br />';
			<?php } ?>
			<?php if (!empty($mrow->phone_02)) { ?>
				content_<?php echo $mrow->id; ?> += '<strong>Telefon (2):</strong> <?php echo $mrow->phone_02; ?> <br />';
			<?php } ?>
			content_<?php echo $mrow->id; ?> += '</div>';
			//                content_<?php echo $mrow->id; ?> += '<div id="map-form-78"><br /><div class="error" id="error-console-78"></div><input type="text" maxlength="4" size="4" id="map-zipcode-78" name="zipcode" placeholder="IRSZ" class="input-zipcode" /> Budapest, ' +
			//                 '<input type="text" id="map-address-78" name="own_address" class="input-address" placeholder="Utca, házszám" />';
			//               content_<?php echo $mrow->id; ?> += '<input type="button" name="map-button" value=" Útvonal " class="map-button" id="map-button-78" onclick="searchLocation( 78 ); return false;" /></div>';

			var myLatlng_<?php echo $mrow->id; ?> = new google.maps.LatLng(<?php echo $mrow->lat; ?>, <?php echo $mrow->lng; ?>);


			var infowindow_<?php echo $mrow->id; ?> = new google.maps.InfoWindow({
				content: content_<?php echo $mrow->id; ?>
			});

			var marker_<?php echo $mrow->id; ?> = new google.maps.Marker({
				position: myLatlng_<?php echo $mrow->id; ?>,
				map: map,
				icon: '<?php echo get_template_directory_uri(); ?>/library/images/icon_map/map-pin-02-large.png',
				title: '<?php echo $mrow->name; ?>'
			});

			google.maps.event.addListener(marker_<?php echo $mrow->id; ?>, 'click', function() {
				infowindow_<?php echo $mrow->id; ?>.open(map, marker_<?php echo $mrow->id; ?>);
			});
		<?php } ?>
	});
</script>

<script src="http://cdn.sobekrepository.org/includes/gmaps-markerwithlabel/1.9.1/gmaps-markerwithlabel-1.9.1.js" type="text/javascript"></script>