<?php 

class Dashboard  {
  private $idKey;
  private $scope;
  private $formDefs;
  private $scopeId;
  private $baseId;
  private $data;
  private $messageList;
  private $iterator;
  private $select;

  public function __construct($entry) {

    if (isset($entry)) {

      $this->idKey     = $entry->idKey;
      $this->scope     = $entry->scope;
      $this->scopeId   = $entry->scopeId;
      $this->subdetail = $entry->subdetail;
      $this->iterator  = $entry->iterator;

      if (isset($entry->baseId))
        $this->baseId = $entry->baseId;
      else    
        $this->baseId = 0;

    if (isset($entry->selOptions)) {
      foreach ($entry->selOptions as $option) {
        $this->selOptions .= "<option value=$option->id>$option->descr</option> ";
      }
    }  

    $this->select = 
          "<input type=hidden id=opt$this->scope$input->id value=$input->role>" .
          "<select id=$input->id class=sel$this->scope style=margin-bottom:0px;height:25px;width:100%;>" .
            "$this->selOptions" .
          "</select>" ;

      if (null !== $entry->formDefs)
        $this->formDefs = json_encode($entry->formDefs);
      else
        $this->formDefs = '{}';
 
      if (null !== $entry->message) {
        $messages = explode(",", $entry->message);
        $this->messageList = "";

        for ($i=0; $i<count($messages); $i++) {
          $this->messageList .= "notify" . $messages[$i] . "(dbRowId); ";
        }
      }
     
      if ((count($entry->data)) > 0) {
        $this->data = $entry->data;
        $firstRow   = reset($this->data);
        $line       = get_object_vars($firstRow);
        $this->props = array_keys($line);

        if ($firstRow->id == 0)       
          $this->data  = [];

      }
      else {
        $this->data  = [];
        $this->props = [];
      }
    }
  }

  public function outputLayout() {
//$cnt = count($this->data);
//print_r ("d4:$cnt "); 
      
    $layout = "

<div class='scrollable-area' style='height:40em;overflow:auto;'>
  <div style='border:1px solid #ccc;margin-top:5px;background:#dcdcdc'>
    Project - Hours: <div id='hours$line->id' style='display:inline;'>$line->total_dur</div>
  </div>
  <table class='table table-bordered'>
    <thead>
      <tr>
    ";
    for ($i=1; $i<count($this->props); $i++) {
      $prop = $this->props[$i];
      $colLabel = ucfirst(str_replace("_", " ", $prop));
      $layout .= "
        <th>$colLabel</th>  
      ";
    }
    
    $layout .= "
      </tr>
    </thead>
    <tbody> 
    ";

    $current = 0;
    $notFirst = false;
    $row = 0;

    foreach ($this->data as $line) {
      $notFirst = true;
      $current = $line->id;
      for ($i=1; $i<count($this->props); $i++) {

        $prop = $this->props[$i];
        $cellValue = $line->$prop;
        if ($prop == "operation_name")
          $cellValue = "
          <input type='hidden' id='opt$this->scope$line->id' value='$line->id'>
          <select id='$line->id' class='edit$this->scope' style='margin-bottom:0px;height:25px;width:100%;'>
            <option value=$line->id selected>$cellValue</option>
            <option value=0>Edit</option>
          </select>
          <span style='display:inline;'>
            <input id='input$this->scope$line->id' type='text'>
          </span>  
          <span style='display:inline;'>
            <button id='check$this->scope$line->id' class='btn btn-inverse'>editue</button>
          </span>

          " ;
        else if ($prop == "operation_type")
          $cellValue = "
          <input type='hidden' id='opt$this->scope$line->id' value='$line->operation_type'>
          <select id='$line->id' class='sel$this->scope' style='margin-bottom:0px;height:25px;width:100%;'>
            $this->selOptions
          </select>
          " ;

    	$layout .= "    
        <td>
        $cellValue
        </td>
        ";
      }
  	  $layout .= "    
   	  </tr>
   	  ";
      }
 	  $layout .= "    
    </tbody>  
  </table>
</div>
<div id='msg$this->scope' style='color:red'></div>  

    ";
    return $layout;
}	

  public function outputScript() {

	$script = "


$(document).ready(function() {

  $('[id^=input$this->scope]').hide();
  $('[id^=check$this->scope]').hide();

  $('.sel$this->scope').each(function(){
    var selOpt= $(this).prev('input').val(); // Value from the database (hidden) 
    $(this).find('option[value='+selOpt+']').prop('selected', true);
  });

});  

$(document).on('change', '.sel$this->scope', function (e) {
  var id = $(this).prop('id');
  var value = $(this).val();
  putsel$this->scope (id, value);
});

var editSelId = 0;
var editSelVal = '';
$(document).on('change', '.edit$this->scope', function (e) {

  editSelId  = $(this).prop('id');
  editSelVal = $('option:selected', this).prev('option').text();

  $('#'+editSelId+'.edit$this->scope').hide(500);
  $('#input$this->scope'+editSelId).show(500);
  $('#input$this->scope'+editSelId).focus();
  $('#check$this->scope'+editSelId).show(500);

  $('#input$this->scope'+editSelId).val(editSelVal);

});

$(document).on('click', '[id^=check$this->scope]', function (e) {
  var value = $('#input$this->scope'+editSelId).val();
  if (value == '') {
    $('#check$this->scope'+editSelId).hide(500);
    $('#input$this->scope').hide(500);
    $('#'+editSelId+'.edit$this->scope').show(500);
  }
  else {
    getchk$this->scope (editSelId, value)  
  }
});  


// Previously added selections and addition selection to add
function getchk$this->scope (id, val) {

  var params = 'checkval=' + val + '&' + 'baseid=$this->baseId';  
  
  var xhr = new XMLHttpRequest();
  xhr.open('POST', 'ajax/getchk$this->scope.php', true);
  xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  xhr.onreadystatechange = function() {//Call a function when the state changes.
    if (xhr.readyState == 4 && xhr.status == 200) {
      var response = JSON.parse(xhr.responseText);
      if (response != '') {
        $('#error$this->scope').html('');
        putadd$this->scope (id, val);
      }
      else {
        $('#msg$this->scope').html('Operation name is not editue');
      } 
    }
  }  
  xhr.send(params);
} 

function putadd$this->scope (id, val) {

  var params = 'previd=' + editSelId + '&' + 'newid=' + id + '&' +
               'val=' + val; 
  
  var xhr = new XMLHttpRequest();
  xhr.open('POST', 'ajax/putadd$this->scope.php', true);
  xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  xhr.onreadystatechange = function() {//Call a function when the state changes.
    if (xhr.readyState == 4 && xhr.status == 200) {
      var response = JSON.parse(xhr.responseText);
      $('#msg$this->scope').html('');
      $('#check$this->scope'+id).hide(500);
      $('#input$this->scope'+id).hide(500);
      $('#'+id+'.edit$this->scope').show(500);
      $('#'+id+'.edit$this->scope option[value='+id+']').text(response);
      $('#'+id+'.edit$this->scope option[value='+id+']').prop('selected',true);
    }
  }  
  xhr.send(params);
}	

function putsel$this->scope (id, value) {
  var params = 'id=' + id + '&' + 'value=' + value;  
  
  var xhr = new XMLHttpRequest();
  xhr.open('POST', 'ajax/putsel$this->scope.php', true);
  xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  xhr.onreadystatechange = function() {//Call a function when the state changes.
    if (xhr.readyState == 4 && xhr.status == 200) {
      var response = JSON.parse(xhr.responseText);
    }
  }  
  xhr.send(params);
}


  ";

  return $script;
  }
}

?>