<?php 

class ListSelects  {
  private $scope;
  private $label;
  private $scopeId;
  private $rowList;
  private $readOnly;
  private $selections;
  
  public function __construct($entry) {

    if (isset($entry)) {
      $this->scope    = $entry->scope;
      $this->label    = $entry->label;
      $this->scopeId  = $entry->scopeId;
      
      if (null == $entry->rowList) 
        $this->rowList = [];
      else
        $this->rowList  = $entry->rowList;

      if (null != $entry->readOnly)
        $this->readOnly = $entry->readOnly;
      else 
        $this->readOnly = -2;

      if (isset($entry->defOption))
        $this->selections .= "<option>$entry->defOption</option>";
      
      foreach ($entry->selList as $option) {
        $disabled = "";
//print_r ("id: $option->id");        
        if ($option->id == 7)
          $disabled = " disabled";
        $this->selections .= "<option value=$option->id $disabled>$option->text</option>";
      }
      if (isset($entry->addOption))
        $this->selections .= "<option value=0>$entry->addOption</option>";

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

    }    
  }
  
  public function outputLayout() {

    $layout = "
<label for='$this->scope' style='margin-bottom:0px;'>$this->label</label><br>
<div style='border:1px solid #ccc;height:172px;overflow-x:auto;'>
  <table class='list$this->scope' >
    <tbody id='list$this->scope'
    ";    
    
    foreach ($this->rowList as $row) {
      $delOption = "";
      if ($row->addedFlag)
        $delOption = "<option value=-1>Delete</option>";
      $disabled = "";
      if ($row->role_id == 7)
        $disabled = "disabled"; 

      $layout .= "
    <tr>
      <td nowrap>
        $row->text
     </td>
     <td>  
       <input type='hidden' id='opt$row->id' value='$row->role_id'>
       <select id='$row->id' $disabled class='sel$this->scope' style='margin:0px;height:23px;'>
          $this->selections
          $delOption
       </select>
     </td>
    </tr>
      "; 
    }
                      
    $layout .= "
    </tbody>
  </table>
</div>
   ";
    return $layout;

  }	

    public function outputScript() {

	  $script = "

var curr$this->scope = 0;
var msgId$this->scope = 0;
var currid$this->scope = 0;

$(document).ready(function() {

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

});

$(document).on('change', '.sel$this->scope', function (e) {
  var value = $(this).val();
  var id    = $(this).prop('id');
  currid$this->scope = id;
  if (value == 0) {
    putadd$this->scope (id);
  }
  else if (value == -1) {
    if ('$this->confirmModal' != '')
      show$this->confirmModal ();
    else 
      putdel$this->scope
  }
  else {  
    var update = true;
    $('.sel$this->scope').each(function() {

//      if (value != '3' && id != $(this).prop('id') && $(this).val() == value )
//        update = false;
    });    
  
    if (update) {
      put$this->scope (value, id, curr$this->scope);
      $('#msg$this->scope').html('');     
    }  
    else 
      $('#msg$this->scope').html(value + ' already selected');
  }
  
});

function refresh$this->scope (msgId) {
  $('#msg$this->scope').html('');
  get$this->scope ();
}

function notify$this->scope (msgId) {
  $('#msg$this->scope').html('');
  msgId$this->scope = msgId;
  get$this->scope ();
}

function gen$this->scope(options) {

  var text;
  var value;
  var selections = '$this->selections';
  var inner = '';
  var disabled = '';
  var delOption;
  for (var i=0; i<options.length; i++) {
    delOption = '';  
    disabled = '';
    id = options[i].id;
    text = options[i].text;
    role = options[i].role_id;
    if (role == 7)
      disabled = ' disabled'
    if (options[i].addedFlag == 1)
      delOption = '<option value=-1>Delete</option>';
    inner += '<tr><td nowrap>' + text + '</td>' +
             '<td><input type=hidden id=opt' + id + ' value=' + role + '>' +
             '<select id=' + id + disabled + ' class=sel$this->scope style=margin-bottom:0px;height:23px;>' + selections + delOption + '</select></td></tr>';
  }
  return inner;
} 

function get$this->scope () {

  var params = 'id=' + curr$this->scope + '&' + 'selid=' + msgId$this->scope;
  
  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)

      $('#list$this->scope').html(gen$this->scope(response));
      $('#list$this->scope').focus();
      $('.$this->scope').hide();
      $('.$this->scope').closest('label').toggleClass( 'highlight', this.checked );

      $('.sel$this->scope ').each(function(){
        var selOpt = $(this).prev('input').val(); 
        $(this).find('option[value='+selOpt+']').prop('selected', true);
      });
   }
  }  
  xhr.send(params);
}

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

  var params = 'val=' + val + '&' + 'id=' + id + '&' + 'currid=' + currid;
  
  var xhr = new XMLHttpRequest();
  xhr.open('POST', 'ajax/put$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);
}

function putadd$this->scope (id) {

  var params = 'id=' + id;
  
  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)
      get$this->scope ();
    }
  }  
  xhr.send(params);
}

function confdel$this->scope () {
}

function putdel$this->scope (id) {

  var params = 'id=' +  currid$this->scope;
  
  var xhr = new XMLHttpRequest();
  xhr.open('POST', 'ajax/putdel$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 ('$this->confirmModal' != '')
        hide$this->confirmModal ();
    }
  }  
  xhr.send(params);
}
	  
    "; 
    return $script;
  }
}

?>