<%@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:
*/
%>

<!-- #include file="../Shared/common.inc" -->
<!-- #include file="../mld/mld.inc" -->
<!-- #include file="./resultset_table.inc" -->
<!-- #include file="./mobile.inc" -->
<!-- #include file="./iface.inc" -->

<%
FCLTHeader.Requires({ js: ["./modernizr-3.3.0.custom.min.js"] });

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

var sql = "";

    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;
            pnote.onlyFErights  = mfe.haveOnlyFErights;


            var mld_info = mld.mld_melding_info(pkey);
            lcl.set_dialect(mld_info.srtdisc, "MLD_SRTDISCIPLINE_KEY");
            pnote.subject = mld_info.prefix + pkey;
            pnote.fenotes = mld_info.fenotes;
            pnote.zichtbaar = (mld_info.fenotes & 1);
            pnote.eigenMelding = user_key == mld_info.melder_key || user.isCollega(mld_info.melder_key);

            if (pnote.canwritenotes)
            {
                var sql_n = "SELECT mld_melding_note_key"
                          + "     , prs_perslid_key"
                          + "     , mld_melding_note_omschrijving"
                          + "     , mld_melding_note_flag"
                          + "  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)
                {
                    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.zichtbaar = oRs_n("mld_melding_note_flag").Value == 1;
                        pnote.flag = oRs_n("mld_melding_note_flag").Value;
                    }
                }
                oRs_n.Close();
            }
            if (pnote.eigenMelding)
                pnote.zichtbaar = true;

            // Zelfde query in mld_edit_note.asp
            var sql = "SELECT n.mld_melding_note_aanmaak note_aanmaak"
                    + "     , n.mld_melding_note_key note_key"
                    + "     , pf.prs_perslid_key"
                    + "     , pf.prs_perslid_naam_friendly"
                    + "     , n.mld_melding_note_omschrijving note_omschrijving"
                    + "     , mld_melding_note_flag"
                    + "     , NULL fac_srtnotificatie_code"
                    + "  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
                    + (pnote.onlyFErights ? " AND mld_melding_note_flag = 1" : "");

            if (tracking)    // tracking info erbij?
            {
                var sql_t = "SELECT tr.fac_tracking_datum"
                          + "     , tr.fac_tracking_key"
                          + "     , tr.prs_perslid_key"
                          + "     , pf.prs_perslid_naam_friendly"
                          + "     , tr.fac_tracking_oms"
                          + "     , NULL mld_melding_note_flag"
                          + "     , str.fac_srtnotificatie_code"
                          + "  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'"
                          + " AND (tr.fac_tracking_refkey = " + pkey + " AND str.fac_srtnotificatie_xmlnode IN (" + safe.quoted_sql(pnode) + "))";

                sql = "SELECT * FROM ("+ sql + " UNION " + sql_t + ")";
            }
            break;
    }
    sql = sql + " ORDER BY 1 DESC";

    function fnNoteText (oRs)
    {
        var val = oRs("note_omschrijving").value||"";
        val = shared.stripbbcodes(val);

        return "<div class='listbodytext'>" + safe.html(val) + "</div>";
    }

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

        return url;
    }

    function back_reference()
    {
        var url = "";
        switch (pnode)
        {
            case "melding": url = "melding.asp?mld_key=" + pkey; break; // Terug naar de melding.
        }

        return url;
    }
%>
<html>
  <head>
<% FCLTMHeader.Generate({}); %>
  </head>
  <body>
    <div>
<%


    PAGE_START({id: "note-1-"+pkey});
%>
<script  type="text/javascript" >
    function note_add()
    {
        $("#note").val("");
        $("#note_key").val(-1);
        $.mobile.navigate("#note-2-<%=pkey%>");
    }
    function note_change()
    {
        $("#note").val("<%=safe.jsstring(pnote.omschrijving)%>");
        $("#note_key").val(<%=pnote.note_key%>);
        $.mobile.navigate("#note-2-<%=pkey%>");
    }
    function note_back()
    {
        window.location.href = "<%=back_reference()%>";
    }
</script>
<%
      var p_useraction = {icon: "back", action: "note_back()", label: L("lcl_back")};
      HEADER({title: L("lcl_mobile_notes")+" " + pnote.subject, useraction: p_useraction, nohome: true});
      CONTENT_START();
        CONTAINER_START()
        if (pnote.canwritenotes)
        {
            BUTTON(L("lcl_add"), {click: "note_add()", dataicon: "plus", dataajax: "false"});
        }
        CONTAINER_END();

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

      CONTENT_END();
      FOOTER();
    PAGE_END();


    PAGE_START({id: "note-2-"+pkey});
%>
<script type="text/javascript">
    function note_submit()
    {
        if (!validateForm("note_edit"))
            return false;

        var url = $("form[name=note_edit]")[0].action + "&note_key="+$("#note_key").val();
        $.post(url,
               $("form[name=note_edit]").serialize(),
               note_submit_callback,
               "json");
        return false;
    }
    function note_submit_callback(data)
    {
        if (data.success)
        {
            jqToast(L("lcl_mobile_data_saved"));
            window.location.href = "<%=back_reference()%>";
        }
    }
</script>
<%
      HEADER({title: L("lcl_mobile_notes")+" " + pnote.subject, nohome: true});
      CONTENT_START()
%>
      <form name="note_edit" id="note_edit" action="../mld/mld_edit_note_save.asp?mld_key=<%=pkey%>" method="post">
        <input type="hidden" id="note_key" value="<%=pnote.note_key%>">
<%
        RWFIELD("note", L("lcl_mld_note_new"), pnote.omschrijving, {multi: true, pclass: "required"});

        if (pnote.fenotes & 2 && !pnote.onlyFErights)
        {
            if (pnote.note_key == -1 && !pnote.eigenMelding)
                CHB_FIELD("zichtbaarFE", "<nobr>" + L("lcl_mld_zichtbaar_FE") + "</nobr>", pnote.zichtbaar, {disabled: false});
            else
                CHB_FIELD("zichtbaarFE", "<nobr>" + L("lcl_mld_zichtbaar_FE") + "</nobr>", pnote.zichtbaar, {disabled: true });
        }

        CONTROLGROUP_START();
          BUTTON(L("lcl_submit"), {click:  "note_submit()", dataicon: "check"});
          BUTTON(L("lcl_cancel"), {linkid: "#note-1-"+pkey, dataicon: "back"});
        CONTROLGROUP_END();
      IFACE.FORM_END();
%>
      </form>
<%
      CONTENT_END();
    PAGE_END();
%>
    </div>
  </body>
</html>