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

    File:           fac_edit_faq.asp
    Description:    show detailed information of an faq

    Parameters:     faq_key

    Context:
    Note:

*/ %>
<!-- #include file="../Shared/common.inc"-->
<!-- #include file="../Shared/iface.inc"-->
<!-- #include file="../Shared/calendar.inc" -->
<!-- #include file="../Shared/persoonselector.inc" -->
<!-- #include file="../Shared/autorisatieselector.inc" -->
<!-- #include file="../Shared/disciplineselector.inc" -->
<!-- #include file="../Shared/selector.inc" -->
<!-- #include file="faq_flexkenmerk.inc" -->
<!-- #include file="../fac/fac.inc" -->

<%
FCLTHeader.Requires({plugins:["jQuery", "suggest"], js: []})

var faq_key = getQParamInt("faq_key", -1);
var alg_loc_key = -1;

var this_faq = fac.func_enabled_faq(faq_key); // Wat heb ik zoal aan rechten op deze specifieke kennisbankitem.
user.auth_required_or_abort(faq_key > 0 ? this_faq.canChange : this_faq.canAdd);

var level = 3; // default beide
var vervaldatum;
if (faq_key > -1)
{
  var sql = "SELECT fac_faq_question"
          + "     , fac_faq_answer"
          + "     , fac_faq_answer2"
          + "     , fac_faq_source"
          + "     , fac_faq_url"
          + "     , fac_faq_level"
          + "     , fac_faq_datum"
          + "     , fac_faq_vervaldatum"
          + "     , fac_faq_lang"
          + "     , fac_functie_key"
          + "     , fac_faq_hint"
          + "     , fac_faq_displaymode"
          + "     , prs_perslid_key"
          + "     , faq_ins_discipline_key"
          + "  FROM fac_faq"
          + " WHERE fac_faq_key = " + faq_key;
  var oRs = Oracle.Execute(sql);

  var question = oRs("fac_faq_question").Value;
  var answer = oRs("fac_faq_answer").Value;
  var answer2 = oRs("fac_faq_answer2").Value;
  var source = oRs("fac_faq_source").Value;
  var url = oRs("fac_faq_url").Value;
  level = oRs("fac_faq_level").Value;
  var perslidkey = oRs("prs_perslid_key").Value;
  var datum = oRs("fac_faq_datum").Value;
  vervaldatum = oRs("fac_faq_vervaldatum").Value;
  var lang = oRs("fac_faq_lang").Value;
  var functie_key = oRs("fac_functie_key").Value;
  var hint = oRs("fac_faq_hint").Value;
  var displaymode = oRs("fac_faq_displaymode").Value;
  var disc_key = oRs("faq_ins_discipline_key").Value;
  oRs.Close();
}
else
{
    level = S("faq_default_visibility");
    if (this_faq.canBOwrite)
        datum = new Date();
    var displaymode = 3; //  Niet automatisch
    var disc_key = -1;

    var sql = "SELECT COUNT(*) aantal"
            + "  FROM ins_tab_discipline"
            + " WHERE ins_discipline_module = 'FAQ'"
            + "   AND ins_discipline_verwijder IS NULL";
    var oRs = Oracle.Execute(sql);
    var disc_aantal = oRs("aantal").Value;
    oRs.Close();
    if (disc_aantal == 1)
    {
      var sql = "SELECT ins_discipline_key"
              + "  FROM ins_tab_discipline"
              + " WHERE ins_discipline_module = 'FAQ'"
              + "   AND ins_discipline_verwijder IS NULL";
      var oRs = Oracle.Execute(sql);
      disc_key = oRs("ins_discipline_key").Value;
      oRs.Close();
    }
}

// Deze wordt alleen gebruikt voor de vakgroep-selector
var authFuncs = faq_key < 0 ? ["WEB_FAQFOF"] : ["WEB_FAQBOF"];
if (datum === null) // Reeds gepubliceerde items hebben BOF nodig
{
    authFuncs.push("WEB_FAQUSE");
    if (faq_key > 0) // Anders hebben we die al
        authFuncs.push("WEB_FAQFOF");
}
var authparams = user.checkAutorisation(authFuncs);

