<% /*
     $Revision$
     $Id$

    File:               job_list.inc
    Description:
    Parameters:
    Context:
    Note:
*/ %>

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

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

function fnrowData(oRs) {
  var data = { job_key: oRs("fac_activiteit_key").Value
             , eenheid: oRs("fac_activiteit_eenheid").Value
             , template_key: oRs("rkey").Value
             };
  return JSON.stringify(data);
}

%>
<script type="text/javascript">
  function jobDelete(rowArray, isMulti)
  {
    FcltMgr.confirm(L("lcl_job_del_txt_job"), { autoconfirm: isMulti }, function() {
        var jobKeyString = getKeyString(rowArray);
        var data = {
            key: jobKeyString,
            level: "J"
        };
        <% protectRequest.dataToken("data"); %>
        $.post("job_delete.asp",
            data,
            FcltCallbackRefresh,
            "json");
    });
  }
  function jobEdit(row)
  {
    var jobData = JSON.parse(row.getAttribute("ROWDATA"));
    var isTemplate = (jobData.eenheid == 5);
    var url = "appl/fac/job.asp?job_key=" + jobData.job_key + (isTemplate ? "&template=1" : "");
    FcltMgr.openDetail(url, (isTemplate ? L("lcl_job_template") : L("lcl_job_title")) + " " + jobData.job_key);
  }
  function jobStart(row)
  { // Alleen voor sjablonen (jobData.eenheid == 5).
    // Maak een kopie van de melding en alle bijbehorende opdrachten.
    var jobData = JSON.parse(row.getAttribute("ROWDATA"));
    var url = "appl/mld/mld_melding.asp?urole=fo&mld_key=" + jobData.template_key + "&mld_copy=1&template=1&act_key=" + jobData.job_key;
    FcltMgr.openDetail(url,  L("lcl_mld_copy") + " " + L("lcl_shared_complain").toLowerCase());
  }

  var autosearch = 1; // automatisch laden van resultaat op search pagina
</script>
<%

