<?php
  include ('include_fns.php');

  session_start();

  function Show_Statements($provider, $results)
  {
    if(!check_admin_user())
    {
     echo "You do not seem to be logged in.";
     exit;
    }

    ?>


    <script>

      var JTable = function() {};

      JTable.Setup = function() {
        var table = $('.jtable');
  //       $('caption', table).addClass('ui-state-default');
         $('th', table).addClass('ui-state-default');
         $('td', table).toggleClass('ui-widget-content');
      };

      $(function() {
        JTable.Setup();

        $('.btnViewForm').button({ icons: { primary: 'ui-icon-search'} }).click(function(){

          var statementkey = encodeURIComponent($(this).closest('tr').find('.statementkey').text());
           
          var reportaddress ="pdfframe.php?statementkey=" + statementkey;
          window.open(reportaddress, '_blank');
        });


      });
    </script>
    
    <?php
    
     if (!$results)
     {
      echo "No statements found !";
     } 
     else
     {
     echo "<table class='jtable shadetable leftmargined'   cellspacing = 0 cellpadding = 2 id = 'diarytable' name = 'diarytable'>";
     //echo "<tr>";
     //echo "  <th colspan = 4 bgcolor='#5B69A6'>Statements</th>";
    // echo "</tr>";

      echo "<tr><th>Date Printed</th><th>Administrator</th><th>View</th></tr>";

     while ($row=ibase_fetch_row ($results))
      {
        
        $administratorname = ExecuteQuery("SELECT ADMINISTRATOR
                                           FROM ADMINISTRATOR 
                                           WHERE ADMINISTRATOR_KEY = $row[2]");


        echo "<tr>";
        echo "<td style= 'display:none;' class = 'statementkey'>".$row[0]."</td>";
        echo "<td width = '90' align ='left'>".$row[1]."</td>";
        echo "<td width = '200' align ='left'>".$administratorname[0][0]."</td>";
        echo "<td width = '50' align ='middle'><button id='btnViewForm' class='btnViewForm'>View</button></td>";
        echo "</tr>";
      } 

     echo "</table>";
     }

  }
  
  function GetResults($provider, $administrator)
  {
    if($conn = statementdb_connect())
    {    

      $query = "SELECT STATEMENT_KEY, DATEPRINTED,  ADMINISTRATOR_KEY
                FROM STATEMENT
                WHERE PROVIDERNO = '$provider'";

      if ($administrator <> '-1')
         $query .= " AND ADMINISTRATOR_KEY = $administrator";

      $query .= " ORDER BY DATEPRINTED DESC;";

      $result = ibase_query($conn, $query);
    
      return $result;
    } 
  }


    $provider      = $_SESSION['admin_user'];
    $administrator = $_GET['administrator'];
  
    $results = GetResults($provider, $administrator);

    Show_Statements($provider, $results);
  

?>
