<%@ page import="ro.emid.common.db.framework.DatabaseException" %>
<%@ page import="ro.emid.common.utils.ObjectUtils" %>
<%@ page import="ro.emid.common.db.CategoryAccessDB" %>
<%@ page import="ro.emid.common.db.DataDB" %>
<%@ page import="ro.emid.common.db.EchipamentAccessDB" %>
<%@ page import="ro.emid.common.types.DataObj" %>
<%@ page import="ro.emid.common.types.Echipament" %>
<%@ page import="ro.emid.common.types.Incident" %>
<%@ page import="ro.emid.common.utils.StringUtils" %>
<%@ page import="ro.emid.common.db.NomAccessDB" %>
<%@ page import="ro.emid.common.constants.NomConstants" %>
<%@ page import="ro.emid.common.types.Nomenclator" %>
<%@ page import="java.util.HashMap" %>
<%@ page import="java.util.Map" %>
<%@ page import="ro.emid.common.types.Category" %>


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


<%
    //preluare date filtru ...
//    final String tip = StringUtils.getNullSafe(request.getParameter("tip"), "STATIE_ID");
    final String tip = StringUtils.getNullSafe(request.getParameter("tip"), "STATIE_COD");
    final int idInstalatie = ObjectUtils.getIntValue(request.getParameter("idInstalatie"), -1);
    final String codInstalatie = ObjectUtils.getStringValue(request.getParameter("codInstalatie"));

    final StringBuffer condSql = new StringBuffer( );
    if(idInstalatie == -1){
        condSql.append(" AND ").append(tip).append(" = ").append('\'').append(codInstalatie).append('\'');
    }else{
        condSql.append(" AND ").append( tip).append( " = ").append( idInstalatie );
    }

    final StringBuffer parentFilter = (StringBuffer)session.getAttribute( "raportTop10_FI_FE_FilterEchipamente");

    if( parentFilter != null){
        condSql.append( parentFilter);
    }

    final Echipament[] echipamente = EchipamentAccessDB.getAllEchipamente(condSql.toString(), "FILIALA_ID ASC, SUCURSALA_ID ASC, SECTIE_ID", "ASC", 0, 500);
%>


<style type="text/css">
    .underlined{
        /*border-top-style: solid;*/
        /*border-top-width: 1px;*/
        /*border-top-color: gray;*/
    }
</style>

<TABLE id="incidente" border=0 cellPadding=2 cellSpacing=1 width="96%" class="gSAPTHEAD" style="BACKGROUND-COLOR: white; margin: 10px">
    <col align="right" style="padding-right:5px">
    <tr noSelect="true">
        <TH class="gsapthead" height=25 width="3%" noWrap>&nbsp;Nr. Echipament:&nbsp;</TH>
        <TH class="gsapthead" height=25 width="3%" noWrap>&nbsp;Nr. Echipamente <br/>identice deteriorate:&nbsp;</TH>
        <TH class="gsapthead" height=25 noWrap align="left">&nbsp;Localizare:&nbsp;</TH>
        <TH class="gsapthead" height=25 noWrap align="left">&nbsp;Ansamblu:&nbsp;</TH>
        <TH class="gsapthead" height=25 noWrap align="left">&nbsp;Instalatii afectate:&nbsp;</TH>
        <TH class="gsapthead" height=25 width="1%" noWrap align="left">&nbsp;Op.:&nbsp;</TH>
    </tr>

    <%  long total = 0; %>
    <% if( echipamente.length == 0){%>
        <tr>
            <td>No data ...</td>
        </tr>
    <% }else {%>
    <%  long centruCurentId = -1;%>
    <%  final Map cacheAnsamblu = new HashMap(); %>

    <%  for (int i = 0; i < echipamente.length; i++) {
        final Echipament echipament = echipamente[i];

        total += echipament.getNumarEchipIdenticeDet();

        Category f = CategoryAccessDB.getById(echipament.getFilialaId());
        if( f == null) f =  new Category();

        Category s = CategoryAccessDB.getById( echipament.getSucursalaId());
        if( s == null) s = new Category();

        Category c = CategoryAccessDB.getById( echipament.getCentruId());
        if( c == null) c = new Category();

        Nomenclator ansamblu = (Nomenclator) cacheAnsamblu.get(String.valueOf(echipament.getAnsambluId()));
        if( ansamblu == null){
            ansamblu = NomAccessDB.getNom(echipament.getAnsambluId());
            if( ansamblu != null){
                cacheAnsamblu.put(String.valueOf(echipament.getAnsambluId()), ansamblu);
            }
        }

        final String denAnsamblu = getDenAnsamble(echipament);
        
        String className = "";
        if( centruCurentId != c.getId()){
            centruCurentId = c.getId();
            className = "underlined";
        }
    %>

        <tr ondblclick="printEchipament( <%=echipament.getId()%>)" title="Dubluclick pentru a vizualiza Fisa de Echipament">
            <td align="" class="<%=className%>">
                <%=echipament.getNumarFI_FE()%>
            </td>
            <td align="center">
                <%=echipament.getNumarEchipIdenticeDet()%>
            </td>
            <td class="<%=className%>">
                <%=f.getName()%> - <%=s.getName()%> - <%=c.getName()%>
            </td>
            <td>
                <%=ansamblu != null ? ansamblu.getDescriere() : "-" %>
            </td>
            <td>
                    <%=denAnsamblu%>
            </td>
            <td>
                <a class="link" href="javascript: void( null)" onclick="printEchipament( <%=echipament.getId()%>); return false;" title="Click pentru a vizualiza Fisa de Echipament">Vizualizare</a>
            </td>
        </tr>
        <%}%>
    <%}%>
    <tr style="font-weight:bold;" noSelect="true">
        <td class="gsapthead" align="right">&nbsp;</td>
        <td class="gsapthead" align="center"><%=total%></td>
        <td class="gsapthead" align="center"><%=echipamente.length%></td>
        <td class="gsapthead">&nbsp;</td>
        <td class="gsapthead">&nbsp;</td>
    </tr>
