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

    File:               job.asp
    Description:
    Parameters:
    Context:
    Note:

*/ %>

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

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

var job_key = getQParamInt("job_key", -1);
var template = getQParamInt("template", 0);
var act_xmlnode = "";
var act_omschr = "";
if (job_key > -1)
{
  var sql = "SELECT fac_activiteit_xmlnode"
          + "     , fac_activiteit_omschrijving"
          + "     , fac_activiteit_eenheid"
          + "  FROM fac_activiteit"
          + " WHERE fac_activiteit_key = " + job_key;
  var oRs = Oracle.Execute(sql);
  act_xmlnode = (oRs("fac_activiteit_xmlnode").Value || "").toLowerCase();
  act_omschr = oRs("fac_activiteit_omschrijving").Value;
  template = (oRs("fac_activiteit_eenheid").Value == 5 ? 1 : 0);
  oRs.Close();
}

%>
<html>
  <head>
<%  FCLTHeader.Generate(); %>
  </head>
  <body>
<%
    var pageHeader = (template == 0 ? L("lcl_job_title") : L("lcl_menu_fac_jobs_templates"));
    var jobFrameSrc = "../shared/empty.html";
    if (job_key == -1)
    {
      var job_info = job.func_enabled_job();

      // Dan maar vragen
      var buttons = [];
      if (template == 0)
      {
        if (job_info.canBESWrite)
          buttons.push({ title: L("lcl_bes_bestelling"),
                         icon: "fa-angle-right",
                         action: "rtable=1;$('div#dmodal').dialog('close').remove();",
                         showText: false,
                         rtable: 1
                       });

        if (job_info.canMLDWrite)
          buttons.push({ title: L("lcl_complain"),
                         icon: "fa-angle-right",
                         action: "rtable=2;$('div#dmodal').dialog('close').remove();",
                         showText: false,
                         rtable: 2
                       });

        if (job_info.canORDWrite)
          buttons.push({ title: L("lcl_bes_orders"),
                         icon: "fa-angle-right",
                         action: "rtable=3;$('div#dmodal').dialog('close').remove();",
                         showText: false,
                         rtable: 3
                       });
      }
      else
      {
        buttons.push({ title: L("lcl_job_template"),
                       icon: "fa-angle-right",
                       action: "rtable=4;$('div#dmodal').dialog('close').remove();",
                       showText: false,
                       rtable: 4
                     });
      }

      user.auth_required_or_abort(buttons.length);
      if (buttons.length === 1) {
        jobFrameSrc = "job_edit_job.asp?template=" + template + "&rtable=" + buttons[0].rtable;
      } else {
%>      <script>
          FcltMgr.startEdit(window);

          var rtable = -1;
          function dialogClose()
          {
            if (rtable == -1)
              FcltMgr.closeDetail(window, { cancel: true } );
            else
            {
              $("#jobFrame")[0].src = "job_edit_job.asp?job_key=<%=job_key%>&rtable="+rtable+"&template=<%=template%>";
            }
          }

          $(function()
          {
            var params = { title: L("lcl_job_select_action"),
                           modal: true,
                           width: "auto",
                           resizable: false,
                           position: { my: "center", at: "center", of: window },
                           close: dialogClose,
                           create: FcltMgr.fnCreateCloseButton("div#dmodal")
                         };
            $('div#dmodal').dialog(params).dialog('open');
          });
        </script>

        <div id="dmodal" style="display:none">
<%        CreateButtons(buttons, { vertical: 1, isDialog: true}); %>
        </div>
<%    }
    } else /* if (job_key > 0) */ {
      var this_job = job.func_enabled_job(job_key);
      jobFrameSrc = "job_show_job.asp";
      pageHeader = act_omschr || pageHeader;
      jobFrameSrc += (template == 1 ? "?template=1" : "");

      var plannedEntityPage, plannedTitle, frameName;
      switch (act_xmlnode) {
        case "bestelling":
          frameName = "besframe";
          if (this_job.ref_key === -1) {
            plannedEntityPage = "../bes/bes_bestelling.asp?urole=xx&act_key=" + job_key;
          } else {
            plannedTitle = L("lcl_bes_frame_algemeen_actie");
            plannedEntityPage = "../bes/bes_show_bestelling.asp?urole=xx&act_key=" + job_key + "&bes_key=" + this_job.ref_key;
          }
          break;
        case "melding":
          frameName = "mldframe";
          if (this_job.ref_key === -1) {
            plannedEntityPage = "../mld/mld_melding.asp?urole=xx&act_key=" + job_key;
          } else {
            plannedTitle = (template == 1 ? L("lcl_mld_frame_algemeen_sjabloon") : L("lcl_mld_frame_algemeen_actie"));
            plannedEntityPage = "../mld/mld_show_melding.asp?urole=xx&act_key=" + job_key + "&mld_key=" + this_job.ref_key + (template == 1 ? "&template=1" : "");
          }
          break;
        case "opdracht":
          frameName = "opdrframe";
          if (this_job.ref_key === -1) {
            plannedEntityPage = "../mld/mld_opdr.asp?urole=bo&act_key=" + job_key;
          } else {
            plannedTitle = L("lcl_opdr_frame_algemeen_actie");
            plannedEntityPage = "../mld/mld_show_opdr.asp?urole=bo&act_key=" + job_key + "&opdr_key=" + this_job.ref_key;
          }
          break;
      }
%>      <script>

<%    if (plannedEntityPage) {
        if (this_job.ref_key === -1) { %>
          $(_ => {
            // Activeer de entity subtab
            $("#frametabs").tabs({ "active": 1 });

            // Markeer entity subtab als missing
            $("ul.subtabs li[aria-controls=frm_<%=frameName%>] a .subtabextra")
              .html("<span class='subtab-badge-icon subtab-badge-missing'>" + I("fa-exclamation-triangle fa-lg", { "fastyle": "fas" }) + "</span>");

            // En 'hot'
            FcltMgr.startEdit(window.frames["<%=frameName%>"]);
          });
<%      } %>

          function closeEntityFrame(params) {
            if (params.success) {
              // Gewoon maar complete reload
              window.location.href = "../fac/job.asp?job_key=<%=job_key%>";
            } else if (params.cancel) {
<%            if (this_job.ref_key === -1) {
                // Komt als het goed is niet voor, alle 'annuleren' knoppen zijn hier weggehaald
              } else {
                if (act_xmlnode === "bestelling") { %>
              $("#besframe")[0].src = "../bes/bes_show_bestelling.asp?urole=xx&act_key=<%=job_key%>&bes_key=<%=this_job.ref_key%>";
<%              } else if (act_xmlnode === "melding") { %>
              $("#mldframe")[0].src = "../mld/mld_show_melding.asp?urole=xx&act_key=<%=job_key%>&mld_key=<%=this_job.ref_key%>";
<%              } else if (act_xmlnode === "opdracht") { %>
              $("#opdrframe")[0].src = "../mld/mld_show_opdr.asp?urole=bo&act_key=<%=job_key%>&opdr_key=<%=this_job.ref_key%>";
<%              }
              } %>
            }
          }
<%    } %>
        </script>
<%  } %>

        <script>
          function closeJobFrame(params) {
            if (params.keepForm) { // Er is iets fout bij het opslaan
              return;
            }

            if (params.success) {
              if (params.deleted) {
                FcltMgr.closeDetail(window);
                return true;
              }
            }

<%    if (job_key == -1)
      { %>
            if (params.cancel) {
              FcltMgr.closeDetail(window, params);
              return true;
            }

            // Altijd complete reload, we hebben nu een job_key namelijk
            window.location.href = "job.asp?job_key=" + params.job_key;
<%    }
      else
      { // Bestaande job bewerkt, switch naar show-mode
%>
          $("#jobFrame")[0].src = "job_show_job.asp?job_key=<%=job_key%>&template=1";
<%    } %>
          }
        </script>
