<%@ page import="java.sql.Date" %>
<%@ page import="ro.emid.common.constants.AppConstants" %>
<%@ page import="ro.emid.common.constants.NomConstants" %>
<%@ page import="java.util.Arrays" %>
<%@ page import="java.util.Comparator" %>
<%@ page import="java.util.Set" %>
<%@ page import="java.util.HashSet" %>
<%@ page import="ro.emid.common.utils.StringUtils" %>
<%@ page import="org.apache.log4j.Logger" %>
<%@ page import="java.io.File" %>
<%@ page import="ro.emid.incidente.util.raport.RaportEchipamenteExcelExporter" %>
<%@ page import="ro.emid.common.types.*" %>
<%@ page import="ro.emid.incidente.IncidenteSessionUtils" %>
<%@ page import="ro.emid.common.db.*" %>
<%@ page import="ro.emid.common.constants.RoleConstants" %>

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

<TABLE border=0 cellPading=0 cellSpacing=0 width="100%">
    <TR>
        <TD>
            <%@ include file="../header-localizare.jsp"%>
            <span class="slash">/</span> <A class="link" href="<%=AppConstants.APP_NAME%>?action=rapoarte">Rapoarte</A>
            <span class="slash">/</span> Echipamente
        </TD>
        <TD align="RIGHT">
            <IMG alt=FAQ border=0 width=12 height=13 hspace=3 src="<%= AppConstants.WEB_RESOURCE_PATH + "/images/icon_mini_search.gif" %>">
            <A href="#" onClick="openConfigurareFiltruRapoarte(); return false" class="link">Configurare FILTRU</A>
        </TD>
    </TR>
</TABLE>

<%!
    private static java.text.SimpleDateFormat _df = new java.text.SimpleDateFormat("yyyyMMdd");
%>


