<?php 
defined('PUBLIC_ACCESS') or die('Restricted Access');

class PutRequest  {
  private $scope;
  private $scopeId;
  private $messageList;
  private $iterator;  
  private $idProp;  

  public function __construct($entry) {

    if (isset($entry)) {
      $this->scope    = $entry->scope;
      $this->scopeId  = $entry->scopeId;
      $this->iterator = $entry->iterator;
      $this->idProp   = $entry->idProp;
	
      if (null !== $entry->message) {
        $messages = explode(",", $entry->message);
        $this->messageList = "";

        for ($i=0; $i<count($messages); $i++) {
          $this->messageList .= "notify" . $messages[$i] . "(dbRowId); ";
        }
      }
    }
  }

  public function outputLayout() {
      
    $layout = "
<div id=err$this->scope style='color:red'></div>  
    "; 

    return $layout;
  }	

  public function outputScript() {

	$script = "

// PUT REQUEST  
var curr$this->scope = 0; 
$(document).ready(function() {

  curr$this->scope = $this->scopeId;

  if (curr$this->scope > 0)
    get$this->scope(curr$this->scope);
  reset$this->scope = '{';
  var firstOf = true;
  $('.$this->iterator').each(function(){
    var prop = $(this).attr('name');
    var valu = $(this).val();
    if (firstOf) {
      reset$this->scope += '\"' + prop +  '\":\"' + valu + '\"'  ;
      firstOf = false;
    }
    else  
      reset$this->scope += ',\"' + prop +  '\":\"' + valu + '\"'  ;
  }); 
  reset$this->scope += '}';
});

function notify$this->scope (msgId) {
//alert (msgId);
  put$this->iterator();  
  curr$this->scope = msgId;

  $('#msg$this->scope').html('');
  $('#hid$this->scope').val(msgId);
  
  get$this->scope(msgId);
}

    
// Populate detail form
function detSet$this->scope (resp) {
  
  $('.$this->iterator').each(function(){
//alert(resp[$(this).prop('name')]);      
    if ($(this).prop('type') == 'text')
      $(this).prop('value', resp[$(this).prop('name')]);
    else if ($(this).prop('type') == 'hidden')
      $(this).prop('value', resp[$(this).prop('name')]).trigger('chg$this->iterator');
    else if($(this).is('textarea'))
      $(this).html(resp[$(this).prop('name')]);

    else if($(this).is('select')) {
      var selId = $(this).prop('name'); // 
      var optId = resp[$(this).prop('name')];
      $('#'+selId+' option[value='+optId+']').prop('selected',true);
    }
  }); 
}

function gen$this->scope (resp) {
  var inner = '';
  return inner;
}

function put$this->iterator() {
  if (curr$this->scope == 0) {
    if ('$this->scope' == 'travel_to' || '$this->scope' == 'travel_from')
      $('#err$this->scope').html('Select a vehicle');
    else if ('$this->scope' == 'dailyjournal')
      $('#err$this->scope').html('Select a project');
//  else if ($('#listlefttravel_to').length == 0)
//    $('#err$this->scope').html('Add occupants');
  }      
  else {    
    var data = new FormData();
    data = 'table=$this->iterator' + '&' + 'scopeid='+ curr$this->scope;
    $('.$this->iterator').each(function(){
      data += '&' + $(this).attr('name') + '=' + $(this).val();
    }); 
  
    var xhr = new XMLHttpRequest();
    xhr.open('POST', 'ajax/put$this->iterator.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)
        selDetail = $('.detail-summ:checkbox:checked').val();
//      detSet$this->scope(JSON.parse(reset$this->scope));
      }
    }  
    xhr.send(data);
  }  
}

function del$this->iterator() {
  var data = new FormData();
  data = 'table=$this->iterator' + '&' +
         'id=' + $('.detail-summ:checkbox:checked').val();
         
  if ('$this->subdetail' != '')
    data += '&' + 'child=$this->subdetail';
  
  var xhr = new XMLHttpRequest();
  xhr.open('POST', 'ajax/putdelete.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);
      selDetail = 0;
      getsumm$this->scope();
      detSet$this->scope(JSON.parse(reset$this->scope));
      $('#del$this->iterator').prop('disabled', true);
    }
  }  
  xhr.send(data);
}

// Ajax select detail for selected summary row
function get$this->scope (id) {

  var xhr = new XMLHttpRequest();
  var params = 'id=' + id;

  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)
      detSet$this->scope(response);
    }
  }  
  xhr.send(params);
}

  ";

  return $script;
  }
}

?>