<%  PAGE_START({ pageHeader: { icon: "fa-calendar-clock", title: pageHeader } });
      // 1. Het frame voor de planning
      jobFrameSrc += (jobFrameSrc.indexOf("?") === -1 ? "?" : "&") + "job_key=" + job_key;
      IFRAMER("jobFrame", jobFrameSrc, { FcltClose: "closeJobFrame", title: (template == 1 ? L("lcl_job_frame_template") : L("lcl_job_frame_algemeen")), icon: "fa-calendar-clock" } );

      // 2. Het frame voor de ingeplande taak
      if (plannedEntityPage) {
        // 2a. ingeplande taak of melding sjabloon
        IFRAMER(frameName, plannedEntityPage, { FcltClose: "closeEntityFrame", title: plannedTitle } );
        if (template == 1) {
          // 2b. opdrachten bij meldingsjabloon
          page = "../mld/opdr_list.asp?tiny=1&embedded=1&urole=bo&act_key="+job_key+"&mld_key=" + this_job.ref_key;
          IFRAMER("opdrlistframe", page, { title: L("lcl_ord_frame_algemeen_sjabloon"), refreshOnActivate: true, refreshOnClose: true, icon: "fa-list" } );
        }
      }

      // 3. Het frame voor de mutaties
      if (job_key > 0)
      {
        page = protectQS.create("../shared/status_info.asp?urole=xx&act_key=" + job_key);
        IFRAMER("acthistoryframe", page, { icon: "fa-list", title: L("lcl_tracking") } );
      }

    PAGE_END();
%>
  </body>
</html>
<% ASPPAGE_END(); %>
