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

<%@ 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("raportTop10DinFE.filiala");
    Category sucursalaRaport = (Category) session.getAttribute("raportTop10DinFE.sucursala");
    Category centruRaport = (Category) session.getAttribute("raportTop10DinFE.centru");

    session.setAttribute("filiala", filialaRaport);
    session.setAttribute("sucursala", sucursalaRaport);
    session.setAttribute("centru", centruRaport);


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


    StringBuffer sessionCond = (StringBuffer)session.getAttribute( "raportTop10InstalatiiFilterDinFE");
	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"));
	}
    final StringBuffer condSql = new StringBuffer( sessionCond.toString());

     switch ( tipInstalatie){
        case 1: condSql.append(" AND STATIE_COD = ").append('\'').append(codInstalatie).append('\'').append(
                " AND isSelected( LINIE_ID) = 0 ")
                .append(" AND isSelected( DERIVATIE_ID) = 0")
                .append(" AND isSelected( BARA_ID) = 0")
                .append(" AND isSelected( BOBINA_ID) = 0")
                .append(" AND isSelected( CELULA_ID) = 0")
                .append(" AND isSelected( TRAFO_ID) = 0")
                .append(" AND isSelected( PAPT_ID) = 0 ");
        break;
        case 2: condSql.append(" AND LINIE_COD = ").append('\'').append(codInstalatie).append('\'').append(" AND isSelected( 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("rapoarteEchipamenteFiltru.append_to", condSql.toString());
    session.setAttribute("rapoarteEchipamenteFiltru.data_start", new Long( dataStart != null ? dataStart.getTime() : 0));
    session.setAttribute("rapoarteEchipamenteFiltru.data_stop", new Long( dataStop != null ? dataStop.getTime() : 0));


    final RaportByCauzeRow[] rows1;
    final RaportByCauzeRow[] rows2;
    if (sucursalaRaport.getId() != -1) {
        rows2 = EchipamentAccessDB.get_raport_by_cauze_by_sucursala(sucursalaRaport.getId(), dataStart != null ? dataStart.getTime() : 0, dataStop != null ? dataStop.getTime() : 0, condSql.toString());
        rows1 = EchipamentAccessDB.get_raport_by_grupe_cauze_by_sucursala(sucursalaRaport.getId(), dataStart != null ? dataStart.getTime() : 0, dataStop != null ? dataStop.getTime() : 0, condSql.toString());
    } else {
        rows2 = EchipamentAccessDB.get_raport_by_cauze_by_filiala(filialaRaport.getId(), dataStart != null ? dataStart.getTime() : 0, dataStop != null ? dataStop.getTime() : 0, condSql.toString());
        rows1 = EchipamentAccessDB.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>Deteriorari de echipamente 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">Cod<BR>grupa</TH>
            <TH class="gsapthead" noWrap rowspan="2" width="50">Cod<BR>cauza</TH>
            <TH class="gsapthead" noWrap rowspan="2" width="100">Cauza</TH>
            <TH class="gsapthead" noWrap rowspan="2">Nr.Echipamente</TH>
            <TH class="gsapthead" noWrap rowspan="2">Durata indisp.</TH>
            <TH class="gsapthead" noWrap colspan="4">Cheltuieli (ron)</TH>
        </TR>
        <TR>
            <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
                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" valign="top"><%=codGrupa%></TD>
                        <td>&nbsp;</td>
                        <TD >
                            <%--<%=rows1[i].cauzaGrupa.getDescriere()%>--%>
                            <%if( i+1 < rows1.length ){%><a class="link" href="javascript: void(null)" onclick="openEchipamenteByCauzaID( <%=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"><%if("Totale".equals( rows1[i].cauzaGrupa.getDescriere())){%> &nbsp; <%}else{%> <%=rows1[i].t_durata_indisp/60%>h <%=rows1[i].t_durata_indisp%60%>' <%}%></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 >
                            <%--<%=rows2[j].cauza.getDescriere()%>--%>
                            <a class="link" href="javascript: void(null)" onclick="openEchipamenteByCauzaID( <%=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].t_durata_indisp/60%>h <%=rows2[j].t_durata_indisp%60%>'</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 echipamenteWindow = null;
    function openEchipamenteByComponentaId( idAnsamblu, idComponenta){
        openEchipamenteWindow( '<%=AppConstants.APP_NAME%>?action=showEchipamenteFromRaport&idAnsamblu='+idAnsamblu + "&idComponenta="+idComponenta);
    }

    function openEchipamenteByCauzaID( idCauzaGrupa, idCauza){
        openEchipamenteWindow( '<%=AppConstants.APP_NAME%>?action=showEchipamenteFromRaport&idCauzaGrupa='+idCauzaGrupa+'&idCauza='+idCauza);
    }

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

