<% /*
     $Revision$
     $Id$
   */

%>
<!-- #include file="../fac/fac_list.inc" -->
<!-- #include file="../Shared/status.inc" -->
<% /* Gebruik queries van desktop voor harmonisatie */

function fac_list(days_in_the_future)
{
    var xfunc = user.func_enabled("*", null, null, user_key);

    function fncolLink(oRs)
    {
        var module = oRs.Fields("xmlnode").Value;
        // Gewoon intern
        var url = fncolLinkKey(module, oRs.Fields("item_key").Value);

        return url;
    };

    // Ook te gebruiken met key=-1 voor nieuwe
    function fncolLinkKey(module, key)
    {
        var nurl = "";
        switch (module)
        {
        case "melding":
            nurl = "../pda/melding.asp?mld_key=" + key;
            break;
        case "reservering":
            nurl = "../pda/reservering.asp?rsv_ruimte_key=" + key;
            break;
        case "deel":
            nurl = "../pda/ins_deel.asp?urole=&ins_key=" + key;
            break;
        case "bestelling":
            nurl = "../pda/bestelling.asp?bes_key=" + key;
            break;
        case "afspraak":
            nurl = "../pda/afspraak.asp?afs_key=" + key;
            break;
        }
        return nurl;
    };

    function fncolHeader(oRs, thisModule)
    {
        var module = oRs.Fields("xmlnode").Value;
        var txt = "";
        if (module != "afspraak") { // Afspraak-key is geen informatie
            txt = oRs.Fields("item").Value;
        }
        txt = "<span class='semibold text-primary'>" + safe.html(txt) + "</span><span>"
            + (oRs("mobile_detail").Value || "")
            + "</span>";

        if (typeof thisModule.fnStatus === "function") {
            var status = thisModule.fnStatus(oRs);
            if (status) {
                txt = "<span class='text-secondary'>(" + status + ")</span>" + txt;
            }
        }
        return txt;
    }

    function fncolContent(oRs, thisModule) {
        var result = "";
        if (typeof thisModule.fnSubheader === "function") {
            result += thisModule.fnSubheader(oRs);
        } else {
            result += oRs("datum").Value;
        }
        return result;
    }

    var modules = [];
    var specific_day = false;
    if (typeof days_in_the_future === "number" && inArray(days_in_the_future, [-1, 0, 1])) { // Alleen supported; gisteren, vandaag, morgen
        specific_day = true;
    }
    if (!specific_day && xfunc.canRead("WEB_MLDUSE")) { // Momenteel onlogisch icm een specifieke dag
        modules.push({
            xmlnode: 'melding',
            groupName: L("lcl_mobile_meldingen"),
            fnStatus: function (oRs) { return (oRs.Fields("status_key").Value != 0 &&
                                               oRs.Fields("status_key").Value != 4 &&
                                               oRs.Fields("status_key").Value != 7
                                               ? status.getmldstatustext(oRs.Fields("status_key").Value)
                                               : ""); },
            fnSubheader:
                function (oRs) {
                    var mld_sql = "SELECT mld_melding_onderwerp, mld_melding_omschrijving FROM mld_melding WHERE mld_melding_key = " + oRs("item_key").Value;
                    var mld_oRs = Oracle.Execute(mld_sql); // Deze zoeken we zelf wel even op
                    var result = (mld_oRs("mld_melding_onderwerp").Value
                                    ? "<span class='subject'>" + safe.html(mld_oRs("mld_melding_onderwerp").Value)+ "</span>"
                                    : toDateTimeString(oRs.Fields("datum").Value, false, false, true))
                                + (mld_oRs("mld_melding_omschrijving").Value
                                    ? "<div class='listbodytext'>" +safe.html(shorttxt(shared.stripbbcodes(mld_oRs("mld_melding_omschrijving").Value), S("rs_mobile_maxchar"))) + "</div>"
                                    : "");
                    mld_oRs.Close();
                    return result;
                },
            sql: fac_list_query ({ "showmelding": true }) + " ORDER BY DECODE(status_key, 6, 3, 5, 2, 1)" // Van onder naar boven; Verwerkt, Afgemeld, de rest
                                                          + "        , item_key DESC"
        });
    }

    if (!specific_day && xfunc.canRead("WEB_BESUSE")) { // Momenteel onlogisch icm een specifieke dag
        modules.push({
            xmlnode: 'bestelling',
            groupName: L("lcl_mobile_bestellingen"),
            fnStatus: function (oRs) { return status.getbesbestellingstatustext(oRs.Fields("status_key").Value); },
            fnSubheader: function (oRs) { return "<div class='listbodykop'>" + toDateTimeString(oRs.Fields("datum_eind").Value, false, false, true) + "</div>"; },
            sql: fac_list_query ({ "showbestelling": true }) + " ORDER BY item_key DESC"
        });
    }

    if (!specific_day && (xfunc.canRead("WEB_INSUSE") || xfunc.canRead("WEB_INSMIN"))) {
        modules.push({
            xmlnode: 'deel',
            groupName: L("lcl_geleend_object"),
            fnStatus: function (oRs) { return ""; },
            fnSubheader: function (oRs) { return toDateTimeString(oRs.Fields("datum").Value, false, false, true); },
            sql: fac_list_query ({ "showdeel": true }) + " ORDER BY datum DESC"
        });
    }

    if (xfunc.canRead("WEB_RESUSE")) {
        modules.push({
            xmlnode: 'reservering',
            groupName: L("lcl_mobile_reserveringen"),
            fnStatus: function (oRs) { return oRs.Fields("status_key").Value != 2 ? status.getresfostatustext(oRs.Fields("status_key").Value) : ""; },
            fnSubheader:
                function (oRs) {
                    return "<div class='listbodykop'>"
                           + "<div>" + I("fa-clock") + "&nbsp;"
                           + (specific_day
                               ? toTimeString(oRs.Fields("datum").Value, { isLocal: true })
                               : toDateTimeString(oRs.Fields("datum").Value, { isLocal: true }))
                           + "-" + toTimeString(oRs.Fields("datum_eind").Value, { isLocal: true }) + "</div>"
                           + (oRs.Fields("plaats").Value ? "<div>" + I("fa-map-marker") + "&nbsp;" + oRs.Fields("plaats").Value + "</div>" : "")
                           + "</div>";
                },
            sql: fac_list_query ({ "showreservering": true, "specific_day": specific_day, "days_in_the_future": days_in_the_future }) + " ORDER BY datum ASC"
        });
    }

    if (xfunc.canRead("WEB_BEZUSE")) {
        modules.push({
            xmlnode: 'afspraak',
            groupName: L("lcl_mobile_bezoek"),
            fnStatus: function (oRs) { return fac.localstring("" + oRs.Fields("status").Value); },
            fnSubheader:
                function (oRs) {
                    return "<div class='listbodykop'>"
                           + "<div>" + I("fa-clock") + "&nbsp;"
                           + (specific_day
                               ? toTimeString(oRs.Fields("datum").Value)
                               : toDateTimeString(oRs.Fields("datum").Value, false, false, true))
                           + "</div></div>"; },
            sql: fac_list_query ({ "showafspraak": true, "specific_day": specific_day, "days_in_the_future": days_in_the_future }) + " ORDER BY datum ASC"
        });
    }

    var records = 0, oRs;
    if (modules.length) {
        for (module in modules) {
            oRs = Oracle.Execute("SELECT COUNT('x') FROM (" + modules[module].sql + ")");
            modules[module].cnt = oRs(0).Value;
            records += modules[module].cnt;
            oRs.Close();
        }
        for (var i = modules.length - 1; i >= 0; i--) {
            if (modules[i].cnt === 0) {
                modules.splice(i, 1);
            }
        }
    }

    if (records > 0) {
        if (specific_day) {
            var title = [
                L("lcl_date_yesterday"),
                L("lcl_date_today"),
                L("lcl_date_tomorrow")
            ][days_in_the_future + 1];
        }

        var nav_html = "";
        if (!specific_day && modules.length > 1) {
            nav_html += '<ul class="nav nav-underline nav-fill fclt-sticky-nav" role="tablist">';
            for (var i = 0; i < modules.length; i++) {
                var safe_name = safe.htmlattr(modules[i].groupName).toLowerCase();
                nav_html += '<li class="nav-item" role="presentation">'
                          + '    <a id="tab-' + safe_name + '"'
                          + '       class="nav-link' + (i ? '' : ' active') + '"'
                          + '       href="#pane-' + safe_name +'"'
                          + '       role="tab"'
                          + '       aria-controls="pane-' + safe_name + '"'
                          + '       aria-selected="' + (i ? "true" : "false") + '"'
                          + '       data-bs-toggle="tab">'
                          +          safe.html(modules[i].groupName)
                          + '    </a>'
                          + '</li>';
            }
            nav_html += '</ul>';

            Response.Write(nav_html);
        }

        Response.write('<section' + (specific_day ? '' : ' class="tab-content"') + '>');
        if (specific_day) {
            var title = [
                L("lcl_date_yesterday"),
                L("lcl_date_today"),
                L("lcl_date_tomorrow")
            ][days_in_the_future + 1];
            Response.Write('<h2 class="section-title">' + safe.html(title) + '</h2>');
        }

        for (var i = 0; i < modules.length; i++) {
            var safe_name = safe.htmlattr(modules[i].groupName).toLowerCase();

            var resultsetParams = {
                sql: modules[i].sql,
                keyColumn: "item_key",
                linkColumn: fncolLink,
                headerColumn: function (oRs) { return fncolHeader(oRs, modules[i]); },
                contentColumn: function (oRs) { return fncolContent(oRs, modules[i]); },
                ID: "fac_list_m",
                showAll: true,
                noSearch: false,
                layout: "card"
            };
            if (!specific_day) {
                Response.Write('<div class="tab-pane fade' + (i ? '' : ' show active') + '" id="pane-' + safe_name + '" role="tabpanel" aria-labelledby="tab-' + safe_name + '">');
            } else {
                resultsetParams.wrapper = {
                    title: modules[i].groupName,
                    nested: true
                };
            }

            var rst = new ResultsetTable(resultsetParams);
            var cnt = rst.processResultset();

            if (!specific_day) {
                Response.Write('</div>');
            }
        }

        Response.Write('</section>');

    } else if (!specific_day) {
        Response.Write("<p>" + L("lcl_empty_rstable") + "</p>");
    }
    return records;
}
%>