<%@ page import="ro.emid.common.db.*" %>
<%@ page import="ro.emid.common.db.framework.DatabaseException" %>
<%@ page import="ro.emid.common.db.framework.Filter" %>
<%@ page import="ro.emid.common.db.framework.IFilter" %>
<%@ page import="ro.emid.common.types.Category" %>
<%@ page import="ro.emid.common.types.DataObj" %>
<%@ page import="ro.emid.common.types.Incident" %>
<%@ page import="ro.emid.common.types.Nomenclator" %>
<%@ page import="ro.emid.common.utils.ObjectUtils" %>
<%@ page import="ro.emid.common.utils.pagination.PaginationController" %>
<%@ page import="ro.emid.common.utils.pagination.PaginationData" %>
<%@ page import="java.sql.SQLException" %>
<%@ page import="java.text.MessageFormat" %>
<%@ page import="java.util.HashMap" %>
<%@ page import="java.util.Map" %>


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



<%
    final int idInstalatie = ObjectUtils.getIntValue( request.getParameter("idInstalatie"), -1);

    final DataObj instalatie = DataDB.getById( idInstalatie, true);
    if( instalatie == null){%>
        <span style="color: red">Nu exista intalatie cu id : <%=idInstalatie%></span>
    <% return;}


    final IFilter filtru = new Filter();
    filtru.addStringCriteria(" AND LINIE_ID = "+ idInstalatie);


    final PaginationData pd = PaginationController.getPaginationData(request, 50);

    final Incident[] incidente = IncidentAccessDB.getAllIncidente( filtru, "FILIALA_ID ASC, SUCURSALA_ID ASC, SECTIE_ID ASC, NUMAR_INCIDENT ", "ASC", pd.getStartIndex(), pd.getQueryEndIndex());

    final Integer tc = (Integer) AccessDB.executeSQL(new AccessDB( "Get Incidente count") {
        protected Object doWork() throws SQLException, DatabaseException {
            Integer obj = new Integer( 0);
            createStatementFromString(MessageFormat.format( "SELECT count(*) CNT FROM incidente i WHERE 0=0 {0} ", new String[]{ filtru.getSqlFilterCondition() }));
            filtru.bindStatement( this.statement);
            getResultSetFromQuery();
            if (resultSet.next()) {
                obj = new Integer ( resultSet.getInt( "CNT"));
            }

            return obj;
        }
    });

    pd.setResultRowCount( incidente != null ? incidente.length : 0 );
    pd.setTotalRowsCount( tc != null ? tc.intValue() : 0 );

%>


<TABLE border=0 cellPading=0 cellSpacing=0 width="100%">
    <TR>
        <TD>
            <%@ include file="../header-localizare.jsp"%> <span class="slash">/</span> Lista incidente pentru <%=instalatie.getNume()%>
        </TD>
        <TD align="RIGHT">
        </TD>
    </TR>
</TABLE>



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

<form method="post" action="">
    <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. Fisa <br/> Inc.:&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="Dublu-click 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="5" align="right">
                <%=PaginationController.printPaginationData(pd)%>
            </td>
        </tr>
    </TABLE>
</form>

<%!
    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) {
        if (id < 0 ){ return "";}
        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>

