<%@language = "javascript" %>
<% /*
     $Revision$
     $Id$

    File:        pda/notitie.asp
    Description: Toont de notities bij melding
                 en voegt nieuwe notitie toe.
    Parameters:
    Context:     Vanuit mobile device short url
    Note:
*/
var pnode    = getQParamSafe("node");
var orgnode  = getQParamSafe("orgnode", ""); // Als ik reply op een opdracht notitie vanuit de melding, wil ik alsnog daarna terug naar de melding
var pkey     = getQParamInt("key");
var plvl     = getQParamSafe("lvl", "");
var org_key  = getQParamInt("org_key", -1);
var urole    = getQParamSafe("urole", "fe");
var noteedit = getQParamInt("edit", 0) == 1;
var notekey  = getQParamInt("notekey", -1);
var parent_note_key = getQParamInt("parent_note_key", -1);
var LOCKED_USER_OK = { "xmlnode": pnode, "key": pkey };%>

<!-- #include file="../Shared/common.inc" -->
<!-- #include file="../Shared/selector.inc" -->
<!-- #include file="../alg/alg.inc" -->
<!-- #include file="../mld/mld.inc" -->
<!-- #include file="../cnt/cnt.inc" -->
<!-- #include file="../ins/ins.inc" -->
<!-- #include file="./resultset_table.inc" -->
<!-- #include file="./mobile.inc" -->
<!-- #include file="./iface.inc" -->
<!-- #include file="../Shared/notes.inc" -->

<%
FCLTHeader.Requires({ js: ["notes_m.js"] });

var qrc      = getQParamInt("qrc", 0) != 0;
var tracking = getQParamInt("tracking", 0) == 1; // tracking erbij tonen?
var pnote = { note_key: notekey,
              subject: ""
            };

var timestamp_notes = new Date();
var sql = "";
var fac_note_group_key, note_module;
var selector_sql = "(SELECT fac_note_group_naam"
                 + "   FROM fac_note_group fng"
                 + "  WHERE n.fac_note_group_key = fng.fac_note_group_key"
                 + "    AND fac_note_group_vervaldatum IS NULL"
                 + ") fac_note_group_naam";

