<?php
//debug($schooltype);
//$test = '["<a href=\"test\">SNR<\/a>","Schule","RS Modellversuch","RS geb. Ganztagsklassen","RS St\u00fctzpunktschule Schulsport","RS Partnerschule Wintersport","RS Sonderp\u00e4d. F\u00f6rderbedarf","Abordnung Grundschullehrkraft","Bilingualer Unterricht","Inklusion","sonstige Budgetzuschl\u00e4ge","RS Koop. mit and. Schulart","RS Integrierte Lehrerres.","RS Bestenf\u00f6rderung","MINT","Musikrealschule"]';
$cols = array();
$columns = array();
array_push($cols,'SNR');
array_push($columns,['renderer' => 'text', 'readOnly' => true]);

array_push($cols,'Schule');
array_push($columns,['renderer' => 'html', 'readOnly' => true]);

$col_width = array();
array_push($col_width,'50');
array_push($col_width,'300');
foreach($wlBudgets as $budget) {
    //debug($budget);die;
    array_push($cols,$this->Html->link($budget['wl_kurz_bezeichnung'],array('controller' => 'budget', 'action' => 'edit-budget',$budget['id'])));
    array_push($col_width,'30');
    array_push($columns,['renderer' => 'text']);
}
for($i=0;$i<sizeof($data);$i++) {
    $data[$i][1] = $this->Html->link($data[$i][1],array('controller' => 'schools', 'action' => 'showbudgets',$data[$i][0]));
  }
//debug($columns);
//debug(json_encode($columns));die;
//debug(json_encode($cols));
?>
<div class="controls">
        <button name="load" id="load" class="intext-btn">Load</button>
        <button name="save" id="save" class="intext-btn">Save</button>
      </div>
<div id="overview"></div>
<script>
var data = <?php echo json_encode($data); ?>;
  
  var container = document.getElementById('overview');
  var load = document.getElementById('load');
  var save = document.getElementById('save');
  var autosaveNotification;
  var rotated = false;
  
  var hot = new Handsontable(container, {
    data: data,
    width: 1400,
    height: 1000,
    rowHeaders: true,
    wordWrap: false,
    columns: <?php echo json_encode($columns) ?> ,
    colHeaders: <?php echo json_encode($cols) ?> ,
    colWidths: <?php echo json_encode($col_width) ?>,
    filters: true,
    dropdownMenu: true,
    afterGetColHeader: function(i, TH) {    
        if($(TH).find(".testhead").length == 0) {
            TH.innerHTML = '<div class="testhead">' + TH.innerHTML + '</div>';
        }
    }
  });

  Handsontable.dom.addEvent(save, 'click', function() {
    // save all cell's data
    //console.log(hot.getData());
    //console.log(JSON.stringify({data: hot.getData()}));
    $.ajax({
            //url: "sajax",
            url: "<?php echo $this->Url->build(array('controller' => 'budget', 'action' => 'sajax',$schooltype), true); ?>",
            data: JSON.stringify(hot.getData()),
            type: 'POST',
            success: function (res) {
                //console.log(res);
                //var data = JSON.parse(res);
              
            }
        });
  });

  </script>