</TABLE>

<%!
    private String getDenAnsamble(final Echipament incident){
        final StringBuffer temp = new StringBuffer();
//        final String statie = getNomSAPText(incident.getStatieId(), NomConstants.NOM_STATII);
        final String statie = AppConstants.format(incident.getStatieDen());
//        final String bobina = getNomSAPText(incident.getBobinaId(), NomConstants.NOM_BOBINE);
        final String bobina = AppConstants.format(incident.getBobinaDen());
//        final String celula = getNomSAPText(incident.getCelulaId(), NomConstants.NOM_CELULE);
        final String celula = AppConstants.format(incident.getCelulaDen());
//        final String bara = getNomSAPText(incident.getBaraId(), NomConstants.NOM_BARE);
        final String bara = AppConstants.format(incident.getBaraDen());
//        final String linie = getNomSAPText(incident.getLinieId(), NomConstants.NOM_LINII);
        final String linie = AppConstants.format(incident.getLinieDen());
//        final String derivatie = getNomSAPText(incident.getDerivatieId(), NomConstants.NOM_DERIVATIE);
        final String derivatie = AppConstants.format(incident.getDerivatieDen());
//        final String paPt = getNomSAPText(incident.getPA_PT_Id(), NomConstants.NOM_PA_PT);
        final String paPt = AppConstants.format(incident.getPaptDen());
//        final String trafo = getNomSAPText(incident.getTrafoId(), NomConstants.NOM_TRAFO);
        final String trafo = AppConstants.format(incident.getTrafoDen());

        if(statie != null && statie.length() >= 2){
            temp.append("5.5 Statie : ").append(statie);
            temp.append("<br>");
        }
        if(bobina != null && bobina.length() >= 2){
            temp.append("5.5.1 Bobina : ").append(bobina);
            temp.append("<br>");
        }
        if(celula != null && celula.length() >= 2){
            temp.append("5.6 Celula : ").append(celula);
            temp.append("<br>");
        }
        if(bara != null && bara.length() >= 2){
            temp.append("5.6.1 Bara : ").append(bara);
            temp.append("<br>");
        }
        if(linie != null && linie.length() >= 2){
            temp.append("5.7 Linie : ").append(linie);
            temp.append("<br>");
        }
        if(derivatie != null && derivatie.length() >= 2){
            temp.append("5.7.1 Derivatie : ").append(derivatie);
            temp.append("<br>");
        }
        if(paPt != null && paPt.length() >= 2){
            temp.append("5.8 PA/PT : ").append(paPt);
            temp.append("<br>");
        }
        if(trafo != null && trafo.length() >= 2){
            temp.append("5.9 Trafo : ").append(trafo);
            temp.append("<br>");
        }

        return temp.toString();
    }

    //returns den from nomenclator
    private String getNomSAPText(final long id, final int tip) {
        DataObj data = null;

        try {
            if( id > 0) data = DataDB.getById(id, DataDB.isLocFunctional(tip));
        } catch (DatabaseException e) {
        }

        String result = "-";
        if (data != null) {
            result = data.getNume();
        }
        return result;
    }
%>

<script type="text/javascript">
    var tableIncidente = new Table('incidente', "tableIncidente");
</script>
</span>