<%

    String publicPath = null;

    if( request.getParameter("firstShow") != null){
        IncidenteSessionUtils.setTipRaport(session, "ECHIPAMENTE");
        IncidenteSessionUtils.resetParametriRaport(session);
    }

    long filialaId = IncidenteSessionUtils.getFilialaId(session);
    long sucursalaId = IncidenteSessionUtils.getSucursalaId(session);
    long centruId = IncidenteSessionUtils.getCentruId(session);

    Category sFiliala = (Category) session.getAttribute("filiala");
    Category sSucursala = (Category) session.getAttribute("sucursala");
    Category sCentru = (Category) session.getAttribute("centru");

    if (sFiliala != null && sFiliala.getId() > 0) filialaId = sFiliala.getId();
    if (sSucursala != null && sSucursala.getId() > 0) sucursalaId = sSucursala.getId();
    if (sCentru != null && sCentru.getId() > 0) centruId = sCentru.getId();



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

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

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

    Long sdata_start = (Long) session.getAttribute("data_start_e");
    long data_start = 0;
    if (sdata_start != null) data_start = sdata_start.longValue();

    Long sdata_stop = (Long) session.getAttribute("data_stop_e");
    long data_stop = System.currentTimeMillis();
    if (sdata_stop != null) data_stop = sdata_stop.longValue();

    Long sessionLong;
    sessionLong = (Long) session.getAttribute("f_formatie");
    long f_formatie = -1;
    if (sessionLong != null) {
        f_formatie = sessionLong.longValue();
    }

    sessionLong = (Long) session.getAttribute("f_ansamblu");
    long f_ansamblu = -1;
    if (sessionLong != null) {
        f_ansamblu = sessionLong.longValue();
    }

    sessionLong = (Long) session.getAttribute("f_echipamentComponenta");
    long f_echipamentComponenta = -1;
    if (sessionLong != null) {
        f_echipamentComponenta = sessionLong.longValue();
    }

    sessionLong = (Long) session.getAttribute("f_statie");
    long f_statie = -1;
    if (sessionLong != null) {
        f_statie = sessionLong.longValue();
    }

    sessionLong = (Long) session.getAttribute("f_bobina");
    long f_bobina = -1;
    if (sessionLong != null) {
        f_bobina = sessionLong.longValue();
    }

    sessionLong = (Long) session.getAttribute("f_celula");
    long f_celula = -1;
    if (sessionLong != null) {
        f_celula = sessionLong.longValue();
    }

    sessionLong = (Long) session.getAttribute("f_bara");
    long f_bara = -1;
    if (sessionLong != null) {
        f_bara = sessionLong.longValue();
    }

    sessionLong = (Long) session.getAttribute("f_linie");
    long f_linie = -1;
    if (sessionLong != null) {
        f_linie = sessionLong.longValue();
    }

    sessionLong = (Long) session.getAttribute("f_derivatie");
    long f_derivatie = -1;
    if (sessionLong != null) {
        f_derivatie = sessionLong.longValue();
    }

    sessionLong = (Long) session.getAttribute("f_pa_pt");
    long f_pa_pt = -1;
    if (sessionLong != null) {
        f_pa_pt = sessionLong.longValue();
    }

    sessionLong = (Long) session.getAttribute("f_trafo");
    long f_trafo = -1;
    if (sessionLong != null) {
        f_trafo = sessionLong.longValue();
    }

    sessionLong = (Long) session.getAttribute("f_grupa");
    long f_grupa = -1;
    if (sessionLong != null) {
        f_grupa = sessionLong.longValue();
    }

    sessionLong = (Long) session.getAttribute("f_cauza");
    long f_cauza = -1;
    if (sessionLong != null) {
        f_cauza = sessionLong.longValue();
    }

    sessionLong = (Long) session.getAttribute("f_nivel_tensiune");
    long f_nivel_tensiune = -1;
    if (sessionLong != null) {
        f_nivel_tensiune = sessionLong.longValue();
    }

    ro.emid.common.types.CategoryCheckData checkData = IncidenteSessionUtils.getCategoriiSelectate(session);
    String append_to = EchipamentAccessDB.setFilterParameters (checkData, centru.getId(), f_formatie, f_ansamblu, f_echipamentComponenta, f_statie, f_bobina, f_celula, f_bara, f_linie, f_derivatie, f_pa_pt, f_trafo, f_grupa, f_cauza, f_nivel_tensiune);
%>


