<?php 

class SearchDate {

  private $scope;
  private $scopeId;
  private $label;
  private $date;
  private $iterator;
  private $entryNow;

  public function __construct($entry) {
    if (isset($entry)) {
      $this->scope    = $entry->scope;
      $this->scopeId  = $entry->scopeId;
      $this->label    = $entry->label;
      $this->iterator = $entry->iterator;
      $this->entryNow = $entry->entryNow;
    } 
  }
  
  public function outputLayout() {

    $layout = "
<label>$this->label</label><br>
<input type='date' id='date$this->scope' class='$this->iterator' 
  name='$this->scope' required style='width:150px;'>
    "; 

    return $layout;
  }

  public function outputScript() {

    $script = "

var currWorkday = -1;

$(document).on('change', '#date$this->scope', function (e) {
//alert ('on:' + $(this).val());
  send$this->scope ($(this).val(), $(this).val(), 'sequent_frm');
  get$this->scope ($(this).val());
  currWorkday = -1;
});

function set$this->scope (dbValue, dbText) {
  if (currWorkday == -1) {
    document.getElementById('date$this->scope').value = dbValue;
    get$this->scope (dbValue);
  }
}

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

function send$this->scope (value, text, source) {
//alert ('send');
  var msgContent = [];
  msgContent.$this->scope = [value, text, source, '---', 'new name id'];
  newValue$this->iterator (msgContent);
}

function sendId$this->scope (value, text, source) {
  var msgContent = [];
  msgContent.$this->iterator = [value, text, source, '---', 'new name id'];
  newValue$this->iterator (msgContent);
}

function get$this->scope (date) {
  var xhr = new XMLHttpRequest();
  var params = 'date=' + date;

  xhr.open('POST', 'ajax/get$this->scope.php', true);
  xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  xhr.onreadystatechange = function() {//Call function when the state changes.

    if (xhr.readyState == 4 && xhr.status == 200) {
      var response = JSON.parse(xhr.responseText)
//      send$this->scope (response.date, response.date, 'sequent_frm');
//alert (response.id + ' ' + currWorkday + ' ' + (response.id != currWorkday));
      if (response.id != currWorkday) {
        sendId$this->scope (response.id, response.date, 'sequent_upd');
        currWorkday = response.id;
      }
    }
  }    
  xhr.send(params);
}

    ";
    return $script;
  }
}

?>

