<%@ page import="ro.emid.common.types.Category" %>
<%@ page import="ro.emid.common.db.CategoryAccessDB" %>
<%@ page import="ro.emid.common.constants.GlobalAppConstants" %>
<%@ page import="ro.emid.common.db.framework.ReportStatement" %>
<%@ page import="ro.emid.common.utils.ObjectUtils" %>
<%@ page import="ro.emid.common.types.Deranjament" %>
<%@ page import="java.sql.Date" %>
<%@ page import="ro.emid.callcenter.CallcenterSessionUtils" %>
<%@ page import="ro.emid.common.constants.RoleConstants" %>
<%@ page import="ro.emid.common.constants.AppConstants" %>

<%@ include file="../callcenter/declaratii.jsp"%>
<%
    long filialaId = CallcenterSessionUtils.getFilialaId(session) ;
    long sucursalaId = CallcenterSessionUtils.getSucursalaId(session);
    long centruId = CallcenterSessionUtils.getCentruId(session);

    if (0 < ObjectUtils.getLongValue(request.getParameter("filiala"), -2)) {
        filialaId = ObjectUtils.getLongValue(request.getParameter("filiala"));
    }

    if (0 < ObjectUtils.getLongValue(request.getParameter("sucursala"), -2)) {
        sucursalaId = ObjectUtils.getLongValue(request.getParameter("sucursala"));
    }

    if (0 < ObjectUtils.getLongValue(request.getParameter("centruId"), -2)) {
        centruId = ObjectUtils.getLongValue(request.getParameter("centruId"));
    }

    Category filialaRaport = CategoryAccessDB.getById(filialaId);
    if (filialaRaport == null) filialaRaport = new Category();

    Category sucursalaRaport = CategoryAccessDB.getById(sucursalaId);
    if (sucursalaRaport == null) sucursalaRaport = new Category();

    Category centruRaport = CategoryAccessDB.getById(centruId);
    if (centruRaport == null) centruRaport = new Category();


    final Date dataStart = ObjectUtils.getDateValue(request.getParameter("data_start"), AppConstants.FORMAT_DATA);
    final Date dataStop = ObjectUtils.getDateValue(request.getParameter("data_stop"), AppConstants.FORMAT_DATA);

    final String value_data_start = AppConstants.format(dataStart);
    final String value_data_stop = AppConstants.format(dataStop);

    final ReportStatement totalIncidenteFiliala = new ReportStatement("SELECT /*+ INDEX(d RAPORT_STATISTICA_EV_CC_IDX) */ COUNT(*) FROM LittleIncidente");
    final ReportStatement totalDeranjamenteFiliala = new ReportStatement("SELECT COUNT(*) FROM deranjamente");

    if (filialaRaport.getId() > 0) {
        totalIncidenteFiliala.addOrSetFilter("FILIALA_ID", "=", new Long(filialaRaport.getId()));
        totalDeranjamenteFiliala.addOrSetFilter("FILIALA_ID", "=", new Long(filialaRaport.getId()));
    }

    final ReportStatement totalDerIluminatPublic = new ReportStatement("SELECT /*+ INDEX(d RAPORT_STATISTICA_EV_CC_IDX) */ COUNT(*) FROM deranjamente ")
            .addOrSetFilter("CAT_INSTALATII IN (" + Deranjament.INDEX_ILUMINAT_PUBLIC_STRADAL_LEAJT + ',' + Deranjament.INDEX_ILUMINAT_PUBLIC_STRADAL_LESJT + ") AND 1", "=", new Integer(1));

    final ReportStatement totalDerPA_PT = new ReportStatement("SELECT /*+ INDEX(d RAPORT_STATISTICA_EV_CC_IDX) */ COUNT(*) FROM deranjamente ")
            .addOrSetFilter("CAT_INSTALATII IN (" + Deranjament.INDEX_PA + ',' + Deranjament.INDEX_PT + ") AND 1", "=", new Integer(1));

    final ReportStatement totalDerLEA = new ReportStatement("SELECT /*+ INDEX(d RAPORT_STATISTICA_EV_CC_IDX) */ COUNT(*) FROM deranjamente ")
            .addOrSetFilter("CAT_INSTALATII", "=", new Integer(Deranjament.INDEX_LEA));

    final ReportStatement totalDerLES = new ReportStatement("SELECT /*+ INDEX(d RAPORT_STATISTICA_EV_CC_IDX) */ COUNT(*) FROM deranjamente ")
            .addOrSetFilter("CAT_INSTALATII", "=", new Integer(Deranjament.INDEX_LES));

    final ReportStatement totalDerBransamente = new ReportStatement("SELECT /*+ INDEX(d RAPORT_STATISTICA_EV_CC_IDX) */ COUNT(*) FROM deranjamente ")
            .addOrSetFilter("CAT_INSTALATII IN (" + Deranjament.INDEX_BRANSAMENTE_AERIENE + ',' + Deranjament.INDEX_BRANSAMENTE_SUBTERANE + ") AND 1", "=", new Integer(1));

    final ReportStatement totalDerConsumatori = new ReportStatement("SELECT /*+ INDEX(d RAPORT_STATISTICA_EV_CC_IDX) */ COUNT(*) FROM deranjamente")
            .addOrSetFilter("APARTENENTA_INST", "=", new Integer( Deranjament.APARTINE_CONSUMATOR));

    if (centruRaport.getId() > 0) {
        totalDerIluminatPublic.addOrSetFilter("SECTIE_ID", "=", new Long(centruRaport.getId()));
        totalDerPA_PT.addOrSetFilter("SECTIE_ID", "=", new Long(centruRaport.getId()));
        totalDerLEA.addOrSetFilter("SECTIE_ID", "=", new Long(centruRaport.getId()));
        totalDerLES.addOrSetFilter("SECTIE_ID", "=", new Long(centruRaport.getId()));
        totalDerBransamente.addOrSetFilter("SECTIE_ID", "=", new Long(centruRaport.getId()));
        totalDerConsumatori.addOrSetFilter("SECTIE_ID", "=", new Long(centruRaport.getId()));
    } else if (sucursalaRaport.getId() > 0) {
        totalDerIluminatPublic.addOrSetFilter("SUCURSALA_ID", "=", new Long(sucursalaRaport.getId()));
        totalDerPA_PT.addOrSetFilter("SUCURSALA_ID", "=", new Long(sucursalaRaport.getId()));
        totalDerLEA.addOrSetFilter("SUCURSALA_ID", "=", new Long(sucursalaRaport.getId()));
        totalDerLES.addOrSetFilter("SUCURSALA_ID", "=", new Long(sucursalaRaport.getId()));
        totalDerBransamente.addOrSetFilter("SUCURSALA_ID", "=", new Long(sucursalaRaport.getId()));
        totalDerConsumatori.addOrSetFilter("SUCURSALA_ID", "=", new Long(sucursalaRaport.getId()));
    } else if (filialaRaport.getId() > 0) {
        totalDerIluminatPublic.addOrSetFilter("FILIALA_ID", "=", new Long(filialaRaport.getId()));
        totalDerPA_PT.addOrSetFilter("FILIALA_ID", "=", new Long(filialaRaport.getId()));
        totalDerLEA.addOrSetFilter("FILIALA_ID", "=", new Long(filialaRaport.getId()));
        totalDerLES.addOrSetFilter("FILIALA_ID", "=", new Long(filialaRaport.getId()));
        totalDerBransamente.addOrSetFilter("FILIALA_ID", "=", new Long(filialaRaport.getId()));
        totalDerConsumatori.addOrSetFilter("FILIALA_ID", "=", new Long(filialaRaport.getId()));
    }

     if( dataStop != null){
        totalIncidenteFiliala.addOrSetFilter("DATA_ANUNTARE", ">=", dataStart);
        totalDeranjamenteFiliala.addOrSetFilter("DATA_ANUNTARE", ">=", dataStart);
        totalDerIluminatPublic.addOrSetFilter("DATA_ANUNTARE", ">=", dataStart);
        totalDerPA_PT.addOrSetFilter("DATA_ANUNTARE", ">=", dataStart);
        totalDerLEA.addOrSetFilter("DATA_ANUNTARE", ">=", dataStart);
        totalDerLES.addOrSetFilter("DATA_ANUNTARE", ">=", dataStart);
        totalDerBransamente.addOrSetFilter("DATA_ANUNTARE", ">=", dataStart);
        totalDerConsumatori.addOrSetFilter("DATA_ANUNTARE", ">=", dataStart);
    }

    if( dataStop != null){
        final Date dataStopx = new Date(dataStop.getTime() + 24*60*60*1000);
        totalIncidenteFiliala.addOrSetFilter("DATA_ANUNTARE", "<=", dataStopx);
        totalDeranjamenteFiliala.addOrSetFilter("DATA_ANUNTARE", "<=", dataStopx);
        totalDerIluminatPublic.addOrSetFilter("DATA_ANUNTARE", "<=", dataStopx);
        totalDerPA_PT.addOrSetFilter("DATA_ANUNTARE", "<=", dataStopx);
        totalDerLEA.addOrSetFilter("DATA_ANUNTARE", "<=", dataStopx);
        totalDerLES.addOrSetFilter("DATA_ANUNTARE", "<=", dataStopx);
        totalDerBransamente.addOrSetFilter("DATA_ANUNTARE", "<=", dataStopx);
        totalDerConsumatori.addOrSetFilter("DATA_ANUNTARE", "<=", dataStopx);
    }
