<%@language="javascript"%>
<% /*
     $Revision$
     $Id$

    File:        mgt_generic_list.asp
    Description: resultaat van mgt_generic.asp
    Parameters:  sql
    Context:
*/ %>

<!-- #include file="../Shared/common.inc" -->
<!-- #include file="../Shared/adovbs.inc" -->
<!-- #include file="../Shared/iface.inc" -->
<!-- #include file="../Shared/resultset_table_v2.inc" -->

<%
FCLTHeader.Requires({ plugins:["jQuery"] });

__Logging = 0; // Is te laat...
Application("otap_environment") == "O" || user.checkAutorisation("WEB_FACFAC");
var sql = getFParam("sql", getQParam("sql", ""));
var outputmode = getFParamInt("outputmode", 0);
var showall = getFParamInt("showall", 0) == 1;


%>
<html>
  <head>
<%  FCLTHeader.Generate({outputmode: outputmode}); %>
    <style>
        table.rstable td {
            border-left: 1px solid #bbb;
        }
        body#searchbody td {
            line-height:1.2em!important;
        }
    </style>
    <script>
        function fliptable(evt)
        {
            FcltMgr.stopPropagation(evt);
            $("table#sqldatatable").each(function() {
                var $this = $(this);
                var newrows = [];
                $this.find("tr").each(function(){
                    var i = 0;
                    $(this).find("td,th").each(function()
                    {
                        if ($(this).closest("thead").hasClass("tableFloatingHeader"))
                            return;
                        i++;
                        if(newrows[i] === undefined) { newrows[i] = $("<tr></tr>"); }
                        this.align = 'left'; // de data
                        if (this.tagName == 'TH')
                            this.style = 'text-align:right'; // de headers
                        newrows[i].append($(this));
                    });
                });
                $this.find("tr").remove();
                $.each(newrows, function(){
                    $this.append(this);
                });
            });
            $("table#sqldatatable").width('auto');
            FcltMgr.resized();
            return false;
        }

        $(function ()
        {
        });

    </script>
  </head>

  <body id="searchbody">
<%
    if (!sql)
        Response.End;

    if (!sql.match(/^\s*(SELECT|WITH)/i))
        shared.simpel_page("SQL statement must start with SELECT or WITH"); // beschermt tegen INSERT, DELETE en UPDATE

    try
    {
        var hiresTimer = new ActiveXObject("SLNKDWF.About");
        var tm_start = hiresTimer.usTimer;
        var oRs = Oracle.RealConnection.Execute(sql); // de wrapper beschermt tegen INSERT, DELETE en UPDATE
        var tm = (hiresTimer.usTimer - tm_start) / 1000;
    }
    catch (e)
    {
        var msg = e.description;
        if (msg.match('^ROW\-00004')) // ROW-00004: Invalid column datatype
            msg += '\nHint: Query should not contain mld_t_uitvoertijd columns';

        var message_bar = "<div class='warning'>" + I("fa-exclamation-triangle fa-lg") + " " + safe.html(msg) + "</div>";
        Response.Write(message_bar);
        Response.End;
    }
    var rst = new ResultsetTable({ sql: "dummy",
                                   pRs: oRs, // oRs is al eerder bepaald
                                   ID: "sqldatatable",
                                   canCSV: true,
                                   showAll: true, //showall,
                                   outputmode: outputmode, //0,
                                   title: "SQL ({0}ms)".format(tm.toFixed(1)),
                                   buttons: [{icon: "fa-table-pivot fa-lg", title: "Flip", action: "fliptable()"}] // print en excel komen vanzelf
                                 });
    function fnfnmyFloat(kolom)
    {
        return function (oRs)
        {
            var v = oRs(kolom).Value;
            return safe.displayfloat(v, 2, true); // true voor trimZeros, we weten helemaal niet of die 2 zo bedoeld was
        }
    }

    // kolommen maken
    var donecols = {};
    for (i= 0; i < oRs.Fields.Count; i++)
    {
        var kolomnaam = oRs.Fields(i).Name.toLowerCase();
        if (kolomnaam in donecols)
            shared.internal_error("Duplicate column name '{0}' not supported".format(kolomnaam));
        donecols[kolomnaam] = true;
        var colpar = {caption: kolomnaam,
                      content: kolomnaam,
                      datatype: null,
                      align: null
                     }
        var kolomtype = oRs.Fields(i).Type;
        if (kolomtype == adInteger || kolomtype == adDecimal)
        {
           colpar.datatype = 'number';
        }
        else if (kolomtype == adCurrency || kolomtype == adNumeric || kolomtype == adVarNumeric || kolomtype == adDouble)
        {
           colpar.align = "right"
           colpar.content = fnfnmyFloat(kolomnaam);
        }
        else if (kolomtype == adDBTimeStamp)
        {
           colpar.datatype = 'datetime'; // ??
           colpar.withSeconds = true;
        }
        rst.addColumn(new Column(colpar));
    }

    var cnt = rst.processResultset();
    if (cnt == 1)
    {   // Zet de waarden onder elkaar als er maar 1 record gevonden is.
        %><script>fliptable()</script><%
    }
%>
  </body>
</html>
<% ASPPAGE_END(); %>
