<?php 
defined('PUBLIC_ACCESS') or die('Restricted Access');

class modDBTablesView {
  private $searchTableLayout;
  private $searchTableScript;
  private $columns;
  private $content;
  private $scope;
  private $table;
    
  public function __construct(modDBTables $dbTables) {
    if (isset($dbTables)) {
      $this->scope   = $dbTables->get("scope");
      $this->columns = $dbTables->get("columns");
      $this->content = $dbTables->get("content");
      $this->table   = $dbTables->get("table");
     
      $entry = (object)array(
                 "baseDir" => $dbTables->get("baseDir"),
                 "scope"   => "tablename",
                 "dbValue" => "",
  	         "dbRowId" => "0",
  	         "message" => "tabledata"    
 	        );

      $tableName     = new LiveSearch($entry);
      $this->searchTableLayout = $tableName->outputLayout();
      $this->searchTableScript = $tableName->outputScript();
    }
  }

  public function output() {

    $layout = "
<div 
  style='width:100%;height:50px;background-color:#f1f1f1;cursor:pointer;border: 1px solid black;
         border-radius:5px 5px 0px 0px;border-bottom:none;'>
  <h4>Database Tables</h4>
</div>

<div class='container'>   
  <div class='row'>  
    <div class='col-md-12'>
      <form method='POST' autocomplete='off'>
        <input type='hidden' name='action' value='save'>

        <table class='table table-bordered'>
   ";

        // Display all columns (in rows on the form)
        
      foreach ($this->columns as $column) {
        $col = $column->name;
        $id = $this->scope .  $col;
        $layout .= "
          <tr>
            <td style='width:15%'>$col</td>
            <td>
              <input id='$id' class='$this->scope' type='text' name='$col' style='width:100%'> 
            </td>
          </tr>
    ";
  }
    $layout .= "
          <tr> 
            <td>
              <input type='hidden' name='$this->scope' value='$this->table_name'> 
              <input type='button' class='btn btn-inverse' value='New' onclick='insertAction()'>
            </td>
            <td >
              <button class='btn btn-inverse'>Save</button>
            </td>
          </tr>
        </table>
      </form>
     
    </div>
  </div>
  <div class='row'>
    <div class='col-md-12'>
      <label for='search-form'>Table Name: $this->table<br>Search</label>
      <form id='search-form' method='POST' name='table-name' autocomplete='off'> 
        <input type='hidden' name='action' value='select'>
   $this->searchTableLayout
      </form>
    </div>
  </div>
  <div class='row'>
  
    <div class='col-md-12'>

      <table class='table'>
        <thead>
          <tr>        
    ";
    
    $i = 0;  
    foreach ($this->columns as $column) {
      if ($i < 3)
        $layout .= "
            <th>$column->name</th>
        ";
      $i++;
    }
    $layout .= "
            <th></th>
          </tr>
        </thead>
        <tbody>
          <tr>
    ";
    
    $insertAction = ""; 
    $firstOf = true;
    foreach ($this->content as $data) {

      $i = 0;
      $updateAction = ""; 

      foreach ($this->columns as $column) {
        $col = $column->name;
        $id = $this->scope . $col;
        $value = $data->$col; // column->name is a variable.
        if ($firstOf) {
          if ($col == 'id') { 
            $insertAction .= "document.getElementById('$id').value=0;document.getElementById('$id').readOnly=true;";
            $updateAction .= "document.getElementById('$id').readOnly=true;";
          } 
          else          
            $insertAction .= "document.getElementById('$id').value='';";
        } 
        if ($i == 1) {
          $updateAction .= "document.getElementById('$id').focus();";
          $insertAction .= "document.getElementById('$id').focus();";
        }   
       
        $updateAction .= "document.getElementById('$id').value='$value';";

        if ($i < 3)
          $layout .= "
            <td>$value</td>
          ";
      
        $i++;
      }
  
      // Edit button (for each row)
      $layout .= "
            <td>
              <button type='button' class='btn' onclick=\"$updateAction\">Update</button>
            </td>
          </tr>
      "; 
      $firstOf = false;
    }
    $layout .= " 
        </tbody>    
      </table>
    </div>
  </div>
</div>

 
<script>
 
function insertAction () {
  $insertAction
}
 
function select$this->scope () {
alert ('select');  
  document.forms['table-name'].submit();  
}

function notifytabledata (msgId) {
  document.forms['table-name'].submit();  
}

var offset = $('.navbar').height();
$('html:not(.legacy) table').stickyTableHeaders({fixedOffset: offset});


$this->searchTableScript
   
</script>	  
  "; 
  return $layout;
  }
}

?>