<?php
  global $FORM;
  require_once "lib/menus/default/list_functions.php";

  global $menu, $tableName, $schema;
  list($listDetails, $listRecords) = listMatchingRecords($tableName, @$FORM{'page'}, @$FORM{'perPage'}, @$FORM{'keyword'});
  $listFields = preg_split("/\s*,\s*/", $schema['listPageFields']);  // fields to show on list page

?>


<form method="get" action="<?php echo $_SERVER['PHP_SELF'] ?>" autocomplete="off" style="display: inline: margin: 0px">
<input type="submit" style="width: 0px; height: 0px; position: absolute; border: none" /> <!-- bugfix: hitting enter in textfield submits first submit button on form -->
<input type="hidden" name="menu" id="menu" value="<?php echo htmlspecialchars($menu) ?>" />
<input type="hidden" name="_defaultAction" value="list" />
<input type="hidden" name="page" value="<?php echo $listDetails{'page'} ?>" />

<table border="0" cellspacing="0" cellpadding="0" width="690">
 <tr>
  <td class="title">
   <a href="?menu=<?php echo $menu ?>" ><?php echo $schema{'menuName'} ?></a>

  </td>
  <td align="right">
   <?php if (!@$schema['_disableAdd']): ?>
     <input type="submit" name="action=add"  value="Create" class="inputButton" />
   <?php endif ?>
   <input type="submit" name="action=list" value="Refresh" class="inputButton" />
  </td>
 </tr>
</table>

<div class="divider_line"></div>

<!-- search and results bar -->
<table border="0" cellspacing="0" cellpadding="0" width="690">
 <tr>
  <td>

   <table border="0" cellspacing="0" cellpadding="0">
    <tr>
     <td>Search for keyword</td>
     <td>perPage</td>
     <td>&nbsp;</td>
    </tr>
    <tr>
     <td><input type="text" name="keyword" value="<?php echo htmlspecialchars(@$FORM{'keyword'}) ?>" size="25" />&nbsp;</td>
     <td>
       <select name="perPage">
       <option <?php selectedIf($listDetails{'perPage'}, '5')?>>5</option>
       <option <?php selectedIf($listDetails{'perPage'}, '10')?>>10</option>
       <option <?php selectedIf($listDetails{'perPage'}, '25')?>>25</option>
       <option <?php selectedIf($listDetails{'perPage'}, '50')?>>50</option>
       <option <?php selectedIf($listDetails{'perPage'}, '100')?>>100</option>
       </select>&nbsp;
      </td>
     <td>
      <input type="submit" name="search"      value="Search" class="inputButton" />
      <input type="button" name="resetSearch" value="Reset" class="inputButton" onclick="window.location='?menu=<?php print urlencode($menu) ?>'"/><br />
     </td>
    </tr>
   </table>

  </td>
  <td align="right">
   <table border="0" cellspacing="0" cellpadding="1">
    <tr>
     <td class="lSearchText" align="center">
      <?php if (@$FORM{'keyword'} == ''): ?>
        Total Records: <?php echo $listDetails{'totalRecords'} ?>
      <?php else: ?>
        Matched: <?php echo $listDetails{'totalMatches'} ?> of <?php echo $listDetails{'totalRecords'} ?>
      <?php endif ?>
     </td>
    </tr>
    <tr>
     <td align="center">
      <table border="0" cellspacing="0" cellpadding="0">
       <tr>
        <td class="lSearchText">Page&nbsp;</td>
        <td>
          <!-- This field is renamed to "page" when changed by user -->
          <input type="text" size="4" class="lSearchBox" name="unused" value="<?php echo $listDetails{'page'} ?>" onchange="this.name='page';"/><br/>
        </td>
        <td class="lSearchText">&nbsp;of <?php echo $listDetails{'totalPages'} ?> </td>
       </tr>
      </table>
     </td>
    </tr>
    <tr><td class="lSearchText noBreak" align="center">
     <a href="?menu=<?php echo htmlspecialchars($menu) ?>&amp;action=list&amp;page=<?php echo $listDetails{'prevPage'}?>&amp;perPage=<?php echo $listDetails{'perPage'}?>&amp;keyword=<?php echo urlencode(@$FORM{'keyword'}) ?>">&lt;&lt; Prev Page</a> |
     <a href="?menu=<?php echo htmlspecialchars($menu) ?>&amp;action=list&amp;page=<?php echo $listDetails{'nextPage'}?>&amp;perPage=<?php echo $listDetails{'perPage'}?>&amp;keyword=<?php echo urlencode(@$FORM{'keyword'}) ?>">Next Page &gt;&gt;</a><br />
    </td></tr>
   </table>

  </td>
 </tr>
</table>

<div class="divider_line"></div>

<!-- list column headings -->
<table border="0" cellspacing="1" cellpadding="0" width="690">
 <tr>
  <?php
    foreach ($listFields as $field):
      if (empty($field)) { continue; }
    ?>
    <td class="listHeader"><?php echo @$schema[$field]['label'] ?></td>
  <?php endforeach; ?>
  <td class="listHeader" style="padding: 0px;" align="center" colspan="2">Action</td>
 </tr>

<?php foreach ($listRecords as $record):
      $bgColorClass = (@$bgColorClass == "listRowOdd") ? 'listRowEven' : 'listRowOdd'; # rotate bgclass
?>
 <tr>
  <?php foreach ($listFields as $field): ?>
    <td class="<?php echo $bgColorClass ?>"><?php echo @$record{$field} ?></td>
  <?php endforeach; ?>
  <?php
    if (!@$schema['_disableErase']) { $tableCellAttributes = "width='64'  class='$bgColorClass' colspan='1' align='center'"; }
    else                            { $tableCellAttributes = "width='90' class='$bgColorClass' colspan='2' align='center'"; }

    // show modify link
    $modifyLink = "?menu=" .htmlspecialchars($menu). "&amp;action=edit&amp;num={$record['num']}";
    print "<td $tableCellAttributes><a href=\"$modifyLink\">modify</a></td>\n";

    // show erase link
    if (!@$schema['_disableErase']) {
      $eraseLink  = "javascript:confirmEraseRecord('" .htmlspecialchars($menu). "','{$record['num']}');";
      print "<td $tableCellAttributes><a href=\"$eraseLink\">erase</a></td>\n";
    }
  ?>
 </tr>
<?php endforeach ?>


<?php if (count($listRecords) == 0): ?>
  <tr>
   <td class="listRowOdd" colspan="<?php print count($listFields) + 2 ?>">
   <?php if (@$FORM['keyword'] == ''): ?>  Sorry, no records were found!  <?php endif ?>
   <?php if (@$FORM['keyword'] != ''): ?>  Sorry, the <b>search</b> returned no results! <?php endif ?>
   </td>
  </tr>
<?php endif ?>

</table>

<div class="divider_line"></div>

<table border="0" cellspacing="0" cellpadding="0" width="690">
 <tr>
  <td align="left">
  </td>
  <td align="right">
   <?php if (!@$schema['_disableAdd']): ?>
     <input type="submit" name="action=add"  value="Create" class="inputButton" />
   <?php endif ?>
   <input type="submit" name="action=list" value="Refresh" class="inputButton" />
  </td>
 </tr>
</table>

</form>

<script type="text/javascript" src="lib/menus/default/list_functions.js"></script>
