<?php 

class SelTableCell  {
  private $scope;
  private $label;
  private $selectId;
  private $options;
  private $iterator;
  private $prompt;

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

      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;

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

      $this->optCount = 0;
      foreach ($this->options as $option) {
        if ($option->id > 0)
          $this->optCount ++;
      }      
    }
  }
  
  public function outputLayout() {
//  <span style='position:absolute;bottom:19px;left:365px'>▼</span>
  
    $layout = "
    
  $this->label    
  <input type='text' id='$this->field' >
  <table  class='$this->form' $this->attribute> 
    <tbody id='sel$this->field' class='sel$this->field'
      style='border:1px solid black;position:relative;'>
    </tbody>
  </table>
	";
	return $layout;
  }	

  public function outputScript() {

    $script = "

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

$(document).on('click', '#$this->field', function (e) {
//alert (showing);
//  if (showing == false) {
//alert ('show');
    document.getElementById('sel$this->field').style.display = 'block';
//    showing = true;
//  }    
});  

$('#selTableCell').mouseout (function() {
//alert ('out');
  $('input, select, a, textarea, td').click (function(event){
//alert (showing);
//    if (event.target.nodeName != 'input')
      document.getElementById('sel$this->field').style.display = 'none';
//      showing = false;
  });
});

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

$('.cell$this->field').click(function() {
  $(this).toggleClass( 'highlight');
  $(this).parent().children().removeClass('selected');
    $(this).addClass('selected');
});

$(document).on('click', '#$this->field', function (e) {
//alert (showing);
//  if (showing == false) {
//alert ('show');
    document.getElementById('sel$this->field').style.display = 'block';
//    showing = true;
//  }    
});  

//$(document).on('click', '.cell$this->field', function (e) {
//  $( this ).closest('label').toggleClass( 'highlight', this.checked );
//  document.getElementById('sel$this->field').style.display = 'none';
//});  

//$(document).on('click', '.$this->leftUI', function (e) {
//  $( this ).closest('label').toggleClass( 'highlight', this.checked );
//});

//$(document).on('click', '#sel$this->field', function (e) {
//  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 = [value, attr, source, '---', '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>$this->field</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][0];
//    currentId$this->field = message[key][0];
  }    
  else if (key == 'optionsId')
    get$this->field (message[key][0], '$this->displayId');
}  

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

function gen$this->field(cells) {
//alert ('selid:' + selectId$this->field);
  var layout='';
  var cellValue;
  var cellText;
  var selected;
  var cellCount = 0;
  for (var i=0; i<cells.length; i++) {
    if (cells[i].id > 0)
      cellCount ++;
  }
  
  for (var i=0; i<cells.length; i++) {
    selected = '';
    cellValue = cells[i].id;
    cellText = cells[i].text;
//alert ('$this->field ' + cellCount + ' ' + currentId$this->field + ' ' + cellValue + ' ' + selectedId$this->field)
    if (cellValue == selectedId$this->field || cellCount == 1) {
      selected = ' selected';
      if (selectId$this->field == 0 && cellCount == 1 && $this->autoSel == 1) {
//alert ('send');      
//        send$this->field (cellValue, cellText);
        currentId$this->field = cellValue
      }    
    }
    layout += '<tr id=' + cellValue + ' class=cell$this->field><td>' + cellText + '</td></tr>';
  }
//  selectId$this->field = 0;
  return layout;
} 
	  
function get$this->field (optionsId) {
  var params = 'optionsid=' + optionsId;
  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').value = response[0].text;
      document.getElementById('sel$this->field').innerHTML = gen$this->field(response);
      document.getElementById('sel$this->field').style.display = 'none';
    }
  }  
  xhr.send(params);
}

    "; 
    return $script;
  }
}

?>