<% /*
     $Revision$
     $Id$

    File:           scaffolding_list.asp

    Description:    Toont een lijst
    Parameters:
    Context:        Vanuit scaffolding.inc
    Note:           model variabele is globaal beschikbaar
    */
%>
<%
function scaffolding_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 || [];
    scf_params.list.orderby = scf_params.list.orderby || [];

    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;
    }

    var modal = model.edit.modal || scf_params.edit && scf_params.edit.modal; // dan doen we een detailrecord altijd modal
    var addmodal = modal || scf_params.edit && scf_params.edit.addmodal;

    FCLTHeader.Requires({js: ["jquery-ui.js"]});
    if ("requires" in scf_params.list)
        FCLTHeader.Requires(scf_params.list.requires);

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

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

    // QueryString filters.
    var qsfilter = shared.qs2json(model);
    if (scf_params.incsetting && scf_params.incsetting.joinfield in qsfilter) // Zijn we eigenlijk een include?
    {
        transit += "&" + scf_params.incsetting.joinfield + "=" + safe.url(qsfilter[scf_params.incsetting.joinfield]); // parentkey doorgeven
    }

    var default_url = model.list.default_url;
    var modal_url = scf_params.this_fullpath + "?mode=edit" + transit + "&id={0}"

    if (!default_url &&
           "id" in model.fields &&
           ("show" in model || "show" in scf_params || model["REST_PUT"] || model["REST_DELETE"]))
    {
        if (modal)
            default_url = modal_url;
        else
            default_url = scf_params.this_path + "?mode=wrap" + transit + "&id=";
    }
    if (default_url && default_url.indexOf("{0}") == -1)
        default_url += "{0}";
    %>
    <html>
      <head>
    <%  FCLTHeader.Generate({ outputmode: outputmode }); %>
