<?php 

$restricted = 0;

include 'ipp.php';

function table_section ($db, $WHERE, $TABLE) {

  $sql = "SELECT label,data_group,workdir,state,count(state) as n FROM $TABLE $WHERE LIMIT 10";
  if ("$TABLE" == "destreakRun") {
    $sql = "SELECT label,data_group,outroot,state,count(state) as n FROM magicDSRun $WHERE LIMIT 10";
  }
  if ("$TABLE" == "distRun") {
    $sql = "SELECT label,data_group,outroot,state,count(state) as n FROM $TABLE $WHERE LIMIT 10";
  }

  $qry = $db->query($sql);
  if (dberror($qry)) {
    echo "<tr><td><b>error reading $TABLE table</b></td></tr>\n";
    echo "<tr><td><br><small><b> table query : $sql </b></small></td></tr>\n";
  }
  
  $class = "list";
  $nlines = 0;

  // list the results
  while ($qry->fetchInto($row)) {
    echo "<tr><td class=\"$class\"><b>$TABLE</b></td>\n";
    // ** TABLE DATA **
    write_table_cell ($class, '%s', "", $row[0]);
    write_table_cell ($class, '%s', "", $row[1]);
    write_table_cell ($class, '%s', "", $row[2]);
    write_table_cell ($class, '%s', "", $row[3]);
    write_table_cell ($class, '%s', "", $row[4]);
    echo "</tr>\n";
    $nlines ++;
  }

  if ($nlines == 0) {
    echo "<tr><td class\"$class\"><b>$TABLE</b></td></tr>\n";
  }
}

$ID = checkID ();

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

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

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

menu($myMenu, 'Night Summary', 'ipp.css', $ID['link'], $ID['proj']);

echo "<p> Night Summary </p>\n";

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

$restricted = 0;

// define restrictiosn to the queries
// ** TABLE RESTRICTIONS **
$WHERE = check_restrict ('label', $WHERE, 'string', 1.0);
$WHERE = check_restrict ('data_group', $WHERE, 'string', 1.0);
$WHERE = check_restrict ('workdir', $WHERE, 'string', 1.0);
$WHERE = check_restrict ('state', $WHERE, 'string', 1.0);
$WHERE = check_restrict ('n', $WHERE, 'string', 1.0);
$WHERE = check_restrict ('n', $WHERE, 'min', 1.0);
$WHERE = check_restrict ('n', $WHERE, 'max', 1.0);

// ensure no valid results
if ($restricted == 0) {
  $WHERE = "$WHERE WHERE (0 > 1)";
}

$WHERE = check_ordering ('state,label', $WHERE);

// set up the table
echo "<table class=list>\n";

// echo "<tr><td></td>\n"; // first field is a label set below for the query rows only
// ** TABLE HEADER **
echo "<tr><td></td>\n";
write_header_cell ("list", "label");
write_header_cell ("list", "data_group");
write_header_cell ("list", "workdir");
write_header_cell ("list", "state");
write_header_cell ("list", "count");
echo "</tr>\n";

// query restriction form
// echo "<tr>\n";
// echo "<td class=list> <input type=\"text\" name=\"expID\"></td>\n";
// ** TABLE QUERY ** (restictions)
echo "<tr><td>&ge;</td>\n";
write_query_row ('label', 15, 'string');
write_query_row ('data_group', 10, 'string');
write_query_row ('workdir', 7, 'string');
write_query_row ('state', 7, 'string');
write_query_row ('n', 7, 'min');
echo "</tr><tr><td>&le;</td>\n";
echo "<td> &nbsp; </td>\n";
echo "<td> &nbsp; </td>\n";
echo "<td> &nbsp; </td>\n";
echo "<td> &nbsp; </td>\n";
write_query_row ('n', 7, 'max');
echo "</tr>\n";

// query the database (limit is 10, but should be 1
table_section($db, $WHERE, "chipRun");
table_section($db, $WHERE, "camRun");
table_section($db, $WHERE, "fakeRun");
table_section($db, $WHERE, "warpRun");
table_section($db, $WHERE, "stackRun");
table_section($db, $WHERE, "diffRun");
table_section($db, $WHERE, "magicRun");
table_section($db, $WHERE, "destreakRun");
table_section($db, $WHERE, "chipBackgroundRun");
table_section($db, $WHERE, "warpBackgroundRun");
table_section($db, $WHERE, "distRun");
// table_section($db, $WHERE, "publishRun");

// 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();

?>
