<%@ LANGUAGE = JavaScript %>
<% /*
     $Revision$
     $Id$

    File:               msg_edit_message.asp (formerly message.asp)
    Description:        Muteerscherm voor berichten
    Parameters:         message_key: optioneel==>impliceert een reply op een persoonlijk portalbericht!
                        -
    Context:
    Note:               Aanmaken van een Facilitor bericht
                        Alleen portal-berichten kunnen achteraf gewijzigd worden!
                        Dat gebeurt dan in msg_portal_edit.asp

*/ %>
<!-- #include file="../Shared/common.inc"-->
<!-- #include file="../Shared/persoonselector.inc" -->
<!-- #include file="../Shared/plaatsselector.inc" -->
<!-- #include file="../Shared/selector.inc" -->
<!-- #include file="../Shared/iface.inc"-->
<!-- #include file="../Shared/calendar.inc"-->
<!-- #include file="../Shared/FlexFiles.inc"-->
<%
FCLTHeader.Requires({plugins:["suggest"]});

var urole = getQParamSafe("urole");
var backo = urole=="bo";                     // voor nieuwe berichten
var frontend = (urole == "fe" || !backo );   // voor replies

var message_key    = getQParamInt("message_key", -1);
var message_reply  = message_key > 0;
var lBestaandBericht = message_key != -1;

var subject;
var ilink;
if (lBestaandBericht) {  // Dit moet/zal altijd een reply zijn!
  sql = "SELECT m.prs_perslid_key"
      + "     , m.msg_message_oms"
      + "     , m.msg_message_aanmaak"
      + "     , m.msg_message_replykey"
      + "     , m.msg_message_onderwerp"
      + "     , m.msg_message_prioriteit"
      + "     , m.msg_message_url"
      + "     , pf.prs_perslid_naam_full"
      + "  FROM msg_message m"
      + "     , prs_v_perslid_fullnames_all pf"
      + " WHERE     msg_message_key = " + message_key
      + "       AND m.prs_perslid_key = pf.prs_perslid_key";
  oRsMes = Oracle.Execute(sql);
  if (!oRsMes.EoF) {
      key_sender   = oRsMes("prs_perslid_key").value;
      mess_dsc     = oRsMes("msg_message_oms").value;
      action_datum = oRsMes("msg_message_aanmaak").value;
      ref_mess_key = oRsMes("msg_message_replykey").value;
      subject      = oRsMes("msg_message_onderwerp").value;
      priority_key = oRsMes("msg_message_prioriteit").value;
      ilink        = oRsMes("msg_message_url").value;
      sender_name  = oRsMes("prs_perslid_naam_full").value;
  } else {
    oRsMes.Close();
    Response.write("Internal error or message deleted");
    Response.End;
  }
  oRsMes.Close();
  if (ref_mess_key != null) {
    shared.internal_error("Reply op reply niet mogelijk");
  }

  // prio: number to text
  if (priority_key == 1) {
    priority = L("lcl_mes_high");
  } else if (priority_key == 2) {
    priority = L("lcl_mes_normaal");
  } else {
    priority = L("lcl_mes_low");
  }

  // Goed bepalen: welke rechten veris ik eigenlijk?
  // - BOF/USE -write  om aan te maken
  // - USE read om te lezen  (of is niks voldoende?)
  // - USE write om te replyen

} else { // New message
    var key_sender   = '';  // user_key
    var mess_dsc     = '';
    var action_datum = '';

    var authparams = user.checkAutorisation("WEB_MSGBOF");
}

%>
<html>
<head>
<%
   FCLTHeader.Generate();
%>
<script type="text/javascript">

  async function msg_submit()
  {
    // Tenminste 1 moet gevuld zijn
    if ($("#perslid_key_show").val() == "" && $("#msggroup").val() == -1 && $("#district").val() == "") {
        FcltMgr.alert(L("lcl_shared_validator_missing"));
        return false;
    }

    if (!await validateForm("u2"))
      return false;

    var autoConf = !(($("#perslid_key").val() == -1) &&
                     ($("#districtkey").val() == -1) &&
                     ($("#locatiekey").val() == -1)  &&
                     !$("#msggroup").val()           &&
                     ($("#cb_email:checked").val() ||  $("#cb_fmessage:checked").val()));

    // Wilt U naar alle Facilitor gebruikers sturen?
    FcltMgr.confirm(L("lcl_mes_all"), { autoconfirm: autoConf }, function() {
      $.post($("form[name=u2]")[0].action, $("[name=u2]").serialize(), FcltCallbackClose, "json");
    });
  }

  function checkNrChar()
  {
    var msgLength = $("#ta_message").val().length;
    var valid = msgLength <= 4000;
    validateField($("#ta_message"), valid, L("lcl_mes_to_many_char") + ' (' + msgLength + '). ' + L("lcl_mes_less_500"));
  }

  $(function()
  {
    $('textarea').resize(function () { FcltMgr.resized(window) });
    $('textarea').autogrow();
  });

