<%@ page import="ro.emid.common.constants.AppConstants" %>
<%@ page import="ro.emid.common.db.RaportByCauzeRow" %>
<%@ page import="ro.emid.common.db.IncidentAccessDB" %>
<%@ page import="ro.emid.common.db.NomAccessDB" %>
<%@ page import="ro.emid.common.types.Nomenclator" %>
<%@ page import="ro.emid.common.types.Category" %>
<%@ page import="java.util.Date" %>
<%@ page import="java.util.Base64" %>
<%@ page import="java.text.SimpleDateFormat" %>
<%@ page import="ro.emid.common.utils.ObjectUtils" %>

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

<%
    final int tipInstalatie = ObjectUtils.getIntValue( request.getParameter("tipInstalatie"), -1);
    final String codInstalatie = ObjectUtils.getStringValue( request.getParameter("codInstalatie"));

    Category filialaRaport = (Category) session.getAttribute("raportTop10.filiala");
    Category sucursalaRaport = (Category) session.getAttribute("raportTop10.sucursala");
    Category centruRaport = (Category) session.getAttribute("raportTop10.centru");

    Date dataStart = (Date) session.getAttribute( "raportTop10.dataStart");
    Date dataStop  = (Date) session.getAttribute( "raportTop10.dataStop");


    StringBuffer sessionCond = (StringBuffer)session.getAttribute( "raportTop10InstalatiiFilter");
	if(sessionCond == null) {
		
		byte[] arr = Base64.getDecoder().decode(request.getParameter("sessionCond"));
		String decoded = new String(arr);
		sessionCond = new StringBuffer(decoded);
		
		Long sucursalaId = Long.parseLong(request.getParameter("sucursala"));
		Long centruId = Long.parseLong(request.getParameter("centru"));
		
		filialaRaport = new Category();
		filialaRaport.setId(1L);
		
		sucursalaRaport = new Category();
		centruRaport = new Category();
		
		sucursalaRaport.setId(sucursalaId);
		centruRaport.setId(centruId);
		
		dataStart = new SimpleDateFormat("M/dd/yyyy").parse(request.getParameter("dataStart"));
		dataStop = new SimpleDateFormat("M/dd/yyyy").parse(request.getParameter("dataStop"));
	}
	
	session.setAttribute("filiala", filialaRaport);
    session.setAttribute("sucursala", sucursalaRaport);
    session.setAttribute("centru", centruRaport);
	
//    StringBuffer condSql = (StringBuffer)session.getAttribute( "raportTop10InstalatiiFilter");
    final StringBuffer condSql = new StringBuffer( sessionCond.toString());

     switch ( tipInstalatie){
        case 1: condSql.append(" AND STATIE_COD = ").append('\'').append(codInstalatie).append('\'').append(
                " AND isSelected( i.LINIE_ID) = 0 " ).append(
                        " AND isSelected( i.DERIVATIE_ID) = 0" ).append(
                        " AND isSelected( i.BARA_ID) = 0" ).append(
                        " AND isSelected( i.BOBINA_ID) = 0" ).append(
                        " AND isSelected( i.CELULA_ID) = 0" ).append(
                        " AND isSelected( i.TRAFO_ID) = 0" ).append(
                        " AND isSelected( i.PAPT_ID) = 0 "
        ); break;
        case 2: condSql.append(" AND i.LINIE_COD = ").append('\'').append(codInstalatie).append('\'').append(" AND isSelected( i.DERIVATIE_ID) = 0"); break;
        case 3: condSql.append(" AND PAPT_COD = ").append('\'').append(codInstalatie).append('\''); break;
        case 4: condSql.append(" AND BOBINA_COD = ").append('\'').append(codInstalatie).append('\''); break;
        case 5: condSql.append(" AND CELULA_COD = ").append('\'').append(codInstalatie).append('\''); break;
        case 6: condSql.append(" AND BARA_COD = ").append('\'').append(codInstalatie).append('\''); break;
        case 7: condSql.append(" AND DERIVATIE_COD = ").append('\'').append(codInstalatie).append('\''); break;
        case 8: condSql.append(" AND TRAFO_COD = ").append('\'').append(codInstalatie).append('\''); break;
        default:
            //nothing
            break;
    }

