<%@ 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.IncidentAccessDB" %>
<%@ page import="ro.emid.common.types.DataObj" %>
<%@ page import="ro.emid.common.types.Incident" %>
<%@ 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 int tipInstalatie = ObjectUtils.getIntValue( request.getParameter("tipInstalatie"), -1);
    final String codInstalatie = ObjectUtils.getStringValue(request.getParameter("codInstalatie"));

    final StringBuffer condSql = new StringBuffer( );

    switch ( tipInstalatie){
        case 1: condSql.append(" AND STATIE_COD = ").append('\'').append(codInstalatie).append('\'').append(
                " AND isSelected( i.LINIE_ID) = 0 ").append(
                        " AND isSelected( i.DERIVATIE_ID) = 0" ).append(
                        " AND isSelected( i.BARA_ID) = 0" ).append(
                        " AND isSelected( i.BOBINA_ID) = 0" ).append(
                        " AND isSelected( i.CELULA_ID) = 0" ).append(
                        " AND isSelected( i.TRAFO_ID) = 0" ).append(
                        " AND isSelected( i.PAPT_ID) = 0 "
        ); break;
        case 2: condSql.append(" AND i.LINIE_COD = ").append('\'').append(codInstalatie).append('\'').append(" AND isSelected( i.DERIVATIE_ID) = 0"); break;
        case 3: condSql.append(" AND PAPT_COD = ").append('\'').append(codInstalatie).append('\''); break;
        case 4: condSql.append(" AND BOBINA_COD = ").append('\'').append(codInstalatie).append('\''); break;
        case 5: condSql.append(" AND CELULA_COD = ").append('\'').append(codInstalatie).append('\''); break;
        case 6: condSql.append(" AND BARA_COD = ").append('\'').append(codInstalatie).append('\''); break;
        case 7: condSql.append(" AND DERIVATIE_COD = ").append('\'').append(codInstalatie).append('\''); break;
        case 8: condSql.append(" AND TRAFO_COD = ").append('\'').append(codInstalatie).append('\''); break;
        default:
            //nothing
            break;
    }

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

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

    final Incident[] incidente = IncidentAccessDB.getAllIncidente( 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. Incident:&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>

    <% if( incidente.length == 0){%>
        <tr>
            <td>No data ...</td>
        </tr>
    <% }else {%>
    <%  long centruCurentId = -1;%>
    <%  final Map cacheAnsamblu = new HashMap(); %>
    <%  for (int i = 0; i < incidente.length; i++) {
        final Incident incident = incidente[i];

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

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

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

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

        final String denAnsamblu = getDenAnsamble(incident);

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

        <tr ondblclick="printIncident( <%=incident.getId()%>)" title="Dubluclick pentru a vizualiza Fisa de Incident">
            <td align="" class="<%=className%>">
                <%=incident.getNumarIncident()%>
            </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="printIncident( <%=incident.getId()%>); return false;" title="Click pentru a vizualiza Fisa de Incident">Vizualizare</a>
            </td>
        </tr>
        <%}%>
    <%}%>
    <tr style="font-weight:bold;" noSelect="true">
        <td class="gsapthead" colspan="3" align="right">&nbsp;Total:&nbsp;</td>
        <td class="gsapthead" align="center"><%=incidente.length%></td>
        <td class="gsapthead">&nbsp;</td>
    </tr>
</TABLE>

<%!
    private String getDenAnsamble(final Incident 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("2.5 Statie : ").append(statie);
            temp.append("<br>");
        }
        if(bobina != null && bobina.length() >= 2){
            temp.append("2.5.1 Bobina : ").append(bobina);
            temp.append("<br>");
        }
        if(celula != null && celula.length() >= 2){
            temp.append("2.6 Celula : ").append(celula);
            temp.append("<br>");
        }
        if(bara != null && bara.length() >= 2){
            temp.append("2.6.1 Bara : ").append(bara);
            temp.append("<br>");
        }
        if(linie != null && linie.length() >= 2){
            temp.append("2.7 Linie : ").append(linie);
            temp.append("<br>");
        }
        if(derivatie != null && derivatie.length() >= 2){
            temp.append("2.7.1 Derivatie : ").append(derivatie);
            temp.append("<br>");
        }
        if(paPt != null && paPt.length() >= 2){
            temp.append("2.8 PA/PT : ").append(paPt);
            temp.append("<br>");
        }
        if(trafo != null && trafo.length() >= 2){
            temp.append("2.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>