%>

<TABLE border=0 cellPading=0 cellSpacing=0 width="100%">
    <TR>
        <TD>
            <SPAN style="FONT-SIZE: 12px; FONT-WEIGHT: bold">

                <A class="link" href="<%=GlobalAppConstants.APP_NAME%>?action=rapoarte">Rapoarte</A> - Statistica evenimente
            </span>
        </TD>
    </TR>
</TABLE>

<center>
    <br />
    Statistica evenimentelor produse in instalatiile de JT 
</center>

<div class="spacer"></div>
<table width="100%" class="gsaptd"><tr><td>
<form name="zaforma" action="<%=GlobalAppConstants.APP_NAME%>" method="post">
    <input type="hidden" name="action" value="raport-statistica-evenimentelor-jt">
    Filiala:
    <SELECT name="filiala" onchange="fill_sucursala_select()" class="gSAPSelect" <%=user.getIeLevel() == RoleConstants.NIVEL_FULL ? "" : "disabled"%> >
        <OPTION value="-1">ELECTRICA SA:</OPTION>
        <%  final Category [] filiale = CategoryAccessDB.getAllCategoriesByParentId( -1);
          if (filiale != null) for(int i=0;i<filiale.length;i++) {%>
          <OPTION <%=(filiale[i].getId() == filialaRaport.getId()) ? "selected=\"true\" " : ""%>value="<%=filiale[i].getId()%>"><%=filiale[i].getName()%></OPTION>
        <%} %>
    </SELECT>&nbsp;&nbsp;

    Sucursala:
    <span id="sucursalaDiv">
    <SELECT name="sucursala" class="gSAPSelect" onchange="fill_centru_select()" >
          <option value="-1">Selectati:</option>
    </SELECT></span>&nbsp;&nbsp;

    C.E.:
    <span id="centruDiv">
        <SELECT name="centruId" class="gSAPSelect">
         <OPTION value="-1">Selectati:</OPTION>
        </SELECT>
    </span>

    &nbsp;&nbsp;<input type="text" id="data_start" name="data_start" size="8" value="<%=value_data_start%>" /> <a href="#" id="trigger_data_start" ><img src="<%=AppConstants.WEB_RESOURCE_PATH + "/images/cal.gif"%>" width="16" height="16" border="0" alt="Apasa pt. a selecta data."></a> -
    <input type="text" id="data_stop" name="data_stop" size="8" value="<%=value_data_stop%>" /> <a href="#" id="trigger_data_stop" ><img src="<%=AppConstants.WEB_RESOURCE_PATH + "/images/cal.gif"%>" width="16" height="16" border="0" alt="Apasa pt. a selecta data."></a>

        <script type="text/javascript">
              Calendar.setup(
                {
                  inputField  : "data_start",         // ID of the input field
                  ifFormat    : "%d.%m.%Y",    // the date format
                  button      : "trigger_data_start"       // ID of the button
                }
              );

              Calendar.setup(
                {
                  inputField  : "data_stop",         // ID of the input field
                  ifFormat    : "%d.%m.%Y",    // the date format
                  button      : "trigger_data_stop"       // ID of the button
                }
              );
        </script>

    <input type="submit" value="Aplica">
