 <?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->scopeId  = $entry->scopeId;
      $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->options))
        $this->options = $entry->options;
      else
        $this->options = [];
      
      $this->msgList = "";
      if (null !== $entry->message) {
        $messages = explode(",", $entry->message);
        for ($i=0; $i<count($messages); $i++) {
          $this->msgList .= "notify" . $messages[$i] . "(curr$this->scope); ";
        }
      }
    }      
  }
  
  public function outputLayout() {
      
    $layout = "
<input type='hidden' id='sel$this->scope' class='$this->iterator' name='sel$this->scope' value='0'>
<label for=$this->scope'>$this->label</label><br>
<select id='$this->scope' class='$this->iterator' name='$this->scope' value=0 style='height:27px;' >
  <option value=-1>Select</option>
     ";   

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

      $layout .= "
  <option value='$value' $attribute>$text</option>
      ";
    }
      
    $layout .= "
  <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>
<div id='error$this->scope' style='color:red'></div>  
	  ";
	return $layout;
  }	

  public function outputScript() {

	  $script = "
 
var selid$this->scope = 0; 
var scopeid$this->scope = '$this->scopeId'; 
$(document).ready(function() {
  $('#input$this->scope').hide();
  $('#check$this->scope').hide();
});

$(document).on('change', '#$this->scope', function (e) {
  var value = $(this).val();
  if (value == 0) {
    if ('$this->scope' == 'selectsite_name' || '$this->scope' == 'selectlot_name')
      getchk$this->scope (value);

    else {     
      $('#$this->scope').hide(500);
      $('#input$this->scope').show(500);
      $('#check$this->scope').show(500);
    }
  }
});  

$(document).on('click', '#check$this->scope', function (e) {
  var value= $('#input$this->scope').val();
  if (value != '') {
    getchk$this->scope (value);
  }
  else {
    $('#check$this->scope').hide(500);
    $('#input$this->scope').hide(500);
    $('#$this->scope').show(500);
  }
});  
    
function notify$this->scope (msgId) {
  $('#check$this->scope').hide(500);
  $('#input$this->scope').hide(500);
  $('#$this->scope').show(500);

  if ('$this->scope'  == 'selectlot_names' || '$this->scope'  == 'seloperationname' ) {
    selid$scopeId = msgId;
    get$this->scope (selid$this->scope);
  }
} 


function gen$this->scope(content) {
  var value;
  var text;
  var attr = '';
  var dbRowId = $('#sel$this->scope').val();
  var inner = '\
<input type=hidden id=sel$this->scope class=$this->iterator name=sel$this->scope value=0> \
<label for=$this->scope>$this->label</label><br> \
<select id=$this->scope class=$this->iterator name=$this->scope value=0 style=position:relative > \
  <option value=-1>Select</option> \
  ';
  
  for (var i=0; i<content.length; i++) {
    var selected = '';
    value = content[i].id;
    text = content[i].descr
    attr = content[i].attribute
    if (content[i].id == dbRowId)
      selected = ' selected';    
      
    inner += '<option value=' + value + ' ' + attr + selected + '>' + text + '</option>';
  }

  inner += '\
  <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>Unique</button> \
</span> \
<div id=error$this->scope style=color:red></div> \
   ';
  return inner;
} 

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

  var params = 'checkval=' + val + '&' + 'scopeId=$this->scopeId' + 
               '&' + 'baseid=$this->baseId' + '&' + 'currid=$this->currId';  
  
  var xhr = new XMLHttpRequest();
  xhr.open('POST', 'ajax/getchk$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 != '') {
        $('#error$this->scope').html('');
        putadd$this->scope (response);
      }
      else {
        $('#error$this->scope').html('Property part name is not unique');
      } 
    }
  }  
  xhr.send(params);
}  

function get$this->scope (id) {

  var params = 'baseid=$this->baseId' + '&' + 'scopeid=$this->scopeId'  
  
  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 putadd$this->scope (val) {
  
  var os_id = $('#spatialunitlot_id.culturalunit').val();

//  var cu_id = currtest_pits;

  var params = 'rowval=' + val + '&' + 'scopeid=$this->scopeId' + '&' +
               'baseid=$this->baseId' + '&' + 'currid=$this->currId' + '&' + 
               'os_id=' + os_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);
      $('#error$this->scope').html('');
      $('#check$this->scope').hide(500);
      $('#input$this->scope').hide(500);
      $('#$this->scope').show(500);
      $('#$this->scope option:first').after('<option value='+response+'>'+val+'</option>');
      $('#$this->scope option[value='+response+']').prop('selected',true);
      $('#id.culturalunit').val(response);
      $('#id.spatialunit').val(response);
      $('#id.parcellot').val(response);
   
    }
  }  
  xhr.send(params);
}	

    "; 
    return $script;
  }
}

?>