 <?php 

class SelectOptionAdd  {
  private $scope;
  private $scopeId;
  private $iterator;
  private $label;
  private $options;
  private $msglist;

  public function __construct($entry) {

    if (isset($entry)) {
      $this->scope      = $entry->scope;
      $this->scopeId    = $entry->scopeId;
      $this->iterator   = $entry->iterator;
      $this->label      = $entry->label;

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

      $this->msgList = "";
      if (null !== $entry->message) {
        $messages = explode(",", $entry->message);
        for ($i=0; $i<count($messages); $i++) {
          $this->msgList .= "notify" . $messages[$i] . "(currid$this->scope); ";
        }
      }
    }      
  }
  
  public function outputLayout() {
      
    $layout = "
<input type='hidden' id='selid$this->scope' class='$this->iterator' name='$this->scope' value='$this->selectId'>
<label for='select$this->scope'>$this->label</label><br>
<select id='select$this->scope' onchange='seladd$this->scope(this.value)' >
    ";

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

      if ($item->id == $this->optDefault)
        $attribute = " selected";

      $layout .= "
  <option value=" . $value . $attribute . " >" . $text . "</option>
      ";
    }
  
    if (count($this->options) == 0)
      $layout .= "
  <option value=-1>Select</option>
	    ";
      
    $layout .= "
  <option value=0>ADD</option>
</select>
<div id=err$this->scope style='color:red'></div>  
	  ";
	return $layout;
  }	

  public function outputScript() {

	  $script = "

var summid$this->scope = 0;
var currid$this->scope = 0;

$('#selid$this->scope').on('chg$this->iterator', function() {
  var value = $(this).val();
  $('#select$this->scope option[value='+value+']').prop('selected',true);
});

function notify$this->scope (msgId) {
  summid$this->scope = msgId;
  getsel$this->scope(summid$this->scope);
} 

function seladd$this->scope (optId) {
  if (optId == 0) {
    getadd$this->scope(currid$this->scope);
  }
  else {
    currid$this->scope = optId;
    $this->msgList
  }  
}      

function setadd$this->scope (optId) {
  putadd$this->scope(summid$this->scope, optId);
  getsel$this->scope(summid$this->scope);
}      

function gen$this->scope(content) {
  var selid = $('#selid$this->scope').val();
  var layout='<option>Select</option>';
  var value;
  var text;
  for (var i=0; i<content.length; i++) {
    selected = '';
    id = content[i].id;
    text = content[i].text;
    if (id == selid)
      selected = ' selected';

    layout += '<option value=' + id + selected + '>' + text + '</option>';
  }
  
  return layout;
} 

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

  var params = 'id=' + id;
  var xhr = new XMLHttpRequest();
  xhr.open('POST', 'ajax/getsel$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);
      $('#select$this->scope').html(gen$this->scope(response));
      $('#select$this->scope')[0].setAttribute('onchange', 'seladd$this->scope(this.value)');
    }
  }  
  xhr.send(params);
}  

// Populate dropdown with 'add' selections
function getadd$this->scope (id) {

  var params = 'id=' + id;  
  
  var xhr = new XMLHttpRequest();
  xhr.open('POST', 'ajax/getadd$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)
  
      $('#select$this->scope').html(gen$this->scope(response));
      $('#select$this->scope')[0].setAttribute('onchange', 'setadd$this->scope(this.value)');
    }
  }  
  xhr.send(params);
}

function getdetl$this->scope (id) {

  var params = 'id=' + id;  
  
  var xhr = new XMLHttpRequest();
  xhr.open('POST', 'ajax/getdetl$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)
  
      $('#select$this->scope').html(gen$this->scope(response));
      $('#select$this->scope')[0].setAttribute('onchange', 'setadd$this->scope(this.value)');
    }
  }  
  xhr.send(params);
}

// Assigned added item to the select list
function putadd$this->scope (id, opt) {

  var params = 'id=' + id + '&' + 'opt='+ opt;  
  
  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)
//      $('#select$this->scope').html(gen$this->scope(response));
//      $('#select$this->scope')[0].setAttribute('onchange', 'putsel$this->scope(this.value)');
//      curr$this->scope = $('#select$this->scope option[selected]').val();
      currid$this->scope = response;
      $this->msgList      
    }
  }  
  xhr.send(params);
}	

function del$this->iterator() {
  if (curr$this->scope > 0) { 
    
    var params = 'delid=' + curr$this->scope + '&' + 'listid=$this->scopeId' ;
  
    var xhr = new XMLHttpRequest();
    xhr.open('POST', 'ajax/del$this->iterator.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);
        $('#sel$this->scope').html(gen$this->scope(response));
        $('#sel$this->scope').focus();
        $('#sel$this->scope')[0].setAttribute('onchange', 'putsel$this->scope(this.value)');
        curr$this->scope = (response.length > 0) ? response[0].id : 0;
        $('#sel$this->scope').find('option[value='+curr$this->scope+']').prop('selected', true);
        $this->msgList      
      }
    }  
    xhr.send(params);
  }
  
  else
    $('#err$this->scope').html('Nothing to delete');
}

    "; 
    return $script;
  }
}

?>