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

     Accepteer een melding door FO (alleen direct na invoer) of BO (vanuit lijst of expliciet)
     Zet de huidige user (die kennelijk dus een behandelaar kan zijn) direct als de behandelaar.
     Deze eerste behandelaar wordt vooralsnog niet expliciet getrackt, dat wordt geimpliceerd
     door het accepteren
*/ %>

<!-- #include file="../Shared/common.inc" -->
<!-- #include file="../Shared/iface.inc" -->
<!-- #include file="../Shared/stdmeldingselector.inc" -->
<!-- #include file="../Shared/persoonselector.inc" -->
<!-- #include file="../Shared/json2.js" -->
<!-- #include file="../Shared/save2db.inc" -->
<!-- #include file="mld.inc" -->

<%
FCLTHeader.Requires({ plugins: ["jQuery"] });

var mld_key_arr = getQParamIntArray("mld_key");
var multi = getQParamInt("multi", 0) == 1;
var maybeDouble = getQParamInt("maybeDouble", 0) == 1;
var urole = getQParam("urole", "bo");
var opmerk = getFParam("opmerk", "");
var fronto = urole == "fo";
var backo  = urole == "bo";

var autfunction = "";
if (fronto)
  autfunction = "WEB_MLDFOF";
else if (backo)
  autfunction = "WEB_MLDBOF";
else
  autfunction = "WEB_MLDUSE";

var TransitParam = buildTransitParam(["mld_key", "multi", "maybeDouble", "urole", "opmerk"]);

// Geen autfunction bepaald. Je komt bij FIN niet binnen met een bepaalde rol (urole).
// Alleen PRSreadlevel en PRSwritelevel van belang want fac_functie_min_level = 7 (lezen en schrijven organisatie)
var authparamsMLDBOF = user.checkAutorisation("WEB_MLDBOF", true);
var authparamsMLDBO3 = user.checkAutorisation("WEB_MLDBO3", true);
var authparamsMLDFOF = user.checkAutorisation("WEB_MLDFOF", true);


var result = {};
var toberejected = 0;
var ingesloten = [];
// Bepaal de meldingen in de selectie die
// ook echt geaccepteerd kunnen of mogen worden.
for (var i = 0; i < mld_key_arr.length; i++)
{
  var this_mld = mld.func_enabled_melding(mld_key_arr[i]); // Wat heb ik zoal aan rechten op deze specifieke melding
  var mld_info = mld.mld_melding_info(mld_key_arr[i]);
  var mld_std = mld.mld_stdmeldinginfo(mld_info.stdm);

  var maybeDouble = (!mld_info.parent_key && (mld_std.doublecheck == 2 || mld_std.doublecheck == 3));
  // Geen child melding en bulk actie mag de maybeDouble meldingen niet op geaccepteerd zetten
  if (this_mld.canAccept && !mld_info.parent_key && !(multi && maybeDouble))
  {
    ingesloten.push(mld_key_arr[i]);
    toberejected++;
  }
}
user.anything_todo_or_abort(toberejected > 0); // We klagen niet over enkele wel en enkele niet

var mld_melding = mld.mld_melding_info(ingesloten[0]); // Neem voor informatie de eerste melding.
lcl.set_dialect(mld_melding.srtdisc, "MLD_SRTDISCIPLINE_KEY");
%>
<html>
	<head>
<%	FCLTHeader.Generate(); %>
		<script type="text/javascript">
      jQuery(document).ready(function()
                             {
                               $('textarea').resize(function () { FcltMgr.resized(window) } );
                               $('textarea').autogrow();
                               FcltMgr.fixActiveelementError();
                               $("#opmerk").focus();
                             });

			function mld_accept()
			{
				if (!validateForm("u2"))
					return false;

        $.post($("form[name=acceptform]")[0].action, $("[name=acceptform]").serialize() + "<%=TransitParam%>", FcltCallbackClose, "json");
        return true; // disable button
			}

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

		</script>
	</head>

	<body class="modal" id="mod_mldaccept">

    <form id="acceptform" name="acceptform" action="mld_accept_save.asp" method="post">
