<%@ page import="ro.emid.common.types.Category" %>
<%@ page import="ro.emid.common.db.CategoryAccessDB" %>
<%@ page import="ro.emid.common.constants.GlobalAppConstants" %>
<%@ page import="java.util.Vector" %>
<%@ 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.util.List" %>
<%@ page import="ro.emid.common.db.DeranjamentAccessDB" %>
<%@ page import="ro.emid.callcenter.CallcenterSessionUtils" %>
<%@ page import="ro.emid.common.constants.RoleConstants" %>

<%@ include file="../callcenter/declaratii.jsp"%>

<%
    long filialaId = CallcenterSessionUtils.getFilialaId(session);
    long sucursalaId = CallcenterSessionUtils.getSucursalaId(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"));
    }

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

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


    final Vector vani = DeranjamentAccessDB.getAniProducereDeranjament();

    Long anSelectat = new Long(ObjectUtils.getLongValue(request.getParameter("an"), -1));
    if (anSelectat.longValue() == -1) anSelectat = (Long) (vani.size() > 0 ? vani.firstElement() : new Long(0));
%>

<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> - Raport indicatori performanta distributie
            </span>
        </TD>
    </TR>
</TABLE>

<center>
    <br />
    Realizarea indicatorilor de performanta pentru serviciul de distributie a energiei electrice <br />
    <span style="font-size: 14; font-weight: bold;" >DERANJAMENTE </span>
    <span style="font-size: 14; font-weight: bold;" >RAPORT ANUAL</span>
</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-deranjamente-anual">
    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;

    &nbsp;&nbsp;&nbsp;Anul:
    <select name="an" >
        <% for (int i = 0; i < vani.size(); i++) {
            final Long an = (Long)vani.get( i);%>
            <option value="<%=an%>" <%=anSelectat.equals(an) ? "selected" : ""%> ><%=an%></option>
        <% } %>
    </select>
    &nbsp;
    <input type="submit" value="Aplica">
</form>


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

            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);
                });
            }
        }

        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 parentId = filialaRaport.getId();
    String columnTitle = "FDFEE";
    if ( sucursalaRaport.getId() != -1) {
        parentId = sucursalaRaport.getId();
        columnTitle = "";
    } else if( filialaRaport.getId() != -1){
        parentId = filialaRaport.getId();
        columnTitle = "SDFEE";
    }
    final Category[] sucursale_sectii = CategoryAccessDB.getAllCategoriesByParentId( parentId);
%>

<TABLE id="table" border=1 cellPadding=2 cellSpacing=1 width="100%" class="gSAPTHEAD" style="BACKGROUND-COLOR: white">
    <colgroup align="left">
    <tr noSelect="true">
        <th class="gsapthead" rowspan="2">&nbsp;</th>
        <th class="gsapthead" rowspan="2" width="100">Denumire</th>
        <th class="gsapthead" rowspan="2">&nbsp;</th>
        <th class="gsapthead" colspan="19">Valoare Realizata lunar/anual</th>        
    </tr>
    <tr noSelect="true">
        <th class="gsapthead">1</th>
        <th class="gsapthead">2</th>
        <th class="gsapthead">3</th>
        <th class="gsapthead">Trim. I</th>
        <th class="gsapthead">4</th>
        <th class="gsapthead">5</th>
        <th class="gsapthead">6</th>
        <th class="gsapthead">Trim. II</th>
        <th class="gsapthead">Sem. I</th>
        <th class="gsapthead">7</th>
        <th class="gsapthead">8</th>
        <th class="gsapthead">9</th>
        <th class="gsapthead">Trim. III</th>
        <th class="gsapthead">10</th>
        <th class="gsapthead">11</th>
        <th class="gsapthead">12</th>
        <th class="gsapthead">Trim. IV</th>
        <th class="gsapthead">Sem. II</th>
        <th class="gsapthead">Anual</th>
    </tr>    
        <%
            final ReportStatement raportNrDeranjamente = new ReportStatement("SELECT /*+ INDEX(RAPORT_ANUAL_CC_IDX) */ TO_CHAR(data_anuntare, 'MM'), COUNT(*) FROM deranjamente", "GROUP BY TO_CHAR(data_anuntare, 'MM')")
                .addOrSetFilter("TO_CHAR(data_anuntare, 'YYYY')", "=", anSelectat);

            final ReportStatement raportNrDeranjamente4OreUrban = new ReportStatement("SELECT /*+ INDEX(RAPORT_ANUAL_CC_IDX) */ TO_CHAR(data_anuntare, 'MM'), COUNT(*) FROM deranjamente", "GROUP BY TO_CHAR(data_anuntare, 'MM')")
                .addOrSetFilter("TO_CHAR(data_anuntare, 'YYYY')","=", anSelectat)
                .addOrSetFilter("MEDIUL", "=", new Long(Deranjament.MEDIU_URBAN))
