<%@ page import="ro.emid.common.constants.NomConstants" %>
<%@ page import="ro.emid.common.utils.ObjectUtils" %>
<%@ page import="ro.emid.common.constants.AppConstants" %>
<%@ page import="ro.emid.incidente.obj.*" %>
<%@ page import="java.sql.Date" %>
<%@ page import="ro.emid.common.db.framework.DatabaseException" %>
<%@ page import="ro.emid.incidente.db.*" %>
<%@ page import="ro.emid.common.types.*" %>
<%@ page import="ro.emid.common.db.*" %>
<%@ page import="ro.emid.common.constants.IncidentConstants" %>

<%!
    private String getNomText(final long id) {
        Nomenclator nom = null;
        try {
            if( id > 0) nom = NomAccessDB.getNom(id);
        } catch (DatabaseException e) {
        }

        String result = "-";
        if (nom != null) {
            result = nom.getCod() + " - " + nom.getDescriere();
        }
        return result;
    }

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

    private String getText(final String str){
        String result = (str != null && !"null".equalsIgnoreCase(str.trim())) ? str : "-";

        int i = 0;
        while( (i = result.indexOf('\n', i)) >=0){
            result = result.substring(0, i) + "<br />" + result.substring(i+1);
        }
        return result;
    }
%>


<style type="text/css">
    .fl {
        /* field Label*/
        font-weight: bolder;
    }

    .fv {
        /* field Value */
        /*font-weight: bolder;*/
    }

    span.fv{
        white-space:wrap !important;

    }

</style>

<%
        final long id = ObjectUtils.getLongValue(request.getParameter("id"), -1);
        final Incident bean = IncidentAccessDB.getIncident( id);

        Category filialaBean = CategoryAccessDB.getById(bean.getFilialaId());
        if (filialaBean == null) filialaBean = new Category();

        Category sucursalaBean = CategoryAccessDB.getById(bean.getSucursalaId());
        if (sucursalaBean == null) sucursalaBean = new Category();

        Category centruBean = CategoryAccessDB.getById(bean.getCentruId());
        if (centruBean == null) centruBean = new Category();

        Category formatiaDeLucruBean = CategoryAccessDB.getById( bean.getFormatiaDeLucruId());
        if( formatiaDeLucruBean == null) formatiaDeLucruBean = new Category();
%>

<TABLE border=1 cellPadding=2 cellSpacing=1 width="100%" class="gSAPTHEAD">
<TR>
    <TH class="gsapthead" colSpan=2 height=25 noWrap>
        <span style="float:right;">v. <%=ObjectUtils.getStringValue(bean.getVersion())%></span>
        Fisa de Incident (vizualizare)
    </TH>
</TR>
<TR>
    <TD width="25%">
        <b>1.</b> <span class="fl">Numar curent incident:</span>
    </TD>

    <TD width="75%">
        <span class="fv"><%=bean.getNumarIncident()%></span>
    </TD>
</TR>

