<% /*
     $Revision$
     $Id$

    File:           scaffolding_list.asp

    Description:    Toont een lijst
    Parameters:
    Context:        Vanuit scaffolding.inc
    Note:           model variabele is globaal beschikbaar
    */
%>
<%
function scaffolding_m_list(model, scf_params)
{
    scf_params.list = scf_params.list || {};
    scf_params.list.columns = scf_params.list.columns || [];
    scf_params.list.groupby = scf_params.list.groupby || [];

    var transit = scf_transit2url(scf_params);

    var cols = getQParamArray("columns", []);
    if (cols.length && cols[0])
        scf_params.list.columns = cols;

    var grps = getQParamArray("groupby", []);
    if (grps.length)
        scf_params.list.groupby = grps;

    if (!scf_params.list.columns.length && "list" in model)
    {
        scf_params.list.columns = model.list.columns;
        scf_params.list.groupby = model.list.groupby;
    }

    FCLTHeader.Requires({js: []});

    if (model.autfunction)
        var authparams = user.checkAutorisation(model.autfunction);
    else
        var authparams = { PRSreadlevel: -1, ALGreadlevel: -1 };

    var showall = getQParamInt("showall", 0) == 1;
    var outputmode = getQParamInt("outputmode", 0)

    var default_url = model.list.default_url;
    if (!default_url && model["REST_PUT"])
    {
        default_url = scf_params.this_fullpath + "?mode=edit" + transit + "&id=";
    }
    if (default_url && default_url.indexOf("{0}") == -1)
        default_url += "{0}";
    %>
    <html>
      <head>
    <%
        FCLTMHeader.Generate({ outputmode: outputmode });
    %>
      </head>
      <body id="listbody">
    <%
        PAGE_START();
        var qrc = false;
        HEADER({title: model.records_title, back:!qrc, home: false, refresh: true, datatheme: jQFrontEndTheme });
        CONTENT_START();
        buttons = [];
        buttons.push({ icon: "page_refresh.png", title: L("lcl_refresh"), action: "FcltMgr.reload()" });
        if (model["REST_POST"] && authparams.PRSwritelevel < 9 && authparams.ALGwritelevel < 9)
        {
          var addurl = scf_params.this_path + "?mode=edit" + transit;
          buttons.push({ icon: "plus.png", title: L("lcl_add"), action: "FcltMgr.openDetail('" + addurl + "', '" + L("lcl_add") + " " + model.record_title + "')" });
        }

        var xxx_params = { filter : scf_params.filter || shared.qs2json(model), columns: scf_params.list.columns, groupby: scf_params.list.groupby };
        var xxx_array = model.REST_GET(xxx_params);

        function fnHeader(oRs)
        {
            var fld = scf_params.list.columns[0];
            if (!fld)
                return "";
            var field = model.fields[fld];
            return ResultsetTable.formatValue(oRs.Fields(fld).Value, { datatype: field.typ});;
        };
        function fnDetail(oRs)
        {
            var fld = scf_params.list.columns[1];
            if (!fld)
                return "";
            var field = model.fields[fld];
            return ResultsetTable.formatValue(oRs.Fields(fld).Value, { datatype: field.typ});;
        };
        function fnSubheader(oRs)
        {
            var fld = scf_params.list.columns[2];
            if (!fld)
                return "";
            var field = model.fields[fld];
            var txt = "";
            if (oRs.Fields(fld).Value)
            {
                txt = "<div class='listbodykop'>" + (field.label?field.label + ": ": "")
                                     +          ResultsetTable.formatValue(oRs.Fields(fld).Value, { datatype: field.typ}) + "</div>";
            }
            var subs = [];
            for (var i = 3; i < scf_params.list.columns.length; i++)
            {
                var fld = scf_params.list.columns[i];
                var field = model.fields[fld];
                subs.push((field.label?field.label + ": ":"")
                                     +          ResultsetTable.formatValue(oRs.Fields(fld).Value, { datatype: field.typ})
                                  );
            }
          return txt + "<div class='listbodytext'>" + subs.join("<br/>") + "</div>";
        };

        function fncolLink(oRs, processParams)
        {
          return rooturl + "/" + default_url.format(oRs.Fields("id").Value);
        };

        var rst = new ResultsetTable({ dataset: xxx_array,
                                       keyColumn: ("id" in model.fields?"id":null),
                                       ID: model.table,
                                       title: model.records_title,
                                       headerColumn: fnHeader,
                                       detailColumn: scf_params.list.columns.length > 1?fnDetail:null,
                                       subheaderColumn: scf_params.list.columns.length > 2?fnSubheader:null,
                                       //groupColumn: "gebouw",
                                       linkColumn: scf_params.list.fncolLink||(default_url?fncolLink:null),
                                       showAll: showall, // anders op 30 afgekapt
                                       outputmode: outputmode,
                                       buttons: buttons
                                      });

        function fnfncolCheck(fld)
        {
            return function (oRs)
                {
                    if (oRs.Fields(fld).Value == 1)
                        return L("lcl_Yes")
                    else
                        return L("lcl_No")
                }
        }

        var cnt = rst.processResultset();

        CONTENT_END();
        FOOTER();
        PAGE_END();
  %>
      </body>
    </html>
<%
}
%>