//    session.setAttribute("rapoarteIncidenteFiltru.append_to", condSql.toString());
    session.setAttribute("rapoarteIncidenteFiltru.data_start", new Long( dataStart != null ? dataStart.getTime() : 0));
    session.setAttribute("rapoarteIncidenteFiltru.data_stop", new Long( dataStop != null ? dataStop.getTime() : 0));


    final RaportByCauzeRow[] rows1;
    final RaportByCauzeRow[] rows2;
    if (sucursalaRaport.getId() != -1) {
        rows2 = IncidentAccessDB.get_raport_by_cauze_by_sucursala(sucursalaRaport.getId(), dataStart != null ? dataStart.getTime() : 0, dataStop != null ? dataStop.getTime() : 0, condSql.toString());
        rows1 = IncidentAccessDB.get_raport_by_grupe_cauze_by_sucursala(sucursalaRaport.getId(), dataStart != null ? dataStart.getTime() : 0, dataStop != null ? dataStop.getTime() : 0, condSql.toString());
    } else {
        rows2 = IncidentAccessDB.get_raport_by_cauze_by_filiala(filialaRaport.getId(), dataStart != null ? dataStart.getTime() : 0, dataStop != null ? dataStop.getTime() : 0, condSql.toString());
        rows1 = IncidentAccessDB.get_raport_by_grupe_cauze_by_filiala(filialaRaport.getId(), dataStart != null ? dataStart.getTime() : 0, dataStop != null ? dataStop.getTime() : 0, condSql.toString());
    }
    
    String classul = "class=";
    int j = 0;

    Nomenclator nom = null;
%>

<div style="margin: 10px">

    <h3>Situatia incidentelor pe cauze</h3>