<TR>
    <TD valign=top>
        <b>2.</b> <span class="fl">Localizare incident:</span>
        <br>
        <% final String nrComanda = EvenimentAccessDB.getNumarComandaByResultIdAndResultType( bean.getId(), Eveniment.STATUS_TRECUT_INCIDENT);%>
        <b>Nr. Comanda:</b> <%=getText( nrComanda)%>
    </TD>
    <TD>
        <TABLE class="gsapthead">

            <TR class="gSAPGroup">
                <TD>
                    <b>2.1</b> <span class="fl">Cod filiala:</span>
                </TD>
                <TD>
                    <span class="fv"><%=filialaBean.getName()%></span>
                </TD>
            </TR>

            <TR class="gSAPGroup">
                <TD>
                    <b>2.2</b> <span class="fl">Cod sucursala:</span>
                </TD>
                <TD>
                    <span class="fv"><%=sucursalaBean.getCodSap()%> - <%=sucursalaBean.getName()%></span>
                </TD>
            </TR>

            <TR class="gSAPGroup">

                <TD>
                    <b>2.3</b> <span class="fl">Cod sectie/C.E.:</span>
                </TD>
                <TD valign="MIDDLE" width="100%">
                    <span class="fv"><%=centruBean.getCodSap()%> - <%=centruBean.getName()%></span>
                    &nbsp;&nbsp;<b>2.3.1</b>&nbsp; <span class="fl"><%=sucursala.isCuSectiiSiCentre() || centruExploatare.isCuSectiiSiCentre() ? "Centru exploatare" : "Formatia de lucru"%>:</span>
                    <span class="fv"><%=getText( formatiaDeLucruBean.getName())%></span>
                </TD>
            </TR>

            <TR class="gSAPGroup">
                <TD>
                    <b>2.4</b> <span class="fl">Ansamblu functional:</span>
                </TD>
                <TD>
                    <span class="fv">  <%=getNomText( bean.getAnsambluId())%></span>
                    &nbsp;<br> <b>2.4.1</b> <span class="fl">Explicatii:</span> <span class="fv"> <%=getText( bean.getExplicatii())%></span>
                </TD>
            </TR>

            <TR class="gSAPGroup" valign="top">
                <TD>
                    <b>2.5</b> <span class="fl">Statie:</span>
                </TD>
                <TD>
                    <span class="fv"><%=AppConstants.format(bean.getStatieDen(), "-")%></span>
                    &nbsp;&nbsp;&nbsp;
                    <b>2.5.1</b> <span class="fl">Bobina:</span>
                    <span class="fv"><%=AppConstants.format(bean.getBobinaDen(), "-")%></span>
                </TD>
            </TR>

            <TR class="gSAPGroup" valign="top">
                <TD>
                    <b>2.6</b> <span class="fl">Celula:</span>
                </TD>
                <TD>
                    <span class="fv"><%=AppConstants.format(bean.getCelulaDen(), "-")%></span>
                    &nbsp;&nbsp;&nbsp;
                    <b>2.6.1</b> <span class="fl">Bara:</span>
                    <span class="fv"><%=AppConstants.format(bean.getBaraDen(), "-")%></span>
                </TD>
            </TR>

            <TR class="gSAPGroup" valign="top">
                <TD>
                    <b>2.7</b> <span class="fl">Linie:</span>
                </TD>
                <TD>
                    <span class="fv"><%=AppConstants.format(bean.getLinieDen(), "-")%></span>
                    &nbsp;&nbsp;&nbsp;
                    <b>2.7.1</b> <span class="fl">Derivatie:</span>
                    <span class="fv"><%=AppConstants.format(bean.getDerivatieDen(), "-")%></span>
                </TD>
            </TR>
            <TR class="gSAPGroup">
                <TD>
                    <b>2.8</b> <span class="fl">PA/PT:</span>
                </TD>
                <TD>
                    <span class="fv"><%=AppConstants.format(bean.getPaptDen())%></span>
                </TD>
            </TR>
            <TR class="gSAPGroup">
                <TD>
                    <b>2.9</b> <span class="fl">Trafo:</span>
                </TD>

                <TD>
                    <%--<span class="fv"><%=getNomSAPText( bean.getTrafoId(), NomConstants.NOM_TRAFO)%></span>--%>
                    <span class="fv"><%=AppConstants.format(bean.getTrafoDen())%></span>
                </TD>
            </TR>

            <TR class="gSAPGroup">
                <TD>
                    <b>2.10</b> <span class="fl">Parametri nominali:</span>
                </TD>
                <TD>
                    <b>2.10.1</b> <span class="fl">Putere:</span> <%=bean.getPutere()%>
                    <%=(bean.getTipPutere() == 1 ? "MVA" : bean.getTipPutere() == 2 ? "MVAr" :
                            bean.getTipPutere() == 3 ? "kVA" :
                                    bean.getTipPutere() == 4 ? "kVAr" : "")%>
                    &nbsp;&nbsp;&nbsp;<b>2.10.2</b> <span class="fl">Tensiune:</span> <%=bean.getTensiune() + "/" + bean.getTensiune2() + '/' + bean.getTensiune3()%> KV
                </TD>
            </TR>

            <TR class="gSAPGroup">
                <TD>
                    <b>2.11</b> <span class="fl">Mod de tratare neutru:</span>
                </TD>
                <TD>
                    <span class="fv"><%=getNomText( bean.getModTratareNeutruId())%></span>
                </TD>
            </TR>


        </TABLE>
    </TD>
