<%@ page import="ro.emid.common.constants.AppConstants" %>
<%@ page import="ro.emid.common.constants.LucrareProgramataConstants" %>
<%@ page import="ro.emid.common.db.CategoryAccessDB" %>
<%@ page import="ro.emid.common.db.NomAccessDB" %>
<%@ page import="ro.emid.common.db.framework.DatabaseException" %>
<%@ page import="ro.emid.common.types.*" %>
<%@ page import="ro.emid.common.utils.IntervalLucrare" %>
<%@ page import="ro.emid.common.utils.Mediu" %>
<%@ page import="ro.emid.common.utils.ObjectUtils" %>
<%@ page import="ro.emid.lpod.db.InstalatieDB" %>
<%@ page import="ro.emid.lpod.obj.CategoriiInstalatii" %>
<%@ page import="ro.emid.lpod.obj.TipEchipaDeLucru" %>
<%@ page import="java.util.List" %>


<%
    final LucrareProgramata bean = (LucrareProgramata) request.getAttribute(LucrareProgramataConstants.LUCRARE);
    if( bean == null){ %> The bean is null !!!  <%
        return;
    }

    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();

%>


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

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



<TABLE border=1 cellPadding=2 cellSpacing=1 width="100%" class="gSAPTHEAD" id="formTable">
    <TR>
       <TH class="gsapthead" colSpan=2 height=25 noWrap>&nbsp;Fisa Lucrare Programata&nbsp;</TH>
    </TR>

    <TR>
      <TD width="25%">
       <b>1.</b> Numar curent lucrare:
      </TD>

      <TD width="75%">
        <span style="float:right;">v. <%=ObjectUtils.getStringValue( bean.getVersion())%></span>
        <%=bean.getNumar()%>
      </TD>
    </TR>

<TR>
<TD valign=top>
   <b>2.</b> Localizare lucrare:
</TD>
<TD>
<TABLE class="gsapthead">

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

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

<TR class="gSAPGroup">
  <TD><b>2.3</b> C.E.: </TD>
  <TD valign="MIDDLE" width="100%" >
      <%=centruBean.getCodSap()%> - <%=centruBean.getName()%>
      &nbsp;&nbsp;<b>2.3.1</b>&nbsp;Formatia de lucru: <%=getText( formatiaDelucruBean.getName())%>
  </TD>
</TR>

<TR class="gSAPGroup">
    <TD><strong>Nivel tensiune:</strong> </TD>
    <td>
        <%=bean.getNivelTensiune()%>

        <span style="display: none">
            <input type="radio" id="nt_jt"
                   name="<%=LucrareProgramataConstants.JSP.NIVEL_TENSIUNE%>"
                   <%=NivelTensiune.is( bean.getNivelTensiune(), NivelTensiune.JT) ? "checked" : ""%>
                   value="<%=NivelTensiune.JT%>"
                   onclick="showNivelTensiuneFields()" > <label for="nt_jt">JT</label>
            &nbsp;&nbsp;
        </span>

        <span style="display: none" >
            <input type="radio" id="nt_mt" name="<%=LucrareProgramataConstants.JSP.NIVEL_TENSIUNE%>"
                   <%=NivelTensiune.is( bean.getNivelTensiune(), NivelTensiune.MT) ? "checked" : ""%>
                   value="<%=NivelTensiune.MT%>" onclick="showNivelTensiuneFields()" > <label for="nt_mt">MT</label>
            &nbsp;&nbsp;
        </span>

        <span style="display: none" >
            <input type="radio" id="nt_it" name="<%=LucrareProgramataConstants.JSP.NIVEL_TENSIUNE%>" <%=NivelTensiune.is( bean.getNivelTensiune(), NivelTensiune.IT) ? "checked" : ""%>
                   value="<%=NivelTensiune.IT%>" onclick="showNivelTensiuneFields()" > <label for="nt_it">IT</label>
        </span>


        <script type="text/javascript">

            function showNivelTensiuneFields(){
                showJtFields( false);
                showItFields( false);


                if( $("nt_jt").checked){
                    showJtFields( true);
                    showItFields( false);
                }else if( $("nt_mt").checked || $("nt_it").checked){
                    showJtFields( false);
                    showItFields( true);
                }
            }

            function showJtFields( show){
                commonShowNivelTensiuneFields("<%=NivelTensiune.JT%>", show);
            }

            function showMtFields( show){

            }

            function showItFields( show){
                commonShowNivelTensiuneFields("<%=NivelTensiune.IT%>", show);
            }

            function commonShowNivelTensiuneFields( nivelTensiune, show){
                var list = $("formTable").getElementsByTagName("tr");
                for( var i = 0; i < list.length; i++){
                    var el = list[i];
                    var attrNivelTensiune = el.getAttribute("nivelTensiune");
                    if( attrNivelTensiune && nivelTensiune == attrNivelTensiune){
                        if( show){
                            Element.show( el);
                        }else{
                            Element.hide( el);
                        }
                    }
                }
            }

            showNivelTensiuneFields();
        </script>

    </td>