</form>


    <script type="text/javascript">
        function fill_sucursala_select(selectedValue, disabled, firstShow){
            resetComboLeaveFirstOption( document.zaforma.sucursala);
            resetComboLeaveFirstOption( document.zaforma.centruId);

            var filialaId = document.zaforma.filiala[ document.zaforma.filiala.selectedIndex].value;
            if( filialaId > -1){
                var params = "parentId="+filialaId + "&selectName=sucursala&disabled=" + disabled + (selectedValue ? "&selectedValue=" + selectedValue : "");
                //alert( params);

                getCategoriesFromServer(params, function(originalRequest){
                    //alert( getResponseMesg (originalRequest.responseText));
                    $( 'sucursalaDiv').innerHTML = getResponseMesg (originalRequest.responseText);

                    document.zaforma.sucursala.onchange=fill_centru_select;

                    if( firstShow){
                        fill_centru_select(<%=centruRaport.getId()%>, <%=user.getIeLevel() == RoleConstants.NIVEL_CENTRU || user.getIeLevel() == RoleConstants.NIVEL_FORMATIE%>);
                    }
                });
            }
        }

        function fill_centru_select(selectedValue, disabled){
            resetComboLeaveFirstOption( document.zaforma.centruId);

            var sucursalaId = document.zaforma.sucursala[ document.zaforma.sucursala.selectedIndex].value;
            if( sucursalaId > -1){
                var params = "parentId="+sucursalaId + "&selectName=centruId&disabled=" + disabled + (selectedValue ? "&selectedValue=" + selectedValue : "");
                //alert( params);

                getCategoriesFromServer(params,function(originalRequest){
                    //alert( getResponseMesg (originalRequest.responseText));
                    $( 'centruDiv').innerHTML = getResponseMesg (originalRequest.responseText);
                });
            }
        }


        fill_sucursala_select(<%=sucursalaRaport.getId()%>, <%=user.getIeLevel() == RoleConstants.NIVEL_SUCURSALA || user.getIeLevel() == RoleConstants.NIVEL_CENTRU || user.getIeLevel() == RoleConstants.NIVEL_FORMATIE%>, true);
   </script>                 
