<?php

  require '../conf/config.php';

  //////////////////////////////////  Connecting with DB
  if( !$conn = new mysqli($host, $user, $pswd, $db)) {
    error_log("Connection failed.\n");
    exit;
  }

  ////////////////////////////////// prepare 
  
  $ids = array();
  $dbs = array();
  $grp = array();
  $str;
  $uniq_array;
  $x=0;
  $y=0;
  $flag=0;
  
  if ( file_exists( "../conf/order.conf" )) {
	$line = file_get_contents("../conf/order.conf");
	$array = explode("\n",$line);
	$uniq_array = array_unique($array);
	$x=count($uniq_array);
	
	$query = "SELECT accession_id FROM sys_accession where accession_id= ? ";
	$stmt = $conn->stmt_init();
    $stmt->prepare($query);
	
	for($i=0;$i<$x;$i++){
		$id_test = array_shift($array);
		$stmt->bind_param("s", $id_test); 
		$stmt->execute();
		$stmt->store_result();
		$stmt->bind_result($row0);
		while ($stmt->fetch()) {
			$chk[]=$row0;
			$flag=1;
		}
	}
	if($flag == 1){
		$y=count($chk);
	}else{
		$y=0;
	}
	$stmt->free_result();
    $stmt->close();
  }
  
  if ($x>0 and $y>0 ){
    $query = "SELECT accession_id, accession_db, accession_name, variety, sub_variety, origin, origin_2, type FROM sys_accession where accession_id= ? ";
	$stmt = $conn->stmt_init();
    $stmt->prepare($query);
	
	for($i=0;$i<$x;$i++){
		$ac_id = array_shift($uniq_array);
		$stmt->bind_param("s", $ac_id); 
		$stmt->execute();
		$stmt->store_result();
		$stmt->bind_result($row0,$row1,$row2,$row3,$row4,$row5,$row6,$row7);

		while ($stmt->fetch()) {
			$ids[]=$row0;
			$dbs[]=$row1;
			$names[]=$row2;
			$variety[]=$row3;
			$sub_variety[]=$row4;
			$origins[]=$row5;
			$origins_2[]=$row6;
			$type[]=$row7;
		}
	}
	$stmt->free_result();
    $stmt->close();
	
	$str="[";
	  for ($i=0; $i<count($ids); $i++) {
		$str=$str."{\"id\":\"".$ids[$i]."\",\"db\":\"".$dbs[$i]."\",\"name\":\"".$names[$i].
				  "\",\"variety\":\"".$variety[$i]."\",\"grp\":\"".$sub_variety[$i].
				  "\",\"origin\":\"".$origins[$i]."\",\"origin_2\":\"".$origins_2[$i]."\",\"type\":\"".$type[$i]."\"}"; 
		if ( $i<count($ids)-1 ) {
		  $str=$str.",";
		}
	  }
	  $str=$str."]|[";

  }else{
	  $query = "SELECT accession_id, accession_db, accession_name, variety, sub_variety, origin, origin_2, type FROM sys_accession order by cast(substring(accession_db, 2) as unsigned) asc";
	  $stmt = $conn->stmt_init();
      $stmt->prepare($query);
	  $stmt->execute();
	  $stmt->store_result();
	  $stmt->bind_result($row0,$row1,$row2,$row3,$row4,$row5,$row6,$row7);
	  
	  while ($stmt->fetch()) {
		$ids[]=$row0;
		$dbs[]=$row1;
		$names[]=$row2;
		$variety[]=$row3;
		$sub_variety[]=$row4;
		$origins[]=$row5;
		$origins_2[]=$row6;
		$type[]=$row7;
	  }
	  $stmt->free_result();
      $stmt->close();
	  
	  $str="[";
	  for ($i=0; $i<count($ids); $i++) {
		$str=$str."{\"id\":\"".$ids[$i]."\",\"db\":\"".$dbs[$i]."\",\"name\":\"".$names[$i].
				  "\",\"variety\":\"".$variety[$i]."\",\"grp\":\"".$sub_variety[$i].
				  "\",\"origin\":\"".$origins[$i]."\",\"origin_2\":\"".$origins_2[$i]."\",\"type\":\"".$type[$i]."\"}"; 
		if ( $i<count($ids)-1 ) {
		  $str=$str.",";
		}
	  }
	  $str=$str."]|[";
  }

  ////////////////////////////////////////////////////////// List of tracks
  $queryT = "SELECT DISTINCT track FROM sys_track order by seq";
  $stmtT = $conn->stmt_init();
  $stmtT->prepare($queryT);
  $stmtT->execute();
  $stmtT->store_result();
  $stmtT->bind_result($rowT0);
  
  $tracks = array();
  while ($stmtT->fetch()) {
    $tracks[]=$rowT0;
  }
  $stmtT->free_result();
  $stmtT->close();
  
  $queryC = "SELECT count(*) FROM sys_track WHERE parent=0 AND track= ? ";
  $stmtC = $conn->stmt_init();
  $stmtC->prepare($queryC);
  
  for ($i=0; $i<count($tracks); $i++) {
	$stmtC->bind_param("s", $tracks[$i]); 
	$stmtC->execute();
	$stmtC->store_result();
	$stmtC->bind_result($rowC);
	
    while ($stmtC->fetch()) {
		$color = "#000000";
		if ( $rowC > 0 ) {
		  $color = "#000080";
		}
		$str=$str."{\"track\":\"".$tracks[$i]."\",\"p\":\"1\",\"h\":\"4\",\"c\":\"".$color."\",\"s\":\"".$i."\"}";
		if ( $i<count($tracks)-1 ) {
		  $str=$str.",";
		}
	}
  }
  $stmtC->free_result();
  $stmtC->close();
  
  $str=$str."]";

  echo $str;

?>

