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

    File:           api_ressoap_info.asp

    Description:    Planbord achtige informatie achterhalen.
    Parameters:     apiname, apikey
    Context:        Door een remote systeem (geen persoon) om info uit FACILITOR te halen aan te roepen

    Notes:
*/
    JSON_Result = true;
    LCL_Disable = 1
    THIS_FILE = "appl/api/api_ressoap_info.asp";

//    Session("logging")=1;

%>
<!-- #include file="../Shared/common.inc" -->
<!-- #include file="../Shared/json2.js" -->
<!-- #include file="../api/api.inc" -->
<!-- #include file="../Shared/discxalg3d.inc"-->
<!-- #include file="../fac/prodsearch.inc"-->
<!-- #include file="../res/res.inc" -->
<%
    Session.Codepage = 65001;
    Response.Charset = 'utf-8';
    Session.Abandon(); // Voor de zekerheid

try
{
    var API = new API_func();   // Valideert ook

    var asJSON = getQParam("json","0")!="0";

    var params = RequestJSON();
    if (!params)
        API.error("Error parsing input JSON");

    // FAC_API_OPTIONS_JSON
    var urole = "fe"; // Altijd?
    var autfunction = urole == "fe"? "WEB_RESUSE" : "WEB_RESFOF";
    params.authparams = user.checkAutorisation(autfunction, null, null, true); // pessimistisch

/*
    xparams = {id: "tab_room_sched",
                  res_van: new Date(2013, 11, 3, 08, 00),
                  res_tot: new Date(2013, 11, 3, 17, 00),
                  loc: 71, // Alkmaar
                  bld: -1,
                  ruimtecat: [],
                  res_ruimte_key: -1,
                  rsv_ruimte_key: -1,
                  vis: 1, // vis,
                  activiteit_key: 1, // UWVA vergadering
                  srtact: -1,  // niet nodig als activiteit_key?
                  forSelectRoom: true,
                  authparams: user.checkAutorisation(autfunction, null, null, true), // pessimistisch
                  urole: urole
              };
*/
    var headerinfo = {
//                      file: String(Request.ServerVariables("SCRIPT_NAME")).toLowerCase(),
//                      datum: new Date(),
                      naam: user.naam(),
                      custId: customerId,
                      language: user.lang()
                     }

    params.anonymous = true; // erg anoniem
    params.get_deepurl = true; // we willen graag een bookmark

    if (params.prs_externId && !(params.prs_key > 0))
    {
        var sql = "SELECT prs_perslid_key"
                + "  FROM prs_perslid"
                + " WHERE prs_perslid_oslogin = " + safe.quoted_sql("_" + params.prs_externId);
        var oRs = Oracle.Execute(sql);
        if (!oRs.Eof)
        {
            params.prs_key = oRs("prs_perslid_key").Value;
        }
        oRs.Close()
    }

    switch (params.request)
    {
        case "plan_rooms_info":
            plan_info = res.plan_rooms_info(params);
            set_deep_info(params, plan_info);
            break;
        case "plan_bezet_info":
            plan_info = res.plan_bezet_info(params);
            break;
        case "prodsearch_rooms_info":
            var w_discs = write_discs();
            var filter = {  s:      params.s
                         ,  soort: { val: "resruimte",  lcl: L("lcl_srch_entiteit") }
                         ,  satisfaction: params.satisfaction
                         ,  locatieKey: -1
                         ,  gebouwKey: -1
                         ,  verdiepingKey: -1
                         };

            var inc_queries = ["resruimte"];
            var queries = prodsearch_queries(w_discs, filter);
            var q_items = { alg_locatie_plaats: params.alg_locatie_plaats };
            var sql = prodsearch_sql(filter, queries, w_discs, q_items, inc_queries);
            var oRs = Oracle.Execute(sql);
            var plan_info = Oracle.rs2hash(oRs);
            oRs.Close();
            set_deep_info(params, plan_info);
            break;
        case "user_lopend_info": // Alle lopende reserveringen van params.prs_externId

            plan_info = [];
            if (params.prs_key > 0)
            {
                plan_info = res.user_lopend_info(params);
            }
            break;
        default:
            API.error("Unknown API request: " + params.request);
    }

    Response.ContentType = "application/json";
    Response.AddHeader("Access-Control-Allow-Origin", "*"); // Opdat FireFox cross-domain toestaat

var result = { header: headerinfo,
               success: true
             }
    result[params.request] = plan_info;

    var antwoord = JSON.stringify(result, null, getQParam("pretty","0")=="1"?2:0);

    var oCrypto = new ActiveXObject("SLNKDWF.Crypto");
    var eTag = '"' + oCrypto.hex_sha1(antwoord).toLowerCase() + '"';
    Response.AddHeader("ETag", eTag);
    if (Request.ServerVariables("HTTP_IF_NONE_MATCH") == eTag)
    {   // We hebben een match! Effectief besparen wel alleen op dataverkeer, de queries zijn al geweest
        Response.Clear();
        Response.Status = "304 Not modified";
        Response.End;
    }
    if (API.apidata.loglevel) __Log2File(antwoord, API.APIname + "_OUT");
    Response.write(antwoord);
}
catch (e)
{
    API.error(typeof e == "string"? e : "API_error: " + e.description);
}

function set_deep_info(params, plan_info)
{
    // Bepaal de URL waar user straks op kan klikken
    if (params.mobile)
    {
        var deepurl = HTTP.urlzelf() + "/appl/pda/reservering.asp?fac_id=" + customerId;
    }
    else
    {
        var deepurl = HTTP.urlzelf() + "/?fac_id=" + customerId + "&u=reservering";
    }
    for (var r in plan_info)
    {
        plan_info[r].deepurl = deepurl;
        if (plan_info[r].image)
            plan_info[r].image_url = HTTP.urlzelfnoroot() + S("res_image_path") + plan_info[r].image;
        else if (plan_info[r].foto) // prodsearch
            plan_info[r].image_url = HTTP.urlzelfnoroot() + S("res_image_path") + plan_info[r].foto;
    }
}
%>