</script>
</head>

<body class="editmode">
<% SUBFRAME_START();

var buttons = [{ icon: "fa-fclt-save",    title: L("lcl_mes_send"), action: "msg_submit()" },
               { icon: "fa-fclt-cancel",  title: L("lcl_cancel"),   action: "FcltMgr.closeDetail(window, { cancel: true } );" } ];

    IFRAMER_HEADER(L("lcl_mes_message"), buttons);%>
  <form name="u2"
        action="msg_message_save.asp?message_reply=<%=message_reply?1:0%>&message_key=<%=message_key%>"
        method="post">
<%

    BLOCK_START("msgPrs", L("lcl_msg_medewerkergroep"), { icon: "fa-users-medical" });
    if (!lBestaandBericht)
    {
        FCLTpersoonselector(
                        "perslid_key",
                        "sgPerson",
                      { perslidKey: -1,
                        label: L("lcl_name"),
                        fieldNameKey: "person",
                        fieldId: "fldmelder",
                        filtercode: "MSG"
                      });
        sql = "SELECT fac_groep_key, fac_groep_omschrijving, fac_groep_upper"
            + "  FROM fac_groep"
            + " ORDER BY 3";

        FCLTselector("msggroup", sql,
                        { label: L("fac_groep"),
                          multi: true,
                          emptyOption: ""
                        }
                      );
    }
%>
      <tr>
        <td class="label"><label for="priority"><%=L("lcl_mes_priority")%></label></td>
        <td>
          <select  class="fld" name='priority' id="priority">
            <option value='1'><%=L("lcl_mes_high")%></option>
            <option value='2' selected><%=L("lcl_mes_normaal")%></option>
            <option value='3'><%=L("lcl_mes_low")%></option>
          </select>
      </td>
      </tr>
<%
    BLOCK_END();

    if (!lBestaandBericht)
    {
        BLOCK_START("msgAlg", L("lcl_msg_plaats"), { icon: "fa-map-marker" });
            FCLTplaatsselector( authparams.ALGreadlevel, { startlevel: 1, eindlevel: 4 });
        BLOCK_END();
    }


  BLOCK_START("msgMessage", L("lcl_mes_message"), { wide: true, icon: "fa-comment-alt-lines" });

  // Een tabel met 4 kolommen: label-veld-label-veld

    if (ilink != null) {
%>
  <tr>
    <td class="label"><label><%=L("lcl_mes_link")%></label></td>
    <td><a class="fldmsgurl" href="<%=safe.htmlattr(ilink)%>" target="_blank" rel="noopener noreferrer"><%=safe.html(ilink)%></a></td>
  </tr>
<% }

%>
  <tr>
    <td class="label"><label><%=L("lcl_mes_send_option")%></label></td>
    <td>
      <div id="msgoptions">
    <%
        // ik moet zelf een e-mail adres hebben om mail te mogen sturen..
    if ( user.prs_perslid_email() != "" ) {
%>
        <input type="checkbox" id="cb_email" name="cb_email" value="1" checked>
    <%  } else { %>
        <input type="checkbox" id="cb_email" name="cb_email" disabled>
    <%  } %>
        <label for="cb_email"><%=L("lcl_mes_email")%></label>
    <%
    if (S("puo_smsaccount"))
    { %>
        <input type="checkbox" id="cb_sms" name="cb_sms" value="1">
        <label for="cb_sms"><%=L("lcl_mes_sms")%></label>
    <%
    }
    %>
        <input type="checkbox" id="cb_fmessage" name="cb_fmessage" value="1" >
        <label for="cb_fmessage"><%=L("lcl_mes_fmessage")%></label>

        <input type="checkbox" id="cb_pushapp" name="cb_pushapp" value="1">
        <label for="cb_pushapp"><%=L("lcl_mes_pushapp")%></label>
      </div>
    </td>
  </tr>
  <tr>
    <td class="label"><label for="subject"><%=L("lcl_mes_subject_sms")%></label></td>
    <td><input class="required fld" type="text" id="subject" name="subject" value="<%=(subject?"Re:":"")+ safe.htmlattr(subject)%>"></td>
  </tr>

<%
  RWTEXTAREATR("ta_message",
               "fldtxt required",
               L("lcl_mes_message"),
               "",
               { html: "onblur='checkNrChar()'",
                 placeholder: L("lcl_mes_empty"),
                 bb_codes: true,
                 upload_params: {
                   module: "MES",
                   refkey: message_key
                 }
               });

  BLOCK_END();
IFACE.FORM_END();
%>
</form>
<% SUBFRAME_END(); %>
</body>
</html>
<% ASPPAGE_END(); %>
