<?php 

class SelectOption  {
  private $field;
  private $label;
  private $selectId;
  private $options;
  private $form;

  public function __construct($field) {
    if (isset($field)) {
      $this->field  = $field->field;
      $this->selectId = $field->selectId;
      $this->form = $field->form;

      if (isset($field->options))
        $this->options = $field->options;
      else
        $this->options[] = NULL;
//print_r ("options:");
//print_r ($this->options);

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

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

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

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

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

      $this->optCount = 0;
      foreach ($this->options as $option) {
        if ($option->id > 0)
          $this->optCount ++;
      }      
    }
  }
  
  public function outputLayout() {
  

    $layout = "
  $this->label    
  <select id='$this->field' class='$this->form' $this->attribute>
     ";

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

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

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

  public function outputScript() {

    $script = "

var currentId$this->field = 0;
var selectId$this->field = '$this->selectId'; 
var selectedId$this->field = '$this->selectId'; 

$(document).ready(function() {
//  get$this->field (1);
});

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

function set$this->field (dbValue, dbAttr) {
  currentId$this->field = dbValue;
  selectedId$this->field = dbValue;
  $('#$this->field.$this->form option:selected').removeAttr('selected');;
  $('#$this->field.$this->form option[value='+dbValue+']').prop('selected',true);
}

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

function send$this->field (value, attr) {
//alert ('send:' + value + ' ' + currentId$this->field);
  if (value != currentId$this->field) {
    selectId$this->field = value;
    var source = 'sequent_frm';
    var msgContent = [];
    msgContent.$this->field = ['$this->field', value, attr, '$this->form', 'new field value'];
    newValue$this->form (msgContent);
  }    
}

function com$this->field (message) {
  var key = Object.keys(message);
logMessage ('<td>com msg</td><td>' + key + '</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->field = message[key][1];
//    currentId$this->field = message[key][1];
  }    
  else if (key == 'optionsId')
    get$this->field (message[key][1], '$this->displayId');
}  

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

function gen$this->field(options) {
//alert ('here');
  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->field ' + optCount + ' ' + currentId$this->field + ' ' + optValue + ' ' + selectedId$this->field)
    if (optValue == selectedId$this->field || optCount == 1) {
      selected = ' selected';
      if (selectId$this->field == 0 && optCount == 1 && $this->autoSel == 1) {
//alert ('send');      
        send$this->field (optValue, optText);
        currentId$this->field = optValue
      }    
    }
    layout += '<option value=' + optValue + selected + '>' + optText + '</option>';
  }
//  selectId$this->field = 0;
  return layout;
} 
	  
function get$this->field (optionsId, displayId ) {

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

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

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

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

    "; 
    return $script;
  }
}

?>