<?php 

class SelectSummary {
  private $scope;
  private $label;
  private $selectId;
  private $options;
  private $iterator;
  private $prompt;

  public function __construct($entry) {
    if (isset($entry)) {
      $this->scope  = $entry->scope;
      $this->selectId = $entry->selectId;
      $this->iterator = $entry->iterator;

      if (isset($entry->label))
        $this->label = "<label>$entry->label</label><br>";
      else
        $this->label = "";

      if (isset($entry->attribute))
        $this->attribute = "$entry->attribute";
      else
        $this->attribute = '';

      if (isset($entry->autoSel) && $entry->autoSel == "no")
        $this->autoSel = "0";
      else
        $this->autoSel = "1";

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

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

      if (isset($entry->options))
        $this->options = $entry->options;
      else
        $this->options[] = NULL;

      $this->optCount = 0;
      foreach ($this->options as $option) {
        if ($option->id > 0)
          $this->optCount ++;
      }      
    }
  }
  
  public function outputLayout() {
  
    $layout = "
    
  $this->label    
  <select id='$this->scope' class='$this->iterator' $this->attribute>
     ";

    foreach ($this->options as $option) {
      $value = $option->id;

      $text  = $option->text;
//print_r ("$this->scope text:$text<br>");     
 	  $attribute = "";	

      if ($option->id == $this->selectId || ($this->optCount == 1 && $this->autoSel == 1))
        $attribute = " selected";

      $layout .= "
    <option value=" . $value . $attribute . " class='opt$this->scope'>" . $text . "</option>";
    }
  
    $layout .= "
  </select>
	";
	return $layout;
  }	

  public function outputScript() {

    $script = "

var selectId$this->scope = 0;

$(document).on('change', '#$this->scope', function (e) {
  selectId$this->scope = $(this).val();
  var id2 = $('#$this->scope option:selected').attr('data-fky2');
  send$this->scope (selectId$this->scope, $('#$this->scope option:selected').text());
  sendId$this->scope (id2, $('#$this->scope option:selected').text());
});  

function set$this->scope (dbValue, dbText) {
//alert (dbValue);

  selectedId$this->scope = dbValue;
  $('#$this->scope.$this->iterator option:selected').removeAttr('selected');;
  $('#$this->scope.$this->iterator option[value='+dbValue+']').prop('selected',true);
}

multiFunction['set$this->scope'] = {
  set$this->scope:set$this->scope
};  

function send$this->scope (value, text) {
//alert ('send:' + value + ' ' + currentId$this->scope);
  var source = 'sequent_ent';
  var msgContent = [];
  msgContent.$this->scope = [value, text, source, '---', 'new field value'];
  newValue$this->iterator (msgContent);
}

function sendId$this->scope (value, text) {
//alert ('send:' + value + ' ' + currentId$this->scope);
  var source = 'sequent_ent';
  var msgContent = [];
  msgContent.$this->iterator = [value, text, source, '---', 'new field value'];
  newValue$this->iterator (msgContent);
}

    
function com$this->scope (message) {
  var key = Object.keys(message);
logMessage ('<td>com msg</td><td>' + key +  '</td><td>$this->scope</td><td>' + message[key][0] + '</td><td>' + message[key][1] + '</td><td>' + message[key][2] + '</td><td>' + message[key][3] + '</td><td>' + message[key][4] + '</td>');  
//alert ('get');  
  if (key == 'summaryId') {
    summaryId$this->scope = message[key][0];
    get$this->scope (summaryId$this->scope);
  }
}  

multiFunction['com$this->scope'] = {
  com$this->scope:com$this->scope
};  

function gen$this->scope(options) {
  var layout='';

  for (var i=0; i<options.length; i++) {
    var selected = '';
    var id   = options[i].id;
    var text = options[i].text;
    var fky1 = options[i].fky1
    var fky2 = options[i].fky2

//alert (selectId$this->scope == 0);

    if (selectId$this->scope == 0)
      selectId$this->scope = id;

//alert (id + ' ' + selectId$this->scope);

    if (id == selectId$this->scope) {
      selectId$this->scope = id;
      selected = ' selected';
      sendId$this->scope (fky2, text);
    }

    layout += '<option value=' + id + ' data-fky1=' + fky1 +
      ' data-fky2=' + fky2 + selected + '>' + text + '</option>';
  }
  return layout;
} 
	  
function get$this->scope (summaryId) {

  var params = 'summaryid=' + summaryId;

  var xhr = new XMLHttpRequest();
  xhr.open('POST', 'ajax/get$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)
      document.getElementById('$this->scope').innerHTML = gen$this->scope(response);
    }
  }
  xhr.send(params);
}

    "; 
    return $script;
  }
}

?>