</td></tr></table>
<div class="spacer"></div>
<%
//    long totalIncidente = totalIncidenteFiliala.getLongResult();
    final long totalDeranjamente = totalDerIluminatPublic.getLongResult() + totalDerPA_PT.getLongResult() + totalDerLEA.getLongResult() +
            totalDerLES.getLongResult() + totalDerBransamente.getLongResult(); //totalDeranjamenteFiliala.getLongResult();
%>

<TABLE id="table" border=1 cellPadding=2 cellSpacing=1 width="100%" class="gSAPTHEAD" style="BACKGROUND-COLOR: white">
    <col>
    <col>
    <col align="right">
    <%--<tr>--%>
        <%--<td>1.</td>--%>
        <%--<td>Total evenimente in instalatiile "<%=filialaRaport.getId() > 0 ? filialaRaport.getName() : "Electrica S.A."%>"</td>--%>
        <%--<td><%=totalIncidente + totalDeranjamente%> </td>--%>
    <%--</tr>--%>
    <tr>
        <td>1.</td>
        <td>Total deranjamente in instalatiile "<%=filialaRaport.getId() > 0 ? filialaRaport.getName() : "Electrica S.A."%>"</td>
        <td><%=totalDeranjamente%></td>
    </tr>
    <tr>
        <td>1.1.</td>
        <td>Deranjamente in iluminat public stradal</td>
        <td><%=totalDerIluminatPublic.getLongResult()%></td>
    </tr>
    <tr>
        <td>1.2.</td>
        <td>Deranjamente in PA-PT</td>
        <td><%=totalDerPA_PT.getLongResult()%></td>
    </tr>
    <tr>
        <td>1.3.</td>
        <td>Deranjamente in LEA</td>
        <td><%=totalDerLEA.getLongResult()%></td>
    </tr>
    <tr>
        <td>1.4.</td>
        <td>Deranjamente in LES</td>
        <td><%=totalDerLES.getLongResult()%></td>
    </tr>
    <tr>
        <td>1.5.</td>
        <td>Deranjamente in Bransamente</td>
        <td><%=totalDerBransamente.getLongResult()%></td>
    </tr>
    <tr>
        <td>1.5.1.</td>
        <td>
            din care: <br>
            &nbsp;&nbsp;&nbsp;&nbsp;-avand cauze localizate in agregate de masura <br>(transformatoare de curent si contoare)
        </td>
        <td><%=totalDerBransamente.addOrSetFilter("INTERVENTIE", "=", new Integer(1)).getLongResult()%></td>
    </tr>
    <tr>
        <td>2.</td>
        <td>Deranjamente avand cauze in instalatiile abonatilor</td>
        <td><%=totalDerConsumatori.getLongResult()%></td>
    </tr>
</TABLE>

<script type="text/javascript">
    var table = new Table("table", "table");
</script>
