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

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

  public function __construct($entry) {

    if (isset($entry)) {
      $this->scope     = $entry->scope;
      $this->iterator  = $entry->iterator;

      if (null !== $entry->summary)
        $this->summary = json_encode($entry->summary);
      else
        $this->summary = '{}';

    }
  }

  public function outputLayout() {
      
    $layout = "

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

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


    return $layout;
  }	

  public function outputScript() {

	$script = "

// SUMMARY DETAIL

var selectId$this->scope;
var scopeId$this->scope;

$(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);
  }	
});

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>
             '<input id=' + resp[i].id + 'class=meta$this->scope type=hidden value=' + resp[i].metadata + '></td>';
      }       
      else
        inner += '<td>' + resp[i][rkeys[j]] + '</td>'; 
    }
    inner += '</tr>';
  }
  inner += '</tbody>';
  return inner;
}

function put$this->iterator(program) {
  var xhr = new XMLHttpRequest();

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

?>