<%@ page import="org.apache.log4j.Logger" %>
<%@ page import="ro.emid.common.db.IncidentAccessDB" %>
<%@ page import="ro.emid.common.types.DataObj" %>
<%@ page import="ro.emid.common.db.RaportByCauzeRow" %>
<%@ page import="ro.emid.common.types.RaportRow_01" %>
<%@ page import="ro.emid.common.utils.StringUtils" %>
<%@ page import="ro.emid.incidente.util.raport.RaportIncidenteExporter" %>
<%@ page import="java.io.File" %>
<%@ page import="java.sql.Date" %>
<%@ page import="java.util.HashSet" %>
<%@ page import="java.util.Set" %>
<%@ page import="ro.emid.common.utils.ObjectUtils" %>
<%@ page import="ro.emid.common.constants.AppConstants" %>
<%@ page import="ro.emid.common.types.*" %>
<%@ page import="ro.emid.common.db.*" %>
<%@ page import="ro.emid.common.constants.RoleConstants" %>
<%@include file="../incidente/declaratii.jsp"%>

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

<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> Incidente
        </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 final java.text.SimpleDateFormat DF = new java.text.SimpleDateFormat("dd.MM.yyyy");
%>
<%
    String publicPath = null;

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

        boolean grupAnsamble = ObjectUtils.getIntValue(request.getParameter("grupAnsamble"), 0) == 1;

        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();
        //if (sCentru != null ) 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");
        long data_start = 0;
        if (sdata_start != null) data_start = sdata_start.longValue();

        Long sdata_stop = (Long) session.getAttribute("data_stop");
        long data_stop = 0;
        if (sdata_stop != null) {
            data_stop = sdata_stop.longValue();
        }else{
            data_stop = ObjectUtils.getDateValue(
                    DF.format(new Date(System.currentTimeMillis())),
                    "dd.MM.yyyy"
            ).getTime();
        }

        Long sessionLong;
        String sessionString;

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

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

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

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

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



        sessionString = (String) session.getAttribute("f_statie_cod");
        String f_statie = "";
        if (sessionString != null) {
            f_statie = sessionString;
        }

        sessionString = (String) session.getAttribute("f_bobina_cod");
        String f_bobina = "";
        if (sessionString != null) {
            f_bobina = sessionString;
        }

        sessionString = (String) session.getAttribute("f_celula_cod");
        String f_celula = "";
        if (sessionString != null) {
            f_celula = sessionString;
        }

        sessionString = (String) session.getAttribute("f_bara_cod");
        String f_bara = "";
        if (sessionString != null) {
            f_bara = sessionString;
        }

        sessionString = (String) session.getAttribute("f_linie_cod");
        String f_linie = "";
        if (sessionString != null) {
            f_linie = sessionString;
        }

        sessionString = (String) session.getAttribute("f_derivatie_cod");
        String f_derivatie = "";
        if (sessionString != null) {
            f_derivatie = sessionString;
        }

        sessionString = (String) session.getAttribute("f_pa_pt_cod");
        String f_pa_pt = "";
        if (sessionString != null) {
            f_pa_pt = sessionString;
        }

        sessionString = (String)  session.getAttribute("f_trafo_cod");
        String f_trafo = "";
        if (sessionString != null) {
            f_trafo = sessionString;
        }



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


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

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

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

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

        sessionLong = (Long) session.getAttribute("f_prilej");
        long f_prilej = -1;
        if (sessionLong != null) {
            f_prilej = 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_fenomen_meteo");
        long f_fenomen_meteo = -1;
        if (sessionLong != null) {
            f_fenomen_meteo = sessionLong.longValue();
        }

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

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


        ro.emid.common.types.CategoryCheckData checkData = SessionUtils.getCategoriiSelectate(session);
        String append_to = IncidentAccessDB.setFilterParameters(checkData,  centru.getId(), f_formatie, f_clasificare_incident, f_ansamblu, f_statie, f_bobina, f_celula, f_bara, f_linie, f_derivatie, f_pa_pt, f_trafo, f_nr_componente_afectate, f_mod_tr_neutru, f_manifestare, f_prilej, f_grupa, f_cauza, f_fenomen_meteo, f_clasificare, f_forta_majora, f_tensiune);
		
%>

<table width="100%" border=0>
<TR>
<TD align="center" valign="middle">
<%
    String titlu;
    if (sucursalaRaport.getId() != -1) {
        titlu = " sucursala " + sucursalaRaport.getName();
    } else if (filialaRaport.getId() == -1) {
        titlu = "SC ELECTRICA SA";
    } else {
        titlu = " filiala " + filialaRaport.getName();
    }
%>
<%--<H4>Situatia incidentelor pe ansambluri functionale - <%=titlu%></H4>--%>
<span style="font-size:16px; font-weight:bold;">Situatia incidentelor pe ansambluri functionale</span><br>
<%
    String mesaj_filtru = "";
    if (f_formatie != -1) {
        Category formatie = CategoryAccessDB.getById(f_formatie);
        if (formatie != null)
            mesaj_filtru += "<b>" + (sucursalaRaport.isCuSectiiSiCentre() ? "Centru exploatare" : "Formatia de lucru") + ":</b> - " + formatie.getName() + ";";
    }

    if (f_clasificare_incident != -1) {
        String[] values = {"izolat", "primar", "asociat"};
        mesaj_filtru = "<b>tip incident</b> - " + values[(int) f_clasificare_incident - 1] + ";";
    }

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

    if (f_clasificare != -1) {
        String[] values = {"IT", "MT", "Toate"};
        mesaj_filtru += " <b>nivel</b> - " + values[(int) f_clasificare - 1] + ";";
    }
    long l = new Long(10).longValue();
    DataObj dataObj = null;
    Nomenclator nom = null;


    if( !StringUtils.isZeroLength( f_statie)){
        mesaj_filtru += "<b>statie</b> - " + session.getAttribute("f_statie_den") + ";";
    }
    if( !StringUtils.isZeroLength( f_bobina)){
        mesaj_filtru += "<b>bobina</b> - " + session.getAttribute("f_bobina_den") + ";";
    }
    if( !StringUtils.isZeroLength( f_celula)){
        mesaj_filtru += "<b>celula</b> - " + session.getAttribute("f_celula_den") + ";";
    }
    if( !StringUtils.isZeroLength( f_bara)){
        mesaj_filtru += "<b>bara</b> - " + session.getAttribute("f_bara_den") + ";";
    }
    if( !StringUtils.isZeroLength( f_linie)){
        mesaj_filtru += "<b>linie</b> - " + session.getAttribute("f_linie_den") + ";";
    }
    if( !StringUtils.isZeroLength( f_derivatie)){
        mesaj_filtru += "<b>derivatie</b> - " + session.getAttribute("f_derivatie_den") + ";";
    }
    if( !StringUtils.isZeroLength( f_pa_pt)){
        mesaj_filtru += "<b>pa/pt</b> - " + session.getAttribute("f_pa_pt_den") + ";";
    }
    if( !StringUtils.isZeroLength( f_trafo)){
        mesaj_filtru += "<b>trafo</b> - " + session.getAttribute("f_trafo_den") + ";";
    }


    if( !StringUtils.isZeroLength( f_statie)){
        mesaj_filtru += "<b>statie</b> - " + session.getAttribute("f_statie_den") + ";";
    }
    if( !StringUtils.isZeroLength( f_bobina)){
        mesaj_filtru += "<b>bobina</b> - " + session.getAttribute("f_bobina_den") + ";";
    }
    if( !StringUtils.isZeroLength( f_celula)){
        mesaj_filtru += "<b>celula</b> - " + session.getAttribute("f_celula_den") + ";";
    }
    if( !StringUtils.isZeroLength( f_bara)){
        mesaj_filtru += "<b>bara</b> - " + session.getAttribute("f_bara_den") + ";";
    }
    if( !StringUtils.isZeroLength( f_linie)){
        mesaj_filtru += "<b>linie</b> - " + session.getAttribute("f_linie_den") + ";";
    }
    if( !StringUtils.isZeroLength( f_derivatie)){
        mesaj_filtru += "<b>derivatie</b> - " + session.getAttribute("f_derivatie_den") + ";";
    }
    if( !StringUtils.isZeroLength( f_pa_pt)){
        mesaj_filtru += "<b>pa/pt</b> - " + session.getAttribute("f_pa_pt_den") + ";";
    }
    if( !StringUtils.isZeroLength( f_trafo)){
        mesaj_filtru += "<b>trafo</b> - " + session.getAttribute("f_trafo_den") + ";";
    }

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

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

    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 (f_fenomen_meteo != -1) {
        nom = NomAccessDB.getNom(f_fenomen_meteo);
        mesaj_filtru += " <b>fenomen meteo</b> - " + nom != null ? nom.getDescriere() : "-" + ";";
    }

    if (f_forta_majora > 0) {
        mesaj_filtru += " <b>Forta majora</b>; ";
    }

    if (f_tensiune > 0) {
        mesaj_filtru += " <b>Tensiune </b> - " + f_tensiune + " kV;";
    }

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

    long oneDay = 24 * 60 * 60 * 1000;

    session.setAttribute( "rapoarteIncidenteFiltru.data_start", new Long( data_start));
    session.setAttribute( "rapoarteIncidenteFiltru.data_stop", new Long( data_stop + oneDay));
    session.setAttribute( "rapoarteIncidenteFiltru.append_to", append_to);

    String value_data_start = data_start != 0 ? AppConstants.format(new Date(data_start)) : "";
    String value_data_stop  = data_stop  != 0 ? AppConstants.format(new Date(data_stop))  : "";

    Date dataStart = data_start != 0 ? new Date(data_start) : null;
    Date dataStop  = data_stop  != 0 ? new Date(data_stop) : null;

    data_stop += oneDay;

    //extragem datele
    RaportRow_01[] rows;
    if (sucursalaRaport.getId() != -1) {
        rows = IncidentAccessDB.get_ans_raport_by_sucursala(sucursalaRaport.getId(), data_start, data_stop, append_to);
    } else {
        rows = IncidentAccessDB.get_ans_raport_by_filiala(filialaRaport.getId(), data_start, data_stop, append_to);
    }

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

    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 incidente - " + System.currentTimeMillis() + ".xls";
        String privatePath = root + File.separator + folder + File.separator + fileName;

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


        RaportIncidenteExporter exporter = new RaportIncidenteExporter( privatePath );

        exporter.begin();

        exporter.setHeader( "Raport incidente " + value_data_start + " " + value_data_stop, filialaRaport, sucursalaRaport, centruExploatare);

        exporter.export( rows, rows1, rows2);

        exporter.end();
    }