<table width="100%" border=0>
<TR>
<TD align="center" valign="middle">
<span style="font-size:16px; font-weight:bold;">Situatia deteriorarilor echipamentelor</span><br>
<%
    String mesaj_filtru = "";

    if (f_ansamblu != -1) {
        Nomenclator ansamblu = NomAccessDB.getNom(f_ansamblu);
        mesaj_filtru += "<b>ansamblu</b> - " + ansamblu.getDescriere() + ";";
    }

    if (f_echipamentComponenta != -1) {
        Nomenclator echipamentComponenta = NomAccessDB.getNom(f_echipamentComponenta);
        mesaj_filtru += "<b>echipament</b> - " + echipamentComponenta.getDescriere() + ";";
    }

    long l = new Long(10).longValue();
    DataObj dataObj = null;
    if (f_statie != -1) {
        dataObj = DataDB.getById(f_statie, DataDB.isLocFunctional( NomConstants.NOM_STATII));
        if (dataObj != null) mesaj_filtru += " <b>statia</b> - " + dataObj.getNume() + ";";
    }

    Nomenclator nom = null;
    if (f_linie != -1) {
        dataObj = DataDB.getById(f_linie, DataDB.isLocFunctional( NomConstants.NOM_LINII));
        mesaj_filtru += " <b>linia</b> - " + dataObj != null ? dataObj.getNume() : "-" + ";";
    }

    if (f_derivatie != -1) {
        dataObj = DataDB.getById(f_derivatie, DataDB.isLocFunctional( NomConstants.NOM_DERIVATIE));
        mesaj_filtru += " <b>derivatia</b> - " + dataObj != null ? dataObj.getNume() : "-" + ";";
    }

    if (f_pa_pt != -1) {
        dataObj = DataDB.getById(f_pa_pt, DataDB.isLocFunctional(NomConstants.NOM_PA_PT));
        mesaj_filtru += " <b>pa_pt</b> - " + dataObj != null ? dataObj.getNume() : "-" + ";";
    }
    if (f_grupa != -1) {
        nom = NomAccessDB.getNom(f_grupa);
        mesaj_filtru += " <b>grupa</b> - " + nom != null ? nom.getDescriere() : "-" + ";";
    }

    if (f_cauza != -1) {
        nom = NomAccessDB.getNom(f_cauza);
        mesaj_filtru += " <b>cauza</b> - " + nom != null ? nom.getDescriere() : "-" + ";";
    }

    if (mesaj_filtru.equals("")) {
        mesaj_filtru = "total nefiltrat";
    } else {
        mesaj_filtru = "filtrare dupa: " + mesaj_filtru.substring(0, mesaj_filtru.length() - 1);
    }

    RaportByCauzeRow[] rows1;
    RaportByCauzeRow[] rows2;
    if (sucursalaRaport.getId() != -1) {
        rows2 = EchipamentAccessDB.get_raport_by_cauze_by_sucursala(sucursalaRaport.getId(), data_start, data_stop, append_to);
        rows1 = EchipamentAccessDB.get_raport_by_grupe_cauze_by_sucursala(sucursalaRaport.getId(), data_start, data_stop, append_to);
    } else {
        rows2 = EchipamentAccessDB.get_raport_by_cauze_by_filiala(filialaRaport.getId(), data_start, data_stop, append_to);
        rows1 = EchipamentAccessDB.get_raport_by_grupe_cauze_by_filiala(filialaRaport.getId(), data_start, data_stop, append_to);
    }




    RaportRow_01[] rowsAnsamble;
    RaportRow_01[] rowsComponente;
    if (sucursalaRaport.getId() != -1) {
        rowsAnsamble = EchipamentAccessDB.get_ans_raport_by_sucursala(sucursalaRaport.getId(), data_start, data_stop, append_to);
        rowsComponente = EchipamentAccessDB.get_componenta_raport_by_sucursala(sucursalaRaport.getId(), data_start, data_stop, append_to);
    } else {
        rowsAnsamble = EchipamentAccessDB.get_ans_raport_by_filiala(filialaRaport.getId(), data_start, data_stop, append_to);
        rowsComponente = EchipamentAccessDB.get_componenta_raport_by_filiala(filialaRaport.getId(), data_start, data_stop, append_to);
    }


    if( rowsAnsamble != null) Arrays.sort(rowsAnsamble, new Comparator() {
        public int compare(Object o1, Object o2) {
            RaportRow_01 r1 = (RaportRow_01)o1;
            RaportRow_01 r2 = (RaportRow_01)o2;

            if( r1.ansamblu.getId() == -1) return 1;
            if( r2.ansamblu.getId() == -1) return -1;
            return new Integer(r1.ansamblu.getCod()).compareTo( new Integer( r2.ansamblu.getCod()));
        }
    });

    if( rowsComponente != null) Arrays.sort(rowsComponente, new Comparator() {
        public int compare(Object o1, Object o2) {
            RaportRow_01 r1 = (RaportRow_01)o1;
            RaportRow_01 r2 = (RaportRow_01)o2;

            //if( r1.componenta.getId() == -1) return 1;
            //if( r2.componenta.getId() == -1) return -1;
            return new Integer(r1.componenta.getCod()).compareTo( new Integer( r2.componenta.getCod()));
        }
    });

    if( !StringUtils.isZeroLength( request.getParameter("toExport"))){
        String root = AppConstants.PUBLIC_RESOURCE_PATH + File.separator + "raport";
        String folder = _df.format( new java.util.Date());
        //cleanup...
        File[]folders = new File( root).listFiles();
        if( folders != null) for (int i = 0; i < folders.length; i++) {
            File file = folders[i];
            if( !file.getName().equals( folder)){
                File[]d=file.listFiles();
                if( d!=null) for (int j = 0; j < d.length; j++) {
                    File file1 = d[j];
                    file1.delete();
                }
                file.delete( );  //cleanup
            }
        }

        File f = new File( root, folder);
        if( !f.exists()) f.mkdir();

        String fileName = "Raport echipamente - " + System.currentTimeMillis() + ".xls";
        String privatePath = root + File.separator + folder + File.separator + fileName;

        publicPath = AppConstants.WEB_RESOURCE_PATH + "/raport/" + folder + "/" + fileName;

        String templatePath = AppConstants.PRIVATE_PATH + File.separator + "template" + File.separator + "Raport Echipamente.xls";

        f = new File( privatePath);
        if( f.exists()){
            f.delete();
        }

        RaportEchipamenteExcelExporter exporter = new RaportEchipamenteExcelExporter( );
        exporter.begin( templatePath, privatePath);
        exporter.export( rows1, rows2, rowsAnsamble, rowsComponente);
        exporter.end();
    }