</TR>

<TR>
    <TD>
        <b>3.</b> <span class="fl">Forma de manifestare:</span>
    </TD>

    <TD>
      <span class="fv">
	   <%=getNomText( bean.getFormaManifestareId()) %>
      </span>
    </TD>

</TR>

<TR>
    <TD>
        <b>4.</b> <span class="fl">Prilej:</span>
    </TD>
    <TD>
        <span class="fv"><%=getNomText( bean.getPrilejId())%></span>
    </TD>
</TR>

<TR>
    <TD>
        <b>5.</b> <span class="fl">Cauza:</span>
    </TD>

    <TD>
        <b>5.1</b> <span class="fl">Grupa:</span>
        <span class="fv"><%=getNomText( bean.getCauzaGrupaId())%></span>
        <BR><b>5.2</b> <span class="fl">Cauza:</span>
        <span class="fv"><%=getNomText( bean.getCauzaId())%></span>
    </TD>
</TR>

<TR>
    <TD valign=top>
        <b>6.</b> <span class="fl">Data producerii:</span>
    </TD>
    <TD valign="middle">
        <%=AppConstants.formatx(bean.getDataProducere())%>
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Ora: <%=AppConstants.formatOra(bean.getDataProducere())%>&nbsp;&nbsp;&nbsp;
        Minute: <%=AppConstants.formatMinute(bean.getDataProducere())%>
    </TD>
</TR>
<TR>
    <TD>
        <b>7.</b> <span class="fl">Clasificare incident:</span>
    </TD>
    <TD>
        <b>7.1</b>  <%=( bean.getTipIncident() == 1 ? "Izolat" :
            bean.getTipIncident() == 2 ? "Primar" :
                    bean.getTipIncident() == 3 ? "Asociat" : "-")%>
        &nbsp;&nbsp;&nbsp;
        <% if( bean.getTipIncident() == 3){%>
        <b>7.2</b> Numar incident primar: <%=bean.getNumarIncidentPrimar()%>
        <% }%>
    </TD>
</TR>

<TR>
    <TD>
        <b>8.</b> <span class="fl">Durata incident:</span>
    </TD>

    <td>
        Ore: <%=bean.getDurataIncident()/60%>&nbsp;
        Minute: <%=bean.getDurataIncident()%60%>
    </td>
</tr>
<tr>
    <TD>
        <b>9.</b> <span class="fl">Durata indisponibilitate:</span>
    </TD>

    <td>
        Ore:<%=bean.getDurataIndisponibilitate()/60%>&nbsp;
        Minute:<%=bean.getDurataIndisponibilitate()%60%>
    </td>
</TR>

<TR>
    <TD colspan=2>
        <b>10.</b> <span class="fl">Energia electrica nelivrata consumatorilor:</span>
        <span class="fv"> <%=bean.getEnergieNelivrata()%>  kWh</span>
    </TD>
</TR>

<TR>
    <TD colspan=2>
        <b>11.</b> <span class="fl">Schema de principiu a instalatiilor afectate: </span>
    <span class="fv">
    <% if (bean.getFileName() != null) { %>
        <%=bean.getFileName()%>
        <% } else { %>
        netras<br>
        <% } %>
    </span>
    </td>
<TR>
    <TD valign=top>
        <b>12.</b> <span class="fl">Desfasurare incident:</span>
    </TD>
    <TD>
        <span class="fv"><%=getText( bean.getDesfasurareIncident())%></span>
    </TD>
</TR>

<TR>
    <TD valign=top>
        <b>12.1</b> <span class="fl">Consumatori afectati:</span>
    </TD>
    <TD>Nr. consumatori afectati: <%=bean.getNrConsumatoriAfectati()%><br>
        Denumire / Ora producerii / Ora realimentarii / Modul de realimentare / Putere intrerupta / Energie nelivrata<BR>
        <span class="fv"><%=getText( bean.getConsumatoriAfectati())%></span>
    </TD>

</TR>

