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

class SummSubdet  {
  private $baseDir;
  private $scope;
  private $summary;
  private $selSubdet;
  

  public function __construct($entry) {

    if (isset($entry)) {
      $this->baseDir   = $entry->baseDir;
      $this->scope     = $entry->scope;
      $this->iterator  = $entry->iterator;
      $this->selSubdet = $entry->selSubdet;
  
      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 = "
    <table id='sub-summ' class='table table-condensed'>
    </table>
    ";
  
     return $layout;
  }	

  public function outputScript() {

	$script = "

var selSubdet = 0;
var reset$this->scope = null;
function setReset$this->scope () {
  reset$this->scope = '{';
  var firstOf = true;
  $('.$this->iterator').each(function(){
    var prop = $(this).prop('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 (id) {
  if (reset$this->scope) {
    setSubdet$this->scope (JSON.parse(reset$this->scope));
  }
  fk$this->scope = id;
  $('#fk$this->iterator').prop('value', id);
  setReset$this->scope();
  getsumm$this->scope(id);

  if (id == 0) {
    $('#subd-panel').slideUp('slow');
    $('#summ-panel').slideUp('slow');
  }
}	

$(document).on('click', '.subdet-summ', function (e) {
  selSubdet = 0;  
  setSubdet$this->scope (JSON.parse(reset$this->scope));
  if (!$(this).prop('checked')) {
    $('#del$this->iterator').prop('disabled', true);
    $('#put$this->iterator').html($('#put$this->iterator').prop('value').split('_')[0]);
    $('#$this->iterator').each(function(){
      $(this).prop('value', fk$this->scope);
    });  
  }
  else {
    $('.subdet-summ').prop('checked', false);
    $(this).prop('checked', true);
    $('#del$this->iterator').prop('disabled', false);
    $('#put$this->iterator').html($('#put$this->iterator').prop('value').split('_')[1]);
    subDet$this->scope ($(this).val());
  }	
});

function setSubdet$this->scope (resp) {
  $('.$this->iterator').each(function(){
    if (($(this).prop('type') == 'text') || ($(this).prop('type') == 'hidden'))
      $(this).prop('value', resp[$(this).prop('name')]);
    else if($(this).is('textarea'))
      $(this).html(resp[$(this).prop('name')]);
  }); 
}

function gen$this->scope (resp) {
  var inner = '';
  for (var i=0; i<resp.length; i++) {
      
    var rkeys = Object.keys (resp[i]);
    inner += '<tr><td><input class=subdet-summ type=checkbox name=column value=\"' + resp[i].id + '\"><div id=upd-sub' + resp[i].id +
             '></div></td><td>' + formatLabel(rkeys[1]) +
             '<br><input type=text name=' + rkeys[1] + ' value=\"' + resp[i][rkeys[1]] + '\" style=width:100% ></td><td>' + formatLabel(rkeys[2]) +
             '<br><input type=text name=' + rkeys[2] + ' value=\"' + resp[i][rkeys[2]] + '\" style=width:100% readonly></td></tr>';      
  }
  return inner;
}

function put$this->iterator() {
  var data = new FormData();
  data = 'table=$this->iterator';
  $('.$this->iterator').each(function(){
    data += '&' + $(this).prop('name') + '=' + $(this).val();
  }); 
  
  var xhr = new XMLHttpRequest();
  xhr.open('POST', 'ajax/putupdate.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);
      selSubdet = $('.subdet-summ:checkbox:checked').val();
      getsumm$this->scope (fk$this->scope);
      setSubdet$this->scope(JSON.parse(reset$this->scope));
      $('#del$this->iterator').prop('disabled', true);
      $('#put$this->iterator').html($('#put$this->iterator').prop('value').split('_')[0]);
    }
  }  
  xhr.send(data);
}

function del$this->iterator() {
  var data = new FormData();
  data = 'table=$this->iterator' + '&' + 'id=' + $('.subdet-summ:checkbox:checked').val();;
  
  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);
      selSubdet = 0;
      getsumm$this->scope (fk$this->scope);
      setSubdet$this->scope(JSON.parse(reset$this->scope));
      $('#del$this->iterator').prop('disabled', true);
      $('#put$this->iterator').html($('#put$this->iterator').prop('value').split('_')[0]);
    }
  }  
  xhr.send(data);
}

function getsumm$this->scope (id) {
	
  if (id == 0) { 
    document.getElementById('sub-summ').innerHTML = '';
    return;
  }

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

  xhr.open('POST', 'ajax/getsumm$this->scope.php', true);
  xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  xhr.onreadystatechange = function() {
    if (xhr.readyState == 4 && xhr.status == 200) {
      var response = JSON.parse(xhr.responseText);
      $('#sub-summ').html(gen$this->scope(response));
      
      if (selSubdet > 0) {  
        $('#upd-sub'+selSubdet).html('<span class=\'glyphicon glyphicon-asterisk\'></span>');
      }	
    }
  }  
  xhr.send(params);
}

function subDet$this->scope (id) {
	
 if (id == 0) { 
    document.getElementById('vars').innerHTML='';
    return;
  }

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

  xhr.open('POST', 'ajax/getsubd$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);
      
      setSubdet$this->scope(response);
    }
  }  
  xhr.send(params);
}

";

  return $script;
  }
}

?>