<%@language = "javascript" %>

<!-- #include file="../scf/scaffolding.inc" -->
<!-- #include file="../api2/model_ins_srtcontrole.inc" -->

<%
var taskdiscipline = getQParamInt("taskdiscipline", -1);
var objectdiscipline  = getQParamInt("objectdiscipline", -1);
var objectgroup  = getQParamInt("objectgroup", -1);
var objecttype  = getQParamInt("objecttype", -1);
var canImport = user.has("WEB_IMPORT");

if (mode != "save" && mode != "delete" && mode != "api2")
{ %>
  <script>
    var mode = "<%=mode%>";
  </script>
<%
}

var ins_srtcontrole_key = getQParamInt("id", -1);
var sql = "SELECT cdp.ctr_disc_params_ismjob"
        + "     , isc.ins_srtcontrole_eenheid"
        + "     , isc.ins_srtcontrole_bits"
        + "     , isc.ins_srtcontrole_mode"
        + "  FROM ins_srtcontrole isc"
        + "     , ctr_disc_params cdp"
        + " WHERE isc.ctr_discipline_key = cdp.ctr_ins_discipline_key"
        + "   AND isc.ins_srtcontrole_key = " + ins_srtcontrole_key;
var oRs = Oracle.Execute(sql);
var ismjob = false;
var bits = -1;
var eenheid = -1;
var ctrmode = -1;
if (!oRs.eof)
{
  var ismjob = oRs("ctr_disc_params_ismjob").Value == 1;
  eenheid = oRs("ins_srtcontrole_eenheid").Value;
  bits = oRs("ins_srtcontrole_bits").Value;
  ctrmode = oRs("ins_srtcontrole_mode").Value;
}
oRs.Close();

var this_model = new model_ins_srtcontrole();

function canChangeCycle(srtcontrole_arr)
{ // Kan ik de cyclus (eenheid en/of  periode) aanpassen?
  // De eenheid en periode van de taak (ins_srtcontrole) kunnen niet aangepast worden als:
  // 1) er geen xcp record is bij de taak van een object,
  // 2) er een xcp record is bij de taak van een object waarbij de periode niet is aangepast,
  // 2) er een xcp record is bij de taak van een object waarbij de eenheid niet is aangepast en het NIET gaat om een mjob taak,
  sql = "SELECT COUNT(idsc.ins_deelsrtcontrole_key) aantal"
      + "  FROM ins_deelsrtcontrole idsc"
      + "     , ins_srtcontroledl_xcp xcp"
      + "     , ins_srtcontrole isc"
      + "     , ctr_disc_params cdp"
      + " WHERE idsc.ins_deel_key = xcp.ins_deel_key(+)"
      + "   AND idsc.ins_srtcontrole_key = xcp.ins_srtcontrole_key(+)"
      + "   AND idsc.ins_scenario_key = xcp.ins_scenario_key(+)"
      + "   AND xcp.ins_srtcontrole_key = isc.ins_srtcontrole_key(+)"
      + "   AND isc.ctr_discipline_key = cdp.ctr_ins_discipline_key(+)"
      + "   AND idsc.ins_srtcontrole_key IN (" + srtcontrole_arr.join(",") + ")"
      + "   AND idsc.ins_scenario_key = 1"
      + "   AND idsc.ins_deelsrtcontrole_status IN (2, 3, 5)"
      + "   AND (cdp.ctr_disc_params_ismjob IS NULL OR ins_srtcontroledl_xcp_periode IS NULL OR (cdp.ctr_disc_params_ismjob = 0 AND ins_srtcontroledl_xcp_eenheid IS NULL))";
  var oRs = Oracle.Execute(sql);
  var aantal = oRs("aantal").Value;
  oRs.Close();
  // Als er geen lopende taken zijn dan kan ik de cyclus (eenheid en/of  periode) aanpassen.
  return aantal == 0;
}
var canChangeCycle = canChangeCycle([ins_srtcontrole_key]);

String.prototype.capitalizeTxt = function()
{
  return this.charAt(0).toUpperCase() + this.slice(1);
}

