<?php
/**
 * this view generates fields for browsing all records
 * <br/><br/>
 * @author Gabriele Droege, Patricia Kelbert (BGBM, GGBN/DNA Bank Network) <contact@dnabank-network.org>
 * @version yii 2.0.3
 * @package View
 * @copyright Copyright © 2011 DNA Bank Network http://www.dnabank-network.org
 * <br/>The contents of this file are subject to the Mozilla Public License Version 1.1
 * @filesource
 * @license http://www.mozilla.org/MPL/ MPL
 */
use yii\helpers\Html;
use yii\widgets\LinkPager;
use app\controllers\SearchController;

use yii\helpers\Url;
use yii\web\View;

$this->title = 'GGBN Portal';
?>

<div class="left-column">
<?php
echo"Filter by institution";
foreach ($facets as $fn=>$f ){
	echo $f."<br/>";
}
?>
</div>
<div class="jumbotron"><h2>Tree - comparison to the Catalogue of Life <img id="help" alt="help" title="help - how does the search work" src="<?php echo Yii::$app->request->baseUrl?>/images/icons/help.png" />
</h2>

<h2>Number of unique taxa included / number of occurrences (DNA or tissue) in the database</h2></div> 
<div class="browser">
Click on the butterfly to display the DNA samples. 
<div id="tree">
<ul>
<?php foreach($tree as $branch=>$content){
	if($content[2]>0)
		$cl = "col_present";
	else
		$cl = "col_absent";
	echo "<li><span  class='collapsed ".$cl."' rank='kingdom' kingdom='".$content[0]."' phylum='' classis='' ordo='' family='' genus='' id='".$branch."' name='".$content[0]."' >".$content[0]." (".$content[1]." / ".$content[2].")</a></span></li>";
}
?>

	
	</ul>
</div>
</div>
<script type="text/javascript">
$(document).ready(function(){
    $( ".dropDownLabel" ).change(function() {
        var url="<?php echo Yii::$app->request->baseUrl?>/search/tree?institution="+$(this).val();
        $(location).attr('href',url);
      });
    
    	$(document).on( "click", 'span',function(event) {
    	    var target = $(event.target);
    	    if (target.is('a') || target.is('img')) {
    	    
    	    }else
    	    {
    	    var c =  $(this).attr('class');
        	if(c.indexOf('ignore')==-1){
        	  $(this).attr('class',c+" ignore");	
	           	var v = 'id='+ $(this).attr('id').split("__")[0]+'&rank='+$(this).attr('rank')+'&name='+$(this).attr('name').split('(')[0];
	           	v+='&kingdom='+$(this).attr('kingdom');
	        	v+='&phylum='+$(this).attr('phylum').split("(")[0];
	        	v+='&classis='+$(this).attr('classis').split("(")[0];
	        	v+='&ordo='+$(this).attr('ordo').split("(")[0];
	        	v+='&family='+$(this).attr('family').split("(")[0];
	        	v+='&genus='+$(this).attr('genus').split("(")[0];
		        v+='&institution='+$( ".dropDownLabel" ).val();
	        	var id =$(this).attr('id');
	    	    $.ajax({url: "<?php echo Yii::$app->request->baseUrl?>/ajax/tree", data: v,
	        	    success: function(result){
	    	        if(result!=0)
	    	       $('#'+id).after(result);
	    	    }});
        	}
        	c =  $(this).attr('class');
        	if(c.indexOf('expanded')==-1){
            	if(c.indexOf('noexp')==-1){
        	    $(this).attr('class',c.replace('collapsed','')+" expanded");
        	    $(this).next().show();
            	}
            }else{
                if(c.indexOf('noexp')==-1){
                $(this).attr('class',c.replace('expanded','')+" collapsed");
                $(this).next().hide();
                }
                }
    	    }
        });
    	$("#help").on('click', function(e)  {
    	    window.open('http://wiki.ggbn.org/ggbn/GGBN_Data_Portal_Explanations','_blank');
    	        });
    
});
</script>