<?php 

class SelectOption  {
  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 currentId$this->scope = 0;
var selectId$this->scope = '$this->selectId'; 
var selectedId$this->scope = '$this->selectId'; 

$(document).on('change', '#$this->scope', function (e) {
//alert ('send');
  send$this->scope ($(this).val(), $('#$this->scope option:selected').text());
});  

function set$this->scope (dbValue, dbText) {
  currentId$this->scope = 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);
  if (value != currentId$this->scope) {
    selectId$this->scope = value;
    var source = 'sequent_frm';
    var msgContent = [];
    msgContent.$this->scope = [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>');  
  if (key == 'selectId') {
    selectId$this->scope = message[key][0];
//    currentId$this->scope = message[key][0];
  }    
  else if (key == 'optionsId')
    get$this->scope (message[key][0], '$this->displayId');
}  

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

function gen$this->scope(options) {
//alert ('selid:' + selectId$this->scope);
  var layout='';
  var optValue;
  var optText;
  var selected;
  var optCount = 0;
  for (var i=0; i<options.length; i++) {
    if (options[i].id > 0)
      optCount ++;
  }
  
  for (var i=0; i<options.length; i++) {
    selected = '';
    optValue = options[i].id;
    optText = options[i].text
//alert ('$this->scope ' + optCount + ' ' + currentId$this->scope + ' ' + optValue + ' ' + selectedId$this->scope)
    if (optValue == selectedId$this->scope || optCount == 1) {
      selected = ' selected';
      if (selectId$this->scope == 0 && optCount == 1 && $this->autoSel == 1) {
        send$this->scope (optValue, optText);
        currentId$this->scope = optValue
      }    
    }
    layout += '<option value=' + optValue + selected + '>' + optText + '</option>';
  }
//  selectId$this->scope = 0;
  return layout;
} 
	  
function get$this->scope (optionsId, displayId ) {

//alert (optionsId + ' ' + displayId );

  if (optionsId == 0 || (optionsId > 0 && displayId != 0 && optionsId != displayId)) {
    document.getElementById('$this->scope').innerHTML = '';
    send$this->scope (0, '');
  }

  else {
    var params = 'optionsid=' + optionsId + '&' + 'parentid=$this->parentId';

    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;
  }
}

?>