if (pnode == "gebouw" || pnode == "locatie")
{
    pnode  = "onroerendgoed";
}
    switch (pnode)
    {
        case 'melding':
            var mfe = mld.func_enabled_melding (pkey);
            // Heb ik rechten om notities te zien
            user.auth_required_or_abort(mfe.canReadNotes);

            pnote.canwritenotes = mfe.canWriteNotes;

            note_module = "MLD";
            var action = "../mld/mld_edit_note_save.asp?urole="+urole+"&mld_key=" +pkey;

            var mld_info = mld.mld_melding_info(pkey);
            lcl.set_dialect(mld_info.srtdisc_key, "MLD_SRTDISCIPLINE_KEY");
            pnote.subject = mld_info.prefix + pkey;

            if (pnote.canwritenotes && (notekey > 0 || !noteedit))
            {
                var sql_n = "SELECT mld_melding_note_key"
                          + "     , prs_perslid_key"
                          + "     , mld_melding_note_omschrijving"
                          + "     , mld_melding_note_flag"
                          + "     , fac_note_group_key"
                          + "  FROM mld_melding_note"
                          + " WHERE mld_melding_key =" + pkey
                          + " ORDER BY mld_melding_note_aanmaak DESC";
                var oRs_n = Oracle.Execute(sql_n);
                if (!oRs_n.eof)
                {
                    fac_note_group_key = oRs_n("fac_note_group_key").Value;
                    if (oRs_n("prs_perslid_key").value == user_key)
                    {
                        pnote.note_key = oRs_n("mld_melding_note_key").Value;
                        pnote.omschrijving = oRs_n("mld_melding_note_omschrijving").Value;
                        pnote.note_flag = oRs_n("mld_melding_note_flag").Value;
                    }
                }
                oRs_n.Close();
            }

            // Melding notes
            var sql = "SELECT n.mld_melding_note_aanmaak note_aanmaak"
                    + ",      n.mld_melding_note_key note_key"
                    + ",      n.mld_melding_note_parent_key note_parent_key"
                    + ",      pf.prs_perslid_key"
                    + ",      pf.prs_perslid_naam_friendly"
                    + ",      n.mld_melding_note_omschrijving note_omschrijving"
                    + ",      mld_melding_note_flag note_flag"
                    + ",      NULL fac_srtnotificatie_code"
                    + ",      1 regeltype"
                    + ",      null prefix"
                    + ",      null opdr_image"
                    + ",      null opdr_key"
                    + ",      null uitv_key"
                    + ",      null intern"
                    + ", " + selector_sql
                    + "  FROM mld_melding_note n, prs_v_perslid_fullnames_all pf"
                    + " WHERE n.prs_perslid_key = pf.prs_perslid_key (+)"
                    + "   AND n.mld_melding_key = " + pkey
                    + (mfe.haveOnlyFErights ? " AND BITAND(mld_melding_note_flag, 1) = 1" : "");
            var sqls = [sql];

            // Opdracht notes
            if (mld_info.stdm_notes & 4)
            {
                selector_sql = "(SELECT fac_note_group_naam"
                                + "  FROM fac_note_group fng"
                                + " WHERE nop.fac_note_group_key = fng.fac_note_group_key"
                                + "   AND fac_note_group_vervaldatum IS NULL) fac_note_group_naam";
                sql = "SELECT nop.mld_opdr_note_aanmaak note_aanmaak"
                    + "     , nop.mld_opdr_note_key note_key"
                    + "     , nop.mld_opdr_note_parent_key note_parent_key"
                    + "     , pf.prs_perslid_key"
                    + "     , pf.prs_perslid_naam_friendly"
                    + "     , nop.mld_opdr_note_omschrijving note_omschrijving"
                    + "     , nop.mld_opdr_note_flag note_flag"
                    + "     , NULL"
                    + "     , 2 regeltype"
                    + "     , mld_opdr_bedrijfopdr_volgnr prefix"
                    + "     , ty.mld_typeopdr_image opdr_image"
                    + "     , o.mld_opdr_key opdr_key"
                    + "     , o.mld_uitvoerende_keys uitv_key"
                    + "     , mu.intern intern"
                    + ", " + selector_sql
                    + "  FROM mld_opdr_note nop, prs_v_perslid_fullnames_all pf, mld_opdr o, mld_typeopdr ty, mld_v_uitvoerende mu"
                    + " WHERE nop.prs_perslid_key = pf.prs_perslid_key (+)"
                    + "   AND o.mld_opdr_key = nop.mld_opdr_key"
                    + "   AND ty.mld_typeopdr_key = o.mld_typeopdr_key"
                    + "   AND nop.mld_opdr_key IN (SELECT mld_opdr_key FROM mld_opdr WHERE mld_melding_key = " + pkey + ")"
                    + "   AND o.mld_uitvoerende_keys = mu.mld_uitvoerende_key"
                    + (mfe.haveOnlyFErights? " AND BITAND(mld_opdr_note_flag, 1) = 1" : "");
                sqls.push(sql);
            }

            if (tracking) // tracking info erbij?
            {   // Melding
                sql = "SELECT tr.fac_tracking_datum"
                    + "     , tr.fac_tracking_key"
                    + "     , NULL"
                    + "     , tr.prs_perslid_key"
                    + "     , pf.prs_perslid_naam_friendly"
                    + "     , tr.fac_tracking_oms note_omschrijving"
                    + "     , NULL note_flag"
                    + "     , str.fac_srtnotificatie_code"
                    + "     , 1 regeltype"
                    + "     , NULL prefix"
                    + "     , NULL opdr_image"
                    + "     , NULL opdr_key"
                    + "     , NULL uitv_key"
                    + "     , NULL intern"
                    + "     , NULL fac_note_group_naam"
                    + "  FROM fac_tracking tr, fac_srtnotificatie str, prs_v_perslid_fullnames_all pf"
                    + " WHERE tr.fac_srtnotificatie_key = str.fac_srtnotificatie_key"
                    + "   AND tr.prs_perslid_key = pf.prs_perslid_key (+)"
                    + "   AND str.fac_srtnotificatie_code <> 'MLDNOT'"  // beetje dubbelop
                    + "   AND tr.fac_tracking_refkey = " + pkey
                    + "   AND str.fac_srtnotificatie_xmlnode IN ('melding')";
                sqls.push(sql);

                // Opdracht
                if (mld_info.stdm_notes & 4)
                {
                    sql = "SELECT tr.fac_tracking_datum"
                        + "     , tr.fac_tracking_key"
                        + "     , NULL"
                        + "     , tr.prs_perslid_key"
                        + "     , pf.prs_perslid_naam_friendly"
                        + "     , tr.fac_tracking_oms note_omschrijving"
                        + "     , NULL note_flag"
                        + "     , str.fac_srtnotificatie_code"
                        + "     , 2 regeltype"
                        + "     , mld_opdr_bedrijfopdr_volgnr prefix"
                        + "     , ty.mld_typeopdr_image opdr_image"
                        + "     , o.mld_opdr_key opdr_key"
                        + "     , NULL uitv_key"
                        + "     , NULL intern"
                        + "     , NULL fac_note_group_naam"
                        + "  FROM fac_tracking tr, fac_srtnotificatie str, prs_v_perslid_fullnames_all pf, mld_opdr o, mld_typeopdr ty"
                        + " WHERE tr.fac_srtnotificatie_key = str.fac_srtnotificatie_key"
                        + "   AND o.mld_opdr_key = tr.fac_tracking_refkey"
                        + "   AND tr.prs_perslid_key = pf.prs_perslid_key (+)"
                        + "   AND ty.mld_typeopdr_key = o.mld_typeopdr_key"
                        + "   AND tr.fac_tracking_refkey IN (SELECT mld_opdr_key FROM mld_opdr WHERE mld_melding_key = " + pkey + ")"
                        + "   AND str.fac_srtnotificatie_xmlnode IN ('opdracht')";
                    sqls.push(sql);
                }
            }

            sql = "SELECT * FROM ("+ sqls.join(" UNION ") + ")";

            break;
        case 'opdracht':
            var this_opdr =  mld.func_enabled_opdracht(pkey); // Wat heb ik zoal aan rechten op deze opdracht?
            if (this_opdr.myRole === 1) {
                var mld_info = this_opdr.mld_info;
                var mfe = mld.func_enabled_melding(mld_info.mld_key);
                user.auth_required_or_abort(mfe.canReadNotes);
                pnote.canwritenotes = mld_info.canWriteNotes;
            } else {
                user.auth_required_or_abort(this_opdr.canShowOpdrNote);
                pnote.canwritenotes = this_opdr.canEditOpdrNote;
            }

            note_module = "ORD";
            var action = "../mld/opdr_edit_note_save.asp?opdr_key=" +pkey;

            var mld_opdr = mld.mld_opdr_info(pkey);
            lcl.set_dialect(mld_opdr.opdr_type, "MLD_TYPEOPDR_KEY");

            pnote.subject = L("lcl_opdr_info") + " " + mld_opdr.mld_key + "/" + mld_opdr.mld_opdr_bedrijfopdr_volgnr;

            var sql = "SELECT n.mld_opdr_note_aanmaak note_aanmaak"
                    + "     , n.mld_opdr_note_key note_key"
                    + "     , n.mld_opdr_note_parent_key note_parent_key"
                    + "     , pf.prs_perslid_key"
                    + "     , pf.prs_perslid_naam_friendly"
                    + "     , n.mld_opdr_note_omschrijving note_omschrijving"
                    + "     , n.mld_opdr_note_flag note_flag"
                    + "     , NULL fac_srtnotificatie_code"
                    + ", " + selector_sql
                    + "  FROM mld_opdr_note n"
                    + "     , prs_v_perslid_fullnames_all pf"
                    + " WHERE n.prs_perslid_key = pf.prs_perslid_key (+)"
                    + "   AND n.mld_opdr_key = " + pkey
                    + (this_opdr.myRole === 4 ? " AND BITAND(n.mld_opdr_note_flag, 4) = 4" : "");
            var sqls = [sql];

            if (tracking) // tracking info erbij?
            {
                var sql = "SELECT tr.fac_tracking_datum"
                        + "     , tr.fac_tracking_key note_key"
                        + "     , NULL"
                        + "     , tr.prs_perslid_key"
                        + "     , pf.prs_perslid_naam_friendly"
                        + "     , tr.fac_tracking_oms note_omschrijving"
                        + "     , NULL"
                        + "     , str.fac_srtnotificatie_code"
                        + "     , NULL fac_note_group_naam"
                        + "  FROM fac_tracking tr"
                        + "     , fac_srtnotificatie str"
                        + "     , prs_v_perslid_fullnames_all pf"
                        + " WHERE tr.fac_srtnotificatie_key = str.fac_srtnotificatie_key"
                        + "   AND tr.prs_perslid_key = pf.prs_perslid_key (+)"
                        + "   AND (tr.fac_tracking_refkey = " + opdr_key
                        + "   AND str.fac_srtnotificatie_xmlnode IN ('opdracht'))";
                sqls.push(sql);
            }

            sql = "SELECT * FROM ("+ sqls.join(" UNION ") + ")";

            break;
        case 'contract':
            var this_cnt = cnt.func_enabled_contract(pkey); // Wat heb ik zoal aan rechten op dit contract?
            user.auth_required_or_abort(this_cnt.canReadNotes);

            pnote.canwritenotes = false;

            var cnt_info = cnt.cnt_contract_info(pkey);
            note_module = "CNT";
            pnote.subject = L("lcl_cnt_contract") + " " + cnt_info.nummer_intern+ (cnt_info.versie == null || cnt_info.versie == ""? "" : "." + cnt_info.versie);

            var sql = "SELECT n.cnt_contract_note_aanmaak note_aanmaak"
                    + "     , n.cnt_contract_note_key note_key"
                    + "     , pf.prs_perslid_key"
                    + "     , pf.prs_perslid_naam_friendly"
                    + "     , n.cnt_contract_note_omschrijving note_omschrijving"
                    + "     , NULL fac_srtnotificatie_code"
                    + "  FROM cnt_contract_note n"
                    + "     , prs_v_perslid_fullnames_all pf"
                    + " WHERE n.prs_perslid_key = pf.prs_perslid_key (+)"
                    + "   AND n.cnt_contract_key = " + pkey;
            break;
        case 'deel':
            var  this_ins = ins.func_enabled_deel(pkey); // Wat heb ik zoal aan rechten op deze opdracht?
            user.auth_required_or_abort(this_ins.canChange);

            pnote.canwritenotes = this_ins.canChange;
            note_module = "INS";
            var action = "../ins/ins_deel_note_save.asp?ins_key=" +pkey;
            sql = "SELECT ins_deel_omschrijving"
                + "     , ins_discipline_key"
                + "  FROM ins_deel d"
                + " WHERE ins_deel_key = " + pkey;
            oRs = Oracle.Execute(sql);
            pnote.subject = oRs("ins_deel_omschrijving").Value;
            lcl.set_dialect(oRs("ins_discipline_key").Value, "INS_DISCIPLINE_KEY");
            oRs.Close();

            sql = "SELECT n.ins_deel_note_aanmaak note_aanmaak"
                + "     , n.ins_deel_note_key note_key"
                + "     , pf.prs_perslid_key"
                + "     , pf.prs_perslid_naam_friendly"
                + "     , n.ins_deel_note_omschrijving note_omschrijving"
                + "     , NULL fac_srtnotificatie_code"
                + "  FROM ins_deel_note n"
                + "     , prs_v_perslid_fullnames_all pf"
                + " WHERE n.prs_perslid_key = pf.prs_perslid_key (+)"
                + "   AND n.ins_deel_key = " + pkey;
            break;
        case "onroerendgoed":
            var this_alg = alg.func_enabled(pkey, plvl);
            user.auth_required_or_abort(this_alg.canReadNotes);
            pnote.canwritenotes = this_alg.canWriteNotes;
            note_module = "ALG";
            var action = "../alg/alg_edit_note_save.asp?alg_key=" + pkey + "&alg_typ=" + plvl;
            var sql = "SELECT x.alg_onroerendgoed_omschrijving"
                    + "  FROM (SELECT o.alg_onroerendgoed_keys, o.alg_type, o.alg_onroerendgoed_omschrijving"
                            + "  FROM alg_v_allonrgoed_gegevens o"
                            + " UNION "
                            + "SELECT l.alg_locatie_key, 'L', l.alg_locatie_omschrijving"
                            + "  FROM alg_locatie l"
                    + "       ) x"
                    + " WHERE x.alg_onroerendgoed_keys = " + pkey
                    + "   AND x.alg_type = " + safe.quoted_sql(plvl);
            var oRs = Oracle.Execute(sql);
            pnote.subject = oRs("alg_onroerendgoed_omschrijving").Value;
            oRs.Close();

            sql = "SELECT n.alg_onroerendgoed_note_aanmaak note_aanmaak"
                + "     , n.alg_onroerendgoed_note_key note_key"
                + "     , pf.prs_perslid_key"
                + "     , pf.prs_perslid_naam_friendly"
                + "     , n.alg_onroerendgoed_note_omschrijving note_omschrijving"
                + "     , NULL fac_srtnotificatie_code"
                + "  FROM alg_onroerendgoed_note n"
                + "     , prs_v_perslid_fullnames_all pf"
                + " WHERE n.prs_perslid_key = pf.prs_perslid_key(+)"
                + "   AND n.alg_onroerendgoed_key = " + pkey
                + "   AND n.alg_onroerendgoed_typ = " + safe.quoted_sql(plvl);
            break;
    }
    sql = sql + " ORDER BY 1 DESC";

    function fnNoteText (oRs)
    {
        var val = "";
        if (pnode == "melding" || pnode == "opdracht")
        {
            var note_parent_key = oRs("note_parent_key").Value || -1;
            if (note_parent_key > -1)
            {
                var mod = (pnode == "opdracht" ? "O" : (oRs("regeltype").value == 2 ? "O" : "M"));
                val += notes.inlineReplyNote(note_parent_key, mod, { "mobile": 1 });
            }
            if (oRs("fac_note_group_naam").Value)
            {
                val += "<div class='listbodykop'>" + safe.html(oRs("fac_note_group_naam").Value) + "</div>";
            }
        }
        val += "<div class='listbodytext'>" + safe.notehtml(oRs("note_omschrijving").value || "", note_module, oRs("note_key").Value) + "</div>";


        return  val;
    }

    function fncolLink (oRs)
    {
        var url = "";
        if (oRs("note_key").Value == pnote.note_key)
            url = "javascript:note_change()";

        return url;
    }

    var getCanReply = function(oRs)
    {
        if (last_note_key && last_note_key == oRs("note_key").Value && typeof canReply != "undefined")
            return canReply;

        var canReply = false;
        var last_note_key = oRs("note_key").Value;
        if (oRs("note_key").Value != pnote.note_key) // Alleen indien geen fncolLink (hierboven)
        {
            if (pnode == "melding")
            {
                var note_zichtbaarFE    = oRs("note_flag").Value & 1;
                var note_unanswered     = oRs("note_flag").Value & 32;
                var isvanOpdracht       = oRs("regeltype").value == 2;
                var eigenMelding        = user.isCollega(mld_info.melder_key);
                var srtnoti             = oRs("fac_srtnotificatie_code").Value;
                var canReply = (mfe.haveOnlyFErights && (note_unanswered || mld_info.stdm_notes == 5) && oRs("prs_perslid_key").Value != user_key && isvanOpdracht && eigenMelding && !srtnoti)
                            || !mfe.haveOnlyFErights;
                canReply = canReply && notekey == -1;
            }
            else if (pnode == "opdracht")
            {
                var note_zichtbaarUITV  = oRs("note_flag").Value & 4;
                var canReply = this_opdr.myRole === 2; // Alleen FOBO
                canReply = canReply && notekey == -1; // In edit-mode kunnen we niet aanpassen op welke notitie dit een reply is
            }
        }
        return canReply;
    }

    function fncolReply(oRs)
    {
        var action = "";
        if (getCanReply(oRs))
        {
            if (pnode == "melding")
            {
                action = "javascript:note_reply("
                       + oRs("note_key").Value + ", " + (oRs("regeltype").value == 2 ? oRs("opdr_key").value : pkey) + ", '" + (oRs("regeltype").value == 2 ? "opdracht" : "melding") + "')";
            }
            else if (pnode == "opdracht")
            {
                action = "javascript:note_reply(" + oRs("note_key").Value + ", " + pkey + ", 'opdracht')";
            }
        }
        return action;
    }

    function fnrowIcon(oRs)
    {
        var icon = "";
        if (getCanReply(oRs))
            icon = "fa-reply";
        return icon;
    }

    function fnHeader (oRs)
    {
        var val = toDateTimeString(oRs("note_aanmaak").Value);
        if ((pnode == "melding"  && !(oRs("note_flag").value & 1)) ||
            (pnode == "opdracht" && !(oRs("note_flag").value & 4)))
            val = val + " " + I("fa-lock");
        return val;
    }

    function back_reference()
    {
        var url = "";
        switch (pnode)
        {
            case "melding": url = "melding.asp?mld_key=" + pkey; break; // Terug naar de melding.
            case "opdracht":
                if (!orgnode || orgnode == "opdracht")
                {
                    url = "order.asp?opdr_key=" + pkey; // Terug naar de opdracht.
                    break;
                }
                else // if (orgnode == "melding")
                {
                    url = "melding.asp?mld_key=" + org_key; // Terug naar de melding.
                    break;
                }
            case "contract": url = "contract.asp?cnt_key=" + pkey; break; // Terug naar het contract.
            case "deel":     url = "ins_deel.asp?ins_key=" + pkey; break; // Back to show object.
            case "onroerendgoed": if (plvl == "G") url = "gebouw.asp?bld_key="   + pkey; // Terug naar het gebouw.
                                  if (plvl == "L") url = "locatie.asp?loc_key="  + pkey; // Terug naar de locatie.
                                  break;
        }
        return url;
    }

