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

<!-- #include file="../Shared/common.inc" -->
<!-- #include file="../Shared/iface.inc" -->
<!-- #include file="../Shared/disciplineselector.inc" -->
<!-- #include file="../Shared/selector.inc" -->

<%
FCLTHeader.Requires({ plugins: ["jQuery"],
                      js: [] });
var urole = getQParamSafe("urole");
var mld_key = getQParamInt("mld_key");
var faq_key = -1;
//var faq_question = unescape(getQParam("faq_question"));
//var faq_answer = unescape(getQParam("faq_answer"));
//var faq_perslid_key = getQParamInt("faq_perslid_key");

var authparams = user.checkAutorisation("WEB_FAQFOF");
%>

<html>
  <head>
<%  FCLTHeader.Generate(); %>

    <script type="text/javascript">
      function onChangeCatalogus()
      {
        var disc_key = sgDisc.getValue();
        loadKenmerk(disc_key);
      }

      function loadKenmerk(disc_key)
      {
        var s = "../fac/load_kenmerk.asp?faq_key=-1"
              + "&disc_key=" + disc_key;
        $.get(s, flexReloaded);
      }

      async function faq_save()
      {
           document.activeElement.blur(); // trigger laatste onChanges
           if (!await validateForm("u2"))
               return false;
           document.faqform.submit();
      }

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

      $(function()
      {
        $('textarea').resize(function () { FcltMgr.resized(window) } );
        $('textarea').autogrow();
      });
    </script>
  </head>

  <body class="fclt-modal" id="mod_mldcontinue">
<% MODAL_START();
    var sgQuestion = "";
    var sgAnswer = "";
    if (S("ai_enabled") & 2) {
      var json = mld.getmldjson(mld_key);
      if (json.notities.length > 0) { // Anders niet genoeg context voor AI
        var result = shared.promptAI(JSON.stringify(json), { "instructions": L("lcl_mld_ai_faq_instructions"), "response_format": "json_object" });
        if (result.success) {
          try {
            var content = JSON.parse(result.content);
            sgQuestion = content.question;
            sgAnswer = content.answer;
          } catch (e) {
            // Dan hieronder de ouderwetse manier
          }
        }
      }
    }
    if (sgQuestion == "" || sgAnswer == "") {
      sql = "SELECT mld_melding_omschrijving"
          + "     , mld_melding_opmerking"
          + "  FROM mld_melding mm"
          + "     , mld_stdmelding std"
          + "     , mld_discipline md"
          + "     , ins_srtdiscipline sd"
          + " WHERE mld_melding_key = " + mld_key
          + "   AND mm.mld_stdmelding_key = std.mld_stdmelding_key"
          + "   AND std.mld_ins_discipline_key = md.ins_discipline_key"
          + "   AND md.ins_srtdiscipline_key = sd.ins_srtdiscipline_key";
      oRs = Oracle.Execute(sql);
      sgQuestion = (oRs("mld_melding_omschrijving").value || "").substring(0, 400);
      sgAnswer = oRs("mld_melding_opmerking").value || "";
      oRs.close();
    }
%>
    <form id="faqform" name="faqform" action="mld_2faq_save.asp?mld_key=<%=mld_key%>&urole=<%=urole%>" method="post">
<%
      MODAL_BLOCK_START("mld2faq", L("lcl_faq_item"), { "icon": "fa-book" });
         FCLTdisciplineselector("fac_disc",
                                "sgDisc",
                                { label: L("lcl_discipline"),
                                  disc_key: -1,
                                  emptyOption: "",
                                  module: "FAQ",
                                  perslidKey: user_key,
                                  onChange: "onChangeCatalogus",
                                  required: true,
                                  autfunctionKey: authparams.autfunctionkeys
                               });
         RWTEXTAREATR("fac_question", "fldtxt required", L("lcl_faq_question"), sgQuestion, {html: "rows='6'"});
         RWTEXTAREATR("fac_answer", "fldtxt required", L("lcl_faq_answer"), sgAnswer, {html: "rows='6' maxlength='4000'"});
         RWTEXTAREATR("fac_answer2", "fldtxt", L("lcl_faq_answer2"), "", {html: "rows='3' maxlength='4000'"});
         var sql= "           SELECT 1, " + safe.quoted_sql(L("lcl_faq_level1")) + " FROM DUAL"
                + " UNION ALL SELECT 2, " + safe.quoted_sql(L("lcl_faq_level2")) + " FROM DUAL"
                + " UNION ALL SELECT 3, " + safe.quoted_sql(L("lcl_faq_level3")) + " FROM DUAL"
         RWTEXTAREATR("fac_hint", "fldtxt", L("lcl_faq_hint"), "",{no_bb_codes: true});

         FCLTselector("fac_level",
                      sql,
                      {label: L("lcl_faq_level")
                      }
                     );
         CHECKBOXTR(L("lcl_faq_stdmelding"), "", "fac_link", 1, {});
      MODAL_BLOCK_END();
%>    <flex-marker></flex-marker><% // Flex-marker div voor dynamisch herladen.

      var buttons =  [ {title: L("lcl_submit"), icon: "fa-fclt-save", action: "faq_save()", importance: 1 },
                       {title: L("lcl_cancel"), icon: "fa-fclt-cancel",    action: "faq_cancel()", importance: 3 } ];
      SIMPLE_BLOCK_START();
        CreateButtons(buttons);
      SIMPLE_BLOCK_END();
IFACE.FORM_END();
%>
    </form>
    <% MODAL_END(); %>
  </body>
</html><% ASPPAGE_END(); %>