%>
(<%=mesaj_filtru%>)<BR>
[<a class="link" href="<%=AppConstants.APP_NAME%>?action=resetFiltru&fromRaport=true">reset filtru</a>] <br>
<span style="font-size:16px; font-weight:bold;" >Deteriorari pe tipuri de echipamente</span>
<div class="spacer"></div>
<FORM name="zaforma" action="<%=AppConstants.APP_NAME%>" method="post">
    <INPUT TYPE="hidden" NAME="action" VALUE="saveRapoarteEchipamente" />
    <table class="gsaptd" width="100%" border="0">
        <tr><td >
            <% if( !checkData.someDataChecked()){ %>
            <nobr>
                Filiala:
                <SELECT name="filiala" onchange="fill_sucursala_select()" class="gSAPSelect" <%=user.getIeLevel() == RoleConstants.NIVEL_FULL ? "" : "disabled"%> >
                    <OPTION value="-1">ELECTRICA SA:</OPTION>
                    <%  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;

                <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(<%=centru.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>
            </nobr>

            <%if( user.getIeLevel() < RoleConstants.NIVEL_CENTRU){ %>
            <A href="#" onClick="openCheckCategorii(); return false" class="link"><IMG alt="Click pentru a selecta mai multe sucursale/C.E." border=0 width=12 height=13 hspace=3 src="<%= AppConstants.WEB_RESOURCE_PATH + "/images/icon_mini_search.gif" %>"></A>
            <script type="text/javascript">
                var checkCategoriiWindow = null;
                function openCheckCategorii(){
                    if( checkCategoriiWindow) checkCategoriiWindow.close();
                    checkCategoriiWindow = window.open('<%=AppConstants.APP_NAME%>?action=checkCategorie','checkCategorie', 'width=800 height=620, scrollbars=yes,status=yes,resizable=yes,top=10,left=50,dependent=yes,alwaysRaised=yes');
                }
            </script>
            <% } %>
            <%}else{%>
            <%
                StringBuffer strCategorii = new StringBuffer("");
                long idCentre[] = checkData.getIdCentre();
                Set sucursaleSelectate = new HashSet();
                Set filialeSelectate = new HashSet();
                for (int i = 0; i < idCentre.length; i++) {
                    long id = idCentre[i];
                    Category c = CategoryAccessDB.getById(id);
                    if (c != null) {
                        Category s = CategoryAccessDB.getById(c.getId_parent());
                        if (s != null) {
                            Category f = CategoryAccessDB.getById(s.getId_parent());
                            if (f != null) {
                                if (strCategorii.length() > 0) strCategorii.append("<br>");
                                strCategorii.append(f.getName()).append(", ").append(s.getName()).append(", ").append(c.getName());

                                sucursaleSelectate.add(new Long(s.getId()));
                                filialeSelectate.add(new Long(f.getId()));
                            }
                        }
                    }
                }

                long idSucursale[] = checkData.getIdSucursale();
                for (int i = 0; i < idSucursale.length; i++) {
                    long id = idSucursale[i];
                    if( !sucursaleSelectate.contains( new Long( id))){
                        Category s = CategoryAccessDB.getById( id );
                        if( s != null){
                            Category f = CategoryAccessDB.getById( s.getId_parent());
                            if( f != null){
                                if( strCategorii.length() > 0) strCategorii.append("<br>");
                                strCategorii.append(f.getName()).append(", ").append(s.getName());
                            }
                        }
                    }
                }

                long idFiliale[] = checkData.getIdFiliale();
                for (int i = 0; i < idFiliale.length; i++) {
                    long id = idFiliale[i];
                    if( !filialeSelectate.contains( new Long( id))){
                        Category f = CategoryAccessDB.getById( id);
                        if( f != null){
                            if( strCategorii.length() > 0) strCategorii.append("<br>");
                            strCategorii.append(f.getName());
                        }
                    }
                }
            %>

            <table><tr><td>
                <%=strCategorii%>
            </td></tr></table>
            <% } %>
        </td>

        <td align="right"><nobr>
            <%
                Date dataStart = data_start != 0 ? new Date(data_start) : null;
                Date dataStop = data_stop != 0 ? new Date(data_stop) : null;
            %>

            <label title="Selectati date egale pentru a trece prin toate echipamentele">
                Perioada: <emid:datefield name="data_start" value="<%=dataStart%>" width="80px"/>
            </label>
             -
            <emid:datefield name="data_stop" value="<%=dataStop%>" width="80px"/>

            <input type="submit" value="Rulează" class="ml10"/>
            <input type="hidden" name="toExport" value=""/>
            <input type="button" value="Export" onclick="this.form.toExport.value = 'do'; this.form.submit(); " /></nobr>
        </td></tr>
    </table>
</FORM>
<div class="spacer"></div>
</TD>
</TR>
<TR>
    <TD>
        <TABLE border=1 cellPadding=2 cellSpacing=1 width="100%" class="gSAPTHEAD" style="BACKGROUND-COLOR: white">
            <TR>
                <TH class="gsapthead" noWrap rowspan="2">Cod<br>Ansamblu</TH>
                <TH class="gsapthead" noWrap rowspan="2">Cod<br>Echipament</TH>
                <TH class="gsapthead" noWrap rowspan="2">Echipament</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>
            <%
                long oneDay = 24 * 60 * 60 * 1000;
                data_stop += oneDay;

                session.setAttribute( "rapoarteEchipamenteFiltru.data_start", new Long( data_start));
                session.setAttribute( "rapoarteEchipamenteFiltru.data_stop", new Long( data_stop));
                session.setAttribute( "rapoarteEchipamenteFiltru.append_to", append_to);

                if (null != rowsAnsamble && rowsComponente != null) {
                    for (int i = 0; i < rowsAnsamble.length; i++) {
                        String classul = "";
                        if (i + 1 == rowsAnsamble.length) classul = "class=\"gsapthead\"";%>
            <%if (i > 0 ) {%>
            <tr>
                <td colspan="15">
                    &nbsp;
                </td>
            </tr>
            <%}%>
            <TR class="gsapthead">
                <TD align="center"><%=(rowsAnsamble[i].ansamblu.getId() == -1) ? "&nbsp" : String.valueOf( (nom = NomAccessDB.getNom( rowsAnsamble[i].ansamblu.getId())) != null ? "" + nom.getCod() : "&nbsp")%></TD>
                <td>&nbsp;</td>
                <TD>
                    <%if( i + 1 < rowsAnsamble.length ){%><a class="link" href="javascript: void(null)" title="Click pentru a vizualiza Echipamentele" onclick="openEchipamenteByComponentaId(<%=rowsAnsamble[i].ansamblu.getId()%>)"><%}%>
                    <%=rowsAnsamble[i].ansamblu.getDescriere()%>
                    <%if( i + 1 < rowsAnsamble.length ){%> </a> <%}%>
                </TD>
                <TD align="right"><%=rowsAnsamble[i].t_nr_incidente%></TD>
                <TD align="right"><%if("Totale".equals( rowsAnsamble[i].ansamblu.getDescriere())){%>&nbsp;<%}else{%> <%=rowsAnsamble[i].t_durata_indisp/60%>h <%=rowsAnsamble[i].t_durata_indisp%60%>' <%}%></TD>
                <TD align="right"><%=AppConstants.format3(rowsAnsamble[i].chelt_ment)%></TD>
                <TD align="right"><%=AppConstants.format3(rowsAnsamble[i].chelt_daune)%></TD>
                <TD align="right"><%=AppConstants.format3(rowsAnsamble[i].chelt_altele)%></TD>
                <TD align="right"><%=AppConstants.format3(rowsAnsamble[i].t_chelt_totale)%></TD>
            </TR>
            <% for (int j = 0; j < rowsComponente.length; j++) if( rowsComponente[j].ansamblu.getId() == rowsAnsamble[i].ansamblu.getId()){ %>
            <TR <%=classul%>>
                <td>&nbsp;</td>
                <TD align="center"><%=(rowsComponente[j].componenta.getId() == -1) ? "&nbsp" : String.valueOf( (nom = NomAccessDB.getNom( rowsComponente[j].componenta.getId())) != null ? "" + nom.getCod() : "&nbsp")%></TD>
                <TD>
                    <a class="link" href="javascript: void(null)" title="Click pentru a vizualiza Echipamentele" onclick="openEchipamenteByComponentaId(<%=rowsAnsamble[i].ansamblu.getId()%>, <%=rowsComponente[j].componenta.getId()%>)">
                        <%=rowsComponente[j].componenta.getDescriere()%>
                    </a>
                </TD>
                <TD align="right"><%=rowsComponente[j].t_nr_incidente%></TD>
                <TD align="right"><%if("Totale".equals( rowsComponente[j].ansamblu.getDescriere())){%>&nbsp;<%}else{%> <%=rowsComponente[j].t_durata_indisp/60%>h <%=rowsComponente[j].t_durata_indisp%60%>' <%}%></TD>
                <TD align="right"><%=AppConstants.format3(rowsComponente[j].chelt_ment)%></TD>
                <TD align="right"><%=AppConstants.format3(rowsComponente[j].chelt_daune)%></TD>
                <TD align="right"><%=AppConstants.format3(rowsComponente[j].chelt_altele)%></TD>
                <TD align="right"><%=AppConstants.format3(rowsComponente[j].t_chelt_totale)%></TD>
            </tr>
            <% } %>
            <% } %>
            <%} else {%>
            <TR>
                <TD>
                    Nu exista informatii...
                </TD>
            </TR>
            <%}%>
        </TABLE>
    </TD>
</TR>
<TR>
    <TD align="center">
        <BR>
        <span style="font-size:16px; font-weight:bold;" >Deteriorari de echipamente pe cauze</span>
    </TD>
</TR>
<TR>
    <TD>
        <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>

            <%
                String classul = "class=";
                int j = 0;

                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">
                <% String codGrupa = (rows1[i].cauzaGrupa.getId() == -1) ? "&nbsp" : String.valueOf( (nom = NomAccessDB.getNom( rows1[i].cauzaGrupa.getId())) != null ? "" + 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 ? "" + 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>

    </TD>
</TR>
</table>

<script type="text/javascript">
    <% if ( publicPath != null){ %>

    setTimeout(function(){
        window.location = '<%=publicPath%>';
    }, 500);

    <% } %>

    function reloadPage(){
        window.location = "<%=AppConstants.APP_NAME%>?action=raport-echipamente";
    }

    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,'echipamenteRaport', 'top=50,left=50,width=950, height=550, status=yes,resizable=yes,dependent=yes,alwaysRaised=yes, scrollbars=yes');
    }

    function categoriesChecked(){
        document.zaforma.submit();
    }
</script>

