<?php 
	
	header("Content-type: text/plain; charset=windows-1251");
	header("Cache-Control: no-store, no-cache, must-revalidate");
	header("Cache-Control: post-check=0, pre-check=0", false);
	
	$db = new SQLite3('../mosaic.meta');
	
	$type = $_POST["type"];
	
	if ( $_POST["type"] == '1' ) {
		
		$db = $db->prepare('SELECT filename FROM cells WHERE col = :col AND row = :row');
		$db->bindParam(':col', $col);
		$db->bindParam(':row', $row);
		
		$col = $_POST["col"];
		$row = $_POST["row"];
		$results = $db->execute();
		
		if (empty($results)){
    		echo "Col:".$col." Row:".$row." are out of mosaic bounds." ;
		} else {
			while ($row = $results->fetchArray()) {
    			echo $row['filename'];
    		}
		}
		
	}
	else if( $_POST["type"] == '2' ) {
		
		$sql  = "SELECT filename,row,col FROM cells WHERE filename LIKE :text ORDER BY filename ASC LIMIT 10";
		$valeur = $_POST["text"]; 
		$db = $db->prepare($sql);
		$db->bindValue(':text', '%'.$valeur.'%');
		$results = $db->execute();
		
		if (empty($results)){
			echo "Can not find anything like \"".$valeur."\". Please query another text." ;
		} else {
			while ($row = $results->fetchArray()) {
				
				//echo "<button onclick=\"positionToRowCol(".$row['col'].",".$row['row'].");info.updateMeta('".addslashes($row['filename'])."')\">".htmlentities($row['filename'],ENT_QUOTES)." - ".$row['col'].":".$row['row']."</button><br />" ;
				echo "<button onclick=\"positionToRowCol(".$row['col'].",".$row['row'].");info.updateMeta('".addslashes($row['filename'])."')\">".htmlentities($row['filename'],ENT_QUOTES)." - ".$row['col'].":".$row['row']."</button> " ;
				
			}
			
		}
	} else {
		echo "Other option";
	}
	
	?>