<%
/*
     $Revision$
     $Id$
*/
%>

<%
msg = {
    getmsgnieuwsshowtext: function _getmsgnieuwsshowtext(p) {
        var tekst = "??";
        var s = parseInt(p, 10);
        switch (s)
        {
            case 1: { tekst = L("lcl_msg_nieuws_portal"); break; }
            case 2: { tekst = L("lcl_msg_nieuws_blogpagina"); break; }
            case 3: { tekst = L("lcl_msg_nieuws_beide"); break; }
            case 4: { tekst = L("lcl_msg_community"); break; }
            // FUTURE USE; case n: { tekst = L("lcl_msg_nieuws_sticky"); break;
        }
        if (Session("logging")>0)
            tekst += " ("+String(p)+")";
        return tekst;
    },

    func_enabled: function _func_enabled(msg_key) {
        var mresult = user.func_enabled2("MSG", { "isOptional": true });
        var result = {
            canRead: mresult.canRead("WEB_MSGUSE"),
            canChange: mresult.canWrite("WEB_MSGUSE"),
            canChangeMsg: mresult.canWrite("WEB_MSGUSE")
        }
        if (result.canChangeMsg && msg_key > 0 && !mresult.canWrite("WEB_MSGBOF")) { /* Met MSGBOF mag je alles wel */
            var sql = "SELECT prs_perslid_key FROM fac_nieuws WHERE fac_nieuws_key = " + msg_key + " AND BITAND(fac_nieuws_show, 4) <> 0";
            var oRs = Oracle.Execute(sql);
            result.canChangeMsg = !oRs.EoF && oRs("prs_perslid_key").Value == user_key;
            oRs.Close();
        }
        return result;
    },

    msg_info: function _msg_info(msg_key, params) {
        var params = params || {};
        var sql = "SELECT fac_nieuws_key,"
                + lcl.xsqla("fac_nieuws_titel", "fac_nieuws_key") + ","
                + "       n.fac_nieuws_image,"
                + lcl.xsqla("fac_nieuws_omschrijving", "fac_nieuws_key") + ","
                + "       n.fac_nieuws_groep_key,"
                + "       ng.fac_nieuws_groep_omschrijving,"
                + "       fac_nieuws_aanmaak,"
                + "       fac_nieuws_show,"
                + (!("msgType" in params) || params.msgType & 4
                    ? "   (SELECT COUNT (*)"
                    + "      FROM fac_nieuws fn"
                    + "     WHERE     fn.fac_nieuws_parentkey = n.fac_nieuws_key"
                    + "           AND (   fac_nieuws_titel = '##LIKE##'"
                    + "                OR fac_nieuws_titel = '##METOO##')"
                    + "           AND fac_nieuws_omschrijving IS NULL"
                    + "           AND prs_perslid_key = " + user_key + ")     my_reaction,"
                    + "   (SELECT COUNT (*)"
                    + "      FROM fac_nieuws fn"
                    + "     WHERE     fn.fac_nieuws_parentkey = n.fac_nieuws_key"
                    + "           AND (   fac_nieuws_titel = '##LIKE##'"
                    + "                OR fac_nieuws_titel = '##METOO##')"
                    + "           AND fac_nieuws_omschrijving IS NULL)        nrreactions,"
                    + "   (SELECT COUNT (*)"
                    + "      FROM fac_nieuws fn"
                    + "     WHERE     fn.fac_nieuws_parentkey = n.fac_nieuws_key"
                    + "           AND fac_nieuws_omschrijving IS NOT NULL"
                    + "           AND prs_perslid_key = " + user_key + ")     my_replies,"
                    + "   (SELECT COUNT (*)"
                    + "      FROM fac_nieuws fn"
                    + "     WHERE     fn.fac_nieuws_parentkey = n.fac_nieuws_key"
                    + "           AND fac_nieuws_omschrijving IS NOT NULL)    nrreplies,"
                    : "")
                + "       n.prs_perslid_key,"
                + "       n.mld_melding_key"
                + "  FROM fac_nieuws n, fac_nieuws_groep ng"
                + " WHERE     n.fac_nieuws_groep_key = ng.fac_nieuws_groep_key(+)"
                + "       AND (n.fac_nieuws_van IS NULL OR SYSDATE >= n.fac_nieuws_van)"
                + "       AND (n.fac_nieuws_tot IS NULL OR SYSDATE <= n.fac_nieuws_tot)" /* t/m */
                + "       AND n.fac_nieuws_parentkey IS NULL"
                + ("msgType" in params
                    ? "   AND BITAND (fac_nieuws_show, " + params.msgType + ") <> 0"
                    : "")
                + "       AND n.fac_nieuws_key = " + msg_key
                + " ORDER BY COALESCE(n.fac_nieuws_van, n.fac_nieuws_aanmaak) DESC";
        var oRs = Oracle.Execute(sql);
        if (oRs.Eof) {
            shared.record_not_found(L("lcl_message") + " " + msg_key);
        }
        var result = {
            author_key:     oRs("prs_perslid_key").Value,
            msg_type:       oRs("fac_nieuws_show").Value,
            category_key:   oRs("fac_nieuws_groep_key").Value,
            category:       oRs("fac_nieuws_groep_omschrijving").Value,
            msg_created:    new Date(oRs("fac_nieuws_aanmaak").Value),
            msg_subject:    oRs("fac_nieuws_titel").Value,
            msg_image:      oRs("fac_nieuws_image").Value,
            msg_omschrijving:   oRs("fac_nieuws_omschrijving").Value,
            mld_melding_key:    oRs("mld_melding_key").Value
        }
        if (!("msgType" in params) || params.msgType & 4) {
            result.my_reaction = oRs("my_reaction").Value;
            result.reaction_count = oRs("nrreactions").Value;
            result.my_reply_count = oRs("my_replies").Value;
            result.reply_count = oRs("nrreplies").Value;
        }
        if (params.withReplies) {
            result.replies = [];
            var sql_replies = "SELECT fac_nieuws_key,"
                            + lcl.xsqla("fac_nieuws_omschrijving", "fac_nieuws_key") + ","
                            + "       fac_nieuws_aanmaak,"
                            + "       prs_perslid_key"
                            + "  FROM fac_nieuws"
                            + " WHERE fac_nieuws_parentkey = " + msg_key
                            + "   AND fac_nieuws_titel IS NULL"
                            + "   AND fac_nieuws_omschrijving IS NOT NULL"
                            + " ORDER BY fac_nieuws_aanmaak DESC";
            var oRs_replies = Oracle.Execute(sql_replies);
            while (!oRs_replies.EoF) {
                result.replies.push({
                    reply_key:  oRs_replies("fac_nieuws_key").Value,
                    reply_author_key:   oRs_replies("prs_perslid_key").Value,
                    reply_author:   (oRs_replies("prs_perslid_key").Value == user_key ? null : prs.prs_perslid(oRs_replies("prs_perslid_key").Value)),
                    reply_created:  new Date(oRs_replies("fac_nieuws_aanmaak").Value),
                    reply:      oRs_replies("fac_nieuws_omschrijving").Value
                });
                oRs_replies.moveNext();
            }
            oRs_replies.Close();
        }
        var props = flexProps("NEWS", oRs("fac_nieuws_key").Value, undefined, undefined, { cacheablehmac: true });
        if (props.files && props.files.length) {
            result.msg_image_src = props.files[0].deepurl;
        }
        oRs.Close();
        return result;
    },

    print_mobile_msg: function _print_mobile_msg(msg_key, mode, msg_info) {
        var doReply = getQParamInt("reply", 0) == 1;
        var msg_info = msg_info || msg.msg_info(msg_key, { "withReplies": mode == "show" });
        var carddata = { "key": msg_key };
        if (msg_info.category_key) {
          carddata.category = msg_info.category_key;
        }
        CARD_START({ "cls": "msg" + (mode == "show" ? "" : " preview") + (mode == "list" ? " tappable" : ""), "data": carddata });
        var author = prs.prs_perslid(msg_info.author_key, { withPhoto: true });
%>      <div class="msg-card-header">
            <span class="msg-info">
                <span class="profile-photo" style="background-image: url('<%=safe.htmlattr(author.photopaththumb)%>');"></span>
                <h2 class="msg-author"><%=safe.html(author.naam)%></h2>
            </span>
            <span class="msg-details">
                <div class="msg-category"><%=safe.html(msg_info.category)%></div>
                <div class="msg-date"><%=toDateTimeString(msg_info.msg_created, false, false, true, false)%></div>
            </span>
        </div>
        <div class="msg-image flex-center">
<%        if (msg_key != -1) {
            Response.Write(SYMBOL(msg_info.msg_image || msg_info.msg_image_src, { "module": "NEWS", "key": msg_key }));
          }
          if (mode == "edit" && !(msg_info.msg_image || msg_info.msg_image_src)) { %>
            <div class="msg-img-upload flex-center">
                <span class="flex-center"><%=I("fa-camera fa-3x")%></span>
            </div>
<%        } %>
        </div>
        <div class="card-content">
            <div class="card-title"><%=safe.html(msg_info.msg_subject)%></div>
            <div class="card-descr"><%=safe.fclthtml(msg_info.msg_omschrijving, { "file_params": { "module": "MSG", "key": msg_key }})%></div>
        </div>
<%      if (mode != "edit" && msg_key != -1) { %>
        <div class="msg-actions">
            <span class="msg-interactions">
                <span class="msg-inter-action msg-reaction tappable" data-type="<%=msg_info.mld_melding_key ? "metoo" : "like"%>" data-count="<%=msg_info.reaction_count%>">
                    <%=I("fa-xl " + (msg_info.mld_melding_key ? "fa-hand" : "fa-thumbs-up") + (msg_info.my_reaction ? " far" : ""))%>
                </span>
<%              if (mode == "show") {
                    if (!doReply) { %>
                <span class="msg-inter-action msg-reactions tappable">
                    <%=I("fa-xl fa-reply")%>
                </span>
<%                  }
                } else { %>
                <span class="msg-inter-action msg-reactions tappable" data-count="<%=msg_info.reply_count%>">
                    <%=I("fa-xl fa-comment" + (msg_info.my_reply_count > 0 ? " far" : ""))%>
                </span>
<%              } %>
            </span>
            <span class="msg-modifications">
<%              if (msg_info.author_key === user_key) { %>
                <span class="msg-action msg-action-edit tappable">
                    <%=I("fa-xl fa-fclt-edit")%>
                </span>
                <span class="msg-action msg-action-delete tappable">
                    <%=I("fa-xl fa-trash-alt")%>
                </span>
<%              } %>
            </span>
        </div>
<%          if (msg_info.replies) {
                FORM_START("reply-form"); %>
        <div class="msg-edit-reply<%=doReply ? "" : " invisible"%>">
<%              HIFIELD("nieuws_show", 4);
                HIFIELD("parent_key", msg_key);
                RWFIELD("ta_message", "", "", { "multi": true, "pclass": "required" });
%>          <a onclick="mobile.button.click(event, this)" mobClick="replySubmit()" class="primary-action-button msg-post-reply-container" data-ajax="false" singlepress="true"><%=I("fa-paper-plane")%></a>
        </div>
<%              FORM_RVT();
                FORM_END(); %>
        <div class="msg-replies-container"><%
                for (var i = 0; i < msg_info.replies.length; i++) { %>
            <span class="msg-reply-container card card-body<%=(msg_info.replies[i].reply_author_key == user_key ? " self" : "")%>" data-key="<%=msg_info.replies[i].reply_key%>">
                <span class="reply-header">
<%                  if (msg_info.replies[i].reply_author_key != user_key) { %>
                    <span class="reply-author"><%=safe.html(msg_info.replies[i].reply_author.naam)%></span>
<%                  } %>
                    <span class="reply-timestamp"><%=toDateTimeString(msg_info.replies[i].reply_created, false, false, true, false)%></span>
                </span>
                <span class="reply-body">
                    <%=safe.fclthtml(msg_info.replies[i].reply)%>
                </span>
            </span>
<%              }
      %></div>
<%          }
        }
        CARD_END();
        return true;
    }
}

%>
