<%@ page import="ro.emid.common.types.Category" %>
<%@ page import="ro.emid.common.db.CategoryAccessDB" %>
<%@ page import="ro.emid.common.db.RaportByCauzeRow" %>
<%@ page import="ro.emid.common.types.RaportRow_01" %>
<%@ page import="ro.emid.common.db.DataDB" %>
<%@ page import="ro.emid.common.types.DataObj" %>
<%@ page import="ro.emid.common.db.NomAccessDB" %>
<%@ page import="ro.emid.common.types.Nomenclator" %>
<%@ page import="java.sql.Date" %>
<%@ page import="ro.emid.common.constants.AppConstants" %>
<%@ page import="ro.emid.common.db.EchipamentAccessDB" %>
<%@ page import="ro.emid.common.constants.NomConstants" %>
<%@ page import="ro.emid.incidente.IncidenteSessionUtils" %>
<%@ 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>

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

    final Category sFiliala = (Category) session.getAttribute("filiala");
    final Category sSucursala = (Category) session.getAttribute("sucursala");
    final 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();

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

    final 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 = SessionUtils.getCategoriiSelectate(session);  // ??
    final 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) mesaj_filtru += " ansamblu - " + f_ansamblu + ';';

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

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

      final 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 ("".equals(mesaj_filtru)) mesaj_filtru = "total nefiltrat";
      else mesaj_filtru = "filtrare dupa: " + mesaj_filtru.substring(0, mesaj_filtru.length() - 1);%>
  (<%=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%">
       <tr><td>
            Filiala:
            <SELECT name="filiala" onchange="fill_sucursala_select()" class="gSAPSelect" <%=user.getIeLevel() == RoleConstants.NIVEL_FULL ? "" : "disabled"%> >
                <OPTION value="-1">ELECTRICA SA:</OPTION>
                <%  final Category [] filiale = CategoryAccessDB.getAllCategoriesByParentId(-1);
                  if (filiale != null) for(int i=0;i<filiale.length;i++) {%>
                  <OPTION <%=(filiale[i].getId() == filialaRaport.getId()) ? "selected=\"true\" " : ""%>value="<%=filiale[i].getId()%>"><%=filiale[i].getName()%></OPTION>
                <%} %>
            </SELECT>&nbsp;&nbsp;

            Sucursala:
            <span id="sucursalaDiv">
            <SELECT name="sucursala" class="gSAPSelect" onchange="fill_centru_select()" >
                  <option value="-1">Selectati:</option>
            </SELECT></span>&nbsp;&nbsp;

            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>
        <%
            final String value_data_start = data_start != 0 ? AppConstants.format(new Date(data_start)) : "";
            final String value_data_stop  = data_stop  != 0 ? AppConstants.format(new Date(data_stop))  : "";
        %>
        <span title="Selectati date egale pentru a trece prin toate echipamentele">Perioada:</span>
        <input type="text" id="data_start" name="data_start" size="10" value="<%=value_data_start%>" /> <a href="#" id="trigger_data_start" ><img src="<%=AppConstants.WEB_RESOURCE_PATH + "/images/cal.gif"%>" width="16" height="16" border="0" alt="Apasa pt. a selecta data. Selectati date egale pentru a trece prin toate echipamenele"></a>&nbsp;&nbsp;-&nbsp;&nbsp;
        <input type="text" id="data_stop" name="data_stop" size="10" value="<%=value_data_stop%>" /> <a href="#" id="trigger_data_stop" ><img src="<%=AppConstants.WEB_RESOURCE_PATH + "/images/cal.gif"%>" width="16" height="16" border="0" alt="Apasa pt. a selecta data. Selectati date egale pentru a trece prin toate echipamentele"></a>

        <script type="text/javascript">
              Calendar.setup(
                {
                  inputField  : "data_start",         // ID of the input field
                  ifFormat    : "%d.%m.%Y",    // the date format
                  button      : "trigger_data_start"       // ID of the button
                }
              );

              Calendar.setup(
                {
                  inputField  : "data_stop",         // ID of the input field
                  ifFormat    : "%d.%m.%Y",    // the date format
                  button      : "trigger_data_stop"       // ID of the button
                }
              );


            function reloadPage(){
                window.location = "<%=AppConstants.APP_NAME%>?action=raport-echipamente";
            }
        </script>
    </td>
    <td width="1%">
        <input type="submit" value="Submit" />
    </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</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>
  <%
      final long oneDay = 24 * 60 * 60 * 1000;
      data_stop += oneDay;
      final RaportRow_01[] rows;
      if (sucursalaRaport.getId() != -1) {
          rows = EchipamentAccessDB.get_ans_raport_by_sucursala(sucursalaRaport.getId(), data_start, data_stop, append_to);
      } else {
          rows = EchipamentAccessDB.get_ans_raport_by_filiala(filialaRaport.getId(), data_start, data_stop, append_to);
      }
      if (null != rows) 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 ? String.valueOf(nom.getCod()) : "&nbsp")%></TD>
        <TD><%=rows[i].ansamblu.getDescriere()%></TD>
        <TD align="right"><%=rows[i].t_nr_incidente%></TD>
        <TD align="right"><%if("Totale".equals( rows[i].ansamblu.getDescriere())){%>&nbsp;<%}else{%> <%=rows[i].t_durata_indisp/60%>h<%=rows[i].t_durata_indisp%60%>m <%}%></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;" >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>
   <%
       final RaportByCauzeRow[] rows1;
       final 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);
       }

       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">
            <% 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()%></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%>m <%}%></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()%></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%>m</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>