<TABLE border=1 cellPadding=2 cellSpacing=1 width="100%" class="gSAPTHEAD" style="BACKGROUND-COLOR: white">
    <TR>
        <TH class="gsapthead" noWrap rowspan="2" width="30">Cod<BR>grupa</TH>
        <TH class="gsapthead" noWrap rowspan="2" width="30">Cod<BR>cauza</TH>
        <TH class="gsapthead" noWrap rowspan="2" width="100">Cauza</TH>
        <TH class="gsapthead" noWrap colspan="2">Nr.Incidente</TH>
        <TH class="gsapthead" noWrap colspan="2">Durata inc.</TH>
        <TH class="gsapthead" noWrap colspan="2">Durata indisp.</TH>
        <TH class="gsapthead" noWrap rowspan="2">Durata med.</TH>
        <TH class="gsapthead" noWrap>Energie nelivr.</TH>
        <TH class="gsapthead" noWrap colspan="4">Cheltuieli (ron)</TH>
    </TR>
    <TR>
        <TH class="gsapthead" noWrap>Total</TH>
        <TH class="gsapthead" noWrap>Cu det.</TH>
        <TH class="gsapthead" noWrap>Total</TH>
        <TH class="gsapthead" noWrap>Cu det.</TH>
        <TH class="gsapthead" noWrap>Total</TH>
        <TH class="gsapthead" noWrap>Cu det.</TH>
        <TH class="gsapthead" noWrap>(kWh)</TH>
        <TH class="gsapthead" noWrap>Ment.</TH>
        <TH class="gsapthead" noWrap>Daune</TH>
        <TH class="gsapthead" noWrap>Exploatare</TH>
        <TH class="gsapthead" noWrap>Total</TH>
    </TR>
    <% if (((null != rows1) && (null != rows2))) { //  sint date %>
    <% j = 0; %>
    <% for (int i = 0; i < rows1.length ; i++) {
        if (i+1 == rows1.length) classul="class=\"gsapthead\""; %>
    <%if (i > 0 ) {%>
    <tr>
        <td colspan="15">
            &nbsp;
        </td>
    </tr>
    <%}%>
    <TR class="gsapthead">
        <% final String codGrupa = (rows1[i].cauzaGrupa.getId() == -1) ? "&nbsp" : String.valueOf( (nom = NomAccessDB.getNom( rows1[i].cauzaGrupa.getId())) != null ? String.valueOf(nom.getCod()) : "&nbsp");%>
        <TD align="center"><%=codGrupa%></TD>
        <TD align="center">&nbsp;</TD>
        <TD>
            <%if( i+1 < rows1.length ){%><a class="link" href="javascript: void(null)" onclick="openIncidenteByCauzaID( '<%=rows1[i].cauzaGrupa.getId()%>')" ><%}%>
            <%=rows1[i].cauzaGrupa.getDescriere()%>
            <%if( i+1 < rows1.length ){%></a><%}%>
        </TD>
        <TD align="right"><%=rows1[i].t_nr_incidente%></TD>
        <TD align="right"><%=rows1[i].wd_nr_incidente%></TD>
        <TD align="right" nowrap><%=rows1[i].t_durata_incidente/60%>h <%=rows1[i].t_durata_incidente%60%>'   </TD>
        <TD align="right" nowrap><%=rows1[i].wd_durata_incidente/60%>h <%=rows1[i].wd_durata_incidente%60%>' </TD>
        <TD align="right" nowrap><%=rows1[i].t_durata_indisp/60%>h <%=rows1[i].t_durata_indisp%60%>'         </TD>
        <TD align="right" nowrap><%=rows1[i].wd_durata_indisp/60%>h <%=rows1[i].wd_durata_indisp%60%>'       </TD>
        <TD align="right" nowrap><%=rows1[i].durata_medie/60%>h <%=rows1[i].durata_medie%60%>'                </TD>
        <TD align="right"><%=rows1[i].t_energ_nelivr%></TD>
        <TD align="right"><%=AppConstants.format3(rows1[i].chelt_ment)%></TD>
        <TD align="right"><%=AppConstants.format3(rows1[i].chelt_daune)%></TD>
        <TD align="right"><%=AppConstants.format3(rows1[i].chelt_altele)%></TD>
        <TD align="right"><%=AppConstants.format3(rows1[i].t_chelt_totale)%></TD>
    </TR>
    <% for (; (j < rows2.length) && (rows2[j].cauzaGrupa.getId() == rows1[i].cauzaGrupa.getId()) ; j++) { %>
    <TR <%=classul%>>
        <TD align="center">&nbsp;</TD>
        <TD align="center"><%=(rows2[j].cauza.getId() == -1) ? "&nbsp" :  String.valueOf( (nom = NomAccessDB.getNom( rows2[j].cauza.getId())) != null ? String.valueOf(nom.getCod()) : "&nbsp")%></TD>
        <TD>
            <a class="link" href="javascript: void(null)" onclick="openIncidenteByCauzaID( '<%=rows1[i].cauzaGrupa.getId()%>', '<%=rows2[j].cauza.getId()%>')" >
                <%=rows2[j].cauza.getDescriere()%>
            </a>
        </TD>
        <TD align="right"><%=rows2[j].t_nr_incidente%></TD>
        <TD align="right"><%=rows2[j].wd_nr_incidente%></TD>
        <TD align="right" nowrap><%=rows2[j].t_durata_incidente/60%>h <%=rows2[j].t_durata_incidente%60%>'</TD>
        <TD align="right" nowrap><%=rows2[j].wd_durata_incidente/60%>h <%=rows2[j].wd_durata_incidente%60%>'</TD>
        <TD align="right" nowrap><%=rows2[j].t_durata_indisp/60%>h <%=rows2[j].t_durata_indisp%60%>'</TD>
        <TD align="right" nowrap><%=rows2[j].wd_durata_indisp/60%>h <%=rows2[j].wd_durata_indisp%60%>'</TD>
        <TD align="right" nowrap><%=rows2[j].durata_medie/60%>h <%=rows2[j].durata_medie%60%>'</TD>
        <TD align="right"><%=rows2[j].t_energ_nelivr%></TD>
        <TD align="right"><%=AppConstants.format3(rows2[j].chelt_ment)%></TD>
        <TD align="right"><%=AppConstants.format3(rows2[j].chelt_daune)%></TD>
        <TD align="right"><%=AppConstants.format3(rows2[j].chelt_altele)%></TD>
        <TD align="right"><%=AppConstants.format3(rows2[j].t_chelt_totale)%></TD>
    </TR>
    <% } // for cauze %>
    <% } // for grupe cauze %>
    <% } else { %>
    <TR>
        <TD>
            Nu exista informatii...
        </TD>
    </TR>
    <% } %>
</table>

</div>
<br>
<br>


<script type="text/javascript">
    var incidenteWindow = null;

    function openIncidenteByCauzaID( idCauzaGrupa, idCauza){
        openIncidenteWindow( '<%=AppConstants.APP_NAME%>?action=showIncidenteFromRaport&idCauzaGrupa='+idCauzaGrupa+'&idCauza='+idCauza + "&_cond="+encodeURI( "<%=condSql.toString()%>"));
    }

    function openIncidenteWindow( url){
        if( incidenteWindow != null) incidenteWindow.close();
        incidenteWindow = window.open( url,'incidenteRaportTop10', 'top=50,left=50,width=850, height=550, status=yes,resizable=yes,dependent=yes,alwaysRaised=yes, scrollbars=yes');
    }
</script>

