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

    File:         ins_change_srtdeel.asp
    Description:  Verander het objectsoort
                  Impliciet: ins_deel.ins_srtdeel_key wordt aangepast
                  Expliciet:
    Parameters:
      ins_key       deel key
      srtdeel_new   Nieuwe srtdeel
      submitting    Mode {0=invoer / 1=opslaan}

    Context:      ins_show_deel.asp  tonen object
                  ins_list.inc       multi wijzigen
    Note:

*/ %>

<!-- #include file="../Shared/common.inc" -->
<!-- #include file="../Shared/iface.inc" -->
<!-- #include file="../Shared/save2db.inc" -->
<!-- #include file="../Shared/disciplineselector.inc" -->
<!-- #include file="../Shared/srtgroepselector.inc" -->
<!-- #include file="../Shared/srtdeelselector.inc" -->
<!-- #include file="./ins.inc" -->

<%
FCLTHeader.Requires({ plugins:["jQuery"]
                    , js: ["../ins/ins_edit_deel.js"] //, "../ins/bind.js"]
                    });

var autfunction = "WEB_INSMAN";
var authparams = user.checkAutorisation(autfunction);

var ins_key_arr = getQParamIntArray("ins_key");
var tobechanged = 0;
var ingesloten = [];
// Bepaal de objecten in de selectie die ook echt gewijzigd kunnen of mogen worden.
for (var i = 0; i < ins_key_arr.length; i++)
{
  var this_ins = ins.func_enabled_deel(ins_key_arr[i]);
  if (this_ins.canChange)
  {
    ingesloten.push(ins_key_arr[i]);
    tobechanged++;
  }
}
user.anything_todo_or_abort(tobechanged > 0);

%>

<html>
  <head>
<%  FCLTHeader.Generate();
    var submitting = getQParamInt("submit", 0) == 1;
    if (submitting)
    {
        protectRequest.validateToken();
        var success = false;
        var srtdeel_new = getFParamInt("srtdeel_new"); // Nieuwe deelsoort
        for (var i=0; i<ingesloten[i]; i++)
        {
            var sql_upd = "BEGIN ins.altersrtdeel(" + ingesloten[i] + ", " + srtdeel_new + "); END;";
            var err = Oracle.Execute(sql_upd, true);

            // Controle of ins_srtdeel_key gewijzigd is.
            var sql = "SELECT d.ins_srtdeel_key"
                    + "     , s.ins_srtdeel_omschrijving"
                    + "  FROM ins_deel d"
                    + "     , ins_srtdeel s"
                    + " WHERE d.ins_srtdeel_key = s.ins_srtdeel_key"
                    + "   AND d.ins_deel_key = " + ingesloten[i];
            var oRs = Oracle.Execute(sql);
            success = success || (oRs("ins_srtdeel_key").Value === srtdeel_new);
            var srt_oms = oRs("ins_srtdeel_omschrijving").Value || "";
            oRs.Close();
        }
        var warning = (success ? L("lcl_obj_is_inssuptrack").format((ingesloten.length==1?srt_oms:"")) : L("lcl_obj_srtdeel_changed_err"));
%>
        <script type="text/javascript">
            FcltMgr.closeDetail(window, { refresh: true, warning: "<%=safe.jsstring(warning)%>" } );
        </script>
<%
        Response.End;
    }
%>
    <script type="text/javascript">
        async function ins_submit()
        {
            document.activeElement.blur(); // trigger laatste onChanges
            if (!await validateForm("u2"))
                return false;
            if (parseInt($("#srtdeel").val()) != parseInt("<%=srtdeel_key%>"))
                $("#srtdeel_new").val($("#srtdeel").val());
            document.forms.u2.submit();
        }
        function ins_cancel()
        {
            FcltMgr.closeDetail(window);
        }
    </script>
  </head>

  <body class="fclt-modal" id="mod_changesrtdeel">
    <% MODAL_START(); %>
    <form name="u2"
          action="ins_change_srtdeel.asp?submit=1<%=buildTransitParam(["ins_key"])%>"
          method="post"
          onSubmit="ins_submit();">
      <input type="hidden" id="srtdeel_new" name="srtdeel_new" value="-1">
<%
      // Bepaal grootste gemene deler van objecten mbt discipline, soortgroep en soortdeel.
      var ggd = { disc: { key: -1, cnt: 0}
                , grp:  { key: -1, cnt: 0}
                , srt:  { key: -1, cnt: 0}
                };
      var sql = "SELECT DISTINCT"
              + "       g.ins_discipline_key"
              + "     , s.ins_srtgroep_key"
              + "     , d.ins_srtdeel_key"
              + "  FROM ins_deel d"
              + "     , ins_srtdeel s"
              + "     , ins_srtgroep g"
              + " WHERE d.ins_srtdeel_key = s.ins_srtdeel_key"
              + "   AND s.ins_srtgroep_key = g.ins_srtgroep_key"
              + "   AND d.ins_deel_key IN (" + ingesloten.join(",") + ")"
              + " ORDER BY g.ins_discipline_key"
              + "        , s.ins_srtgroep_key"
              + "        , d.ins_srtdeel_key";
      var oRs = Oracle.Execute(sql);
      while (!oRs.eof)
      {
          var srtdeel_key = oRs("ins_srtdeel_key").Value;
          if (ggd.srt.key != srtdeel_key)
          {
              ggd.srt.key = srtdeel_key;
              ggd.srt.cnt++;
              var srtgroep_key = oRs("ins_srtgroep_key").Value;
              if (ggd.grp.key != srtgroep_key)
              {
                  ggd.grp.key = srtgroep_key;
                  ggd.grp.cnt++;
                  var disc_key = oRs("ins_discipline_key").Value;
                  if (ggd.disc.key != disc_key)
                  {
                      ggd.disc.key = disc_key;
                      ggd.disc.cnt++;
                  }
              }
          }
          oRs.MoveNext();
      }
      oRs.Close();

      if (ggd.disc.cnt == 1) {
        lcl.set_dialect(ggd.disc.key, "INS_DISCIPLINE_KEY");
      }

      MODAL_BLOCK_START("srtdeelInfo", L("lcl_select_srtdeel"), { icon: "fa-file-signature" });
        FCLTdisciplineselector("disc",
                               "sgDisc",
                               { label: L("lcl_discipline"),
                                 disc_key: (ggd.disc.cnt == 1 ? ggd.disc.key : -1),
                                 module: "INS",
                                 perslidKey: user_key,
                                 autfunctionKey: authparams.autfunctionkey,
                                 extracode: "A",
                                 onChange: "onChangeDiscipline", // disgroepChanged
                                 required: true
                               });

        FCLTsrtgroepselector("srtgroep",
                             "sgSrtgroep",
                             { label: L("lcl_obj_group"),
                               srtgroep_key: (ggd.grp.cnt == 1 ? ggd.grp.key : -1),
                               autfunction: autfunction,
                               onChange: "onChangeSrtgroep",
                               urlAdd: [{urlParam: "disc_key", field: "disc"}],
                               required: true
                             });

        FCLTsrtdeelselector("srtdeel",
                            "sgSrtdeel",
                            { label: L("lcl_obj_sort"),
                              srtdeel_key: (ggd.srt.cnt == 1 ? ggd.srt.key : -1),
                              autfunction: autfunction,
                              onChange: "onChangeSrtdeel",
                              urlAdd: [{urlParam: "srtgroep_key", field: "srtgroep"},
                                       {urlParam: "disc_key", field: "disc"}],
                              extraParamField: "binding",
                              extracode: "B",
                              module: "INS",
                              required: true
                            });

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