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

    File:               get_mld_info_ajax.asp
    Description:        Ophalen van allerlei info om client-side data te hebben
    Parameters:         req_info:   "mld_stdmelding"    --> requires "mld_stdmelding_key"

    Context:
    Note:
    Sample:             appl/mld/get_mld_info_ajax.asp?req_info=mld_stdmelding&mld_stdmelding_key=1

*/ %>

<%
var JSON_Result = true;
%>

<!-- #include file="../Shared/common.inc"-->
<!-- #include file="./mld.inc" -->
<!-- #include file="../prs/prs.inc" -->
<!-- #include file="../ins/ins.inc" -->
<!-- #include file="../fac/faq.inc" -->
<!-- #include file="../Shared/selector.inc" -->
<!-- #include file="../Shared/get_objecten_sql.inc" -->
<!-- #include file="../Shared/xml_converter.inc" -->
<!-- #include file="../Shared/iface.inc" -->

<%
var req_info = getQParam("req_info");
var result = {};
switch (req_info)
{
    case "mld_status":
    {
        protectQS.verify({ allowparams: ["req_info"]});
        var mld_key = getQParamInt("mld_key", -1, true); // Als niet meegegeven doen we unlink.
        var sql = "SELECT m.mld_melding_status FROM mld_melding m WHERE mld_melding_key = " + mld_key;
        var oRs = Oracle.Execute(sql);
        if (oRs.eof)
          result = null;
        else
          result.mld_status = oRs("mld_melding_status").Value;
        oRs.Close();
        break;
    }
    case "mld_discipline":
    {
        var mld_ins_discipline_key = getQParamInt("mld_ins_discipline_key");
        result = ins.ins_disciplineinfo(mld_ins_discipline_key);
        break;
    }
    case "mld_ins_discipline":
    {
        var ins_discipline_key = getQParamInt("stdm_ins_discipline_key", -1);
        // Wil ik info over vakgroep of behandelteam?
        var behandelteam = (ins_discipline_key == -1 ? 1 : 0);
        if (behandelteam)
            ins_discipline_key = getQParamInt("mld_ins_discipline_key");

        var stdmChanged = getQParam("stdmChanged", false) ? 1 : 0;
        var sql = "SELECT " + (behandelteam
                              ? "DECODE(COALESCE(sd.ins_srtdiscipline_prefix, ''), '', '', sd.ins_srtdiscipline_prefix || '-') || "
                                + lcl.xsqla('md.ins_discipline_omschrijving', 'md.ins_discipline_key')
                              : "dp.mld_disc_params_srtgroep")
                + "  FROM mld_disc_params dp"
                + "     , mld_discipline md"
                + "     , ins_srtdiscipline sd"
                + " WHERE dp.mld_ins_discipline_key = " + ins_discipline_key
                + "   AND dp.mld_ins_discipline_key = md.ins_discipline_key"
                + "   AND md.ins_srtdiscipline_key = sd.ins_srtdiscipline_key"
                + (behandelteam ? "  AND BITAND(dp.mld_disc_params_srtgroep, 2) = 2" : "")
        var oRs = Oracle.Execute(sql);
        if (oRs.eof)
          result = null;
        else
        {
          result = {};
          if (stdmChanged)
              result.stdmChanged = 1;
          if (behandelteam)
          {
              result.def_ins_disc_key   = ins_discipline_key;
              result.def_ins_disc_descr = oRs("ins_discipline_omschrijving").Value;
          }
          else
              result.srtgroep = oRs("mld_disc_params_srtgroep").Value;
        }
        oRs.Close();
        break;
    }
    case "mld_limiet": // Returned 'Standaardopdrachtbedrag' voor een bedrijf als deze een geldige waarde heeft.
    {
        var u_key = getQParamInt("u_key", -1, true);
        var sql = "SELECT prs_bedrijf_mld_limiet"
                + "  FROM prs_bedrijf"
                + " WHERE prs_bedrijf_mld_limiet IS NOT NULL"
                + "   AND prs_bedrijf_key = " + u_key;
        var oRs = Oracle.Execute(sql);
        if (!oRs.eof) {
          result.mld_limiet = oRs("prs_bedrijf_mld_limiet").Value;
        }
        oRs.Close();
        break;
    }
    case "mld_stdmelding":
    {
        var mld_stdmelding_key = getQParamInt("mld_stdmelding_key");
        var urole = getQParamSafe("urole", "fe");
        result = mld.mld_stdmeldinginfo(mld_stdmelding_key);
        result.copy = getQParamInt("copy", 0) == 1;
        result.init = getQParamInt("init", 0) == 1;
        var taggable_select_params = {
            "stdm_taggable": result.taggable,
            "srtdisc_key": result.ins_srtdiscipline_key,
            "stdmeldinggroep_key": result.stdmeldinggroep_key
        }
        result.tag_select_html = mld.taggable_select_html(urole, taggable_select_params);

        var caller_key = getQParamInt("caller_key");
        var caller_kpn_key = -1;
        var caller_kpn_string = "";
        var caller_kpn_module = "";
        if (caller_key && caller_key > 0)
        {
          var prs_info = new prs.prs_perslid(caller_key);
          result.caller_kpn_key = prs_info.kostenplaats_key;
          result.caller_kpn_string = prs_info.kpn_string;
          result.caller_kpn_module = prs_info.kpn_module
        }
        break;
    }
    case "get_objecten":
    {
        var alg_key = getQParamInt("alg_key");
        var alg_niveau = getQParam("alg_niveau");
        var prs_key = getQParamInt("prs_key");
        var stdmld_key = getQParamInt("stdmld_key");

        var sql = get_objecten_sql(alg_key, alg_niveau, prs_key, null, stdmld_key, {extracode: "UITV"});
        var oRs = Oracle.Execute(sql);
        var result = [];
        var first = true;
        while(!oRs.eof)
        {
          if (first)
          {
            result.push({ key: oRs("ins_deel_key").Value,
                          omschrijving: oRs("deel").Value,
                          alg_ruimte_key: oRs("ins_alg_ruimte_key").Value,
                          alg_ruimte_type: oRs("ins_alg_ruimte_type").Value,
                          alg_ruimte_nr: oRs("alg_ruimte_nr").Value,
                          changelevel: alg_niveau,
                          uitvoertijd: oRs("uitvoertijd").Value
                        });
            first = false
          }
          else // Dan is alleen de key voldoende
            result.push({ key: oRs("ins_deel_key").Value });
          oRs.MoveNext();
        }
        oRs.Close();
        break;
    }
    case "get_object_by_key":
    {
        var alg_key = getQParamInt("alg_key");
        var alg_niveau = getQParam("alg_niveau");
        var prs_key = getQParamInt("prs_key");
        var stdmld_key = getQParamInt("stdmld_key");
        var ins_key = getQParamInt("ins_key");

        var sql = get_objecten_sql(alg_key, alg_niveau, prs_key, null, stdmld_key, {extracode: "UITV"});
        var oRs = Oracle.Execute(sql);
        var result = [];
        while(!oRs.eof)
        {
            if (ins_key == oRs("ins_deel_key").Value)
                result.push({ key: oRs("ins_deel_key").Value,
                              omschrijving: oRs("deel").Value,
                              alg_ruimte_key: oRs("ins_alg_ruimte_key").Value,
                              alg_ruimte_type: oRs("ins_alg_ruimte_type").Value,
                              alg_ruimte_nr: oRs("alg_ruimte_nr").Value,
                              changelevel: alg_niveau,
                              uitvoertijd: oRs("uitvoertijd").Value
                            });
            else
                result.push({ key: oRs("ins_deel_key").Value });
            oRs.MoveNext();
        }
        oRs.Close();
        break;
    }
    case "faq_info":
    {
        var ins_disc_key = getQParamInt("ins_disc_key", -1); // vakgroep
        var stdm_key = getQParamInt("stdm_key", -1);
        var loc_key = getQParamInt("loc_key", -1);
        var alg_key_arr = getQParamIntArray("alg_key_arr", []);
        var afdeling_key = getQParamInt("afd_key", -1);
        var changed = getQParamInt("changed", 2);

        var result = mld.faq_info(loc_key, alg_key_arr, ins_disc_key, stdm_key, -1, afdeling_key, changed);

        for (i in result.fixed_faq)
        {
            result.fixed_faq[i].safe_answer = safe.fclthtml(result.fixed_faq[i].answer, { "faqlink": true, "file_params": { "module": "FAQA", "key": result.fixed_faq[i].faq_key }});
            result.fixed_faq[i].safe_answer2 = safe.fclthtml(result.fixed_faq[i].answer2, { "faqlink": true, "file_params": { "module": "FAQB", "key": result.fixed_faq[i].faq_key }});
        }

        result.loc_key      = loc_key;
        result.alg_key_arr  = alg_key_arr;
        result.afdeling_key = afdeling_key;
        result.ins_disc_key = ins_disc_key;
        result.stdm_key     = stdm_key;
        result.changed      = changed;

        break;
    }
    case "alg_algfaq_info":
    {
        var alg_key = getQParamInt("alg_key", -1);
        var loc_key = getQParamInt("loc_key", -1);

        var result = faq.alg_algfaq_info(loc_key, alg_key);

        break;
    }
    case "calc_einddatum":
    {   // Einddatum van melding bepalen.
        var startdatum = getQParamDate("startdatum", new Date());
        var srtdisc_key = getQParamInt("srtdisc");
        var disc_key = getQParamInt("disc");
        var stdm_key = getQParamInt("stdm_key");
        var prio = getQParamInt("prio", 3);
        var geb_key = getQParamInt("geb_key", -1);
        var rui_key = getQParamInt("rui_key", -1);
        var ins_key = getQParamInt("ins_key", -1);
        // Wat mag ik zoal op een nieuwe melding.
        if (disc_key < 0)
          var this_mld = mld.func_enabled_mld(srtdisc_key, "S");
        else
          var this_mld = mld.func_enabled_mld(disc_key, "D");
        // Berekenen van de einddatum.
        var einddatum = mld.geteinddatum(startdatum, stdm_key, prio, geb_key, rui_key, ins_key);
        result = { einddatum: einddatum.getTime(), canFOwrite: this_mld.canFOwrite, canBOwrite: this_mld.canBOwrite };
        break;
    }
    case "calc_opdr_einddatum_sla_lev":
    {   // De einddatum van de opdracht bepalen voor Leverancier-SLA .
        // Leverancier-SLA: dus (sla_mode == 1)
        protectQS.verify({ allowparams: ["req_info", "dienst_key", "uitv_key", "startdatum"]});
        var mld_key = getQParamInt("mld_key");
        var dienst_key = getQParamInt("dienst_key");
        var uitv_key = getQParamInt("uitv_key");
        var startdatum = getQParamDate("startdatum", new Date());
        var mld_info = mld.mld_melding_info(mld_key);
        result = { uitvoertijd: null, mld_uitvoertijd_eenheid: mld_info.uitvoertijd_eenheid };

        if (uitv_key > 0)
          result.uitvoertijd = mld.getbedrijfuitvoertijd(uitv_key, mld_key, dienst_key);
        var einddatum = mld.geteinddatumopdracht(mld_key, result.uitvoertijd, startdatum, 1); // sla_mode is 1.
        result.einddatum = einddatum? einddatum.getTime() : null;
        break;
    }
    case "ggd":
    {
        var obj_keys = getQParamIntArray("obj_keys");
        if (obj_keys.length > 0)
        {
          var insGGD = ins.getGGD(obj_keys);
          result = insGGD;
        }
        break;
    }
    case "show_after_instem":
    {
        protectQS.verify({ allowparams: ["req_info"]});
        var mld_key = getQParamInt("mld_key", -1);
        var mfe = mld.func_enabled_melding(mld_key);

        result.show = mfe.canReadAny || mfe.canChangeKenmerkBlok;
        if (!mfe.canReadAny) {
            result.toaster = L("lcl_mld_ingestemd").format(mld.mld_prefix(mld_key) + mld_key);
        }

        break;
    }
    case "show_after_reject":
    {
        protectQS.verify({ allowparams: ["req_info"]});
        var mld_key = getQParamInt("mld_key", -1);
        var mfe = mld.func_enabled_melding(mld_key);

        result.show = mfe.canReadAny || mfe.canChangeKenmerkBlok;
        if (!mfe.canReadAny) {
            result.toaster = L("lcl_mld_is_mldrej");
        }

        break;
    }
    case "show_after_fwd":
    {
        protectQS.verify({ allowparams: ["req_info"]});
        var mld_key = getQParamInt("mld_key", -1);
        var mfe = mld.func_enabled_melding(mld_key);

        result.show = mfe.canReadAny || mfe.canChangeKenmerkBlok;
        if (!mfe.canReadAny)
            result.toaster  = L("lcl_forwarded");

        break;
    }
    case "show_after_upd":
    {
        var mld_key = getQParamInt("mld_key", -1);
        var mfe = mld.func_enabled_melding(mld_key);

        result.show = mfe.canReadAny || mfe.canChangeKenmerkBlok;
        break;
    }
    case "mld_stdmelding_list":
    {
        var niveau = getQParam("niveau");
        var parent_key = getQParamIntArray("parent_key", []);
        var child_key = getQParamIntArray("child_key", []);
        var init_key = getQParamInt("init_key", -1);
        var sql = "";

        switch (niveau)
        {
            case "D":
            {
                sql = "SELECT d.ins_discipline_key  key"
                    + "     , d.ins_discipline_omschrijving  oms"
                    + "     , d.ins_srtdiscipline_key  s_key"
                    + "     , null d_key"
                    + "  FROM mld_v_aanwezigdiscipline d, mld_disc_params dp"
                    + " WHERE d.ins_discipline_key = dp.mld_ins_discipline_key"
                    + "   AND BITAND(dp.mld_disc_params_srtgroep, 1) = 1"
                    + (parent_key.length ? "   AND d.ins_srtdiscipline_key IN ("  + parent_key.join(",") + ")" : "")
                    + "  ORDER BY d.ins_discipline_omschrijving ASC";
                break;
            }
            case "M":
            {
                sql = "SELECT m.mld_stdmelding_key  key"
                    + "     , d.ins_discipline_omschrijving || '/' || m.mld_stdmelding_omschrijving  oms"
                    + "     , d.ins_srtdiscipline_key  s_key"
                    + "     , d.ins_discipline_key     d_key"
                    + "  FROM mld_stdmelding m"
                    + "     , mld_v_aanwezigdiscipline d"
                    + "     , mld_disc_params dp"
                    + " WHERE m.mld_ins_discipline_key = d.ins_discipline_key"
                    + "   AND d.ins_discipline_key = dp.mld_ins_discipline_key"
                    + "   AND BITAND(dp.mld_disc_params_srtgroep, 1) = 1"
                    + (parent_key.length ? " AND d.ins_srtdiscipline_key  IN (" + parent_key.join(",") + ")" : "")
                    + (child_key.length ?  " AND m.mld_ins_discipline_key IN (" + child_key.join(",")  + ")" : "")
                    + " ORDER BY d.ins_discipline_omschrijving ASC"
                    + "        , m.mld_stdmelding_omschrijving ASC";
                break;
            }
        }

        var new_lov = [];
        var oRs = Oracle.Execute(sql);
        while(!oRs.eof)
        {
            new_lov.push({key: oRs("key").Value,
                          oms: oRs("oms").Value,
                          sel: oRs("key").Value == init_key,
                          atr: { srtdisc_key: oRs("s_key").Value,
                                 disc_key: oRs("d_key").Value
                               }
                         });
            oRs.MoveNext();
        }
        oRs.Close();
        result.niveau = niveau;
        result.lov = new_lov;
        break;
    }
    case "locked":
    {
        var mld_key = getFParamInt("module_key", -1);
        var result = {module_key: mld_key, locked: false};
        var sql = "SELECT m.mld_melding_locked_user_key"
                + "     , m.mld_melding_locked_date"
                + "     , (SELECT p.prs_perslid_naam_full"
                        + "  FROM prs_v_perslid_fullnames_all p"
                        + " WHERE p.prs_perslid_key = m.mld_melding_locked_user_key"
                        + ") mld_melding_locked_user_name"
                + "  FROM mld_melding m"
                + " WHERE m.mld_melding_locked_user_key IS NOT NULL"
                + "   AND m.mld_melding_locked_user_key <> " + user_key
                + "   AND m.mld_melding_locked_date IS NOT NULL"
                + "   AND m.mld_melding_key = " + mld_key;
        var oRs = Oracle.Execute(sql);
        if (!oRs.eof)
        {
            var expire = new Date;
            expire.setMinutes(expire.getMinutes() - S("mld_locked_expire"));
            result.locked = oRs("mld_melding_locked_user_key").Value && oRs("mld_melding_locked_date").Value >= expire;
            result.locked_user = oRs("mld_melding_locked_user_key").Value;
            result.locked_date = toTimeString(oRs("mld_melding_locked_date").Value);
            result.locked_name = new Perslid(result.locked_user).naam();
        }
        oRs.Close();
        break;
    }
    case "promptAI":
    {
        protectQS.verify();
        protectRequest.validateToken();
        var mld_key = getQParamInt("mld_key");
        var useCase = getFParamSafe("for");
        if (useCase == "afmelden") {
            var json = mld.getmldjson(mld_key);
            result = shared.promptAI(JSON.stringify(json), { "instructions": L("lcl_mld_ai_afmeldtekst_instructions") });
        } else if (useCase == "summary") {
            var json = mld.getmldjson(mld_key);
            result = shared.promptAI(JSON.stringify(json), { "instructions": L("lcl_mld_ai_summarize_instructions") });
        }
        break;
    }
    case "get_icon_picker":
    {
        var iconPicker = ICONPICKERTR_HTML('default', L("lcl_mld_symbol_name"), null, { "readonly": false });
        result.iconPicker = iconPicker
        break;
    }
}

Response.Write(JSON.stringify(result));
%>
<% ASPPAGE_END(); %>