<% if (outputmode == 0) { %>
        <script>
          function scf_reload (json)
          {
              FcltMgr.reload({ appendurl: "scf_highlight=" + json.key});
          }
          function scf_show(row)
          {
            var key = row.getAttribute("ROWKEY");
            var url = "<%= default_url %>".format(key);
            <% if (modal) { %>
                $("#<%=model.table%> tbody tr").removeClass("editing updated")
                $(row).addClass('editing');
                FcltMgr.openModalDetail(url, "<%=safe.jsstring(model.record_title)%>", { callback: scf_reload, modal_hinter: true, ishtmlsafe: true });
            <% } else { %>
                $(row).addClass('dirty');
                $(row).closest("table").find("tr[rowkey="+key+"]").addClass('dirty'); // regels met gelijke rowkey ook dirty
                FcltMgr.openDetail(url, "<%=safe.jsstring(model.record_title)%>");
            <% } %>
          }
          function scf_show_xclick(row)
          {
            var key = row.getAttribute("ROWKEY");
            var url = "<%= scf_params.urllink && scf_params.urllink.link? scf_params.urllink.link : "" %>" + key;
            $(row).addClass('dirty');
            $(row).closest("table").find("tr[rowkey="+key+"]").addClass('dirty'); // regels met gelijke rowkey ook dirty
            FcltMgr.openDetail(url, "<%=safe.jsstring(model.record_title)%>");
          }
          function scf_add()
          {
            <%
                if (addmodal) {
             %>
                var css_style = "";
                if (window.innerWidth >= 768) { // fix too small modal on wide screen RABO#60763
                    css_style = "min-width: 700px;";
                } else if (window.innerWidth >= 540) {
                    css_style = "min-width: 480px;";
                }
                FcltMgr.openModalDetail("<%= scf_params.this_fullpath + "?mode=edit" + transit %>", L("lcl_add") + " " + "<%=safe.jsstring(model.record_title)%>", { callback: scf_reload, style: css_style });
            <% } else { %>
                FcltMgr.openDetail("<%= scf_params.this_path + "?mode=wrap" + transit %>", L("lcl_add") + " " + "<%=safe.jsstring(model.record_title)%>");
            <% } %>
          }
          function scf_email()
          {
            function scfMailCallback(data)
            {
              FcltToast(L("lcl_schedule_is_planned"));
            }
<%
            var urlMail = "../shared/queuemail_schedule.asp?pmodule=FAC&defemail_key=" + user_key
                        + transit
                        + "&subject=" + Server.URLencode(model.records_title);
            // outputmode komt er in het save-script nog bij
            var geturl = Request.ServerVariables("SCRIPT_NAME") + "?formail=1&showall=1" + transitQS();
            geturl = geturl.substr(rooturl.length + 1); // die er af
%>
            var subject = L("lcl_scf_email");

            var url = "<%=safe.jsstring(protectQS.create(urlMail))%>";
            FcltMgr.openModalDetail(url, subject, { callback: scfMailCallback,
                                                    geturl: "<%=safe.jsstring(geturl)%>"
                                                  });
          }
          function scf_import(key)
          {
              var url = "<%=scf_params.this_path%>?mode=import&format=html<%=transit%>";
              FcltMgr.openDetail(url, L("lcl_scf_paste") + " <%=safe.jsstring(model.record_title)%>");
          }

          function scf_delete(rowArray, isMulti)
          {
            var scfKeyString = getKeyString(rowArray);
            var data = {
                id: scfKeyString
            };
            protectRequest.dataToken(data);
              $.post("<%=scf_params.this_fullpath%>?mode=delete&multi=1<%=transit%>",
                  data,
                  FcltCallbackRefresh,
                  "json");
          }

          function scf_multiedit(rowArray, isMulti)
          {
            var scfKeyString = getKeyString(rowArray);
            var url = "<%= modal_url %>".format(-1) + "&scf_multi=1" + "&sel_id="+scfKeyString;
            FcltMgr.openModalDetail(url, scfKeyString.split(",").length + " " + "<%=safe.jsstring(model.records_title)%>", { callback: scf_reload, id: scfKeyString });
          }

          // Sorteren door drag-and-drop experiment (niet actief)
          // Return a helper with preserved width of cells
          var fixHelper = function(e, ui) {
          	ui.children().each(function() {
          		$(this).width($(this).width());
          	});
          	ui.css('left', "-2px");
          	return ui;
          };

          if (window.$ && 0) // TODO: nog even uit, misschien if ("sequence" in fields)
          $(function () {
            $("#<%=model.table%> tbody").sortable({
            	axis: "y",
            	helper: fixHelper,
            	stop: function( event, ui ) {
                        //ui.item.children('td').css('color', 'red');
            	        var rows = [];
            	        $("#<%=model.table%> tbody tr").each(function()
            	            {
             	                rows.push(this.getAttribute("ROWKEY"));
            	            }
            	        );
            	        FcltMgr.alert(rows);
                }
            }).disableSelection() });;
            <%= scf_params.list.script %>
        </script>
<% } %>
      </head>
      <body id="listbody">
    <%
        function object_merge()
        {
            for (var i=1; i<arguments.length; i++)
                for (var a in arguments[i])
                    arguments[0][a] = arguments[i][a];
            return arguments[0];
        }

        if (model.autfunction_add)
          var authparams_add = user.checkAutorisation(model.autfunction_add, true);
        else
          var authparams_add = authparams;

        // doe maar niet. Headers zijn dan de deur te vroeg uit
        // en we kunnen geen 500-status meer doen? busyLoading(); // via ResultsetTable is te laat (na REST_GET) dus maar zelf
        buttons = scf_params.list.buttons || [];
        if (!nobuttons)
        {
            buttons.push({ icon: "fa-fclt-refresh", title: L("lcl_refresh"), action: "FcltMgr.reload()", id: "btn_scf_refresh" });
            if (getQParamInt("scf_graph_type", 0) == 0) // niet bij grafieken
                buttons.push({ icon: "fa-envelope", title: L("lcl_noti_mld_email"), action: "scf_email()", id: "btn_scf_email" });
        }
        if (model["REST_POST"] && authparams_add && authparams_add.PRSwritelevel < 9 && authparams_add.ALGwritelevel < 9)
        {
          if (!modal && user.has("WEB_FACFAC"))
          {
              buttons.push({ title: L("lcl_scf_paste"), action: "scf_import()", icon: "fa-brackets-curly" });
          }
          buttons.push({ icon: "fa-plus", title: L("lcl_add"), action: "scf_add()", id: "btn_scf_add" });
        }

        // Is er een limiet (limit) meegegeven in de QueryString?
        var hasLimit = qsfilter.limit;

        // Zowel scf_params.filter als qsfilter (shared.qs2json(model)) kunnen filterwaarden opleveren. Deze twee dus samenvoegen.
        var xxx_params = { filter : object_merge( {}, scf_params.filter || {}, qsfilter || {} ),
                           columns: scf_params.list.columns,
                           orderby: scf_params.list.orderby,
                           groupby: scf_params.list.groupby };

        if (scf_params.urllink)
          xxx_params.urllink = scf_params.urllink;

        if (!("limit" in xxx_params.filter))
        {
            xxx_params.filter.limit = showAll?S("qp_maxrows2"):S("qp_maxrows")
        }

        var isExcel = ((outputmode==2 || outputmode==6) );                  // && (S("excel_mode")==0));
        var isCSV   = ((outputmode==2 || outputmode==6 || outputmode==4) ); // && (S("excel_mode")==1));

        xxx_params.filter.nolimit = (isExcel || isCSV);
        var sqltotals = "";
        xxx_params.getTotalSql = false;
        if (scf_params.incsetting) // Zijn we eigenlijk een include? Dan halen we de
        {                          // echte data op via (ons als include van) de 'parent'
            xxx_params.filter.id = xxx_params.filter[scf_params.incsetting.joinfield]; // Die zal er dan zijn
            xxx_params.include = [ getQParam("model") ];
              var xxx_array = scf_params.orgmodel.REST_GET(xxx_params);
              if (xxx_array.length)
                xxx_array = xxx_array[0][getQParam("model")];
        }
        else
        {
            xxx_params.include = xxx_params.include || [];
            for (i in scf_params.list.columns)
            {
                var xmodel = model;
                var fld = scf_params.list.columns[i];
                if (fld.indexOf(".") > 0) // Bij res_ruimte configurations.res_opstelling_key ofwel <include>.<field>
                {
                    var inctable = fld.split(".")[0];
                    __Log("Adding include {0} for column {1}".format(inctable, fld));
                    xxx_params.include.push(inctable);
                }
            }
            xxx_params.getTotalSql = false;
            var xxx_array = model.REST_GET(xxx_params);
            // get sql for generalsum or generalaverage if any
            for (var fld in model.fields)
            {
              if (model.fields[fld].generalaverage || model.fields[fld].generaltotal )
              {
                xxx_params.getTotalSql = true;
                sqltotals = model.REST_GET(xxx_params);
                break; // stop the loop, total/average calculation Sql found
              }
            }
        }

        var highlight = qsfilter.scf_highlight;
        if (typeof highlight == "string")
            highlight = highlight.split(",");

        function fnRowClass(oRs)
        {
            if (!highlight)
                return null;
            var key = oRs.Fields("id").Value;
            if (inArray(key, highlight))
                return "updated";
            return null;
        }

        function fnRowChecked(oRs)
        {
            if (!highlight || highlight.length <= 1)
                return false;
            var key = oRs.Fields("id").Value;
            if (inArray(key, highlight))
                return true;
            return false;
        }

        // Uniques misbruiken we om een intelligentere tabelkop te kunnen maken
        // Ook kijken of we multiedit moeten aanbieden
        var multiedit = false;
        var title = model.records_title;
        var suppress = true;
        var lastColKey = -1;
        for (var fld in model.fields)
        {
            var field = model.fields[fld];
            multiedit = multiedit || field.multiedit;
            if (field.uniquewith && fld in qsfilter) // Dan de andere waarde tonen als tabel
            {
                // Eigenlijk willen we de records_title van de andere tabel.
                // Die is echter niet included
                title = model.fields[field.uniquewith].label;
            }
            suppress = suppress && field.visible != "V";
            if (suppress && field.visible == "S")
                lastColKey++;
        }

        var has_rapport_link = (xxx_params.filter.scf_pivot == 0 && scf_params.urllink != null && scf_params.urllink.link != null)
        var rs_params = { dataset: xxx_array,
                          sqltotals: sqltotals,
                          hasMore: hasLimit? false : model.total_count > xxx_array.length,
                          keyColumn: ("id" in model.fields? (has_rapport_link? scf_params.urllink.col : "id") : null),
                          ID: model.table,
                          title: title,
                          showAll: showAll,
                          canCSV: !nobuttons && model.list.canCSV,
                          rowClass: scf_params.list.rowClass || model.list.rowClass || fnRowClass,
                          rowChecked: fnRowChecked,
                          lastColKey: lastColKey,
                          rowData: scf_params.list.fnRowData,
                          outputmode: outputmode,
                          noPrint: nobuttons,
                          noExcel: nobuttons,
                          buttons: buttons,
                          rstCheckBoxes: scf_params.list.checkboxes || [],
                          roundtripCode: scf_params.list.roundtripCode,
                          filterParams: xxx_array.filterstring, // Filter parameters in de printafdruk weergeven.
                          noLoading: true, // hebben we al eerder gedaan
                          urllink: (scf_params.urllink? scf_params.urllink.link : null),
                          totalCalc: scf_params.list.totalCalc,
                          totalShow: scf_params.list.totalShow,
                          noSetSummary: scf_params.list.noSetSummary
                         };
        if (scf_params.incsetting && scf_params.incsetting.required) // Zijn we eigenlijk een include?
        {
            rs_params.required = true;
            rs_params.emptySetString = L("lcl_scf_add_required").format(model.records_title);
        }
        if (scf_params.incsetting && scf_params.incsetting.emptySetString)
        {
            rs_params.emptySetString = scf_params.incsetting.emptySetString;
        }

        var rst = new ResultsetTable(rs_params);
        function fnfncolCheck(fld)
        {
            return function (oRs)
                {
                    if (oRs.Fields(fld).Value == 1)
                        return L("lcl_Yes")
                    else
                        return L("lcl_No")
                }
        }
        function fnfncolColor(fld)
        {
            return function (oRs)
                {
                    var v = sharedTrim(oRs.Fields(fld).Value || "");
                    var result = "";
                    var reghex = new RegExp("^#?[0-9A-F]{6}$", "i"); // Ben ik een hex-code? (al dan niet met # prefix)
                    if (typeof v === "string" && v.match(reghex))
                    {
                        if (v.charAt(0) !== "#")
                            v = "#" + v;
                        result = "<span class='scf-color-preview' style='color: " + safe.htmlattr(v) + "'>"
                               +     I("fa-square fa-2x", { fastyle: "fas" })
                               + "</span>";
                    }
                    return result + v;
                }
        }
        function fnfncolTranslatable(fld, field)
        {
            return function (oRs)
                {
                    var plabel = oRs.Fields(fld).Value;
                    if (field.islcl && plabel && plabel.substr(0, 4) == 'lcl_')
                    {
                        plabel =  L(plabel);
                    }
                    var fn = "FcltMgr.stopPropagation(event);iface.translate('{0}', {1}, this.innerText, '{2}', 'input')".format(model.fields[fld].dbs, oRs.Fields("id").Value, safe.jsstring(plabel))
                    return "<span class='facmgtmultilang' onclick='"+safe.htmlattr(fn)+"'>" + safe.html(plabel) + "</span>";
                }
        }
        function fnfncolFunction(fn)
        {
            return function (oRs)
                {
                    return fn(oRs);
                }
        }
        function fnfncolInclude(inctable, fld)
        {
            return function (oRs)
                {
                    var incdata = oRs.Fields(inctable).Value;
                    var safebuilder = [];
                    var done = {};
                    // Het sorteren in de query is ondoenlijk (door de foreigns) dus maar hier
                    var sortOnFld = field.sortOnFld || "name";
                    incdata.sort(function (a, b) {
                                var valA = a[fld];
                                if (valA && typeof valA == "object" && sortOnFld in valA)
                                    valA = valA[sortOnFld];
                                var valB = b[fld];
                                if (valB && typeof valB == "object" && sortOnFld in valB)
                                    valB = valB[sortOnFld];
                                return valA <= valB? -1: 1
                            });
                    var maxinline = 99; // hardcoded om te grote pagina's te voorkomen
                    if (incdata.length < maxinline)
                        for (var i = 0; i < incdata.length && i < maxinline; i++)
                        {
                            var val = incdata[i][fld];
                            var safe_val = "";
                            var valForDone = val;
                            if (val && typeof val == "object" && "name" in val)
                            {
                                valForDone = sortOnFld in val ? val[sortOnFld] : val["name"];
                                val = val["name"];
                            }
                            else // Deze geeft al een 'safe' output terug
                                safe_val = ResultsetTable.formatValue(val, { datatype: field.typ }); // kan datetime zijn bij res_rsv_deel

                            if (!(valForDone in done)) // Voorkom dubbelen (opstelling/ruimte)
                            {
                                safebuilder.push(safe_val === "" ? safe.html(val) : safe_val);
                                done[valForDone] = 1;
                            }
                        }
                    if (incdata.length > maxinline)
                        safebuilder.push("More than {0}".format(maxinline));
                    return safebuilder.join("<br>");
                }
        }

        function fnfnmyFloat(kolom)
        {
            return function (oRs)
            {
                var v = oRs.Fields(kolom).Value;
                return safe.displayfloat(v, 2, true); // true voor trimZeros, we weten helemaal niet of die 2 zo bedoeld was
            }
        }
        function fntotalShow(column, nrlines)
        {
            if (column.datatype == 'currency')
                return safe.curr(column.totalsum);
            else
                return safe.displayfloat(column.totalsum, 2, true);
        }
        function fnfnmyVal(kolom)
        {
            return function (oRs)
            {
                return oRs.Fields(kolom).Value;
            }
        }
        function fnfnlcl(kolom)
        {
            return function (oRs)
            {
                var v = oRs.Fields(kolom).Value;
                if (v && v.substr(0, 4) == 'lcl_')
                    return L(v)
                else
                    return safe.html(v);
            }
        }

        function fnfndbcol(tabel, kolom)
        {
            var dbcol = {};
            var sql = "SELECT data_type"
                    + "     , data_length"
                    + "     , data_precision"
                    + "     , data_scale"
                    + "  FROM user_tab_columns"
                    + " WHERE table_name=" + safe.quoted_sql_upper(tabel)
                    + "   AND column_name = " + safe.quoted_sql_upper(kolom);
            var oRs = Oracle.Execute(sql);
            if (!oRs.eof)
                dbcol = { typ:       oRs("data_type").Value
                        , length:    oRs("data_length").Value
                        , precision: oRs("data_precision").Value
                        , decimals:  oRs("data_scale").Value
                        };
            oRs.Close();
            return dbcol;
        }

        for (i in scf_params.list.columns)
        {
          if (scf_params.urllink && scf_params.list.columns[i] == scf_params.urllink.col)
          { // De url key kolom wil ik niet in de resultaten zien.
            continue;
          }

            var xmodel = model;
            var fldname = fld = scf_params.list.columns[i];
            var inctable = null;
            if (fld.indexOf(".") > 0) // Bij res_ruimte configurations.res_opstelling_key ofwel <include>.<field>
            {
                inctable = fld.split(".")[0];
                if (model.includes && inctable in model.includes)
                {
                    xmodel = model.includes[inctable].model;
                    var fld = fld.split(".")[1];
                }
                else
                    inctable = null;
            }

            if (!(fld in xmodel.fields)) // Tijdens ontwikkeling vaak fout
            {
              __DoLog(model.fields);
              __DoLog(scf_params.list.columns);
              shared.internal_error("Field '{0}' not found in model '{1}'".format(fld, inctable||model.table));
            }
            var field = xmodel.fields[fld];
            if (field.hidden) // Bij een detailrecord is de parent_key op hidden gezet
                continue;

            if (field.typ == "float")
                if (field.iscurrency)
                    field.decimals = 2;
                else
                {
                    field.decimals = fnfndbcol(model.table, field.dbs).decimals;
                    if (field.decimals === null) field.decimals = 2; // Float default naar 2
                }

            var coldata = { caption: field.orglabel? field.orglabel: field.label,
                            content: fld,
                            name: fldname,
                            title: field.title,
                            datatype: field.typ,
                            withSeconds: field.seconds,
                            total: field.total,
                            generaltotal: field.generaltotal,
                            generalaverage: field.generalaverage,
                            decimals: field.decimals,
                            trimZeros: field.trimZeros,
                            tdClass: field.tdClass,
                            secret: field.secret,
                            combine: field.combine };
            if (field.iscurrency)
                coldata.datatype = "currency";

            if (field.typ == "date" || field.typ == "datetime")
            {
                coldata.nowrap = true;
                coldata.align = "right";
            }

            if (outputmode == 4)
            {
                coldata.caption = fld; // Naar csv altijd veldnaam
                coldata.caption = coldata.caption.replace(/^(HIDE_F_|FCLT_X_|FCLT_3D_|FCLT_KEY|FCLT_F_|HTML_|FCLT_D_|FCLT_C_)/i, "");
            }
            if (field.foreign || field.LOV) // Foreign en LOV zijn effectief altijd tekst
                coldata.datatype = "varchar";
            else
                if (field.typ == "number" || field.typ == "key")
                    coldata.datatype = "number"; // Zo verwacht resultsettable dat
            if (fld == "id" && field.typ == "key")
                coldata.tdClass = "listid";

            if (field.autofloat && !isExcel) // Vanuit model_reports hebben we niet altijd correct autodetect
           {                                 // van het datatype "float" gedaan. Daarom hier autofloat
                coldata.amount = fnfnmyVal(fld); // voor optellen
                coldata.content = fnfnmyFloat(fld);
                rst.totalShow = fntotalShow;
            }

            if (field.typ == "check" || field.typ == "check0")
            {
                coldata.content = fnfncolCheck(fld);
            }
            else if (field.typ == "color")
            {
                coldata.content = fnfncolColor(fld);
                coldata.columnClass = function() { return "normal-padding"; }
            }
            if (field.listfunction)
            {
                coldata.content = fnfncolFunction(field.listfunction);
            }
            if (field.islcl)
            {
                coldata.content = fnfnlcl(fld);
            }

            // Child tabellen kunnen we inline localizen
            if (field.translate && S("multi_language_option") && modal && outputmode == 0)
            {
                coldata.content = fnfncolTranslatable(fld, field);
                coldata.caption = "<span class='facmgtmultilangheader' title='{0}'>{1}</span>".format(L("lcl_vertalingen"), coldata.caption);
            }

            if (scf_params.list.actionscol == scf_params.list.columns[i])
                coldata.hasActions = true;

            if (inctable)
            {
                coldata.content = fnfncolInclude(inctable, fld);
            }

            rst.addColumn(new Column(coldata));
        }

        if (scf_params.list.default_action || default_url)
          rst.addAction({ action: scf_params.list.default_action || "scf_show", caption: L("lcl_edit"), isDefault: true } );
        else if (xxx_params.filter.scf_pivot == 0 && scf_params.urllink && scf_params.urllink.link)
          rst.addAction({ action: "scf_show_xclick", caption: L("lcl_edit"), isDefault: true } );

        if (scf_params.list.actions)
        {
            for (var i = 0; i < scf_params.list.actions.length; i++)
                rst.addAction(scf_params.list.actions[i]);
        }

        if (model.REST_DELETE)
            rst.addAction({ action: "scf_delete", caption: L("lcl_delete"), multi: true, onlyMulti: true, multiOnce: true } );

        if (model.REST_PUT && multiedit)
            rst.addAction({ action: "scf_multiedit", caption: L("lcl_change"), multi: true, onlyMulti: true, multiOnce: true } );

        var cnt = rst.processResultset();
%>
        <script>
            // let the frame know that the content is ready
            $(window.frameElement).change();
        </script>
    </body>
    </html>
<%
}
%>