<?php 

include 'ipp.php';

$ID = checkID ();
// require an explicit project
if (! $ID['proj']) { projectform ($ID); }

$db = dbconnect($ID['proj']);

if ($ID['menu']) {
  $myMenu = $ID['menu'];
} else {
  $myMenu = "ipp.detrend.dat";
}

menu($myMenu, 'Master Detrend Imfiles', 'ipp.css', $ID['link'], $ID['proj']);

echo "<p> Master Detrend Imfiles </p>";

// define restrictiosn to the queries
// ** TABLE RESTRICTIONS **
$WHERE = check_restrict ('det_id', $WHERE);
$WHERE = check_restrict ('iteration', $WHERE);
$WHERE = check_restrict ('uri', $WHERE);

// query the database
if ($WHERE) {
  $sql = "SELECT det_id,iteration,uri FROM detNormalizedImfile WHERE $WHERE UNION SELECT det_id,iteration,uri FROM detStackedImfile WHERE normalize = 0 AND $WHERE LIMIT 20";
} else {
  $sql = "SELECT det_id,iteration,uri FROM detNormalizedImfile UNION SELECT det_id,iteration,uri FROM detStackedImfile WHERE normalize = 0 LIMIT 20";
}

$qry = $db->query($sql);
if (dberror($db)) {
  echo "<b>error reading detStackedImfile table</b><br>\n";
  menu_end();
}

// ** HEAD CODE **

// set up the table and form
echo "<form action=\"masterDetrendImfiles.php\" method=\"POST\">\n";

echo "<table class=list>\n";
echo "<tr>\n";
// ** TABLE HEADER **
echo "<th class=\"list\"> det_id </th>\n";
echo "<th class=\"list\"> iter </th>\n";
echo "<th class=\"list\"> uri </th>\n";
// echo "<th class=list> FIELD </th>\n";
echo "</tr>\n";

// list the results
while ($qry->fetchInto($row)) {
  // $link = "masterDetrendImfiles.php" . "?expID=" . $row[0] . "&" . $ID['link'];

  echo "<tr>\n";
  // ** TABLE DATA **
  echo "<td class=\"list\"> $row[0] </td>\n";
  echo "<td class=\"list\"> $row[1] </td>\n";
  echo "<td class=\"list\"> $row[2] </td>\n";
  // echo "<td class=list><a href=\"$link\"> $row[0] </a></td>\n";
  // echo "<td class=list> $row[1] </td>\n";
  echo "</tr>\n";
}

// query restriction form
echo "<tr>\n";
// echo "<td class=list> <input type=\"text\" name=\"expID\"></td>\n";
// ** TABLE QUERY **
write_query_row ('det_id', 7);
write_query_row ('iteration', 5);
write_query_row ('uri', 5);
echo "</tr>\n";

// close the table and form
echo "</table>\n";
echo "<input type=\"submit\" name=\"constraints\" value=\"refine search\">\n";
$pass = $ID['pass'];
$proj = $ID['proj'];
$menu = $ID['menu'];
echo "<input type=\"hidden\" name=\"pass\" value=\"$pass\">\n";
echo "<input type=\"hidden\" name=\"proj\" value=\"$proj\">\n";
echo "<input type=\"hidden\" name=\"menu\" value=\"$menu\">\n";
echo "</form>\n";

// ** TAIL CODE **

echo "<small> WHERE: $WHERE<br><br></small>\n";
echo "<small> SQL: $sql<br><br></small>\n";

menu_end();

?>