function defineMoment(readonly)
{
  if (readonly && ctrmode == 0) // Moment mode(0).
  { // Get waarde van moment.
    var bitsNoHoli = "";
    var bitsDay = [];
    var bitsWeek = "";
    var bitsMaand = [];
    var bitsText = "";
    switch (eenheid)
    {
      case 0: // Uurlijks.
              bitsNoHoli = (bits & 128) == 128? L("lcl_ins_controle_notholiday") : L("lcl_ins_controle_also_holiday");
              bitsText = bitsNoHoli;
              break;
      case 1: // Dagelijks.
              bitsNoHoli = (bits & 128) == 128? L("lcl_ins_controle_notholiday") : L("lcl_ins_controle_also_holiday");
              bitsText = bitsNoHoli;
              break;
      case 2: // Wekelijks.
              if ((bits & 1) == 1) bitsDay.push(safe.html(calendar_names.days[0]));
              if ((bits & 2) == 2) bitsDay.push(safe.html(calendar_names.days[1]));
              if ((bits & 4) == 4) bitsDay.push(safe.html(calendar_names.days[2]));
              if ((bits & 8) == 8) bitsDay.push(safe.html(calendar_names.days[3]));
              if ((bits & 16) == 16) bitsDay.push(safe.html(calendar_names.days[4]));
              if ((bits & 32) == 32) bitsDay.push(safe.html(calendar_names.days[5]));
              if ((bits & 64) == 64) bitsDay.push(safe.html(calendar_names.days[6]));

              bitsNoHoli = (bits & 128) == 128? ", " + L("lcl_ins_controle_notholiday").toLowerCase() : ", " + L("lcl_ins_controle_also_holiday").toLowerCase();
              bitsText = (bitsDay.join(", ").capitalizeTxt() + bitsNoHoli);
              break;
      case 3: // Maandelijks.
              // Er kan maar 1 mogelijkheid geselecteerd zijn voor de week en voor de dag.
              bitsWeek = ((bits & 256) == 256? L("lcl_ins_controle_eerste") : "")
                       + ((bits & 512) == 512? L("lcl_ins_controle_tweede") : "")
                       + ((bits & 768) == 768? L("lcl_ins_controle_derde") : "")
                       + ((bits & 1024) == 1024? L("lcl_ins_controle_vierde") : "")
                       + ((bits & 1280) == 1280? L("lcl_ins_controle_vijfde") : "")
                       + ((bits & 1536) == 1536? L("lcl_ins_controle_eennalaatste") : "")
                       + ((bits & 1792) == 1792? L("lcl_ins_controle_laatste") : "");
              bitsDay = ((bits & 1) == 1? " " + safe.html(calendar_names.days[0]) : "")
                      + ((bits & 2) == 2? " " + safe.html(calendar_names.days[1]) : "")
                      + ((bits & 4) == 4? " " + safe.html(calendar_names.days[2]) : "")
                      + ((bits & 8) == 8? " " + safe.html(calendar_names.days[3]) : "")
                      + ((bits & 16) == 16? " " + safe.html(calendar_names.days[4]) : "")
                      + ((bits & 32) == 32? " " + safe.html(calendar_names.days[5]) : "")
                      + ((bits & 64) == 64? " " + safe.html(calendar_names.days[6]) : "");
              bitsNoHoli = (bits & 128) == 128? ", " + L("lcl_ins_controle_notholiday").toLowerCase() : ", " + L("lcl_ins_controle_also_holiday").toLowerCase();
              bitsText = bitsWeek + bitsDay + " " + L("lcl_ins_controle_period_ofthemonth") + bitsNoHoli;
              break;
      case 4: // Jaarlijks.
              if ((bits & 1) == 1) bitsMaand.push(safe.html(calendar_names.months[0]));
              if ((bits & 2) == 2) bitsMaand.push(safe.html(calendar_names.months[1]));
              if ((bits & 4) == 4) bitsMaand.push(safe.html(calendar_names.months[2]));
              if ((bits & 8) == 8) bitsMaand.push(safe.html(calendar_names.months[3]));
              if ((bits & 16) == 16) bitsMaand.push(safe.html(calendar_names.months[4]));
              if ((bits & 32) == 32) bitsMaand.push(safe.html(calendar_names.months[5]));
              if ((bits & 64) == 64) bitsMaand.push(safe.html(calendar_names.months[6]));
              if ((bits & 128) == 128) bitsMaand.push(safe.html(calendar_names.months[7]));
              if ((bits & 256) == 256) bitsMaand.push(safe.html(calendar_names.months[8]));
              if ((bits & 512) == 512) bitsMaand.push(safe.html(calendar_names.months[9]));
              if ((bits & 1024) == 1024) bitsMaand.push(safe.html(calendar_names.months[10]));
              if ((bits & 2048) == 2048) bitsMaand.push(safe.html(calendar_names.months[11]));
              bitsText = bitsMaand.join(", ").capitalizeTxt();
              break;
    }

    this_model.fields._moment =  { "dbs": "",
                                   "label": L("ins_srtcontrole_moment"),
                                   "typ": "label",
                                   "defaultvalue": "defaultvalue",
                                   "labelvalue": bitsText
                                 }
  }
  else if (!readonly)
  {
    this_model.fields._moment =  { "dbs": "",
                                   "label": L("ins_srtcontrole_moment"),
                                   "icon": "fa-calendar",
                                   "typ": "button2",
                                   "defaultvalue": L("lcl_select"),
                                   "track": true
                                 }
  }

}

