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

class SumRunProgram {
  private $scope;
  private $summary;
  private $iterator;  

  public function __construct($entry) {

    if (isset($entry)) {
      $this->scope     = $entry->scope;
      $this->iterator  = $entry->iterator;
      if (isset($entry->summary))
        $this->summary = $entry->summary;
      else
        $this->summary = '{}';
//$cnt = count($this->summary);
//print_r ("tcnt:$cnt");
//print_r($this->summary);

    }
  }

  public function outputLayout() {
      
    $layout = "

<table class='table table-condensed'>
    ";
 
    foreach ($this->summary as $program) {

      $progName = $program->name; 
      $layout .= "
    <tr>
      <td>
        <label><input class='detail-summ$this->scope' type='checkbox'
          value='$progName'> $progName</label> 
      </td>
  </tr>
      ";
    }
      $layout .= "
</table>
    "; 


    return $layout;
  }	

  public function outputScript() {

	$script = "

// SUMMARY DETAIL
var selValue;
$(document).on('click', '.detail-summ$this->scope', function (e) {

  if (!$(this).prop('checked')) {
    $('#put$this->iterator').prop('disabled', true);
  }
  else {  
    $('.detail-summ$this->scope').prop('checked', false);
    $(this).prop('checked', true);
    $('#put$this->iterator').prop('disabled', false);
    selValue = $(this).val();
alert (selValue);    
  }	
});

function gen$this->scope (resp) {
  var inner = '';
  var rkeys;
  for (var i=0; i<resp.length; i++) {
    if (i == 0) {
      rkeys = Object.keys (resp[i]);
    }
    
    inner += '<tr>';
    for (var j=1; j<rkeys.length; j++) { 
      if (j == 1) {
        inner += '<td><label><input class=detail-summ$this->scope type=checkbox value=' + resp[i].id + '> ' + 
             resp[i][rkeys[j]] + '</label></td>';
      }       
      else
        inner += '<td>' + resp[i][rkeys[j]] + '</td>'; 
    }
    inner += '</tr>';
  }
  inner += '</tbody>';
  return inner;
}

function put$this->iterator() {

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

  xhr.open('POST', 'ajax/' + selValue, 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);
    }
  }    
  xhr.send(params);
}



  ";

  return $script;
  }
}

?>