<?php

require_once($_SERVER['DOCUMENT_ROOT'] . '/sys/db.php');

function getCombines ($id, $combines) {

  $dal = new DAL();

  $sql = "SELECT RQ.id, CT.text, combineidx, combinequery_id " .
         "FROM reportquery RQ, combinetype CT " .
         "WHERE RQ.id = $id AND RQ.combineidx > 0 " .
         "AND RQ.combinetype_id = CT.id AND RQ.active = 1 LIMIT 1" ;
  $report = $dal->query($sql);

  if (null != $report) {
    $text  = $report->text . " " . $report->combineidx;
    $combines[] = ["id"=>$report->id, "text"=>$text];
  }
  
  if ($report->combinequery_id > 0) {
    getCombines ($report->combinequery_id, $combines); 
  }
  else {
    array_push($combines, ["id"=>0, "text"=>"Add"], ["id"=>-1, "text"=>"Delete"]);
    echo json_encode ($combines);
  }    
}  

foreach ($_POST as $paramName => $paramValue) {
  if ($paramName == "id")
    $rg_id = $paramValue;
}

$combineList = [];
$combineList = getCombines($rg_id, $combineList);

?>