<TR>
    <TD >
        <b>12.2</b> <span class="fl">Nr. utilizatori:</span>
    </TD>
    <td>
        <%--<table width="100%" cellspacing="1" class="gSAPTHEAD">--%>
        <%--<tr class="gSAPGroup" align="center">--%>
        <%--<td></td>--%>
        <%--<td>Inalta(IT)</td>--%>
        <%--<td>Medie(MT)</td>--%>
        <%--<td>Joasa(JT)</td>--%>
        <%--</tr>--%>
        <%--<tr >--%>
        <%--<td><b>Mari consumatori</b></td>--%>
        <%--<td align="center"><%= bean.getIT_mariConsumatori()%></td>--%>
        <%--<td align="center"><%= bean.getMT_mariConsumatori()%></td>--%>
        <%--<td align="center"><%= bean.getJT_mariConsumatori()%></td>--%>
        <%--</tr>--%>
        <%--<tr >--%>
        <%--<td><b>Mici consumatori</b></td>--%>
        <%--<td align="center"><%= bean.getIT_miciConsumatori()%></td>--%>
        <%--<td align="center"><%= bean.getMT_miciConsumatori()%></td>--%>
        <%--<td align="center"><%= bean.getJT_miciConsumatori()%></td>--%>
        <%--</tr>--%>
        <%--<tr >--%>
        <%--<td><b>Casnici consumatori</b></td>--%>
        <%--<td></td>--%>
        <%--<td align="center"><%= bean.getMT_casniciConsumatori()%></td>--%>
        <%--<td align="center"><%= bean.getJT_casniciConsumatori()%></td>--%>
        <%--</tr>--%>
        <%--</table>--%>
        <table width="100%" cellspacing="1" class="gSAPTHEAD">
            <tr class="gSAPGroup" align="center">
                <td>Inalta(IT)</td>
                <td>Medie(MT)</td>
                <td>Joasa(JT)</td>
            </tr>
            <tr >
                <td align="center"><%= bean.getUtilizatori_IT()%></td>
                <td align="center"><%= bean.getUtilizatori_MT()%></td>
                <td align="center"><%= bean.getUtilizatori_JT()%></td>
            </tr>
        </table>
    </td>
</TR>


<TR>
    <TD>
        <b>12.3</b> <span class="fl">Fenomene meteo:</span>
    </TD>
    <TD valign="MIDDLE" width="100%" >
        <span class="fv">    <%=getNomText(bean.getFenomenMeteoId()) %></span>
    </TD>
</TR>

<TR>
    <TD>
        <b>12.4</b> <span class="fl">Incident produs in zona de protectie a descarcatorului:</span>
    </TD>

    <TD>
        <span class="fv"><%=(bean.isF1()) ? "Da" : "Nu"%></span>
    </TD>
</TR>

<TR>
    <TD>
        <b>12.5</b> <span class="fl">Incident cu deteriorari echip.:</span>
    </td><td>    <span><%=(bean.isF2()) ? "Da" : "Nu"%></span></TD>
</TR>

<%if (bean.getId() > 0) {%>
<TR>
    <TD>
        <span class="fl">Echipamente afectate:</span>
    </TD>
    <TD>
        <TABLE border=1 cellPadding=2 cellSpacing=1 width="100%" class="gSAPTHEAD">
            <% final Echipament[] echipamente = EchipamentAccessDB.getForIncident( bean.getId());
                if (null == echipamente) {%>
            <TR><TD>Fara echipamente afectate.</TD></TR>
            <%}else for(int i=0; i<echipamente.length; i++){
                final String td_color = (i%2 == 0 ? "BACKGROUND-COLOR: #CACADF" : "BACKGROUND-COLOR: #D9D9E8");%>
            <TR>
                <TD style="<%=td_color%>" class="row1" width="100%">
                    <A target="new" class="link" href="<%=AppConstants.APP_NAME%>?action=printEchipament&id=<%=echipamente[i].getId()%>">&nbsp;
                        F.E. nr. <%=(echipamente[i].getNumarEchipament() != -1) ? String.valueOf(echipamente[i].getNumarEchipament()) : "nesetat"%>
                    </A>
                </TD>
            </TR>
            <%}%>
        </TABLE>
    </TD>
</TR>
<%}%>

<TR>
    <TD ><b>12.6</b> <span class="fl">Incident cu perturbatii sistem:</span></td>
    <td><span class="fv"><%=(bean.isF4()) ? "Da" : "Nu"%></span></TD>
</TR>

