<?php 

class SelAddUnique  {
  private $scope;
  private $scopeId;
  private $baseId;
  private $currId;
  private $iterator;
  private $label;
  private $options;
  private $msglist;

  public function __construct($entry) {

    if (isset($entry)) {
      $this->scope    = $entry->scope;
      $this->sequenceId = $entry->sequenceId;
      $this->summaryId = $entry->summaryId;
      $this->baseId   = $entry->baseId;
      $this->currId   = $entry->currId;
      $this->iterator = $entry->iterator;
      $this->label    = $entry->label;
      $this->options  = $entry->options;
//$cnt = count($entry->options);
//print_r ("cnt:$cnt");
//print_r ("m id:$entry->scopeId");

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

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

      if (isset($entry->options))
  
        $this->options = $entry->options;
      else
        $this->options = [];
      
    }      
  }
  
  public function outputLayout() {
      
    $layout = "
<label>$this->label</label><br>
<select id='uniq$this->scope' class='$this->iterator' name='$this->scope' value=0 >
  <option value=-1>Select</option>
  <option value=0>Add</option>
</select>

<span style='display:inline;'>
  <input id='input$this->scope' type='text'>
</span>  
<span style='display:inline;'>
  <button id='check$this->scope' class='btn btn-inverse'>Unique</button>
</span>
	  ";
	return $layout;
  }	

  public function outputScript() {

	  $script = "
 
var selectId$this->scope = 0; 
var fkeyId$this->scope = 0; 

$(document).ready(function() {
  $('#input$this->scope').hide();
  $('#check$this->scope').hide();
});

$(document).on('change', '#uniq$this->scope', function (e) {
  $('#valmsg$this->iterator').html('');
  if ($(this).val() == 0) {
    if ('$this->uniqueType' == 'sequence')
      add$this->scope ('');
    else {    
      $('#uniq$this->scope').hide(500);
      $('#input$this->scope').show(500);
      $('#input$this->scope').focus();
      $('#check$this->scope').show(500);
    }
  } 
});  

$(document).on('click', '#check$this->scope', function (e) {
  var text = $('#input$this->scope').val();
//alert ('val:' + selectId$this->scope);
  if (text != '') {
    chk$this->scope (text);
  }
  else {
    $('#check$this->scope').hide(500);
    $('#input$this->scope').hide(500);
    $('#uniq$this->scope').show(500);
  }
});  


function set$this->scope (value, text) {
  selectId$this->scope = value;
  $('#input$this->scope').val('');

  $('#check$this->scope').hide(500);
  $('#input$this->scope').hide(500);
  $('#uniq$this->scope').show(500);
  
  var inner = '';
  if (value == '') {
    inner += '<option value=-1>Select</option>';
    inner += '<option value=0>Add</option>';
  }    
  else {    
    inner += '<option value=' + value + ' selected disabled>' + text + '</option>';
    inner += '<option value=0>Update</option>';
  }    
  document.getElementById('uniq$this->scope').innerHTML = inner;
} 

function send$this->scope (value, text) {
  var msgContent = [];
  msgContent.$this->iterator = [value, text, 'formentry', '$this->scope', 'new field value'];
  newValue$this->iterator (msgContent);
}


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

function com$this->scope (message) {
  var key = Object.keys(message);
//alert (key + ' ' + message[key][0]);
  if (key == 'optionsId') {
//    selectId$this->scope = 0;
    get$this->scope (message[key][0]);
  }
  else if (key == 'fkeyId') {
     fkeyId$this->scope = message[key][0]; 
  }
    
}  

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

// Previously added selections and addition selection to add
function chk$this->scope (str) {
  var params = 'string=' + str + '&' + 'sequenceid=$this->sequenceId';  
  
  var xhr = new XMLHttpRequest();
  xhr.open('POST', 'ajax/chk$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 (response != '') {
        $('#valmsg$this->iterator').html('');
        add$this->scope (response);
      }
      else {
        $('#valmsg$this->iterator').html('Site Name is not unique');
      } 
    }
  }  
  xhr.send(params);
}  

function gen$this->scope(options) {
  var layout='<option value=0>Select</option>';
  var optValue;
  var optText;
  var selected;

  var optLength = options.length;
  for (var i=0; i<optLength; i++) {
 
    selected = '';
    optValue = options[i].id;
    optText = options[i].descr
// alert (optValue + ' ' + selectId$this->scope)
    if (optValue == selectId$this->scope)
      selected = ' selected';
    layout += '<option value=' + optValue + selected + '>' + optText + '</option>';
  }
  selectId$this->scope = 0;
  return layout;
} 

function get$this->scope (id) {
  var params = 'optid=' + id;  
  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)
      $('#$this->scope').html(gen$this->scope(response));
    }
  }  
  xhr.send(params);
}

// Assigned added item to the select list
function add$this->scope (string) {
  var params = 'string=' + string + '&' + 'fkeyid=' + fkeyId$this->scope +
               '&' + 'selectid=' + selectId$this->scope  +
               '&' + 'sequenceid=$this->sequenceId';
  var xhr = new XMLHttpRequest();

  xhr.open('POST', 'ajax/add$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);
      selectId$this->scope = response; 
//      set$this->scope (response, str);
      send$this->scope (response, '')
    }
  }  
  xhr.send(params);
}	

    "; 
    return $script;
  }
}

?>