%>
<html>
  <head>
<% FCLTMHeader.Generate({}); %>
    <script>
        window.module = "<%=note_module.toLowerCase()%>";
        window.module_key = <%=pkey%>;
<%      notes.note_common_js(notekey); %>
    </script>
  </head>
  <body>
<%

if (!noteedit)
{
    PAGE_START({id: "note-1-"+pkey});
%>
<script>
    function note_add()
    {
        var purl = "notitie.asp?node=<%=pnode%>&urole=<%=urole%>"
                 + "&key=<%=pkey%>&lvl=<%=plvl%>"
                 + "&edit=1&notekey=-1";
        window.location.href = purl;
    }
    function note_change()
    {
        var purl = "notitie.asp?node=<%=pnode%>&urole=<%=urole%>"
                 + "&key=<%=pkey%>&lvl=<%=plvl%>"
                 + "&edit=1&notekey=<%=pnote.note_key%>";
        window.location.href = purl;
    }
    function note_reply(parent_note_key, module_key, node)
    {
        var purl = "notitie.asp?node=" + node + "&urole=<%=urole%>"
                 + "&key=" + module_key
                 + "&edit=1&notekey=-1&parent_note_key=" + parent_note_key
                 + "&orgnode=<%=pnode%>&org_key=<%=pkey%>";
        window.location.href = purl;
    }
    function note_back()
    {
        window.location.href = "<%=back_reference()%>";
    }
</script>
<%
      var p_useraction = {icon: "fa-arrow-left", action: "note_back()", label: L("lcl_back")};
      HEADER({title: L("lcl_mobile_notes") + " " + pnote.subject, useraction: p_useraction});
      CONTENT_START();
        CONTAINER_START()
        if (pnote.canwritenotes)
        {
            BUTTON(L("lcl_add"), {click: "note_add()", icon: "fa-plus" });
        }
        CONTAINER_END();

        var rst = new ResultsetTable({ sql: sql,
                                       keyColumn: "note_key",
                                       linkColumn: fncolLink,
                                       click: fncolReply,
                                       rowIcon: fnrowIcon,
                                       ID: "notetable",
                                       headerColumn: fnHeader,
                                       detailColumn: "prs_perslid_naam_friendly",
                                       subheaderColumn: fnNoteText,
                                       noPrint: true,
                                       noSearch: true,
                                       mobile: true,
                                       showAll: true
                                     });
        var cnt = rst.processResultset();

      CONTENT_END();
      FOOTER();
    PAGE_END();
}
else
{
    PAGE_START({id: "note-2-"+pkey});
%>
<script>
    async function note_submit()
    {
        if (!await validateForm("note_edit"))
            return false;

        var url = $("form[name=note_edit]")[0].action + "&note_key="+$("#note_key").val();
        var data = $("form[name=note_edit]").serialize();
        $.post(url,
               data,
               McltCallbackAndThen(note_submit_callback),
               "json");
        return true;
    }
    function note_submit_callback(data)
    {
        if (data.success)
        {
            jqToast(L("lcl_mobile_data_saved"));
            draft_note(window.module, window.module_key, null); // Verwijder de draft
            window.location.href = "<%=back_reference() + '&notitie=1'%>";
        }
    }
</script>
<%
      HEADER({title: L("lcl_mobile_notes")+" " + pnote.subject});
      CONTENT_START()
%>
        <form name="note_edit" id="note_edit" action="<%=action%>" method="post">
        <input type="hidden" id="note_key" value="<%=pnote.note_key%>">
        <input type="hidden" name="notestamp" id="notestamp" value="<%=timestamp_notes.getTime()%>">
<%
        if (parent_note_key > -1)
        {
            if (pnode == "melding")
            {
                sql = "SELECT prs_perslid_naam_friendly      author,"
                    + "       mld_melding_note_omschrijving  note,"
                    + "       mld_melding_note_flag          flag"
                    + "  FROM mld_melding_note n, prs_v_perslid_fullnames_all pf"
                    + " WHERE n.prs_perslid_key = pf.prs_perslid_key(+)"
                    + "   AND mld_melding_note_key = " + parent_note_key;
            }
            else if (pnode == "opdracht")
            {
                sql = "SELECT prs_perslid_naam_friendly      author,"
                    + "       mld_opdr_note_omschrijving     note,"
                    + "       mld_opdr_note_flag             flag"
                    + "  FROM mld_opdr_note n, prs_v_perslid_fullnames_all pf"
                    + " WHERE n.prs_perslid_key = pf.prs_perslid_key(+)"
                    + "   AND mld_opdr_note_key = " + parent_note_key;
            }
            var oRs = Oracle.Execute(sql);
            ROFIELD("note", L("lcl_note_reply_to").format(oRs("author").Value), oRs("note").Value, { multi: true });
            oRs.Close();
%>
                <input type="hidden" name="parent_key" value="<%=parent_note_key%>">
<%
        }

        RWFIELD("note", L("lcl_mld_note_new"), (pnote.note_key > 0 ? pnote.omschrijving : ""), {multi: true, pclass: "required"});

        var short_mod = "";
        switch (pnode)
        {
            case "melding":     short_mod = "MLD";  break;
            case "opdracht":    short_mod = "OPDR"; break;
        }

        if (pnode == "melding" || pnode == "opdracht")
        {
            if (pnode == "opdracht" && this_opdr.myRole === 1) {
                short_mod = "MLD";
                pkey = mld_info.mld_key;
            }
            notes.note_toggles(short_mod, { pkey: pkey, note_key: notekey, note_flag: (notekey > 0 ? pnote.note_flag : 0), mobile: true });

            var selector_sql = "SELECT fac_note_group_key, fac_note_group_naam, fac_note_group_omschrijving"
                             + "  FROM fac_note_group"
                             + " WHERE fac_note_group_xmlnode = " + safe.quoted_sql(pnode)
                             + "   AND fac_note_group_vervaldatum IS NULL"
                             + " ORDER BY fac_note_group_xmlnode, fac_note_group_volgnr";
            var oRs_notegroup = Oracle.Execute(selector_sql);
            if (!oRs_notegroup.eof)
            {
                FCLTselector("fac_note_group_key",
                            selector_sql,
                            { label: L("fac_note_group_naam"),
                                initKey: fac_note_group_key,
                                initEmpty: true,
                                emptyOption: L("lcl_note_group_default"),
                                mobile: true
                            });

                                if (fac_note_group_key)
                                {
    %>
                                <script>
                                $(function()
                                {
                                    $("#fac_note_group_key option[value=<%=fac_note_group_key%>]").prop("selected", true);
                                });
                                </script>
    <%
                                }
            }
            oRs_notegroup.close();

        }

        CONTROLGROUP_START();
          BUTTON(L("lcl_submit"), {click: "note_submit()", icon: "fa-fclt-save", singlepress: true});
          BUTTON(L("lcl_cancel"), {click: "history.back()", icon: "fa-fclt-cancel"} );
        CONTROLGROUP_END();
      IFACE.FORM_END();
%>
      </form>
<%
      CONTENT_END();
    PAGE_END();
}
PDA_PAGE_END(); %>
	</body>
</html>
<% ASPPAGE_END(); %>
