<?php

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

  //////////////////////////////////  Connecting with DB
  if( !$conn = new mysqli($host, $user, $pswd, $db)) {
    error_log("Connection failed.\n");
    exit;
  }
  ////////////////////////////////// prepare
  (isset($_POST['prm_lprm_from']) && !empty($_POST['prm_lprm_from'])) ? $lprm_from=$_POST['prm_lprm_from']:returnError("Error");
  (isset($_POST['prm_lprm_to']) && !empty($_POST['prm_lprm_to'])) ? $lprm_to=$_POST['prm_lprm_to']:returnError("Error");
  (isset($_POST['prm_rprm_from']) && !empty($_POST['prm_rprm_from'])) ? $rprm_from=$_POST['prm_rprm_from']:returnError("Error");
  (isset($_POST['prm_rprm_to']) && !empty($_POST['prm_rprm_to'])) ? $rprm_to=$_POST['prm_rprm_to']:returnError("Error");
  (isset($_POST['prm_list']) && !empty($_POST['prm_list'])) ? $prm_list=$_POST['prm_list']:returnError("Error");
  (isset($_POST['prm_chr']) && !empty($_POST['prm_chr'])) ? $prm_chr=$_POST['prm_chr']:returnError("Error");
  if (!(is_numeric($lprm_from)) || !(is_numeric($lprm_to)) || !(is_numeric($rprm_from)) || !(is_numeric($rprm_to))) {
        $errmsg = "<div align=\"center\"><font color=\"red\" style=\"font-size:18px;\">Invalid Value!!</font></div>";
        echo $errmsg;
        exit;
  }
  _pregMatch($prm_list);
  _pregMatch($prm_chr);
  $db_lists = explode(",",$prm_list);
  $x = count($db_lists);

  for($i=0;$i<$x;$i++){
    $table = getSerialTableByColumn($conn, $db, $db_lists[$i]."_accession", $prm_chr."_b");
    $query = "SELECT pos FROM ". $table. " where ".$prm_chr. "_b is NULL and pos between ? and ?";
    $stmt = $conn->stmt_init();
    $stmt->prepare($query);

    // for Left
    $stmt->bind_param("ii", $lprm_from, $lprm_to);
    $stmt->execute();
    $stmt->store_result();
    $stmt->bind_result($row0);
    while ($stmt->fetch()) {
        $lposition[] = $row0;
    }

    // for Right
    $stmt->bind_param("ii", $rprm_from, $rprm_to);
    $stmt->execute();
    $stmt->store_result();
    $stmt->bind_result($row0);
    while ($stmt->fetch()) {
        $rposition[] = $row0;
    }

    $stmt->close();
  }
  $lresult = array_unique($lposition);
  $lresult = implode(",",$lresult);

  $rresult = array_unique($rposition);
  $rresult = implode(",",$rresult);
/*
  for($i=0;$i<$x;$i++){
    $query = "SELECT pos FROM ".$db_lists[$i]."_accession where ".$prm_chr."_b is NULL and pos between ? and ?";
    $stmt = $conn->stmt_init();
    $stmt->prepare($query);
    $stmt->bind_param("ii", $lprm_from, $lprm_to);
    $stmt->execute();
    $stmt->store_result();
    $stmt->bind_result($row0);
    while ($stmt->fetch()) {
	$lposition[] = $row0;
    }
  }
  $lresult = array_unique($lposition);
  $lresult = implode(",",$lresult);

  for($i=0;$i<$x;$i++){
    $query = "SELECT pos FROM ".$db_lists[$i]."_accession where ".$prm_chr."_b is NULL and pos between ? and ?";
    $stmt = $conn->stmt_init();
    $stmt->prepare($query);
    $stmt->bind_param("ii", $rprm_from, $rprm_to);
    $stmt->execute();
    $stmt->store_result();
    $stmt->bind_result($row0);
    while ($stmt->fetch()) {
        $rposition[] = $row0;
    }
  }
  $rresult = array_unique($rposition);
  $rresult = implode(",",$rresult);
*/
  $result = $lresult."|".$rresult;
  echo $result;
?>
