<?php
    //debug($data);
    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($data[$i]);
    }
    echo '<h2>' . $budget['wl_kurz_bezeichnung'] .'</h2>';
    $budget_id = $this->request->params['pass'][0];
    echo $this->Html->link('Upload to ASD (add)',array('controller' => 'budget', 'action' => 'writeBudgetById' . '/?id=' . $budget_id. '&overwrite=0')) . '&nbsp;&nbsp;&nbsp;&nbsp;';
    echo $this->Html->link('Upload to ASD (overwrite)',array('controller' => 'budget', 'action' => 'writeBudgetById' . '/?id=' . $budget_id. '&overwrite=1'));
    //debug($data);
?>

<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 class="upload-frm">
  <?php echo $this->Form->create($uploadData, ['type' => 'file']); ?>
    <?php echo $this->Form->input('file', ['type' => 'file', 'class' => 'form-control']); ?>
    <?php echo $this->Form->button(__('Upload File'), ['type'=>'submit', 'class' => 'form-controlbtn btn-default']); ?>
    <?php echo $this->Form->end(); ?>
</div>
<div id="editbudget"></div>
<script>
var data = <?php echo json_encode($data); ?>;
  
  var container = document.getElementById('editbudget');
  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: 800,
    rowHeaders: true,
    wordWrap: false,
    colHeaders: ['SNR','Name','Count','Comment','ASD'] ,
    colWidths: ['50','500','50','500','50'],
    columns: [{renderer: "text", readOnly: true},{renderer: "html", readOnly: true},{renderer: "text"},{renderer: "text"},{renderer: "text", readOnly: true}],
    filters: true,
    dropdownMenu: true
  });

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

  $('#load').click(function() {
    location.reload(true);
  });

  </script>