function job_list(pautfunction, params)
{
  // De functie checkAutorisation(autfunction) geeft een Response.End indien er geen autorisatie voor autfunction wordt gevonden
  var addButton = [];
  var job_info = job.func_enabled_job();
  user.auth_required_or_abort(job_info.canReadAny);

  // Voor resulttable de globalen zetten; zou ng anders moeten.
  var outputmode = params.outputmode; // print to 0 = screen, 1 = printer, 2 = excel, etc.
  var showall = params.showall;

  var isTemplate = params.template == 1;
  var act_key = params.act_key
  var prs_prskey = (isTemplate ? (params.prs_prskey ? params.prs_prskey : -1) : (job_info.has_PRSSYS ? params.prs_prskey : user_key));
  var rtype_key = params.rtype_key
  var cat_key = params.cat_key
  var prod_key = params.prod_key
  var stat_key = params.stat_key
  var fronto = params.urole == "fo";

  function fnrowActionEnabler(oRs) {
    var isTemplate = oRs("fac_activiteit_eenheid").Value == 5;
    eDelete = (fronto ? false : true);
    eCopy = isTemplate && (oRs("rkey").Value != null);
    return ({ eDelete: eDelete, eCopy: eCopy });
  }
%>

<html>
  <head>
    <% FCLTHeader.Generate({ outputmode: outputmode }) %>
  </head>
  <body id="listbody">
<%
    var activiteit_sql = "(SELECT fac_activiteit_key"                            // 0 (id)
                       + "      , fac_activiteit_eerste"                         // 1 (Eerste keer)
                       + "      , fac_activiteit_laatste"                        // 2 (Laatste keer)
                       + "      , fac_activiteit_eenheid"                        // 3 (Eenheid)
                       + "      , fac_activiteit_periode"                        // 4 (Periode)
                       + "      , fac_activiteit_einddatum fac_activiteit_einde" // 5 (Eind)
                       + "      , " + S("prs_pers_string") + " aanvrager "       // 6 (Aanvrager)
                       + "      , fac_activiteit_xmlnode"                        // 7 (Type)
                       + "      , 'Product' type_omschrijving"                   // 8 (Product)
                       + "      , stat.stat_omschrijving"                        // 9 (Status oms)
                       + "      , stat.stat_key"                                 // 10 (Status key)
                       + "      , ''"                                            // 11 (Free)
                       + "      , ''"                                            // 12 (Free)
                       + "      , p.prs_afdeling_key"                            // 13 (Afdeling key)
                       + "      , fac_activiteit_aantal"                         // 14 (Aantal keer)
                       + "      , fac_activiteit_volgende"                       // 15 (Volgende keer)
                       + "      , fac.calcnextcyclusdate(TRUNC(fac_activiteit_eerste, 'DD') - 1,"
                       + "                               0," //  -- mode: moment-modus
                       + "                               COALESCE(fac_activiteit_eenheid, 1),"
                       + "                               fac_activiteit_periode,"
                       + "                               fac_activiteit_bits) volgende_planning"
                       + "      , p.prs_perslid_key"
                       + "      , fac_activiteit_omschrijving"
                       + "   FROM fac_activiteit act "
                       + "      , (SELECT 1 stat_key, " + safe.qL("lcl_job_stat_1_inactief") + " stat_omschrijving"
                       + "           FROM DUAL"
                       + "          UNION ALL"
                       + "         SELECT 2, " + safe.qL("lcl_job_stat_2_actief")
                       + "           FROM DUAL "
                       + "          UNION ALL "
                       + "         SELECT 3, " + safe.qL("lcl_job_stat_3_1xoverslaan")
                       + "           FROM DUAL) stat"
                       + "      , prs_perslid p"
                       + "  WHERE	act.fac_activiteit_verwijder IS NULL"
                       + "    AND act.fac_activiteit_status_key = stat.stat_key(+)"
                       + "    AND act.prs_perslid_key           = p.prs_perslid_key)";

    // Categorie
    var sql_dis_aut = "(SELECT m1.ins_discipline_key"
                    + "      , " + lcl.xsqla("m1.ins_discipline_omschrijving", "m1.ins_discipline_key")
                    + "   FROM mld_v_aanwezigdiscipline m1"
                    + "  WHERE EXISTS (SELECT 'x'"
                    + "                  FROM mld_stdmelding ms"
                    + "                 WHERE M1.ins_discipline_key = ms.mld_ins_discipline_key)"
                    + "    AND m1.ins_discipline_key IN (SELECT ins_discipline_key"
                    + "                                    FROM fac_v_my_disciplines"
                    + "                                   WHERE prs_perslid_key = " + user_key
                    + "                                     AND fac_functie_code IN (" + (fronto ? "'WEB_MLDFOF'" : "'WEB_MLDFOF', 'WEB_ORDBOF'") + ")"
                    + "                                 )"
                    + " ORDER BY 2)";

    // Product
    var sql_prd_aut = "(SELECT m1.ins_discipline_key"
                     + "      ," + lcl.xsqla("m1.ins_discipline_omschrijving", "m1.ins_discipline_key")
                     + "   FROM BES_v_aanwezigdiscipline m1"
                     + "  WHERE m1.ins_discipline_key IN (SELECT ins_discipline_key FROM fac_v_my_disciplines"
                     + "                                   WHERE fac_functie_code = '" + "WEB_BESFOF" + "'"
                     + "                                     AND prs_perslid_key=" + user_key
                     + ")"
                     + " ORDER BY 2) ";


    var no_vakgroep_product_sql_bes = "         SELECT fac_activiteit_key"
                                + "              , -1 rkey"
                                + "              , -1"
                                + "              , ''"
                                + "              , 0 rtable"
                                + "              , 0 read_write"
                                + "           FROM bes_bestelling WHERE fac_activiteit_KEY IS NOT NULL";
    var no_vakgroep_product_sql_mld = "         SELECT fac_activiteit_key"
                                + "              , -1 rkey"
                                + "              , -1"
                                + "              , ''"
                                + "              , 0 rtable"
                                + "              , 0 read_write"
                                + "           FROM mld_melding WHERE fac_activiteit_KEY IS NOT NULL";
    var no_vakgroep_product_sql_ord = "         SELECT fac_activiteit_key"
                                + "              , -1 rkey"
                                + "              , -1"
                                + "              , ''"
                                + "              , 0 rtable"
                                + "              , 0 read_write"
                                + "           FROM mld_opdr WHERE fac_activiteit_KEY IS NOT NULL"

    var no_vakgroep_product_sql_arr = [];
    if (!isTemplate) {
      no_vakgroep_product_sql_arr.push(no_vakgroep_product_sql_mld);
      no_vakgroep_product_sql_arr.push(no_vakgroep_product_sql_ord);
      no_vakgroep_product_sql_arr.push(no_vakgroep_product_sql_bes);
    }
    if (no_vakgroep_product_sql_arr.length > 0) {
        var vakgroep_product_sql_non = " SELECT fa.fac_activiteit_key"
                                    + "      , -1 rkey"
                                    + "      , -1 ins_discipline_key"
                                    + "      , '' ins_discipline_omschrijving"
                                    + "      , 0 rtable"
                                    + "      , read_write"
                                    + "   FROM fac_activiteit fa, "
                                    + "        (" + no_vakgroep_product_sql_arr.join(" UNION ALL ")
                                    + "          GROUP BY fac_activiteit_KEY"
                                    + "        ) fa2"
                                    + "  WHERE fa.fac_activiteit_key =  fa2.fac_activiteit_key(+)"
                                    + "    AND fa2.fac_activiteit_key IS NULL";
    }

    var vakgroep_product_sql_bes = " SELECT bes.fac_activiteit_key"
                                 + "      , bes.bes_bestelling_key rkey"
                                 + "      , dis.ins_discipline_key"
                                 + "      , " + lcl.xsqla("dis.ins_discipline_omschrijving", "dis.ins_discipline_key")
                                 + "      , 1 rtable"
                                 + "      ," + (job_info.canBESWrite ? "1" : "0") + " read_write"
                                 + "  FROM BES_BESTELLING_ITEM bi"
                                 + "     , BES_BESTELLING bes"
                                 + "     , bes_srtDEEL sd"
                                 + "     , bes_srtGROEP sg"
                                 + "     , INS_TAB_DISCIPLINE dis"
                                 + "     ," + sql_prd_aut + " dis_aut"
                                 + " WHERE bes.fac_activiteit_key IS NOT NULL"
                                 + "   AND bes.bes_bestelling_key    = bi.bes_bestelling_key"
                                 + "   AND bi.bes_srtdeel_key        = sd.bes_srtdeel_key"
                                 + "   AND sd.bes_srtgroep_key       = sg.bes_srtgroep_key"
                                 + "   AND sg.ins_discipline_key     = dis.ins_discipline_key"
                                 + "   AND sg.ins_discipline_key     = dis_aut.ins_discipline_key"
                                 + " GROUP BY bes.fac_activiteit_key"
                                 + "     , bes.bes_bestelling_key"
                                 + "     , dis.ins_discipline_key"
                                 + "     , " + lcl.xsql("dis.ins_discipline_omschrijving", "dis.ins_discipline_key");

    var vakgroep_product_sql_mld = " SELECT m.fac_activiteit_key"
                                 + "      , m.mld_melding_key rkey"
                                 + "      , dis.ins_discipline_key"
                                 + "      , " + lcl.xsqla("dis.ins_discipline_omschrijving", "dis.ins_discipline_key")
                                 + "      , 2 rtable"
                                 + "      , " + (job_info.canMLDWrite ? "1" : "0") + " read_write"
                                 + "   FROM mld_melding m"
                                 + "      , mld_stdmelding sm"
                                 + "      , ins_tab_discipline dis"
                                 + "      , " + sql_dis_aut + " dis_aut"
                                 + "  WHERE m.fac_activiteit_key IS NOT NULL"
                                 + "    AND m.mld_stdmelding_key = sm.mld_stdmelding_key"
                                 + "    AND sm.mld_ins_discipline_key = dis.ins_discipline_key"
                                 + "    AND sm.mld_ins_discipline_key = dis_aut.ins_discipline_key"
                                 + " GROUP BY m.fac_activiteit_key"
                                 + "        , m.mld_melding_key"
                                 + "        , dis.ins_discipline_key"
                                 + "        , " + lcl.xsql("dis.ins_discipline_omschrijving", "dis.ins_discipline_key");

    var vakgroep_product_sql_ord = " SELECT o.fac_activiteit_key"
                                 + "      , o.mld_opdr_key rkey"
                                 + "      , dis.ins_discipline_key"
                                 + "      , "+ lcl.xsqla('dis.ins_discipline_omschrijving','dis.ins_discipline_key')
                                 + "      , 3 rtable "
                                 + "      , " + (job_info.canORDWrite ? "1" : "0") + " read_write"
                                 + "  FROM mld_melding r"
                                 + "     , mld_stdmelding sm"
                                 + "     , ins_tab_discipline dis"
                                 + "     , mld_opdr o"
                                 + "     , " + sql_dis_aut + " dis_aut"
                                 + " WHERE o.fac_activiteit_key IS NOT NULL"
                                 + "   AND r.mld_stdmelding_key = sm.mld_stdmelding_key"
                                 + "   AND r.mld_melding_key = o.mld_melding_key"
                                 + "   AND sm.mld_ins_discipline_key = dis.ins_discipline_key"
                                 + "   AND sm.mld_ins_discipline_key = dis_aut.ins_discipline_key"
                                 + " GROUP BY o.fac_activiteit_key"
                                 + "     , o.mld_opdr_key"
                                 + "     , dis.ins_discipline_key"
                                 + "     , " + lcl.xsql("dis.ins_discipline_omschrijving", "dis.ins_discipline_key");

    // rooster_table:
    //  0 = nothing
    //  1 = bes_bestelling
    //  2 = mld_melding
    //  3 = mld_opdr
    var vakgroep_product_sql_arr = (vakgroep_product_sql_non ? [vakgroep_product_sql_non] : []);
    if (isTemplate) {
      vakgroep_product_sql_arr.push(vakgroep_product_sql_mld);
    }
    else {
      if (job_info.canBESRead && ((rtype_key == 1) || (rtype_key == -1)))
        vakgroep_product_sql_arr.push(vakgroep_product_sql_bes);
      if (job_info.canMLDRead && ((rtype_key == 2) || (rtype_key == -1)))
        vakgroep_product_sql_arr.push(vakgroep_product_sql_mld);
      if (job_info.canORDRead && ((rtype_key == 3) || (rtype_key == -1)))
        vakgroep_product_sql_arr.push(vakgroep_product_sql_ord);
    }

    var sqln = "SELECT fa.fac_activiteit_key"
             + "     , TO_CHAR (fa.fac_activiteit_eerste, 'DD-MM-YYYY') fac_activiteit_eerste"
             + "     , TO_CHAR (fa.fac_activiteit_laatste, 'DD-MM-YYYY HH24:MI')  fac_activiteit_laatste"
             + "     , fa.fac_activiteit_eenheid"
             + "     , fa.fac_activiteit_periode"
             + "     , TO_CHAR (fa.fac_activiteit_einde, 'DD-MM-YYYY') fac_activiteit_einde"
             + "     , fa.aanvrager"
             + "     , fa.fac_activiteit_xmlnode"
             + "     , NVL (" + lcl.xsql("vp.ins_discipline_omschrijving", "vp.ins_discipline_key") + ", '') ins_discipline_omschrijving"
             + "     , fa.stat_omschrijving"
             + "     , fa.fac_activiteit_aantal"
             + "     , fa.fac_activiteit_volgende"
             + "     , volgende_planning"
             + "     , vp.rkey"
             + "     , vp.rtable"
             + "     , vp.read_write"
             + "     , fac_activiteit_omschrijving"
             + "  FROM " + activiteit_sql + " FA, "
             + "      (" + vakgroep_product_sql_arr.join(" UNION ALL ") + " ) VP "
             + " WHERE fa.fac_activiteit_key = vp.fac_activiteit_key" + (isTemplate ? "(+)" : "")
    if (isTemplate) {
      sqln += "   AND fa.fac_activiteit_eenheid = 5";
    } else {
      sqln += "   AND (fa.fac_activiteit_eenheid IS NULL OR fa.fac_activiteit_eenheid <> 5)";
    }
    sqln +=    ((prs_prskey > -1) ? "   AND fa.prs_perslid_key = "    + prs_prskey : "")
             + ((act_key > -1)    ? "   AND fa.fac_activiteit_key = " + act_key    : "")
             + ((cat_key > -1)    ? "   AND vp.ins_discipline_key = " + cat_key    : "")
             + ((prod_key > -1)   ? "   AND vp.ins_discipline_key = " + prod_key   : "")
             + ((stat_key > -1)   ? "   AND fa.stat_key = " + stat_key : "")
             + ((!isTemplate && rtype_key > -1) ? "   AND vp.rtable = " + rtype_key : "")
             + (fronto ? " AND vp.rkey IS NOT NULL" : "")
             + " ORDER BY fa.fac_activiteit_key DESC ";


    function fncolEenheid(oRs)
    {
      var eenheid = oRs("fac_activiteit_eenheid").Value;
      var eenheidoms = "";
      switch (eenheid)
      {
        case 1: eenheidoms = L("lcl_ins_controle_dagelijks");
                break;
        case 2: eenheidoms = L("lcl_ins_controle_wekelijks");
                break;
        case 3: eenheidoms = L("lcl_ins_controle_maandelijks");
                break;
        case 4: eenheidoms = L("lcl_ins_controle_jaarlijks");
                break;
        case 5: eenheidoms = L("lcl_ins_controle_aanvraag");
                break;
      }
      return eenheidoms;
    }
    function fncolVolgende(oRs)
    {
        var volgende = oRs("fac_activiteit_volgende").Value;
        if (volgende != null)
            return toDateString(volgende);
        var einde = oRs("fac_activiteit_einde").Value;
        if (einde == null || new Date(einde) > new Date())
            return "<i>" + toDateString(oRs("volgende_planning").Value) + "</i>";
        return "---";
    }


    var addButton = [];
    if (isTemplate)
    {
      if (!fronto) {
        var addurl = "appl/fac/job.asp?template=1";
        addButton = [{ icon: "fa-plus", title: L("lcl_add"), action: "FcltMgr.openDetail('" + addurl + "', '" + safe.jsstring(L("lcl_add")) + "')" }];
      }
    }
    else
    {
      if (job_info.canWriteAny)
      {
        var addurl = "appl/fac/job.asp"
        addButton = [{ icon: "fa-plus", title: L("lcl_add"), action: "FcltMgr.openDetail('" + addurl + "', '" + safe.jsstring(L("lcl_add")) + "')" }];
      }
    }

    var rst = new ResultsetTable({ sql: sqln,
                                   keyColumn: "fac_activiteit_key",
                                   ID: "workTable",
                                   outputmode: outputmode,
                                   showAll: showall,
                                   rowData: fnrowData,
                                   rowActionEnabler: fnrowActionEnabler,
                                   EmptySetString: "Geen berichten weer te geven",
                                   buttons: addButton
                                  });

    rst.addColumn(new Column({caption: (isTemplate ? L("lcl_job_template_nr") : L("lcl_job_nr")), content: "fac_activiteit_key", datatype: "number", thClass: "item-width-1"}));
    rst.addColumn(new Column({caption: L("lcl_descr"), content: "fac_activiteit_omschrijving", hasActions: true}));
    if (!isTemplate) {
      rst.addColumn(new Column({caption: L("lcl_job_status"), content: "stat_omschrijving"}));
      rst.addColumn(new Column({caption: L("lcl_job_firstdate"), content: "fac_activiteit_eerste"}));
      rst.addColumn(new Column({caption: L("lcl_job_lastdate"), content: "fac_activiteit_laatste"}));
      rst.addColumn(new Column({caption: L("lcl_job_next"), content: fncolVolgende}));
      rst.addColumn(new Column({caption: L("lcl_job_n_times"), content: "fac_activiteit_aantal", datatype: "number"}));
      rst.addColumn(new Column({caption: L("lcl_job_unit"), content: fncolEenheid}));
      rst.addColumn(new Column({caption: L("lcl_job_period"), content: "fac_activiteit_periode", datatype: "number"}));
      rst.addColumn(new Column({caption: L("lcl_job_end"), content: "fac_activiteit_einde"}));
      rst.addColumn(new Column({caption: L("lcl_job_requestor"), content: "aanvrager"}));
    }
    rst.addColumn(new Column({caption: L("lcl_job_category")+'/'+L("lcl_job_product"), content: "ins_discipline_omschrijving"}));

    if (fronto)
    {
      rst.addAction({ action: "jobStart",  caption: L("lcl_mobile_doemelding"), enabler: "eCopy", single: true });
      rst.addAction({ action: "jobEdit",   caption: L("lcl_details"), isDefault: true });
    }
    else
    {
      rst.addAction({ action: "jobStart",  caption: L("lcl_mobile_doemelding"), enabler: "eCopy", single: true});
      rst.addAction({ action: "jobEdit",   caption: L("lcl_details"), isDefault: true});
      rst.addAction({ action: "jobDelete", caption: L("lcl_delete"), enabler: "eDelete", multi: true, single: false, multiOnce: true});
    }
    var cnt = rst.processResultset();
%>
  </body>
</html>
<%}%>