<%@language = "javascript" %>

<!-- #include file="../scf/scaffolding.inc" -->
<!-- #include file="../api2/model_generic.inc" -->

<%
    settings.overrule_setting("qp_maxrows", 300); // Toon alle 160 (2025) klanten direct

    var table = "fac_task";

    var model = model_generic(table, "WEB_FACTAB");

    api2.generic_REST(model);
    // Voorkennis:
    model.fields["fac_cust_key"].typ = "key";
    model.fields["fac_cust_key"].foreign = { tbl: "fac_cust", key: "fac_cust_key", desc: "fac_cust_customerid"};
    model.fields["fac_cust_key"].insertonly = true;
    model.fields["name"].insertonly = true;
    model.fields["fac_task_nextrun"].multiedit = true;
    model.fields["fac_task_flags"].multiedit = true;
    model.fields["fac_task_flags"].label = "flags (+1=enabled)"; // in de lijst
    model.fields["fac_task_flags"].defaultvalue = 1;
    model.fields["fac_task_flags"].bits = [
                {
                    "name": "",
                    "label": "",
                    "showasgroup": false,
                    "typ": "check",
                    "mask": 1,
                    "radios": [
                        {
                            "name": "enabled",
                            "label": "Enabled",
                            "mask": 1,
                            "defaultvalue": 0
                        }
                    ]
                }
            ]

    for (var fld in model.fields)
    {
        model.fields[fld].label = model.fields[fld].label.replace("fac_task_", ""); // iets kortere labels
    }

    var sql = "SELECT fac_version_tasker_date"
            + "  FROM fac_version";
    var oRs = Oracle.Execute(sql);
    var dt = oRs("fac_version_tasker_date").Value;
    oRs.Close();

    if (dt != null)
        model.records_title = model.records_name + " (running {0})".format(toDateTimeString(dt, true, null, true));
    else
        model.records_title = model.records_name + ' (Tasker not running?)';

    scaffolding(model, {
        "search": {
            "autosearch": true,
            "filters": [
                "name",
                "fac_cust_key"
            ]
        },
        "list": {
            "columns": [
                "id",
                "fac_cust_key",
                "name", // is fac_task_code
                "fac_task_nextrun",
                "fac_task_lastrun",
                "fac_task_flags"
            ],
            "orderby": [
                "fac_cust_key",
                "name"
            ],
            rowClass: function(oRs)
            {
                if ((oRs.Fields("fac_task_flags").Value & 1) == 0) // disabled
                {
                    return "lowlight";
                }
                return null;
            }

        },
        "edit": {
            "modal": true
        },
        "layout": {
            "miscellaneous": {
                "icon": "fa-clock"
            }
        }
    });
%>
<% ASPPAGE_END(); %>