%>

(<%=mesaj_filtru%>)<BR>
[<a class="link" href="<%=AppConstants.APP_NAME%>?action=resetFiltru&fromRaport=true&raport=raport-incidente">reset filtru</a>]

<div class="spacer"></div>
<FORM name="zaforma" action="<%=AppConstants.APP_NAME%>" method="post">
    <INPUT TYPE="hidden" NAME="action" VALUE="saveRapoarteIncidente" />
    <table class="gsaptd" width="100%">
        <tr><td>
            <% if( !checkData.someDataChecked()){ %>
            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>

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

            <%=strCategorii%>
            <%}%>
        </td>
            <td align="right">

                <input type="checkbox" name="grupAnsamble" value="1" <%=grupAnsamble ? "checked" : "" %> title="Bifa pentru a grupa ansablele functionale" />

                <%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>
                <%}%>

                <label title="Perioada producerii deranjamentelor">
                    Perioada: <emid:datefield name="data_start" value="<%=dataStart%>"/>
                </label>
                 -
                <emid:datefield name="data_stop" value="<%=dataStop%>"/>
            </td>
            <td width="1%"><nobr>
                &nbsp;<input type="submit" value="Rulează" />

                <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 id="tableAnsamble" border=1 cellPadding=2 cellSpacing=1 width="100%" class="gSAPTHEAD" style="BACKGROUND-COLOR: white">
            <TR>
                <TH class="gsapthead" noWrap rowspan="2">Cod</TH>
                <TH class="gsapthead" noWrap rowspan="2">Ansamblu</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.<BR>incident</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 != rows){ %>
            <%    if( grupAnsamble){
                RaportRow_01  buffer = new RaportRow_01();

                //calculare buffer
                for (int i = 0; i < rows.length; i++) {
                    String codAnsamblu = (rows[i].ansamblu.getId() == -1) ? "" : String.valueOf( (nom = NomAccessDB.getNom( rows[i].ansamblu.getId())) != null ? "" + nom.getCod() : "");
                    if( codAnsamblu == null) codAnsamblu = "";

                    boolean toAddToBuffer = ! ("50".equals( codAnsamblu.trim()) || "51".equals( codAnsamblu.trim()) || "Totale".equals( rows[i].ansamblu.getDescriere()));

                    if( toAddToBuffer){
                        buffer.t_nr_incidente  += rows[i].t_nr_incidente;
                        buffer.wd_nr_incidente  += rows[i].wd_nr_incidente;
                        buffer.t_durata_incidente  += rows[i].t_durata_incidente;
                        buffer.wd_durata_incidente  += rows[i].wd_durata_incidente;
                        buffer.t_durata_indisp  += rows[i].t_durata_indisp;
                        buffer.wd_durata_indisp  += rows[i].wd_durata_indisp;
                        buffer.durata_medie  += rows[i].durata_medie;
                        buffer.t_energ_nelivr += rows[i].t_energ_nelivr;
                        buffer.chelt_ment  += rows[i].chelt_ment;
                        buffer.chelt_daune += rows[i].chelt_daune;
                        buffer.chelt_altele += rows[i].chelt_altele;
                        buffer.t_chelt_totale += rows[i].t_chelt_totale;
                    }
                }

                //afisam doar 50 si 51
                for (int i = 0; i < rows.length; i++) {
                    String codAnsamblu = (rows[i].ansamblu.getId() == -1) ? "" : String.valueOf( (nom = NomAccessDB.getNom( rows[i].ansamblu.getId())) != null ? "" + nom.getCod() : "");
                    if( codAnsamblu == null) codAnsamblu = "";

                    boolean toShow = ("50".equals( codAnsamblu.trim()) || "51".equals( codAnsamblu.trim()));

                    if( !toShow) continue;
            %>
            <TR isChild="false" >
                <TD align="center"><%=StringUtils.getEmpty( codAnsamblu, "&nbsp;")%></TD>
                <TD>
                    <%if( i + 1 < rows.length ){%> <a href="javascript: void(null)" class="link" title="Click pentru a vizualiza Fisele de Incident" onclick="openIncidenteByAnsambluID( <%=rows[i].ansamblu.getId()%>)"> <%}%>
                    <%=rows[i].ansamblu.getDescriere()%>
                    <%if( i + 1 < rows.length ){%> </a> <%}%>
                </TD>
                <TD align="right"><%=rows[i].t_nr_incidente%></TD>
                <TD align="right"><%=rows[i].wd_nr_incidente%></TD>
                <TD align="right" nowrap><%=rows[i].t_durata_incidente/60%>h <%=rows[i].t_durata_incidente%60%>'      </TD>
                <TD align="right" nowrap><%=rows[i].wd_durata_incidente/60%>h <%=rows[i].wd_durata_incidente%60%>'    </TD>
                <TD align="right" nowrap><%=rows[i].t_durata_indisp/60%>h <%=rows[i].t_durata_indisp%60%>'            </TD>
                <TD align="right" nowrap><%=rows[i].wd_durata_indisp/60%>h <%=rows[i].wd_durata_indisp%60%>'          </TD>
                <TD align="right" nowrap><%=rows[i].durata_medie/60%>h <%=rows[i].durata_medie%60%>'                  </TD>
                <TD align="right"><%=rows[i].t_energ_nelivr%></TD>
                <TD align="right"><%=AppConstants.format3(rows[i].chelt_ment)%></TD>
                <TD align="right"><%=AppConstants.format3(rows[i].chelt_daune)%></TD>
                <TD align="right"><%=AppConstants.format3(rows[i].chelt_altele)%></TD>
                <TD align="right"><%=AppConstants.format3(rows[i].t_chelt_totale)%></TD>
            </TR>
            <% } %>

            <%--afisam inainte de Totaluri linia buffer--%>
            <TR style="font-weight: bold;">
                <TD align="center">

                    <span id="plusId">&nbsp;</span>
                    <script type="text/javascript">
                        var plus = new Plus("plusId", null, function(){
                            var table = $( "tableAnsamble");
                            for( var i = 0; i <  table.rows.length; i++){
                                var row = table.rows[ i];
                                var attr = row.getAttribute("isChild");
                                if( attr && attr == "true"){
                                    if( this.getStatus() == PlusConstants.STATUS_PLUS){
                                        Element.hide( row);
                                    }else{
                                        Element.show( row);
                                    }
                                }
                            }
                        });
                    </script>
                </TD>
                <TD>
                    Alte instalatii in afara de 50 si 51
                </TD>
                <TD align="right"><%=buffer.t_nr_incidente%></TD>
                <TD align="right"><%=buffer.wd_nr_incidente%></TD>
                <TD align="right" nowrap><%=buffer.t_durata_incidente/60%>h <%=buffer.t_durata_incidente%60%>'      </TD>
                <TD align="right" nowrap><%=buffer.wd_durata_incidente/60%>h <%=buffer.wd_durata_incidente%60%>'    </TD>
                <TD align="right" nowrap><%=buffer.t_durata_indisp/60%>h <%=buffer.t_durata_indisp%60%>'            </TD>
                <TD align="right" nowrap><%=buffer.wd_durata_indisp/60%>h <%=buffer.wd_durata_indisp%60%>'          </TD>
                <TD align="right" nowrap><%=buffer.durata_medie/60%>h <%=buffer.durata_medie%60%>'                  </TD>
                <TD align="right"><%=buffer.t_energ_nelivr%></TD>
                <TD align="right"><%=AppConstants.format3(buffer.chelt_ment)%></TD>
                <TD align="right"><%=AppConstants.format3(buffer.chelt_daune)%></TD>
                <TD align="right"><%=AppConstants.format3(buffer.chelt_altele)%></TD>
                <TD align="right"><%=AppConstants.format3(buffer.t_chelt_totale)%></TD>
            </TR>

            <%--afisam restul de linii plus linia de totaluri--%>
            <% for (int i = 0; i < rows.length; i++) { %>
            <%        String classul = ""; %>
            <%        if (i + 1 == rows.length) classul = "class=\"gsapthead\"";

                String codAnsamblu = (rows[i].ansamblu.getId() == -1) ? "" : String.valueOf( (nom = NomAccessDB.getNom( rows[i].ansamblu.getId())) != null ? "" + nom.getCod() : "");
                if( codAnsamblu == null) codAnsamblu = "";


                boolean isTotalRow = "Totale".equals( rows[i].ansamblu.getDescriere());
                boolean isChild = !( "50".equals( codAnsamblu.trim()) || "51".equals( codAnsamblu.trim())) && !isTotalRow;

                if( !( isChild || isTotalRow)) continue;
            %>

            <TR <%=classul%> isChild="<%=isChild%>" <% if(  isChild ){%> style="display: none; color: gray" <% } %> >
                <TD align="center"><%=StringUtils.getEmpty( codAnsamblu, "&nbsp;")%></TD>
                <TD>
                    <%if( i + 1 < rows.length ){%> <a href="javascript: void(null)" class="link" title="Click pentru a vizualiza Fisele de Incident" onclick="openIncidenteByAnsambluID( <%=rows[i].ansamblu.getId()%>)"> <%}%>
                    <%=rows[i].ansamblu.getDescriere()%>
                    <%if( i + 1 < rows.length ){%> </a> <%}%>
                </TD>
                <TD align="right"><%=rows[i].t_nr_incidente%></TD>
                <TD align="right"><%=rows[i].wd_nr_incidente%></TD>
                <TD align="right" nowrap><%=rows[i].t_durata_incidente/60%>h <%=rows[i].t_durata_incidente%60%>'      </TD>
                <TD align="right" nowrap><%=rows[i].wd_durata_incidente/60%>h <%=rows[i].wd_durata_incidente%60%>'    </TD>
                <TD align="right" nowrap><%=rows[i].t_durata_indisp/60%>h <%=rows[i].t_durata_indisp%60%>'            </TD>
                <TD align="right" nowrap><%=rows[i].wd_durata_indisp/60%>h <%=rows[i].wd_durata_indisp%60%>'          </TD>
                <TD align="right" nowrap><%=rows[i].durata_medie/60%>h <%=rows[i].durata_medie%60%>'                  </TD>
                <TD align="right"><%=rows[i].t_energ_nelivr%></TD>
                <TD align="right"><%=AppConstants.format3(rows[i].chelt_ment)%></TD>
                <TD align="right"><%=AppConstants.format3(rows[i].chelt_daune)%></TD>
                <TD align="right"><%=AppConstants.format3(rows[i].chelt_altele)%></TD>
                <TD align="right"><%=AppConstants.format3(rows[i].t_chelt_totale)%></TD>
            </TR>
            <%        } %>

            <%--gata cu minuntea lu lazar      --%>

            <%    }else for (int i = 0; i < rows.length; i++) { %>
            <%        String classul = ""; %>
            <%        if (i + 1 == rows.length) classul = "class=\"gsapthead\""; %>
            <TR <%=classul%>>
                <TD align="center"><%=(rows[i].ansamblu.getId() == -1) ? "&nbsp" : String.valueOf( (nom = NomAccessDB.getNom( rows[i].ansamblu.getId())) != null ? "" + nom.getCod() : "&nbsp")%></TD>
                <TD>
                    <%if( i + 1 < rows.length ){%> <a href="javascript: void(null)" class="link" title="Click pentru a vizualiza Fisele de Incident" onclick="openIncidenteByAnsambluID( <%=rows[i].ansamblu.getId()%>)"> <%}%>
                    <%=rows[i].ansamblu.getDescriere()%>
                    <%if( i + 1 < rows.length ){%> </a> <%}%>
                </TD>
                <TD align="right"><%=rows[i].t_nr_incidente%></TD>
                <TD align="right"><%=rows[i].wd_nr_incidente%></TD>
                <TD align="right" nowrap><%=rows[i].t_durata_incidente/60%>h <%=rows[i].t_durata_incidente%60%>'      </TD>
                <TD align="right" nowrap><%=rows[i].wd_durata_incidente/60%>h <%=rows[i].wd_durata_incidente%60%>'    </TD>
                <TD align="right" nowrap><%=rows[i].t_durata_indisp/60%>h <%=rows[i].t_durata_indisp%60%>'            </TD>
                <TD align="right" nowrap><%=rows[i].wd_durata_indisp/60%>h <%=rows[i].wd_durata_indisp%60%>'          </TD>
                <TD align="right" nowrap><%=rows[i].durata_medie/60%>h <%=rows[i].durata_medie%60%>'                  </TD>
                <TD align="right"><%=rows[i].t_energ_nelivr%></TD>
                <TD align="right"><%=AppConstants.format3(rows[i].chelt_ment)%></TD>
                <TD align="right"><%=AppConstants.format3(rows[i].chelt_daune)%></TD>
                <TD align="right"><%=AppConstants.format3(rows[i].chelt_altele)%></TD>
                <TD align="right"><%=AppConstants.format3(rows[i].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;" >Situatia incidentelor 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" 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>
            <%

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

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

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



<script type="text/javascript">
    var incidenteWindow = null;
    function openIncidenteByAnsambluID( idAnsamblu){
        openIncidenteWindow( '<%=AppConstants.APP_NAME%>?action=showIncidenteFromRaport&idAnsamblu='+idAnsamblu);
    }

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

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

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

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

    <% if ( publicPath != null){ %>

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

    <% } %>
</script>