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

     Peer-to-peer berichten, forum, community, hoe je het noemt
     Mensen kunnen een bericht plaatsen, lezen en op reageren.

     Context:
     Parameters:  -
*/
%>
<!-- #include file="../Shared/common.inc" -->
<!-- #include file="./resultset_table.inc" -->
<!-- #include file="../Shared/FlexFiles.inc" -->
<!-- #include file="./mobile.inc" -->
<!-- #include file="./iface.inc" -->
<!-- #include file="../MSG/msg.inc" -->
<!-- #include file="../PRS/prs.inc" -->
<%
var SHOW_TYPE = getQParamInt("show_type", 4); // Constante
var authparams = user.checkAutorisation("WEB_MSGUSE");

%>
<html>
  <head>
    <% FCLTMHeader.Generate({}); %>
    <script>

      function viewMsg(e) {
        let msg_key = $(this).closest("article").get(0).dataset.key;
        let url = "bericht.asp?msg_key=" + msg_key;
        for (var i in e.data) {
          url += "&" + i + "=" + e.data[i];
        }
        window.location.href = url;
      }

      function react() {
        react_type = this.dataset.type || "like";
        let msg_key = $(this).closest("article").get(0).dataset.key;
        let data = {
            action: "msg_react",
            react_type: react_type,
            msg_key: msg_key
        };
        <% protectRequest.dataToken("data"); %>
        $.post("get_mobile_ajax.asp",
            data,
            reactCallback,
            "json");
      }

      function reactCallback(json) {
        if (json.success) {
          var $this_reaction = $("article[data-key=" + json.msg_key + "]")
                                .find(".msg-reaction[data-type=" + json.react_type.toLowerCase() + "]");
          $this_reaction.get(0).dataset.count = json.msg_count;
          $this_reaction.find("i").toggleClass("fal", !json.state)
                                  .toggleClass("far", json.state);
        }
      }

      function msg_delete() {
        let msg_key = $(this).closest("article").get(0).dataset.key;
        let data = {
            action: "msg_delete",
            msg_key: msg_key
        };
        <% protectRequest.dataToken("data"); %>

        FcltMgr.confirm(L("lcl_msg_del_confirm"), () => {
          $.post("get_mobile_ajax.asp",
              data,
              FcltCallbackRefresh,
              "json");
        });
      }

      /* Laat alleen items uit deze categorie zien, of toggle dat weer uit */
      function toggleCategory() {
        this.dataset.active = this.dataset.active != "true";
        var cat_key = $(this).closest("article").data("category");
        if (this.dataset.active == "true") { /* Alleen deze tonen */
          $("article[data-category!=" + cat_key + "]").addClass("invisible");
          $("article[data-category=" + cat_key + "] .msg-category").attr("data-active", "true");
        } else { /* Alles tonen */
          $("article").removeClass("invisible")
                      .find(".msg-category").attr("data-active", "false");
        }
      }

      $(() => {
        $(".msg-reaction").on("click", react);
        $(".msg-reactions").on("click", { "reply": 1 }, viewMsg);
        $(".msg-action-edit").on("click", { "mode": "edit" }, viewMsg);
        $(".msg-action-delete").on("click", msg_delete);
        $(".card-content, .msg-image").on("click", viewMsg);
        $(".msg-category").on("click", toggleCategory);
      });

    </script>
  </head>
  <body class="body-msg">
<%
PAGE_START();
  HEADER({ title: L("lcl_msg_community_title") });
  shared.registeraction("community", { daily: -1 }); // Registreer mijn laatste bezoek
  CONTENT_START();
    CARDS_WRAPPER_START();
    var sql = "SELECT fac_nieuws_key"
            + "  FROM fac_nieuws"
            + " WHERE fac_nieuws_parentkey IS NULL"
            + "   AND (fac_nieuws_van IS NULL OR SYSDATE >= fac_nieuws_van)"
            + "   AND (fac_nieuws_tot IS NULL OR SYSDATE <= fac_nieuws_tot)" /* t/m */
            + "   AND BITAND (fac_nieuws_show, " + SHOW_TYPE + ") <> 0"
            + "   AND COALESCE(fac_nieuws_van, fac_nieuws_aanmaak) > TRUNC(SYSDATE - " + S("msg_max_history") + ")"
            + " ORDER BY COALESCE(fac_nieuws_van, fac_nieuws_aanmaak) DESC";
    var oRs = Oracle.Execute(sql);
    if (oRs.EoF) {
      sql = "SELECT fac_gui_counter_count"
          + "  FROM fac_gui_counter"
          + " WHERE fac_gui_counter_group = 'community' AND prs_perslid_key = " + user_key;
      var oRsCount = Oracle.Execute(sql);
      if (    oRsCount.EoF // Dit kan volgens mij niet, want je bent nu op deze pagina, dus dit is minstens 1
          ||  oRsCount("fac_gui_counter_count").Value == 1) { // First visit
%>    <p><%=L("lcl_msg_no_messages_first")%></p><%
      } else {
%>    <p><%=L("lcl_msg_no_messages")%></p><%
      }
      oRsCount.Close();
    }
    while (!oRs.EoF) {
      msg.print_mobile_msg(oRs("fac_nieuws_key").Value, "list");
      oRs.moveNext();
    }
    oRs.Close();
    CARDS_WRAPPER_END();
%>    <a href="<%=rooturl + "/appl/pda/bericht.asp?msg_key=-1"%>" class="primary-action-button link-fixed-bottom-right" data-ajax="false"><%=I("fa-plus")%></a><%
  CONTENT_END();
  FOOTER();
PAGE_END();
%>
<% PDA_PAGE_END(); %>
	</body>
</html>
<% ASPPAGE_END(); %>
