<%@ page import="ro.emid.common.types.DataObj" %>
<%@ page import="ro.emid.common.db.DataDB" %>
<%@ page import="ro.emid.common.utils.ObjectUtils" %>
<%@ page import="ro.emid.common.types.LFInfo" %>
<%@ page import="ro.emid.common.db.LFInfoAccessDB" %>
<%@ page import="ro.emid.incidente.obj.binder.IncidentBinder" %>
<%@ page import="ro.emid.common.types.Incident" %>
<%@ page import="java.util.Calendar" %>
<%@ page import="ro.emid.common.constants.*" %>

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

<%
try{
    final IncidentBinder binder = (IncidentBinder) request.getSession().getAttribute(IncidentConstants.BEAN) ;
    if( binder == null) return;

    final Incident incident = (Incident) binder.getBean();

    int anProducereIncident = 0;
    if( incident.getDataProducere() != null){
        final Calendar c = Calendar.getInstance();
        c.setTime( incident.getDataProducere());
        anProducereIncident = c.get(Calendar.YEAR);
    }

    final String codLF = ObjectUtils.getStringValue(request.getParameter("codLF"));
    DataObj lf = DataDB.getLFByCodSAP( codLF);
    if( lf == null && filiala.isMunteniaSud()){
        //Posibil sa fie derivatie. La MS derivatiile au coduri EMID
        lf = DataDB.getEchipamentByNr( codLF, filiala.getId());
    }

    LFInfo lfInfo = null;
    if( lf != null){
        if( incident.getId() > 0 && anProducereIncident > 0){
            lfInfo = LFInfoAccessDB.getEntryByCodSapLF(lf.getCodCE(), lf.getCod(), anProducereIncident);
        }

        if( lfInfo == null){
            lfInfo = LFInfoAccessDB.getLastEntryByCodSapLF( lf.getCodCE(), lf.getCod());
        }
    }
%>
<respmesg>
    <%if( lf == null){  %>
        Nu s-a gasit locul functional avand codul <%=codLF%>
    <%}else if( lfInfo == null){%>
        Lipsa date ...<br>
        Numarul de consumatori poate fi actualizat in Modulul Administrare -> Nomenclatoare SAP -> (link) Alte informatii
    <%}else{%>
        <table width="100%">
            <tr>
                <td>
                    <%if ( lfInfo.getAn() != anProducereIncident){%>
                        <span style="color: red">
                            Atentie! Datele sunt valabile pentru anul <%=lfInfo.getAn()%>, anul producerii incidentului este <%=anProducereIncident%>. In Rapoarte sunt luate in calcul datele din anul producerii incidentului.
                        </span>
                    <%}%>
                    <fieldset>
                        <legend></legend>
                        <table width="100%">
                            <col>
                            <col align="right">
                            <tr><td>Localitate:</td><td><%=lfInfo.getLocalitate()%></td></tr>
                            <tr><td>Mediul:</td><td><%=lfInfo.getMediu() == LFInfoConstants.MEDIU_URBAN ? "Urban" : ( lfInfo.getMediu() == LFInfoConstants.MEDIU_RURAL ? "Rural" : "-")%></td></tr>
                            <tr><td>Snom:</td><td><%=lfInfo.getSNom()%></td></tr>
                            <tr><td>Pnom=<%=LFInfoConstants.CONSTANT_PNON%> * Snom:</td><td><%=AppConstants.format03(LFInfoConstants.CONSTANT_PNON * lfInfo.getSNom())%></td></tr>
                            <tr><td>Grad Incarcare (%):</td><td><%=ro.emid.common.constants.GlobalAppConstants.printGradIncarcare(lfInfo.getGradIncarcare())%></td></tr>
                            <tr><td>An PIF:</td><td><%=lfInfo.getAnPif()%></td></tr>
                            <tr><td>Valabilitate:</td><td><%=AppConstants.format(lfInfo.getAn())%></td></tr>
                            <tr><td>Ultima actualizare:</td><td><%=AppConstants.format(lfInfo.getDataSave())%></td></tr>
                        </table>
                    </fieldset>

                    <%
                        final long total = lfInfo.getNumarConsumatoriCasnici() + lfInfo.getNumarConsumatoriMici() + lfInfo.getNumarConsumatoriMari();
                    %>

                    <fieldset>
                        <legend>Numar Consumatori : <span><%=total%></span></legend>
                        <table width="100%">
                            <col>
                            <col align="right">
                            <tr><td>Casnici:</td><td><%=lfInfo.getNumarConsumatoriCasnici()%></td></tr>
                            <tr><td>Mici:</td><td><%=lfInfo.getNumarConsumatoriMici()%></td></tr>
                            <tr><td>Mari:</td><td><%=lfInfo.getNumarConsumatoriMari()%></td></tr>
                        </table>
                    </fieldset>

                    <fieldset>
                        <legend>Nr. consumatori / Nivel tensiune</legend>
                        <table width="100%">
                            <col>
                            <col align="right">
                            <tr><td>JT:</td><td><%=lfInfo.getNumarConsumatoriJT()%></td></tr>
                            <tr><td>MT:</td><td><%=lfInfo.getNumarConsumatoriMT()%></td></tr>
                            <tr><td>IT:</td><td><%=lfInfo.getNumarConsumatoriIT()%></td></tr>
                        </table>
                    </fieldset>
                </td>
            </tr>

        </table>
    <%}%>
</respmesg>

<%}catch( Exception t){%>
    <%=t.getMessage()%>
<%}%>