<TR>
    <TD valign=top>
        <b>13.</b> <span class="fl">Masuri pentru limitare/lichidare:</span>
    </TD>
    <TD>
        <span class="fv"><%=getText( bean.getMasuriLimitareLichidare())%></span>
    </TD>
</TR>

<TR>
    <TD valign=top>
        <b>14.</b> <span class="fl">Masuri pentru prevenire incidente similare:</span>
    </TD>
    <TD>
        <span class="fv"><%=getText( bean.getMasuriPrev())%></span>
    </TD>
</TR>

<TR>
    <TD>
        <b>15.</b> <span class="fl">Cheltuieli (RON):</span>
    </TD>
    <TD>
        <table width="80%" cellspacing="1" class="gSAPTHEAD">
            <tr class="gSAPGroup">
                <td>
                    <span <%if(bean.getCheltMentinere() != bean.getSapCheltMentinere()){%> style="color: blue;" <%}else{%> style="color: green;"<%}%>  style=" text-align: right" ><%=AppConstants.format2(bean.getCheltMentinere())%></span> - Mentenanta - Terti<BR>
                    <span <%if(bean.getCheltDaune() != bean.getSapCheltDaune()){%> style="color: blue;" <%}else{%> style="color: green;"<%}%>   style=" text-align: right" ><%=AppConstants.format2(bean.getCheltDaune())%></span> - Daune<BR>
                    <span <%if(bean.getCheltExploatare() != bean.getSapCheltExploatare()){%> style="color: blue;" <%}else{%> style="color: green;"<%}%> style=" text-align: right" ><%=AppConstants.format2(bean.getCheltExploatare())%></span> - Exploatare / Forte proprii<BR>
                    <span style=" text-align: right"> <%=AppConstants.format2(bean.getCheltMentinere() + bean.getCheltDaune() + bean.getCheltExploatare())%></span> - Total
                </td>
                <!--<td align="right">-->
                <input type="hidden" name="<%=IncidentConstants.SAP_CHELT_MENT%>" value="<%=bean.getSapCheltMentinere()%>">
                <input type="hidden" name="<%=IncidentConstants.SAP_CHELT_DAUNE%>" value="<%=bean.getSapCheltDaune()%>">
                <input type="hidden" name="<%=IncidentConstants.SAP_CHELT_EXPLOATARE%>" value="<%=bean.getSapCheltExploatare()%>">
                <%--<%=AppConstants.format2( bean.getSapCheltMentinere())%><BR>--%>
                <%--<%=AppConstants.format2( bean.getSapCheltDaune())%><BR>--%>
                <%--<%=AppConstants.format2( bean.getSapCheltExploatare())%><BR>--%>
                <%--<%=AppConstants.format2( bean.getSapCheltMentinere() + bean.getSapCheltDaune() + bean.getSapCheltExploatare())%>--%>
                <!--</td>-->
            </tr>
        </table>

    </TD>
</TR>

<TR>
    <TD valign=top>

        <b>16.</b> <span class="fl">Propuneri:</span>
    </TD>
    <TD>
        <span class="fv" ><%=getText( bean.getPropuneri())%></span>
    </TD>
</TR>


<TR>
    <TD colspan=2>
        <b>17.</b> <span class="fl">Incidentul a fost analizat de:</span>
        <span class="fv"> <%=getText( bean.getAnalizatDe())%></span>
        &nbsp;&nbsp;&nbsp;&nbsp;<span class="fl">Conducatorul sub-unitatii de exploatare:</span>
        <span class="fv"> <%=getText( bean.getConducatorUnitate())%></span>
    </TD>
</TR>

<TR>
    <TD colspan="2">
        <b>18.</b> <span class="fl">Avizul comisiei tehnice:</span>
        <span class="fv"> <%=getText( bean.getAvizComisie())%></span>
    </TD>
</TR>

<TR>
    <TD colspan=2>
        <span class="fl">Conducatorul tehnic:</span>
        <span class="fv"><%=getText( bean.getConducatorTehnic())%></span>
        &nbsp;&nbsp;&nbsp;&nbsp;<span class="fl">Inspector incidente:</span>
        <span class="fv"><%=getText( bean.getInspectorIncidente())%></span>
    </TD>
</TR>
</TABLE>