%>

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

      async function fac_submit()
      {
        if (!await validateForm("u2"))
          return false;

        if (!$("#faqVisibleZ").prop("checked") && !$("#faqVisibleP").prop("checked")) {
          FcltMgr.alert(L("lcl_faq_level0"));
          return false;
        }

        document.forms.u2.submit();
      }

      function fac_cancel()
      {
        FcltMgr.closeDetail(window, { cancel: true } );
      }

      function onChangeCatalogus()
      {
        var disc_key = sgDisc.getValue();
        $.getJSON("./get_fac_info_ajax.asp",
                  { req_info: "faq_func",
                    discipline_key: disc_key
                  },
                  process_disc_info);

        loadKenmerk(disc_key);
      }

      function process_disc_info(data, textStatus)
      {
        var hasBO = !!(textStatus == "success" && data.canBOwrite);
        $("#pub_date_info").closest("tr").toggle(!hasBO);
        $("#date_from").closest("tr").toggle(hasBO);
      }

      function loadKenmerk(disc_key)
      {
        var s = "load_kenmerk.asp?faq_key=<%=faq_key%>"
              + "&disc_key=" + disc_key;
        $.get(s, flexReloaded);
      }

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

    </script>
  </head>

  <body class="editmode">
<%
  SUBFRAME_START();
    var buttons =  [ {title: L("lcl_submit"), action:"fac_submit()", icon: "fa-fclt-save" },
                     {title: L("lcl_cancel"), action:"fac_cancel()", icon: "fa-fclt-cancel" }];

    IFRAMER_HEADER(L("lcl_faq_kennisitem"), buttons);
%>
      <form name="u2"
            action="fac_edit_faq_save.asp?faq_key=<%=faq_key%>"
            method="post"
            target="hidFrameSubmit"
            onSubmit="job_submit();">
<%
        BLOCK_START("mldFaqedit", L("lcl_faq_item"), { wide: true, icon: "fa-book" });
            if (faq_key > 0)
                ROFIELDTR("fld", L("lcl_faq_kennisbank_nr"), faq_key);
            FCLTdisciplineselector("fac_disc",
                                   "sgDisc",
                                   { label: L("faq_discipline"),
                                     disc_key: disc_key,
                                     module: "FAQ",
                                     perslidKey: user_key,
                                     onChange: "onChangeCatalogus",
                                     required: true,
                                     autfunctionKey: authparams.autfunctionkeys
                                   }
                                  );
            RWTEXTAREATR("fac_question",
                         "fldtxt required",
                         L("lcl_faq_question"),
                         question,
                         { html: "rows='3' maxlength='200'",
                           no_bb_codes: true }
                        );
            RWTEXTAREATR("fac_answer",
                         "fldtxt required",
                         L("lcl_faq_answer"),
                         answer,
                         { html: "rows='3' maxlength='4000'",
                           bb_codes: true,
                           upload_params: { module: "FAQA",
                                            refkey: faq_key,
                                            bb_files_nr: 1 }}
                        );
            RWTEXTAREATR("fac_answer2",
                         "fldtxt",
                         L("lcl_faq_answer2"),
                         answer2,
                         { html: "rows='3' maxlength='4000'",
                           bb_codes: true,
                           upload_params: { module: "FAQB",
                                            refkey: faq_key,
                                            bb_files_nr: 2 }}
                        );
            RWTEXTAREATR("fac_hint",
                         "fldtxt", L("lcl_faq_hint"),
                         hint,
                         { no_bb_codes: true }
                        );
            RWFIELDTR("fac_url",
                      "fld",
                      L("lcl_faq_link"),
                      url,
                      { placeholder: "https://" }
                     );
        BLOCK_END();

// Fast check to see if we have anything to do with orders
sql= "SELECT 1 FROM mld_opdr WHERE ROWNUM = 1";
oRs = Oracle.Execute(sql);
var anyorders = !oRs.eof;
oRs.close();
// Fast check to see if we have anything to do with afspraken
sql= "SELECT 1 FROM bez_afspraak WHERE ROWNUM = 1";
oRs = Oracle.Execute(sql);
var anyafspraken = !oRs.eof;
oRs.close();


        BLOCK_START("FaqViz", L("lcl_faq_itemviz"), { icon: "fa-eye" });
%>
            <tr>
              <td class="label"><label><%=L("lcl_faq_level")%></label></td>
              <td>
                <input type="checkbox" class="fldcheck" name="faqVisibleZ" id="faqVisibleZ" value="1" <%=(level & 1) == 1? "CHECKED" : ""%>>
                <label for="faqVisibleZ"><%=L("lcl_faq_level1")%></label>
                <br>
                <input type="checkbox" class="fldcheck" name="faqVisibleP" id="faqVisibleP" value="1" <%=(level & 2) == 2? "CHECKED" : ""%>>
                <label for="faqVisibleP"><%=L("lcl_faq_level2")%></label>
              </td>
            </tr>
