<?php 

use yii\widgets\Pjax;

?>

<div class="p-t50">
	<div class="searchfield-panel">
		<div class="searchfield-header ta-c">Search</div>
			<div class="searchfield-resultnumber ta-c p-t5"><?=number_format($numFoundTotal)." records found"; ?></div>
			<div class="searchparams">
<?php foreach ($facets as $facetName => $f)
	{ 
	?>
	<div class="searchfield-formfieldgroup">
		<div class="searchfield-fieldheader ta-l collapsed" data-toggle="collapse" data-target="#collapseTarget-<?=$facetName; ?>" aria-expanded="false" aria-controls="collapseTarget-<?=$facetName; ?>"><?=$facetlabels[$facetName]; ?>
		<i class="fa fa-plus float-right"></i>
				</div>
		<div class="searchfield-selection-panel <?php if(!isset($_GET[$facetName])) echo 'collapse'?>" id="collapseTarget-<?=$facetName; ?>">
			<?php if($fieldtypes[$facetName] == 'listandsearch') { ?>
			<div class="listfieldsearch-selection">
					<input class="listfieldsearch" placeholder="search (not working yet)" type="text" value="" id="search-<?=$facetName; ?>">
				</div>
			<?php } ?>
			<div class="facetresults">
				
		<?php 
		
		foreach ($f as $i => $fresult)
				 {  ?>
		<div class="searchfield-selection" >
			
			<div class="checkbox">
				<label>
					<input class="target" type="checkbox" value="<?=$fresult['facetResult']; ?>" name="<?=$facetName; ?>"
					 <?php 
							//turning get parameters into arrays
							$querystring  = explode('&', $_SERVER['QUERY_STRING']);
							$params = array();
							if($_SERVER['QUERY_STRING'] != "") {
								foreach( $querystring as $param )
								{
									// prevent notice on explode() if $param has no '='
									if (strpos($param, '=') === false) $param += '=';
							
									list($key, $value) = explode('=', $param, 2);
									$params[urldecode($key)][] = urldecode($value);
									
									if(str_replace('+', ' ', $value) == $fresult['facetResult']) { echo "checked"; }
									
								}
							}
							//var_dump($params);
					?>> 
					<span class="filterName">
					<span class="facetValueLabel target"> <?=$fresult['facetResult']; ?></span>
					<span class="facetValueCount"> <?=number_format($fresult['facetCount']) ?></span>
					</span>
				</label>
		</div>
			
		</div>
		<?php   } 
		?>
			</div>
		</div>
	</div>
<?php 	}?>	
	
	</div>
	</div>
	
		
<!-- 	<script>
	$( ".target" ).change(function() {
		var pathname = window.location.pathname;
		var query = window.location.search; // Returns ? followed by the parameters or "querystring" of the URL
		if (query) { // check if already one parameter is set
			var temp = query + "&" + this.name + "=" + this.value;
		}
		else { var temp = "?" + this.name + "=" + this.value;}

		window.location = temp;
		});

		
	</script>  -->
	
	<script>
$('.target').click(function(e)
{
    const url_obj = new URL(location.href);
    const params = url_obj.searchParams;


    // Append when checkbox gets checked and delete when unchecked
    if (this.checked) {
        params.append(this.name, this.value);
    } else {
        params.delete(this.name);
        
    }

    window.location = url_obj.href;
});
</script>
	
	
	
	<div class="site-content">
	
	<div class="container">
	

		<div class="container-description">

			
			<div class="results-table">
				
				<?php
				$content_array = array (
						"value" => [],
						"url" => [],
						"isocountrycode" => [],
						"unitID" => [],
						"institutioncode" => [],
						"kindofunit" => [],
						"country" => [],
						"collectioncode" => [] ,
						"collectors" => [],
						"collectornumber" => [],
						"gatheringyear" => [],
						"biodatasourcename" =>[],
						"tripleidstoreid"=>[],
						"guid"=>[]
				);
				
				foreach($docs as $doc) {
					if ($doc->fullScientificName == "" || $doc->fullScientificName == "N/A" ) {
						if ($doc->familia != "" && $doc->familia != "N/A") {
							$value = ucfirst(strtolower($doc->familia));
						} else {
							if ($doc->ordo != "" && $doc->ordo != "N/A") {
								$value = $doc->ordo;
							} else {
								if ($doc->classis != "" && $doc->classis != "N/A") {
									$value = $doc->classis;
								} else {
									if ($doc->phylum != "" && $doc->phylum != "N/A") {
										$value = $doc->phylum;
									} else {
										if ($doc->regnum != "" && $doc->regnum != "N/A") {
											$value = $doc->regnum;
										} else
											$value = "no identification";
									}
								}
							}
						}
						array_push ( $content_array ["value"], $value );
					} else {
						$value = $doc->fullScientificName;
						array_push ( $content_array ["value"], $value );
					}
					
					//array_push ( $content_array ["isocountrycode"], $doc->isocountrycode );
					array_push ( $content_array ["country"], $doc->country );
					if ($doc->gatheringyear == "" || $doc->gatheringyear == "N/A" ) {
						$gatheringyear = "";
					}
					else { $gatheringyear = $doc->gatheringyear; }
					
					array_push ( $content_array ["gatheringyear"], $gatheringyear );
				}	?>
						
				<table class="table table-striped table-members" id="resulttable">
				<th>Scientific Name</th><th>Country</th><th>Year(s)</th><th>gDNA</th><th>eDNA</th><th>Tissue</th><th>Specimen</th><th>Culture</th><th>eSample</th>
					<?php 
					for($i = 0; $i <= sizeof ( $content_array ["value"] ) - 1; $i ++) {
						echo "<tr><td>" . $content_array ["value"] [$i] . "</td>";
						//echo "<td><span title='". $content_array ["country"] [$i] ."'>" . $content_array ["isocountrycode"] [$i] . "</span></td>";
						echo "<td>". $content_array ["country"] [$i] . "</td>";
						echo "<td>". $content_array ["gatheringyear"] [$i] ."</td>";
						echo "</tr>";
					}
					?>	
				</table>
			</div>
			
			
			
			
		</div>
			
	</div>
	</div>
</div>

<script>
        $('.searchfield-fieldheader').click(function() {
            $(this).find('i').toggleClass('fa fa-plus fa fa-minus');
        });
    </script>


<?php




/* nächste Schritte
 * 1. Grunddesign linke Seite, erstmal Feldtitel (=facetName) und Anzeige der zehn top-Ergebnisse
 * 2. Allgemeines Suchfeld für jeden einzelnen Parameter ergänzen
 * 3. rechte Seite: Tabelle vorbereiten, dass es über GET-Suche erstmal was liefert
 * 4. Recherchieren/fragen wie ich die Ergebnisse live aktualisieren kann
 */



?>