<?php
if (isset($actual)) {
    echo '<p><b>' . $actual['title'] . ' ('. $del .'/' . $all . ')</b>';
} else {
    echo '<b>Bitte wählen Sie eine Abschlussprüfung</b>';
}
//debug($actual);die;

echo '&nbsp;&nbsp;&nbsp;';

foreach ($examinations as $examination) {
    //debug($examination);
    echo $this->Html->link($examination['title'], ['controller' => 'Departments', 'action' => 'ListDep', $examination['id']]) . '&nbsp;&nbsp;&nbsp;';
}
echo '<p>';

//echo '<h1>' . $schooltype . '</h1>';
echo '<div id="examination_id" style="display: none">' . $examination_id . '</div>';
//debug($school_type);die;
?>

<form id="targetform">

    <table>
        <tr>
            <td><b>Abgegeben?</b></td>
            <td><label><input type="checkbox" name="delivery_date_on"  checked>Ja</label></td>
            <td><label><input type="checkbox" name="delivery_date_off" checked>Nein</label></td>
        </tr>
    </table>

    <?php
    echo '<table>';
    echo '<tr><td><b>Status</b>';
    foreach ($status as $s) {
        echo '<td><label><input type="checkbox" name="status--' . $s->status . '" value="' . $s->status . '" checked>' . $s->status . '</label></td>';
    }
    echo '</tr></table>';

    echo '<table>';
    echo '<tr><td><b>Schulaufsicht</b></td>';
    if (isset($aufsicht)) {
        foreach ($aufsicht as $a) {
            //debug($a);
            if (!empty($a['super_school_code'])) {
                echo '<td><label><input type="checkbox" name="super_school_code--' . $a['super_school_code'] . '" value="' . $a['super_school_code'] . '" checked>' . $a['super_school_code'] . ' (' . $a['delivered'] . '/' . $a['all'] . ')' . '</label></td>';
            }
        }
    }
    echo '</table>';
    ?>
    <input id="target" type="submit" value="Filter aktualisieren">
</form>
<p>&nbsp;</p>
<div id="hot"></div>
<p>&nbsp;</p>

<script>

    var examination_id = $('#examination_id').text();
    //console.log(school_type);

    var hotElement = document.querySelector('#hot');
    var hotElementContainer = hotElement.parentNode;
    var hotSettings = {
        //data: dataObject,
        columns: [
            {data: 'code', type: 'text'},
            {data: 'title', type: 'text', width: 200},
            {data: 'super_school_code', type: 'text'},
            {data: 'super_school_code_2', type: 'text'},
            {data: 'status', type: 'text'},
            {data: 'delivery_date', type: 'text'},
            {data: 'delivered', type: 'numeric'},
            {data: 'checkedin', type: 'numeric'}
        ],
        stretchH: 'all',
        autoWrapRow: true,
        height: 600,
        columnSorting: true,
        sortIndicator: true,
        manualColumnResize: true,
        manualRowResize: true,
        rowHeaders: true,
        colHeaders: ['Schulnummer', 'Schule', 'Schulaufsicht', 'Schulaufsicht 2', 'Status', 'Lieferdatum', 'Anzahl', 'Einbuchungen'],
        
        cells: function (row, col, prop) {
        var cellProperties = {};
	  if (col === 6) {
		cellProperties.renderer = highlightCellRenderer;
	  }
        return cellProperties; 
        }
    };



    var hot = new Handsontable(hotElement, hotSettings);
    
        function highlightCellRenderer(instance, td, row, col, prop, value, cellProperties) {
        Handsontable.renderers.TextRenderer.apply(this, arguments);
        var del = instance.getDataAtCell(row,col+1);
        if (row > 0 && value !== del) {
            td.style.background = '#F00';
        }
        //console.log(instance.getDataAtCell(row,col));
        //console.log('Zeile: ' + row + ' Wert: ' + value);
    }

    target = document.getElementById('target');

    Handsontable.Dom.addEvent(target, 'click', function (event) {
        var formdata = $("#targetform :input").serializeArray();
        event.preventDefault();
        $.ajax({
            url: "/departments/list-dep-ajax/" + examination_id,
            data: formdata,
            type: 'POST',
            success: function (res) {
                //console.log(res);
                var data = JSON.parse(res);
                //console.log(data);
                var delivered = 0;
                var checkedin = 0;
                var datalength = data.length;
                for (var i = 0; i < datalength; i++) {
                    //console.log(data[i].delivered);
                    if(data[i].delivered != null) {
                        delivered = delivered  + parseInt(data[i].delivered);
                    }
                    if(data[i].checkedin != null) {
                        checkedin = checkedin  + parseInt(data[i].checkedin);
                    }
                   //console.log(delivered);
                }
                var last = {title:"",super_scholl_code:"",super_school_code_2:"",status:"",delivery_date:"Summe",delivered:delivered,code:"",checkedin:checkedin};
                data.push(last);
                hot.loadData(data);
                //console.log('Data loaded MB');
            }
        });
    });

    $(document).ready(function () {
        $("#target").click();
    });
</script>