//                .addOrSetFilter("floor((data_rezolvare - data_anuntare)*24)","<", new Long(4));
                .addOrSetFilter("floor((data_rezolvare - data_anuntare)*24)","<", new Long(12)); // au vrut apoi din 4 in 12 ore


            final ReportStatement raportNrDeranjamente24OreRural = new ReportStatement("SELECT /*+ INDEX(RAPORT_ANUAL_CC_IDX) */ TO_CHAR(data_anuntare, 'MM'), COUNT(*) FROM deranjamente", "GROUP BY TO_CHAR(data_anuntare, 'MM')")
                .addOrSetFilter("TO_CHAR(data_anuntare, 'YYYY')","=", anSelectat)
                .addOrSetFilter("MEDIUL","=", new Long(Deranjament.MEDIU_RURAL))
                .addOrSetFilter("floor((data_rezolvare - data_anuntare)*24)", "<", new Long(24));


            final String[] denumiri = new String[]{
                    "Nr. deranjamente",
                    "Nr. deranjamente rezolvare intr-un interval mai mic de 12 ore (urban)",
                    "Nr. deranjamente rezolvate intr-un interval mai mic de 24 ore(rural)"
            };

            final long[][] totaluri = new long[denumiri.length][19];
            for (int i = 0; i < sucursale_sectii.length; i++) {
                final Category category = sucursale_sectii[i];
                
                if (sucursalaRaport.getId() > 0){
                    raportNrDeranjamente.addOrSetFilter("SECTIE_ID","=", new Long(category.getId()));
                    raportNrDeranjamente4OreUrban.addOrSetFilter("SECTIE_ID","=", new Long(category.getId()));
                    raportNrDeranjamente24OreRural.addOrSetFilter("SECTIE_ID","=", new Long(category.getId()));
                }else if( filialaRaport.getId() > 0){
                    raportNrDeranjamente.addOrSetFilter("SUCURSALA_ID","=", new Long(category.getId()));
                    raportNrDeranjamente4OreUrban.addOrSetFilter("SUCURSALA_ID","=", new Long(category.getId()));
                    raportNrDeranjamente24OreRural.addOrSetFilter("SUCURSALA_ID","=", new Long(category.getId()));
                }else{
                    raportNrDeranjamente.addOrSetFilter("FILIALA_ID","=", new Long( category.getId()));
                    raportNrDeranjamente4OreUrban.addOrSetFilter("FILIALA_ID","=", new Long( category.getId()));
                    raportNrDeranjamente24OreRural.addOrSetFilter("FILIALA_ID","=", new Long( category.getId()));
                }

                for( int j = 0; j < denumiri.length; j++){
                    long valoareSemestriala = 0;
                    long valoareAnuala = 0;
                    long v1 = 0, v2 = 0, v3 = 0;

                    ReportStatement datas = null;
                    switch (j){
                        case 0 : datas = raportNrDeranjamente; break;
                        case 1 : datas = raportNrDeranjamente4OreUrban; break;
                        case 2 : datas = raportNrDeranjamente24OreRural; break;
                    }
                    final List matrix = datas.getListOfArrays("Get M"); %>
                    <tr>
                        <% if( j == 0){ %>
                            <td rowspan="<%=denumiri.length%>" valign="middle"><b><%=columnTitle%> <%=category.getName()%></b></td>
                        <%}%>
                        <td><%=denumiri[j]%></td>
                        <td>jt</td>
                        <% v1 = DeranjamentAccessDB.getLongByMonth(matrix, 1); %>
                        <% v2 = DeranjamentAccessDB.getLongByMonth(matrix, 2); %>
                        <% v3 = DeranjamentAccessDB.getLongByMonth(matrix, 3); %>
                        <% valoareSemestriala   += v1 + v2 + v3; %>
                        <td align="right"><%=v1%></td>
                        <td align="right"><%=v2%></td>
                        <td align="right"><%=v3%></td>
                        <td align="right"><%=v1 + v2 + v3%></td>
                        <% totaluri[j][0] += v1; %>
                        <% totaluri[j][1] += v2; %>
                        <% totaluri[j][2] += v3; %>
                        <% totaluri[j][3] += v1 + v2 + v3; %>
            
                        <% v1 = DeranjamentAccessDB.getLongByMonth(matrix, 4); %>
                        <% v2 = DeranjamentAccessDB.getLongByMonth(matrix, 5); %>
                        <% v3 = DeranjamentAccessDB.getLongByMonth(matrix, 6); %>
                        <% valoareSemestriala   += v1 + v2 + v3; %>
                        <td align="right"><%=v1%></td>
                        <td align="right"><%=v2%></td>
                        <td align="right"><%=v3%></td>
                        <td align="right"><%=v1 + v2 + v3%></td>
                        <td align="right"><%=valoareSemestriala%></td>
                        <% totaluri[j][4] += v1; %>
                        <% totaluri[j][5] += v2; %>
                        <% totaluri[j][6] += v3; %>
                        <% totaluri[j][7] += v1 + v2 + v3; %>
                        <% totaluri[j][8] += valoareSemestriala; %>

                        <% valoareAnuala += valoareSemestriala; %>
                        <% valoareSemestriala = 0;%>

                        <% v1 = DeranjamentAccessDB.getLongByMonth(matrix, 7); %>
                        <% v2 = DeranjamentAccessDB.getLongByMonth(matrix, 8); %>
                        <% v3 = DeranjamentAccessDB.getLongByMonth(matrix, 9); %>
                        <% valoareSemestriala += v1 + v2 + v3; %>
                        <td align="right"><%=v1%></td>
                        <td align="right"><%=v2%></td>
                        <td align="right"><%=v3%></td>
                        <td align="right"><%=v1 + v2 + v3%></td>
                        <% totaluri[j][9]  += v1; %>
                        <% totaluri[j][10] += v2; %>
                        <% totaluri[j][11] += v3; %>
                        <% totaluri[j][12] += v1 + v2 + v3; %>

                        <% v1 = DeranjamentAccessDB.getLongByMonth(matrix, 10); %>
                        <% v2 = DeranjamentAccessDB.getLongByMonth(matrix, 11); %>
                        <% v3 = DeranjamentAccessDB.getLongByMonth(matrix, 12); %>
                        <% valoareSemestriala += v1 + v2 + v3; %>
                        <% valoareAnuala += valoareSemestriala; %>
                        <td align="right"><%=v1%></td>
                        <td align="right"><%=v2%></td>
                        <td align="right"><%=v3%></td>
                        <td align="right"><%=v1 + v2 + v3%></td>
                        <td align="right"><%=valoareSemestriala%></td>
                        <td align="right"><%=valoareAnuala%></td>
                        <% totaluri[j][13] += v1; %>
                        <% totaluri[j][14] += v2; %>
                        <% totaluri[j][15] += v3; %>
                        <% totaluri[j][16] += v1 + v2 + v3; %>
                        <% totaluri[j][17] += valoareSemestriala; %>
                        <% totaluri[j][18] += valoareAnuala; %>
                    </tr>
            <%  }%>
        <%}%>

        <%
            for (int i = 0; i < denumiri.length; i++) { %>
                <tr class="gsapthead"> <%
                    if( i == 0){ %>
                        <td rowspan="3"> <b>Total</b> </td><%
                    }%>
                    <td><%=denumiri[i]%></td><td>jt</td><%
                    for( int j = 0; j < totaluri[0].length; j++){ %>
                        <td align="right"><%=totaluri[i][j]%></td> <%
                    }%>
                </tr><%
            }
        %>
</TABLE>

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