<?php
echo '<h4 style="margin-left: 30px;">US 2019 (PARA) - Stand: ' . $time->i18nFormat('dd.MM.yyyy HH:mm') . '</h4>';
?>

<script type="text/javascript">

    $(function(){
        var origin   = window.location.origin; 
        if (typeof $.cookie('pivotOrg') === 'undefined'){
            var org = true;
            console.log(org);
            $.cookie("pivotOrg", JSON.stringify(org), { expires: 1 });
        }
        else {
            var org = JSON.parse($.cookie("pivotOrg"));
        }
        
        //console.log(origin);
        console.log(origin);
        if (origin == 'http://km-pc-asd-ap.stmukwk.bayern.de') {
            jsonurl = '/files/para-data-us.json';
        }
        else {
            jsonurl = '/asd-control/files/para-data-us.json';
        }
        $.getJSON(jsonurl, function(mps) {  
            if (typeof $.cookie('pivotConfig') === 'undefined'){
                cookie = {};
                cookie.rows = ["Schulart"];
                cookie.cols = ["Übermittlungsstatus"];
            }
            else {
                var cookie = JSON.parse($.cookie("pivotConfig"));
                if (cookie == null || org == true ) {
                    cookie = {};
                    cookie.rows = ["Schulart"];
                    cookie.cols = ["Übermittlungsstatus"]; 
                }
            }

            //console.log(cookie.exclusions);        
            $("#output").pivotUI(mps, {
                rows: cookie.rows,
                cols: cookie.cols,
                exclusions: cookie.exclusions,
                autoSortUnusedAttrs: true,
                sorters:{
                 Übermittlungsstatus: $.pivotUtilities.sortAs(
                    ["n. überm. (kein Liefervers.)", "Schulaufs. abgelehnt", "übermittelt (PL-Fehler)", "n. überm. (Lieferversuch)" ,  "übermittelt (keine PL-Fehler)", "abgegeben", "Schulaufs. freigegeben"]
                )},
                rendererOptions: {
                    table: {
                        clickCallback: function(e, value, filters, pivotData){
                            //console.log(filters);
                            $( "#heading").text(filters.Schulart + ' - ' + filters.Übermittlungsstatus);
                            var names = [];
                            pivotData.forEachMatchingRecord(filters,
                                function(record){ names.push(record.Schule + ' (' + record.Datum.substring(0,10) + ' ' +  record.Datum.substring(11,19) + ')'); });
                            list = '<ul>';
                            $.each(names, function() {
                                list += '<li>' + this + '</li>';
                            });
                            list += '</ul>';
                            $( "#schools" ).html(list);
                        }
                    }
                }           
            });
            $("#standard").on("click", function(){
                org = true;
                console.log(org);
                $.cookie("pivotOrg", JSON.stringify(org), { expires: 1 });
                window.location.reload(true);
            });
            $("#load").on("click", function(){
                org = false;
                console.log(org);
                $.cookie("pivotOrg", JSON.stringify(org), { expires: 1 });
                window.location.reload(true);
            });

            $("#save").on("click", function(){
                var config = $("#output").data("pivotUIOptions");
                //console.log(config);
                var config_copy = JSON.parse(JSON.stringify(config));
                //console.log(config_copy);
                //delete some values which will not serialize to JSON
                delete config_copy["aggregators"];
                delete config_copy["renderers"];
                $.cookie("pivotConfig", JSON.stringify(config_copy), { expires: 365 });
            });

            $("#del").on("click", function(){
                //$.cookie("pivotConfig", null, { path: '/' });
                $.cookie("pivotConfig", null);
                window.location.reload(true);
            });
        });
     });
     
</script>

<div id="output" style="margin-left: 30px;"></div>
<input type="button" value="Load Standard" id="standard" style="font-size: 16px;margin-left: 30px;" />
<input type="button" value="Load Config" id="load" style="font-size: 16px;" />
<input type="button" value="Save Config" id="save" style="font-size: 16px;" />
<!-- <input type="button" value="Reset Config" id="del" style="font-size: 16px;" /> -->
<p>&nbsp;</p>
<p id="heading" style="margin-left: 30px;font-weight: bold;"><p>
<div id="schools" style="margin-left: 30px;"></div>  