<%
      var mld_stdm = "";
      var prefix = "";
      var oldRemark = "";
      var ins_discipline_omschrijving = "";
      var req = false;
      if (ingesloten.length == 1)
      {
        sql = "SELECT sm.mld_ins_discipline_key"
            + "     , sm.mld_stdmelding_key"
            + "     , mm.mld_melding_opmerking"
       		  + "     , " + lcl.xsqla('md.ins_discipline_omschrijving', 'md.ins_discipline_key')
            + "     , sd.ins_srtdiscipline_prefix"
            + "  FROM mld_melding mm"
            + "     , mld_stdmelding sm"
            + "     , mld_discipline md"
            + "     , ins_srtdiscipline sd"
            + " WHERE mld_melding_key = " + ingesloten[0]
            + "   AND mm.mld_stdmelding_key = sm.mld_stdmelding_key"
            + "   AND sm.mld_ins_discipline_key = md.ins_discipline_key"
            + "   AND md.ins_srtdiscipline_key = sd.ins_srtdiscipline_key";
        oRs = Oracle.Execute(sql);

        mld_stdm = oRs("mld_stdmelding_key").value;
        prefix = oRs("ins_srtdiscipline_prefix").value || "";
        oldRemark = oRs("mld_melding_opmerking").value;
        ins_discipline_omschrijving = oRs("ins_discipline_omschrijving").value;
        oRs.close();
      }
			BLOCK_START("mldAccept", L("lcl_menu_mld_bo_accept") + (ingesloten.length == 1? " " + prefix + ingesloten[0]: ""));

        // <!-- Meldingen (Discipline) -->
        FCLTstdmeldingselector("stdm", "sgStdm", {label: L("lcl_complain"),
                                                  perslidKey: user_key,
                                                  autfunctionKey: -1, // toch readonly
                                                  //srtdisc_key: srtdisc, // je mag switchen naar een andere soort TODO: Autorisatie?
                                                  stdmelding_key: (ingesloten.length == 1? mld_stdm : -1),
                                                  onChange: "onChangeStdMelding",
                                                  readonly: true } );
        // Vakgroep
        RWFIELDTR("disc", "flddisc", L("lcl_vakgroup"), (ingesloten.length == 1 && prefix? prefix + "-" : "") + (ingesloten.length == 1? ins_discipline_omschrijving : ""), {readonly: true})

        // Behandelaar
        if (this_mld.canBOwrite || this_mld.canFOwrite)
        {
%>
          <input type="hidden" id="prs_key"  name="prs_key"  value="<%=mld_info.melder_key%>">
          <input type="hidden" id="disc_key" name="disc_key" value="<%=mld_info.disc%>">
          <input type="hidden" id="loc_key"  name="loc_key"  value="<%=mld_info.loc_key%>">
<%
          var urlAdd = [{urlParam: "prs_key",  field: "prs_key",  init: mld_info.melder_key},
                        {urlParam: "disc_key", field: "disc_key", init: mld_info.disc }];

          if (mld_info.show_alg)
            urlAdd.push({urlParam: "loc_key",  field: "loc_key", init: mld_info.loc_key});

          FCLTpersoonselector("sBehandel",
                                "sgBehandelaar",
                                { perslidKey: user_key,
                                  label: L("lcl_mld_behandelaar"),
                                  fieldNameKey: "sBehandel_key",
                                  autlevel: this_mld.authparams(autfunction) && this_mld.authparams(autfunction).PRSwritelevel,
                                  urlAdd: urlAdd,
                                  required: true,
                                  readonly: !this_mld.canAfhandChange,
                                  filtercode: (mld_info.mld_status == 0 ? "BEHF" : "BEHB")
                                });
        }

        // Reden accepteren
%>
      <tr><td class="label"><label></label></td>
          <td ><%=safe.html(oldRemark)%></td>
      </tr>
<%
				RWTEXTAREATR("opmerk", "fldremark" + (req?" required":""), L("lcl_mld_inf_Opmerking"), "", {  html: "rows='8'" });

      BLOCK_END();
      var buttons =  [ {title: L("lcl_accept"), icon: "accept.png", action: "mld_accept()", singlepress: true, id: "btn_accept_submit"  },
                       {title: L("lcl_cancel"), icon: "cancel.png", action: "mld_cancel()" } ];
      CreateButtons(buttons);
IFACE.FORM_END();
%>
    </form>
  </body>
</html>