</TR>

<TR class="gSAPGroup" nivelTensiune="<%=NivelTensiune.IT%>">
 <TD>
    <b>2.4</b> Ansamblu functional:
 </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" nivelTensiune="<%=NivelTensiune.IT%>">
<TD>
    <b>2.5</b> Statie:
</TD>
<TD>
     <%--<%=getNomSAPText( bean.getStatieId(), TipInstalatie.STATIE)%>--%>
     <%=AppConstants.format(bean.getStatieDen())%>

    <b>2.5.1</b> Bobina:
    <%--<%=getNomSAPText( bean.getBobinaId(), TipInstalatie.BOBINA)%>--%>
    <%=AppConstants.format(bean.getBobinaDen())%>
</TD>
</TR>

<TR class="gSAPGroup" nivelTensiune="<%=NivelTensiune.IT%>">
<TD>
    <b>2.6</b> Celula:
</TD>
<TD>
    <%--<%=getNomSAPText( bean.getCelulaId(), TipInstalatie.CELULA)%>--%>
    <%=AppConstants.format(bean.getCelulaDen())%>

    <b>2.6.1</b> Bara:
    <%--<%=getNomSAPText( bean.getBaraId(), TipInstalatie.BARA)%>--%>
    <%=AppConstants.format(bean.getBaraDen())%>
</TD>
</TR>

<TR class="gSAPGroup" nivelTensiune="<%=NivelTensiune.IT%>">
<TD>
    <b>2.7</b> Linie:
</TD>
<TD>
     <%--<%=getNomSAPText( bean.getLinieId(), TipInstalatie.LINIE)%>--%>
    <%=AppConstants.format(bean.getLinieDen())%>
    <b>2.7.1</b> Derivatie:
    <%--<%=getNomSAPText( bean.getDerivatieId(), TipInstalatie.DERIVATIE)%>--%>
    <%=AppConstants.format(bean.getDerivatieDen())%>
</TD>
</TR>

<TR class="gSAPGroup" nivelTensiune="<%=NivelTensiune.IT%>">
<TD> <b>2.8</b> PA/PT: </TD>
    <TD>
        <%--<%=getNomSAPText( bean.getPA_PT_Id(), TipInstalatie.PA_PT)%>--%>
        <%=AppConstants.format(bean.getPaptDen())%>
    </TD>
</TR>
<TR class="gSAPGroup" nivelTensiune="<%=NivelTensiune.IT%>">
    <TD><b>2.9</b> Trafo: </TD>
    <TD>
        <%--<%=getNomSAPText( bean.getTrafoId(), TipInstalatie.TRAFO)%>--%>
        <%=AppConstants.format(bean.getTrafoDen())%>
    </TD>
</TR>

<tr class="gSAPGroup" nivelTensiune="<%=NivelTensiune.JT%>">
    <td>Categoria de instalatii:</td>
    <td>
        <%=CategoriiInstalatii.Helper.getCategoriiInstalatie( bean.getCatInstalatii(), "-")%>
    </td>
</tr>
<tr class="gSAPGroup" nivelTensiune="<%=NivelTensiune.JT%>">
    <td>Instalatia:</td>
    <td>
        <%=CategoriiInstalatii.Helper.getDenumireInstalatie( bean.getInstalatia(), "-")%>
        &nbsp;&nbsp;
        Nr./Denumire: <%=getText( bean.getDenumireInstalatie())%>
    </td>
