<% /*
     $Revision$
     $Id$

    File:           scaffolding_m.inc

    Description:    Als scaffolding maar dan voor mobile
    Parameters:
    Context:
    Notes:          Ondersteunt list, edit en save
                    Heeft geen search, wrap of show
*/

var mode = getQParamSafe("mode", getQParam("id","")!=""?"edit":"list");
if (mode == "save" || mode == "delete")
    var JSON_Result = true;
%>
<!-- #include file="../Shared/common.inc" -->
<!-- #include file="./scaffolding_common.inc" -->
<% if (mode != "save" && mode != "delete") { %>
<% if (mode != "list") { %>
<!-- #include file="../Shared/calendar.inc" -->
<!-- #include file="../Shared/selector.inc" -->
<!-- #include file="../Shared/afdelingselector.inc" -->
<!-- #include file="../Shared/persoonselector.inc" -->
<!-- #include file="../Shared/kostenplaatsselector.inc" -->
<% } %>
<!-- #include file="../pda/iface.inc" -->
<!-- #include file="../pda/mobile.inc" -->
<!-- #include file="../Shared/data_recordset.inc" -->
<!-- #include file="../pda/resultset_table.inc" -->
<% } %>
<!-- #include file="../Shared/json2.js" -->
<!-- #include file="../api2/api2.inc" -->
<!-- #include file="./scaffolding_m_list.inc" -->
<!-- #include file="./scaffolding_m_edit.inc" -->
<!-- #include file="./scaffolding_save.inc" -->
<!-- #include file="./scaffolding_delete.inc" -->
<%
function scaffolding(model, scf_params)
{
    scf_params = scf_params || {};

    var imodel = getQParam("model", ""); // include model
    if (model.includes && imodel in model.includes)
    {
        model = model.includes[imodel].model;
        scf_params.transit = { "model": imodel };
    }

    // Originele URL voor IIRF rewrite
    var orgurl = Request.ServerVariables("HTTP_X_REWRITE_URL");
    if (orgurl.Count)
        scf_params.this_fullpath = String(orgurl).split("?")[0];
    else
    {
        var orgurl = Request.ServerVariables("HTTP_X_ORIGINAL_URL"); // Origineel van IIS Rewriter
        if (orgurl.Count)
            scf_params.this_fullpath = String(orgurl).split("?")[0];
        else
            scf_params.this_fullpath = String(Request.ServerVariables("SCRIPT_NAME"));
    }
    scf_params.this_path = scf_params.this_fullpath.substring(rooturl.length + 1);

    // Algemene normalizing
    def(model, "record_title",  model.record_name);
    def(model, "records_title",  model.records_name);
    for (var fld in model.fields)
        def(model.fields[fld], "label", fld)

    if ("includes" in model)
    {
        for (var inc in model.includes)
            def(model.includes[inc], "joinfield", model.fields.id.dbs);
    }

    def(model, mode, {});
    def(model, "list", {});
    if ("id" in model.fields && "name" in model.fields)
        def(model.list, "columns",  ["id", "name"]);
    else
        def(model.list, "columns",  []);

    def(model, "search", {});

    switch (mode)
    {
        case "search":
            scaffolding_m_search(model, scf_params);
            break;
        case "list":
            scaffolding_m_list(model, scf_params);
            break;
        case "edit":
            scaffolding_m_edit(model, scf_params);
            break;
        case "save":
            scaffolding_save(model, scf_params);
            break;
        case "delete":
            scaffolding_delete(model, scf_params);
            break;
    }
}

function scf_ROFIELD(model, fld, val, key)
{
    var field = model.fields[fld];
    var txt = val;
    if (txt === null || typeof txt === "undefined")
    {
        if (field.typ == "check")
            txt = 0;
        else
            return; // Altijd suppressEmpty
    }
    var title = "";
    if (typeof txt == "object")
    {
        if ("name" in txt) // Foreigns
        {
            title = "Key: " + txt["id"];
            txt = txt["name"];
        }
        else
            if (field.typ == "datetime")
                txt = toDateTimeString(txt);
            else if (field.typ == "time")
                txt = toTimeString(txt);
            else if (typeof txt == "date" || txt instanceof Date)
                txt = toDateString(txt);
    }
    var params = { title: title, contain: true };

    if (field.typ == "check" || field.typ == "check0")
        ROCHECKBOXTR("fldtxt", field.label, txt, params);
    else
    {
        if (field.label)
            Response.Write("<div class='fldtxt' style='margin-top:15px;'>" + field.label + ": " + safe.html(txt)+ "</div>");
        else
            Response.Write("<div class='fldtxt' style='margin-top:15px;'>" + safe.html(txt)+ "</div>");
    }
}
function scf_RWFIELD(field, fld, val)
{
    var iparams = {};
    if (field.len > 80)
        iparams.multi = true;
    if (field.foreign && typeof field.foreign == 'string') // TODO: foreign functie en foreignsql ondersteunen
    {
        var foreign = foreignKeyTable(field.foreign);

        var fupper = field.foreign.toUpperCase();
        if (fupper == "PRS_KOSTENPLAATS")
        {
            FCLTkostenplaatsselector(fld,
                   "sg" + fld,
                   user_key,
                   { label: field.label,
                     kostenplaatsKey: val?val.id:null,
                     filtercode: "AA"
                   });
        }
        else if (fupper == "PRS_PERSLID")
        {
            FCLTpersoonselector(fld,
                   "sg" + fld,
                   { label: field.label,
                     perslidKey: val?val.id:null
                   });
        }
        else if (fupper == "PRS_AFDELING")
        {
            FCLTafdelingselector(fld,
                   "sg" + fld,
                   { label: field.label,
                     departmentKey: val?val.id:null
                   });
        }
        else if (fupper.match(/ALG_RUIMTE|ALG_VERDIEPING|RES_RSV_RUIMTE|MLD_UITVOERENDE|CNT_CONTRACT/) || !foreign)
        {
            ROFIELDTR("fldtxt", field.label, "TODO: suggest met " + field.foreign);
        }
        else
        {
            var sql = "SELECT " + foreign.key
                    + "     , " + foreign.desc
                    + "  FROM " + foreign.tbl + " " + (foreign.alias||"")
                    + " ORDER BY 2";
            FCLTselector(fld, sql, { label: field.label,
                                     initKey: val?val.id:null,
                                     emptyOption: field.required?null:""
                                   });
        }
    }
    else if (field.LOV)
    {
        var sql = api2.splitLOV2sql(field.LOV);
        FCLTselector(fld, sql, { label: field.label,
                                 initKey: val?val.id:null,
                                 emptyOption: field.required?null:(field.LOVinit?field.LOVinit:null)
                               });
    }
    else if (field.typ == "check" || field.typ == "check0")
            RWCHECKBOXTR(fld, "fldcheck", field.label, val);
    else if (field.typ == "date")
            FCLTcalendar(fld, { label    : field.label,
                                datum    : val,
                                timeField: false,
                                initEmpty: !val,
                                initTimeEmpty: !val,
                                volgnr   : 1
                              });
    else if (field.typ == "datetime")
            FCLTcalendar(fld, { label    : field.label,
                                datum    : val,
                                timeField: true,
                                initEmpty: !val,
                                initTimeEmpty: !val,
                                timeStep : S("res_h")*60,
                                startTime: S("res_t1"),
                                endTime  : S("res_t2"),
                                volgnr   : 1
                              });
    else
        RWFIELD(fld, field.label, val, iparams);
}
%>