if (mode != "save")
{
    this_model.hook_pre_show = function (obj, fld)
    {
      defineMoment(true);
    }

    this_model.hook_pre_edit = function (obj, fld)
    {
      if (!canChangeCycle)
      {
        this_model.fields.inspectionmode.readonly = true;
        this_model.fields.period.readonly = true;
        this_model.fields.unit.readonly = true;
      }
      defineMoment(!canChangeCycle);
%>
      <script>
        var srtinstallatieselector_available = 1; // toon alleen actuele disciplines/groepen en objecten.
      </script>
<%
    }

    this_model.hook_pre_multi_edit = function(fld)
    {
      var ins_srtcontrole_key_arr = getQParamIntArray("sel_id", []);
      if (!canChangeCycle)
      {
        this_model.fields.period.multiedit = false;
      }
    }

    this_model.hook_pre_search = function(fld)
    {
%>
  <script>
    var srtinstallatieselector_available = 1; // toon alleen actuele disciplines/groepen en objecten.
  </script>
<%
    }
}
else
{
  defineMoment(false);
}

var this_params =
{
    "search": {
        "autosearch": true,
        "filters": [
            "taskdiscipline",
            "objectdiscipline",
            "objectgroup",
            "objecttype",
            "level",
            "name"
        ],
        "transit": [
            "taskdiscipline",
            "objectdiscipline",
            "objectgroup",
            "objecttype"
        ],
        "requires": {
            "js": [
                "../mgt/srtinstallatieselector.js",
                "../mgt/mgt_tools.js"
            ]
        }
    },
    "list": {
        "columns": [
            "id",
            "level",
            "objectdiscipline",
            "objectgroup",
            "objecttype",
            "name",
            "taskdiscipline",
            "period",
            "unit"
       ],
       "requires": {
         js: ["./ins_srtcontrole.js"]
       }
    },
    "edit": {
        "requires": {
            "js": [
                "../mgt/srtinstallatieselector.js",
                "../mgt/ins_srtcontrole.js",
                "../mgt/mgt_tools.js"
            ]
        }
    },
    "layout": {
        "block": [
            {
                "blockid": "insSrtControle",
                "label": L("ins_srtcontrole"),
                "icon": "fa-history fa-flip-horizontal",
                "fields": [
                    "id",
                    "level",
                    "objectkey",
                    "objectdiscipline",
                    "objectgroup",
                    "objecttype",
                    "taskdiscipline",
                    "name",
                    "info"
                ]
            },
            {
                "blockid": "insPlanning",
                "label": L("lcl_ins_controle_planning"),
                "icon": "fa-calendar-day",
                "fields": [
                    "inspectionmode",
                    "period",
                    "unit",
                    "bits",
                    "_moment",
                    "priority",
                    "enddate",
                    "phasingouttime"
                ]
            },
            {
                "blockid": "insCost",
                "label": L("lcl_ins_kosten"),
                "icon": "fa-euro-sign",
                "fields": [
                    "account",
                    "costs",
                    "costs2",
                    "costs3",
                    "material",
                    "hours",
                    "percentage",
                    "vat"
                ]
            },
            {
                "blockid": "insAfhandeling",
                "label": L("lcl_ins_controle_mode"),
                "icon": "fa-check",
                "fields": [
                    "taskgroup",
                    "issuetype",
                    "service",
                    "controlemode",
                    "options",
                    "remarktext",
                    "remark"
                ]
            }
        ]
    },
    "wrap": {
        "frames": []
    }
};

// Er moeten wel actieve kosten velden aanwezig zijn anders heeft de button geen zin.
var ins_xcp_active_cost = S("ins_xcp_active_cost");
if (ins_xcp_active_cost > 0 && ins_xcp_active_cost < 16)
if (canImport) {
    this_params.list.roundtripCode = "INSPECTIE";
}

{ // Voeg een knop voor wijzigen van de dienst(-locatie)-scope toe.
  this_params.list.buttons = [ { action: "ins_pricechange(" + taskdiscipline + ", " + objectdiscipline + ", " + objectgroup + ", " + objecttype + ")",
                                 title: L("lcl_mjb_price_change"),
                                 icon: "fa-fclt-edit",
                                 id: "btn_price_change" } ];
}

var id = getQParamInt("id", -1);
if (id > 0)
{
    var urole = getQParamSafe("urole", "fe");
    var page = protectQS.create("../shared/status_info.asp?inssc_key=" + id + "&urole=" + urole);
    this_params.wrap.frames.push({
        "url": page,
        "icon": "fa-list",
        "title": L("lcl_tracking"),
        "autfunction": this_model.authfunction
    });
}

scaffolding(this_model, this_params);

%><% ASPPAGE_END(); %>
