<%@language = "javascript" %>

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

<%
    var table = "fac_notificatie";

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


    var puo_const = { STATUS_PORTAL: 1 // Niet via Putorders
                    , STATUS_EMAIL: 2
                    , STATUS_SMS:  4
                    , STATUS_POPUP: 8 // Niet via Putorders
                    , STATUS_SUMMARY_XSL: 16 // SMS/Email subject via XSL?
                    , STATUS_APPPUSH: 32 // Via Google Firebase
                    , STATUS_CUST: 64
                    };

    api2.generic_REST(model);
    // Voorkennis:
    model.fields["prs_perslid_key_sender"].typ = "key";
    model.fields["prs_perslid_key_sender"].foreign = "PRS_PERSLID";
    model.fields["prs_perslid_key_receiver"].typ = "key";
    model.fields["prs_perslid_key_receiver"].foreign = "PRS_PERSLID";
    model.fields["fac_notificatie_status"].bits =
             [
                {
                    "name": "",
                    "label": "",
                    "typ": "check",
                    "mask": 127,
                    "radios": [
                        {
                            "name": "flag_portal",
                            "label": "Portal",
                            "mask": puo_const.STATUS_PORTAL,
                            "defaultvalue": 0
                        },
                        {   "name": "flag_email",
                            "label": "E-mail",
                            "mask": puo_const.STATUS_EMAIL,
                            "defaultvalue": 0
                        },
                        {   "name": "flag_sms",
                            "label": "SMS",
                            "mask": puo_const.STATUS_SMS,
                            "defaultvalue": 0
                        },
                        {   "name": "flag_popup",
                            "label": "Popup",
                            "mask": puo_const.STATUS_POPUP,
                            "defaultvalue": 0
                        },
                        {   "name": "flag_summaryxsl",
                            "label": "Summary-XSL",
                            "mask": puo_const.STATUS_SUMMARY_XSL,
                            "defaultvalue": 0
                        },
                        {   "name": "flag_apppush",
                            "label": "APP Push",
                            "mask": puo_const.STATUS_APPPUSH,
                            "defaultvalue": 0
                        },
                        {   "name": "flag_cust",
                            "label": "Custom",
                            "mask": puo_const.STATUS_CUST,
                            "defaultvalue": 0
                        }
                    ]
                }
            ]

        model.hook_pre_show = function (obj, fld)
        {
            if (obj.fac_srtnotificatie_key)
            {
                var sql = "SELECT fac_srtnotificatie_url, fac_srtnotificatie_xmlnode"
                        + "  FROM fac_srtnotificatie"
                        + " WHERE fac_srtnotificatie_key = " + obj.fac_srtnotificatie_key.id;
                var oRs = Oracle.Execute(sql);
                if (!oRs.eof && oRs("fac_srtnotificatie_url").Value)
                {
                    var xmlnode = oRs("fac_srtnotificatie_xmlnode").Value;
                    model.fields["fac_notificatie_refkey"].label += " (" + xmlnode + ")";
                    if (xmlnode == 'reservering')
                        model.fields["fac_notificatie_extrakey"].infoPointer = { Url: "appl/" + oRs("fac_srtnotificatie_url").Value };
                    else
                        model.fields["fac_notificatie_refkey"].infoPointer = { Url: "appl/" + oRs("fac_srtnotificatie_url").Value };
                }
                oRs.Close();
            }
        }

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

    model.records_title = model.records_name + " (tasker is {0}@{1})".format(S("tsk_master_schema"), Oracle.RealConnection.Properties("Data Source Name"));
	// Waarschuwen als puo_flags uit staat
    var txt = [];
    if ((S("puo_flags") & 1) != 1)
    {
        txt.push("no notifications to people");
    }
    if ((S("puo_flags") & 2) != 2)
    {
        txt.push("no notifications to systems");
    }
    if (txt.length)
    {
        model.records_title = model.records_title + " (puo_flags is {0}, {1})".format(S("puo_flags"), txt.join(", "));
    }

    scaffolding(model, {
        "search": {
            "autosearch": true,
            "filters": [
                "fac_notificatie_naam",
                "fac_srtnotificatie_key",
                "fac_notificatie_refkey"
            ]
        },
        "list": {
            "columns": [
                "id",
                "fac_notificatie_notbefore",
                "fac_notificatie_status",
                "fac_notificatie_datum",
                "fac_srtnotificatie_key",
                "fac_notificatie_refkey",
                "fac_notificatie_extrakey",
                "fac_notificatie_naam",
                "fac_notificatie_receiver_email",
                "fac_notificatie_systeemadres",
                "prs_bedrijfadres_key",
                "fac_notificatie_failcount",
                "fac_notificatie_faildatum",
                "fac_notificatie_failmessage"
            ],
            "orderby": [
                "fac_notificatie_notbefore",
                "id"
            ],
            rowClass: function(oRs)
            {
                if ((oRs.Fields("fac_notificatie_status").Value == 0 || oRs.Fields("fac_notificatie_status").Value == 16) &&
                    oRs.Fields("fac_notificatie_systeemadres").Value == null &&
                    oRs.Fields("prs_bedrijfadres_key").Value == null &&
                    oRs.Fields("fac_notificatie_queue").Value == "DEFAULT")
                {
                    return "lowlight"; // valt niets te versturen
                }
                return null;
            }

        },
        "show": {
            "buttons": [
                { icon: "fa-send", title: "Immediate", tooltip: "Send immediate", action: "send_immediate" }
            ],
            "requires": {
                  js: ["./fac_notificatie.js"]
                }
        },
        "layout": {
            "miscellaneous": {
                "icon": "fa-bell"
            }
        }
    });
%>
<% ASPPAGE_END(); %>