<%
         if (1) // we doen iets met meldingen
         {
%>
            <tr>
              <td class="label faq_displaymode"><label><%=L("lcl_faq_displaymode_mld")%></label></td>
              <td>
                <input type="checkbox" class="fldFaqScreenM" name="faqScreenM" id="faqScreenM" value="1" <%=(displaymode & 1) == 1? "CHECKED" : ""%>>
                <label for="faqScreenM"><%=L("lcl_faq_display_screen")%></label>
                <br>
                <input type="checkbox" class="fldFaqPopupM" name="faqPopupM" id="faqPopupM" value="1" <%=(displaymode & 2) == 2? "CHECKED" : ""%>>
                <label for="faqPopupM"><%=L("lcl_faq_display_popup")%></label>
                <br>
              </td>
            </tr>
<%
         }
         if (anyorders) // we doen iets met opdrachten
         {
%>
            <tr>
              <td class="label faq_displaymode"><label><%=L("lcl_faq_displaymode_opdr")%></label></td>
              <td>
                <input type="checkbox" class="fldFaqScreenO" name="faqScreenO" id="faqScreenO" value="1" <%=(displaymode & 4) == 4? "CHECKED" : ""%>>
                <label for="faqScreenO"><%=L("lcl_faq_display_screen")%></label>
                <br>
                <input type="checkbox" class="fldFaqPopupO" name="faqPopupO" id="faqPopupO" value="1" <%=(displaymode & 8) == 8? "CHECKED" : ""%>>
                <label for="faqPopupO"><%=L("lcl_faq_display_popup")%></label>
                <br>
              </td>
            </tr>
<%
         }
         if (anyafspraken) // we doen iets met afspraken
         {
%>
            <tr>
              <td class="label faq_displaymode"><label><%=L("lcl_faq_displaymode_afspr")%></label></td>
              <td>
                <input type="checkbox" class="fldFaqScreenA" name="faqScreenA" id="faqScreenA" value="1" <%=(displaymode & 16) == 16? "CHECKED" : ""%>>
                <label for="faqScreenA"><%=L("lcl_faq_display_screen")%></label>
                <br>
                <input type="checkbox" class="fldFaqPopupA" name="faqPopupA" id="faqPopupA" value="1" <%=(displaymode & 32) == 32? "CHECKED" : ""%>>
                <label for="faqPopupA"><%=L("lcl_faq_display_popup")%></label>
                <br>
              </td>
            </tr>
<%
        }
        BLOCK_END();

        BLOCK_START("FaqAdm", L("lcl_faq_itemadm"), { icon: "fa-tag" });

            ROFIELDTR("fld", L("lcl_faq_datum"), L("lcl_faq_needaccept"), { "id": "pub_date_info", "trhidden": this_faq.canBOwrite });

            FCLTcalendar( "date_from",
                          { label: L("lcl_faq_datum"),
                            datum: datum != null? new Date(datum) : null,
                            initEmpty: (datum == null),
                            trhidden: !this_faq.canBOwrite,
                            readonly: !this_faq.canBOwrite && datum != null // Dan mogen we hier niet zijn toch .. ?
                          });

            FCLTcalendar( "date_to",
                          { label: L("lcl_faq_vervaldatum"),
                            datum: vervaldatum != null? new Date(vervaldatum) : null,
                            initEmpty: (vervaldatum == null),
                            readonly: !this_faq.canBOwrite
                          });
            RWFIELDTR("fac_bron", "fld", L("lcl_faq_bron"), source);

            if (S("language_toggles").length > 0)
            {
                var sql = "SELECT '', '', 1 FROM DUAL";
                for (var i = 0; i< S("language_toggles").length; i++)
                {
                    sql += (sql != ""? " UNION ": "") + "SELECT " + safe.quoted_sql(S("language_toggles")[i]) + ", " + safe.quoted_sql(S("language_toggles")[i]) + ", 2 FROM DUAL";
                }
                sql += " ORDER BY 3, 2";
                FCLTselector("fac_faq_lang", sql,
                               { initKey: lang,
                                 label: L("lcl_faq_lang")
                               });
            }
            FCLTautorisatieselector("fac_functie",
                                    "sgAuthorization",
                                    { label: L("lcl_usrrap_functie"),
                                      autorisatieKey: functie_key,
                                      whenEmpty: L("lcl_all")
                                    });
        BLOCK_END();

        generateFlexKenmerkCode({
            faq_key: faq_key,
            disc_key: disc_key
        });

        IFACE.FORM_END();
%>
      </form>
      <iframe src="../Shared/empty.html" name="hidFrameSubmit" style="display:none"></iframe>
<%
    SUBFRAME_END();
%>
  </body>
</html>
<% ASPPAGE_END(); %>