</tr>
<tr class="gSAPGroup" nivelTensiune="<%=NivelTensiune.JT%>">
    <td>Adrese</td>
    <td>

        <%
            final List adrese = bean.getAdreseLucrare();
        %>
        <TABLE id="adrese" border=0 cellPadding=2 cellSpacing=1 width="100%" class="gSAPTHEAD" style="BACKGROUND-COLOR: white">
            <tr noSelect="true">
                <TH class="gsapthead" height=25 noWrap>&nbsp;Localitate:&nbsp;</TH>
                <TH class="gsapthead" height=25 noWrap>&nbsp;Tip artera:&nbsp;</TH>
                <TH class="gsapthead" height=25 noWrap>&nbsp;Denumire:&nbsp;</TH>
                <TH class="gsapthead" height=25 noWrap>&nbsp;Numar:&nbsp;</TH>
                <TH class="gsapthead" height=25 noWrap>&nbsp;Bloc:&nbsp;</TH>
                <TH class="gsapthead" height=25 width="1%" noWrap>&nbsp;</TH>
            </tr>

            <% if( adrese. size() == 0){ %>
                <tr>
                    <td colspan="5">Fara adrese completate ...</td>
                </tr>
            <% } else for (int i = 0; i < adrese.size(); i++) {
                final Adresa adresa = (Adresa) adrese.get(i);

                if( adresa.toDelete()) continue; %>

                <tr align="left">
                    <td >
                        <%=AppConstants.format(adresa.getLocalitate())%>
                    </td>
                    <td align="center">
                        <%=AppConstants.format(adresa.getTipArtera()) %>
                    </td>
                    <td>
                        <%=AppConstants.format(adresa.getDenStrada())%>
                    </td>
                    <td align="center">
                        <%=AppConstants.format(adresa.getNr())%>
                    </td>
                    <td align="center">
                        <%=AppConstants.format(adresa.getBloc())%>
                    </td>
                </tr>
            <% } %>
        </TABLE>

        <script type="text/javascript">
            var table = new Table('adrese', "table");
        </script>
    </td>
</tr>
</TABLE>

</TD>
</TR>

<TR>
  <TD>
      <b>3.</b> Mediul:
  </TD>
  <TD>
      <%=Mediu.is(bean.getMediu(), Mediu.URBAN)?"Urban": Mediu.is( bean.getMediu(), Mediu.RURAL) ? "Rural":"-"%>
  </TD>
</TR>


<%
    String tipInterval = "";
    List intervale = null;
    String jsFunctionCalculDurata = "";
%>

<TR>
    <TD vAlign=top><B>4.</B> Durata programata: </TD>
    <TD vAlign=center>
        <%
             tipInterval = IntervalLucrare.Tip.INTERVAL_LUCRARE_PROGR;
             intervale = bean.getIntervaleLucrareProgramata();
             jsFunctionCalculDurata = "calculeazaDurataProgramata";
        %>
        <table border="0">
            <tr><td>
                    Data start: <%=AppConstants.format(bean.getDataProgramataStart())%>
                    &nbsp;&nbsp;&nbsp;
                </td>
                <td>
                    Data stop:  <%=AppConstants.format(bean.getDataProgramataStop())%>
                </td>
            </tr>
            <%@ include file="_intervalePrint.jspf"%>

            <tr>
                <td colspan="3" style="color: green;border-top:1px solid gray">
                    Durata : <%=bean.getDurataProgramare()/60%>h <%=bean.getDurataProgramare()%60%>'
                </td>
            </tr>
        </table>
    </TD>
</TR>

<TR>
  <TD vAlign=top>
      <B>5.</B> Durata realizarii lucrarii:
  </TD>
  <TD>
      <table border="0">
        <tr><td>
            <%
                 tipInterval = IntervalLucrare.Tip.INTERVAL_LUCRARE;
                 intervale= bean.getIntervaleLucrare();
                 jsFunctionCalculDurata = "calculeazaDurataRealizare";
            %>
                Data start: <%=AppConstants.format(bean.getDataLucrareStart())%>
                &nbsp;&nbsp;&nbsp;
            </td>
            <td>
                Data stop: <%=AppConstants.format(bean.getDataLucrareStop())%>
            </td>
        </tr>
        <%@ include file="_intervalePrint.jspf"%>
        <tr>
          <td colspan="3" style="color: green;border-top:1px solid gray">
              Durata : <%=bean.getDurataLucrare()/60%>h <%=bean.getDurataLucrare()%60%>'
          </td>
        </tr>
    </table>
  </TD>
</TR>

