<%@language = "javascript" %>

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

<%
var res_ruimtemodel = new model_res_ruimte();
var res_ruimte_key = getQParamInt("id", -1);

var hasModel = getQParam("model", "") != "" ;
if (!hasModel && mode == "list")
{
    var locatie_key = getQParamInt("locatiekey");
    var gebouw_key = getQParamInt("gebouwkey");
    var verdieping_key = getQParamInt("verdiepingkey");
    var ruimte_key = getQParamInt("ruimtekey");

    var onrgoed_key = -1;
    var niveau = "";
    if (ruimte_key > -1)
    {
        niveau = "ruimte";
        onrgoed_key = ruimte_key;
    }
    else if (verdieping_key > -1)
    {
        niveau = "verdieping";
        onrgoed_key = verdieping_key;
    }
    else if (gebouw_key > -1)
    {
        niveau = "gebouw";
        onrgoed_key = gebouw_key;
    }
    else if (locatie_key > -1)
    {
        niveau = "locatie";
        onrgoed_key = locatie_key;
    }

    if (niveau != "")
    {
        res_ruimtemodel.gparams = { GET: { wheres: ["v_locatie_filter.alg_"+niveau+"_key = " + onrgoed_key]}};
    }
}

if (!hasModel && (mode == "show" || mode == "edit"))
{
    delete res_ruimtemodel.fields.location;
}

var res_ruimtemodel_params = {
    "search": {
        "autosearch": false,
        "filters": [
            "name",
            "description",
            "location",
            "bookingdiscipline",
            "bookingconfigurations.bookingconfiguration"
        ],
        "transit": [
            "bookingdiscipline"
        ]
    },
    "list": {
        "columns": [
            "id",
            "location",
            "name",
            "description",
            "bookingdiscipline",
            "bookingconfigurations.configcapacity",
            "bookingconfigurations.configsetuptime",
            "physicalrooms.ruimtekey"
        ]
    },
    "show": {
        "requires": {
            "js": ["./res_ruimte.js"]
        },
        "buttons": []
    },
    "layout": {
        "block": [
            {
                "blockid": "scfLayout_1",
                "label": L("res_ruimte_info"),
                "icon": "fa-tag",
                "ishtmlsafe": true,
                "hidden": false,
                "buttons": [],
                "fields": [
                    "id",
                    "name",
                    "description",
                    "bookingdiscipline",
                    "sequence",
                    "minbezoekers",
                    "infourl",
                    "image",
                    "waitingarea",
                    "visitoraction",
                    "defaultstatus",
                    "friendlyname",
                    "catering"
                ]
            },
            {
                "blockid": "scfLayout_2",
                "label": L("res_ruimte_kosten"),
                "icon": "fa-euro-sign",
                "fields": [
                    "fixedprice",
                    "_intpricelabel",
                    "intpricehour",
                    "intpricemorning",
                    "intpriceafternoon",
                    "intpriceevening",
                    "intpriceday",
                    "_extpricelabel",
                    "extpricehour",
                    "extpricemorning",
                    "extpriceafternoon",
                    "extpriceevening",
                    "extpriceday"
                ]
            },
            {
                "blockid": "scfLayout_3",
                "label": L("res_ruimte_tijden"),
                "icon": "fa-clock",
                "fields": [
                    "startdate",
                    "expirationdate",
                    "open",
                    "close",
                    "blockstart",
                    "blockend",
                    "minduration",
                    "maxhorizon"
                ]
            },
            {
                "blockid": "scfLayout_4",
                "label": L("res_ruimte_external"),
                "icon": "fa-arrow-up-right-from-square",
                "fields": [
                    "externalid",
                    "onlineMeetingProviders",
                    "externalFlags",
                    "externalsyncdate",
                    "syncdate",
                    "graphhooksecret"
                ]
            }
        ]
    }
};

if (mode == "show" && res_ruimte_key > -1 && user.has("WEB_RESMGT")) {
    var sql = "SELECT res_ruimte_nr"
            + "  FROM res_ruimte"
            + " WHERE res_ruimte_key = " + res_ruimte_key;
    var oRs = Oracle.Execute(sql);
    res_ruimtemodel_params.show.buttons.unshift(
        { title: L("lcl_make_qrc"), tooltip: L("lcl_make_qrc_title"), action: "qrcode(" + res_ruimte_key + ", '" + safe.htmlattr(oRs("res_ruimte_nr").Value) + "')", icon: "fa-qrcode" }
    );
    oRs.Close();
}

if (mode == "show" && res_ruimte_key > -1 && (S("msgraph_sync_level") & 6)) {
    try {
        var fso = new ActiveXObject("Scripting.FileSystemObject");
        var configPath = Server.MapPath(custpath) + "\\Exchange\\exchange.config";
        if (fso.FileExists(configPath)) {
            var f = fso.OpenTextFile(configPath, 1);
            var config = eval("(" + f.ReadAll() + ")");
            f.Close();
        }
    } catch (e) {
        __DoLog(e.message, "#FF0000");
    }
    if (config) {
        var sql = "SELECT ''"
                + "  FROM res_rsv_ruimte rrr, res_ruimte_opstelling rro, res_ruimte rr"
                + " WHERE rrr.res_ruimte_opstel_key = rro.res_ruimte_opstel_key"
                + "   AND rro.res_ruimte_key = rr.res_ruimte_key"
                + "   AND rrr.res_rsv_ruimte_tot >= TRUNC(SYSDATE) - " + config.fullpast
                + "   AND rrr.res_rsv_ruimte_van <= TRUNC(SYSDATE) + " + config.fullfuture
                + "   AND rr.res_ruimte_extern_id IS NOT NULL"
                + "   AND rrr.res_rsv_ruimte_verwijder IS NULL"
                + "   AND rrr.res_rsv_ruimte_externnr IS NULL"
                + "   AND (   (     rrr.res_rsv_ruimte_externnr2 IS NULL"
                + "             AND rrr.res_rsv_ruimte_externsyncdate IS NULL)"
                + "        OR rrr.res_rsv_ruimte_externnr2 = '##PENDING##')"
                + "   AND (   rr.res_ruimte_startdatum IS NULL"
                + "        OR rr.res_ruimte_startdatum <= rrr.res_rsv_ruimte_van)"
                + "   AND (   rr.res_ruimte_vervaldatum IS NULL"
                + "        OR rr.res_ruimte_vervaldatum > rrr.res_rsv_ruimte_tot)"
                + "   AND rro.res_ruimte_key = " + res_ruimte_key;
        var oRs = Oracle.Execute(sql);
        if (!oRs.EoF) {
            res_ruimtemodel_params.show.buttons.unshift(
                { "title": L("res_ruimte_sync"), "tooltip": L("res_ruimte_sync_hint"), "id": "sync_outlook", "singlepress": true, "action": "sync_outlook", "icon": "fa-cloud-arrow-up" }
            );
        }
        oRs.Close();
    }
}

scaffolding(res_ruimtemodel, res_ruimtemodel_params);
%>
<% ASPPAGE_END(); %>
