<%@language = "javascript" %>

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

<%
var widget_key = getQParamInt("id", -1);

var this_model = new model_fac_widget();

if (mode == "show")
{
    var widget_type = "FAC"; /* Default */
    if (S("fac_usrrap_mode") == 1 && widget_key > -1)
    {
        var sql = "SELECT fac_widget_type"
                + "  FROM fac_widget"
                + " WHERE fac_widget_key = " + widget_key;
        var oRs = Oracle.Execute(sql);
        if (!oRs.eof)
            widget_type = oRs("fac_widget_type").Value;
        oRs.Close();
    }

    this_model.fields.linkurl.hidden_fld = true;
    this_model.fields.ref_key_MK.hidden_fld = true;
    this_model.fields.ref_key_MI.hidden_fld = true;
    this_model.fields.ref_key_NWS.hidden_fld = true;
    this_model.fields.ref_key_RAP.hidden_fld = true;
    this_model.fields.ref_key_FAQ.hidden_fld = true;
    this_model.fields.content.hidden_fld = true;
    /* Alleen de relevante laten zien */
    switch (widget_type) {
        case "MK": /* Menukop */
            this_model.fields.ref_key_MK.hidden_fld = false;
            break;
        case "MI": /* Menuitem */
            this_model.fields.ref_key_MI.hidden_fld = false;
            break;
        case "NWS": /* Nieuwspagina */
            this_model.fields.ref_key_NWS.hidden_fld = false;
            break;
        case "RAP": /* Rapportage */
            this_model.fields.ref_key_RAP.hidden_fld = false;
            break;
        case "RAW": /* HTML code */
            this_model.fields.content.hidden_fld = false;
            break;
        case "FAQ": /* Kennisbankitem */
            this_model.fields.ref_key_FAQ.hidden_fld = false;
            break;
        case "FAC": /* Internal URL */
        case "URL": /* External URL */
        case "IMG": /* Image */
        default: /* Default = FAC */
            this_model.fields.linkurl.hidden_fld = false;
            break;
    }
}
else if (mode == "list" || mode == "search")
{
    this_model.fields.wgt_onpage_count = {
        "dbs":  "wgt_onpage_count",
        "sql":    "(SELECT COUNT (1)"
                + "   FROM (  SELECT ''"
                + "             FROM fac_widget_onpage op"
                + "            WHERE op.fac_widget_key = fac_widget.fac_widget_key"
                + "         GROUP BY op.fac_widget_page_key))",
        "label": L("fac_widget_onpage_cnt"),
        "title": L("fac_widget_onpage_cnt_title"),
        "typ":  "number",
        "nosearch": true
    }
}

this_model.hook_pre_post = pre_save;
this_model.hook_pre_put = pre_save;

function pre_save(params, jsondata)
{
    /* Met readonly wordt het overbodige veld niet opgeslagen,
       anders wordt in de SQL 2x hetzelfde veld ge-insert */
    this_model.fields.ref_key_MK.readonly = true;
    this_model.fields.ref_key_MI.readonly = true;
    this_model.fields.ref_key_NWS.readonly = true;
    this_model.fields.ref_key_RAP.readonly = true;
    this_model.fields.ref_key_FAQ.readonly = true;

    switch (jsondata.type) {
        case "MK":
            this_model.fields.ref_key_MK.readonly = false;
            break;
        case "MI":
            this_model.fields.ref_key_MI.readonly = false;
            break;
        case "NWS":
            this_model.fields.ref_key_NWS.readonly = false;
            break;
        case "RAP":
            this_model.fields.ref_key_RAP.readonly = false;
            break;
        case "FAQ":
            this_model.fields.ref_key_FAQ.readonly = false;
            break;
    }
}

this_model.hook_pre_delete = function (params, key)
{
    // Als ik een 'save-as-widget'-widget verwijder, verwijder dan (hier) ook het onderliggende fac_menu-record
    var sql = " DELETE FROM fac_menu"
            + "   WHERE fac_menu_altgroep = 9"
            + "     AND fac_menu_key = "
            + "         (SELECT REGEXP_SUBSTR (fac_widget_url, '.*has_filters=1&' || 'tab_menu_key=(\\d+)$', 1, 1, 'i', 1)"
            + "            FROM fac_widget"
            + "           WHERE fac_widget_key = " + key
            + "             AND fac_widget_type = 'FAC'"
            + "             AND REGEXP_LIKE (fac_widget_url, '.*has_filters=1&' || 'tab_menu_key=\\d+$'))";
    Oracle.Execute(sql);
}

var scf_params = {
    "search": {
        "autosearch": true,
        "filters" : [
            "name",
            "title"
        ]
    },
    "edit": {
        "requires": {
            "js": ["./fac_widget.js"]
        }
    },
    "list": {
        "columns": [
            "name",
            "type",
            "title",
            "linkurl",
            "public",
            "refreshtime",
            "wgt_onpage_count",
            "created"
        ],
        "orderby": [
            "id"
        ]
    },
    "layout": {
        "miscellaneous": {
            "label": L("fac_widget"),
            "icon" : "fa-expand-wide"
        }
    }
}

if (S("fac_usrrap_mode") == 0)
{
    scf_params.list.columns.splice(4, 1); // Public
    scf_params.list.columns.splice(1, 1); // Type
}

scaffolding(this_model, scf_params);

%>
<% ASPPAGE_END(); %>