<TR>
    <TD>
        <b>6.</b> Durata intreruperii consumatorilor:
    </TD>
    <td>
        <%
             tipInterval = IntervalLucrare.Tip.INTERVAL_INTRERUPERE;
             intervale= bean.getIntervaleIntrerupere();
             jsFunctionCalculDurata = "calculeazaDurataIntrerupere";
        %>

        <%--<%@ include file="_intervale2.jspf"%>--%>
        <table border="0">
            <tr>
                <td>
                    Data start: <%=AppConstants.format(bean.getDataIntrerupereStart())%>
                    &nbsp;&nbsp;&nbsp;
                </td>
                <td>
                    Data stop:  <%=AppConstants.format(bean.getDataIntrerupereStop())%>
                </td>
            </tr>
            <%@ include file="_intervalePrint.jspf"%>
            <tr>
              <td colspan="3" style="color: green;border-top:1px solid gray">
                  Durata : <%=bean.getDurataIntreruperi()/60%>h <%=bean.getDurataIntreruperi()%60%>'
              </td>
            </tr>
        </table>
    </TD>
</TR>

<TR niveltensiune="<%=NivelTensiune.IT%>">
    <TD>
        <b>7.</b> Energia electrica nelivrata consumatorilor:
    </TD>
    <td>
        <%=AppConstants.format2(bean.getEnergieNelivrata())%> kWh
    </td>
</TR>

<TR>
    <TD vAlign=top><B>8.</B> Descriere lucrare: </TD>
    <TD>
         <%=getText( bean.getDescriereLucrare())%>
</TD></TR>

<TR>
    <TD><B>9.</B> Nr. utilizatori intrerupti: </TD>
    <TD>
      <TABLE class=gSAPTHEAD cellSpacing=1 width="100%">
        <TBODY>
        <TR class=gSAPGroup align=middle>
          <TD>Inalta(IT)</TD>
          <TD>Medie(MT)</TD>
          <TD>Joasa(JT)</TD></TR>
        <TR>
            <TD align=middle>
                <%=bean.getUtilizatoriIT()%>
            </TD>
            <TD align=middle>
                <%=bean.getUtilizatoriMT()%>
            </TD>
            <TD align=middle>
                <%=bean.getUtilizatoriJT()%>
            </TD>
        </TR>
        <tr>
            <%
                final long totalUtilizatori =
                        bean.getUtilizatoriIT() +
                        bean.getUtilizatoriMT() +
                        bean.getUtilizatoriJT();
            %>
            <td colspan="2" align="right">
                <b>Total:</b>
            </td>
            <td align="center">
                <%=totalUtilizatori%>
            </td>
        </tr>
        </TBODY>
      </TABLE>
    </TD>
</TR>
<TR>
    <TD vAlign=top><B>10.</B> Propuneri/Masuri finale: </TD>
    <TD>
        <%=getText(bean.getPropuneri())%>
    </TD>
</TR>

 <tr>
    <td> <b>11.</b> Echipa de lucru  </td>
    <td>
        <% if ( TipEchipaDeLucru.is(bean.getEchipaDeLucruTip(), TipEchipaDeLucru.SISE)){ %>
            SISE :  <%=getText( bean.getEchipaDeLucruDen())%>
        <% } else if( TipEchipaDeLucru.is(bean.getEchipaDeLucruTip(), TipEchipaDeLucru.TERTI)){ %>
            Terti : <%=getText( bean.getEchipaDeLucruDen())%>
        <% } else if( TipEchipaDeLucru.is(bean.getEchipaDeLucruTip(), TipEchipaDeLucru.PERSONAL_PR)){ %>
            Personal Propriu : <%=getText( bean.getEchipaDeLucruDen())%>
        <% } else {%>
            -
        <% } %>
    </td>
 </tr>
 <TR>
    <TD colSpan=2><B>12.</B>
        Lucrarea a fost realizata de: <%=getText( bean.getRealizataDe())%>. &nbsp;&nbsp;&nbsp;&nbsp;
        Conducatorul sub-unitatii de exploatare: <%=getText( bean.getConducatorUnitateExpl())%>.
    </TD>
 </TR>
 <TR>
    <TD colSpan=2><B>13. </B>
        Lucrarea a fost receptionata de:<%=getText( bean.getReceptionatDe())%>. &nbsp;&nbsp;&nbsp;&nbsp;

        Admitent la lucrare: <%=getText( bean.getAdmitentLucrare())%>
    </TD>
</TR>
</TABLE>

<%!
    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) {
        Instalatie data = null;


        try {
            if( id > 0) data = InstalatieDB.getById( id, TipInstalatie.Helper.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;
    }
%>


<script type="text/javascript">
    showNivelTensiuneFields();
</script>
