<%@ page import="ro.emid.common.constants.NomConstants" %>
<%@ page import="ro.emid.common.constants.WebConstants" %>
<%@ page import="ro.emid.common.utils.ObjectUtils" %>
<%@ page import="ro.emid.common.types.Nomenclator" %>
<%@ page import="ro.emid.common.db.NomAccessDB" %>
<%@ page import="ro.emid.common.types.binder.NomenclatorBinder" %>
<%@ page import="ro.emid.Application" %>
<%@ page import="ro.emid.common.constants.AppConstants" %>

<%
    int tipSelectat = NomConstants.NOM_ANSAMBLURI; //primul tip de nomenclator
    final int tipRequest  = ObjectUtils.getIntValue(request.getParameter(NomConstants.TIP), -1);
    if (tipRequest > 0) {
        tipSelectat = tipRequest;
        session.setAttribute(NomConstants.SESSION_TIP_SELECTAT, new Integer(tipSelectat));
    } else {
        final Integer intTip = (Integer) session.getAttribute(NomConstants.SESSION_TIP_SELECTAT);
        final int tipSession = intTip != null ? intTip.intValue() : -1;

        if (tipSession > 0) {
            tipSelectat = tipSession;
        }
    }

    final NomenclatorBinder binder = (NomenclatorBinder)request.getAttribute(NomConstants.BEAN);
    final Nomenclator bean = (binder != null ? binder.getBean() : new Nomenclator());

    final Nomenclator[] noms = NomAccessDB.getAllNom( tipSelectat);
%>


<table width="100%"><tr><td>
    <form name="<%=WebConstants.FORM_CAUT%>" style="margin-bottom: 0" action="<%=AppConstants.APP_NAME%>?action=show-nomenclator" method="post">
        <label>
            Nomenclator:
            <select name="<%=NomConstants.TIP%>" onchange=" document.<%=WebConstants.FORM_CAUT%>.submit()" class="gSAPSelect">
                <option value="<%=NomConstants.NOM_ANSAMBLURI%>" <%=tipSelectat == NomConstants.NOM_ANSAMBLURI ? "selected" : ""%> >Ansambluri</option>
                <option value="<%=NomConstants.NOM_CAUZE%>" <%=tipSelectat == NomConstants.NOM_CAUZE ? "selected" : ""%> >Cauze</option>
                <option value="<%=NomConstants.NOM_CAUZE_ECH%>" <%=tipSelectat == NomConstants.NOM_CAUZE_ECH ? "selected" : ""%> >Cauze ech</option>
                <option value="<%=NomConstants.NOM_CAUZE_GRUPE%>" <%=tipSelectat == NomConstants.NOM_CAUZE_GRUPE ? "selected" : ""%> >Cauze grupe</option>
                <option value="<%=NomConstants.NOM_CELULE%>" <%=tipSelectat == NomConstants.NOM_CELULE ? "selected" : ""%> >Celule</option>
                <option value="<%=NomConstants.NOM_COMPONENTE%>" <%=tipSelectat == NomConstants.NOM_COMPONENTE ? "selected" : ""%> >Componente</option>
                <option value="<%=NomConstants.NOM_ECHIPAMENTE%>" <%=tipSelectat == NomConstants.NOM_ECHIPAMENTE ? "selected" : ""%> >Echipamente</option>
                <option value="<%=NomConstants.NOM_ELEMENTE%>" <%=tipSelectat == NomConstants.NOM_ELEMENTE ? "selected" : ""%> >Elemente</option>
                <option value="<%=NomConstants.NOM_FRECV_DET %>" <%=tipSelectat == NomConstants.NOM_FRECV_DET ? "selected" : ""%> >Frecv det</option>
                <option value="<%=NomConstants.NOM_FUNCTIONARE_AMC%>" <%=tipSelectat == NomConstants.NOM_FUNCTIONARE_AMC ? "selected" : ""%> >Functionare AMC</option>
                <option value="<%=NomConstants.NOM_GRUPE_CAUZE_ECH%>" <%=tipSelectat == NomConstants.NOM_GRUPE_CAUZE_ECH ? "selected" : ""%> >Grupe Cauze ECH</option>
                <option value="<%=NomConstants.NOM_CAUZE_ECH%>" <%=tipSelectat == NomConstants.NOM_CAUZE_ECH ? "selected" : ""%> >Grupe ECH</option>
                <option value="<%=NomConstants.NOM_GRUPE_INSTALATII%>" <%=tipSelectat == NomConstants.NOM_GRUPE_INSTALATII ? "selected" : ""%> >Grupe instalatii</option>
                <option value="<%=NomConstants.NOM_INSTALATII%>" <%=tipSelectat == NomConstants.NOM_INSTALATII ? "selected" : ""%> >Instalatii</option>
                <option value="<%=NomConstants.NOM_MANIFESTARI%>" <%=tipSelectat == NomConstants.NOM_MANIFESTARI ? "selected" : ""%> >Manifestari</option>
                <option value="<%=NomConstants.NOM_METEO%>" <%=tipSelectat == NomConstants.NOM_METEO ? "selected" : ""%> >Meteo</option>
                <option value="<%=NomConstants.NOM_MOD_TRATARE_NEUTRU%>" <%=tipSelectat == NomConstants.NOM_MOD_TRATARE_NEUTRU ? "selected" : ""%> >Mod tratare neutru</option>
                <option value="<%=NomConstants.NOM_PRILEJURI%>" <%=tipSelectat == NomConstants.NOM_PRILEJURI ? "selected" : ""%> >Prilejuri</option>
                <option value="<%=NomConstants.NOM_PUTERI_PAC%>" <%=tipSelectat == NomConstants.NOM_PUTERI_PAC ? "selected" : ""%> >Puteri pac</option>
                <option value="<%=NomConstants.NOM_PUTERI_PAP%>" <%=tipSelectat == NomConstants.NOM_PUTERI_PAP ? "selected" : ""%> >Puteri pap</option>
                <option value="<%=NomConstants.NOM_REGIMURI_FUNCTIONARE%>" <%=tipSelectat == NomConstants.NOM_REGIMURI_FUNCTIONARE ? "selected" : ""%> >Regimuri de functionare</option>
                <option value="<%=NomConstants.NOM_REPARATII %>" <%=tipSelectat == NomConstants.NOM_REPARATII ? "selected" : ""%> >Reparatii</option>
                <option value="<%=NomConstants.NOM_REZ_INCERC_PROFILACTICE %>" <%=tipSelectat == NomConstants.NOM_REZ_INCERC_PROFILACTICE ? "selected" : ""%> >Rez incerc profilactice</option>
                <option value="<%=NomConstants.NOM_TARI %>" <%=tipSelectat == NomConstants.NOM_TARI ? "selected" : ""%> >Tari</option>
                <option value="<%=NomConstants.NOM_TENSIUNI_NOMINALE%>" <%=tipSelectat == NomConstants.NOM_TENSIUNI_NOMINALE ? "selected" : ""%> >Tensiuni nominale</option>
            </select>
        </label>
    </form></td>
    <td class="align-right">
        <% if( !Application.runAsAparatCentral()){ %>
            <input type="button" onclick="jQuery('#add-nom-row').show()" value="Adaugă"/>
        <% } %>
    </td>
</tr>
</table>

<table cellpadding=2 cellspacing=1 width="100%" class="table">
    <thead>
        <tr>
            <th height=25 noWrap width=70 class="align-center">Cod Grupa</th>
            <th nowrap width=60 class="align-center">Cod</th>
            <th nowrap >Descriere</th>
            <% if( !Application.runAsAparatCentral()){ %>
                <th nowrap width="1%">&nbsp;</th>
            <% } %>
        </tr>
    </thead>
    <tbody>
    <%--formular adaugare inregistrare nomenclator--%>
    <tr align="center" id="add-nom-row" <% if(!"save-nomenclator".equals(request.getParameter("action"))){%> class="hidden" <%}%> >
        <form name="<%=WebConstants.FORM%><%=noms.length%>" action="<%=AppConstants.APP_NAME%>?action=save-nomenclator" method="post" onsubmit="return onSalveaza( this)">
            <input type="hidden" name="<%=NomConstants.TIP%>" value="<%=tipSelectat%>"/>
            <input type="hidden" name="<%=NomConstants.ID%>" value="-1"/>

            <% if( !Application.runAsAparatCentral()){ %>
            <td>
                <input class="gsapinputtext" type="text" size="5" name="<%=NomConstants.COD_GRUPA%>"  value="<%=bean.getCodGrupa() != AppConstants.INVALID_VALUE ? String.valueOf(bean.getCodGrupa()) : "" %>">
            </td>
            <td>
                <input class="gsapinputtext" type="text" size="5" name="<%=NomConstants.COD%>"  value="<%=bean.getCod() != AppConstants.INVALID_VALUE ? String.valueOf(bean.getCod()) : ""%>">
            </td>
            <td align="left">
                <input class="gsapinputtext" type="text" size="70"  align="left" name="<%=NomConstants.DESCRIERE%>"  value="<%=ObjectUtils.getStringValue( bean.getDescriere())%>">
            </td>
            <% } %>

            <% if( !Application.runAsAparatCentral()){ %>
            <td>
                <nobr>
                    <input type="submit" value="Adaugă"/>
                    <input type="button" value="Renunţă" onclick="Element.hide('add-nom-row')"/>
                </nobr>
            </td>
            <% } %>
        </form>
    </tr>
    <% for (int i = 0; i < noms.length; i++) {
        final Nomenclator nom = noms[i]; %>
        <form name="<%=WebConstants.FORM%><%=i%>" action="<%=AppConstants.APP_NAME%>?action=save-nomenclator" method="post" onsubmit="return onSalveaza( this)">
            <input type="hidden" name="<%=NomConstants.TIP%>" value="<%=nom.getTip()%>"/>
            <input type="hidden" name="<%=NomConstants.ID%>" value="<%=nom.getId()%>"/>
            <tr align="center" id="tr_inputs<%=i%>" style="display:none">
                <td>
                    <input class="gsapinputtext" type="text" size="5" name="<%=NomConstants.COD_GRUPA%>"  value="<%=nom.getCodGrupa() != AppConstants.INVALID_VALUE ? String.valueOf(nom.getCodGrupa()) : "" %>">
                </td>
                <td>
                    <input class="gsapinputtext" type="text" size="5" name="<%=NomConstants.COD%>"  value="<%=nom.getCod() != AppConstants.INVALID_VALUE ? String.valueOf(nom.getCod()) : "" %>">
                </td>
                <td align="left">
                    <input class="gsapinputtext" type="text" size="70" align="left" name="<%=NomConstants.DESCRIERE%>"  value="<%=ObjectUtils.getStringValue( nom.getDescriere())%>">
                </td>
                <td nowrap>
                    <input type="submit" value="Salvează" >
                    <input type="button" value="Renunţă" onclick="onRenunta( <%=i%>)" >
                </td>
            </tr>
            <tr align="center" id="tr_text<%=i%>">
                <td >
                    <%=nom.getCodGrupa() != AppConstants.INVALID_VALUE ? String.valueOf(nom.getCodGrupa()) : "" %>
                </td>
                <td >
                    <%=nom.getCod() != AppConstants.INVALID_VALUE ? String.valueOf(nom.getCod()) : "" %>
                </td>
                <td  align="left">
                    <%=ObjectUtils.getStringValue( nom.getDescriere())%>
                </td>
                <% if( !Application.runAsAparatCentral()){ %>
                    <td  nowrap>
                        <nobr>
                            <input type="button" value="Editează" onclick="onEdit(<%=i%>)" />
                            <input type="button" value="Şterge" onclick="onDelete(<%=i%>)" />
                        </nobr>
                    </td>
                <% } %>
            </tr>
        </form>        
    <%}%>

    </tbody>
</table>


<script type="text/javascript">
    var currentEditingFormaIndex = -1;

    function getForma(index){
        return  eval( 'document.<%=WebConstants.FORM%>' + index);
    }

    function onEdit( index){
        if( currentEditingFormaIndex != -1){
            $('tr_inputs'+currentEditingFormaIndex).style.display = 'none';
            $('tr_text'+currentEditingFormaIndex).style.display = '';
        }

        currentEditingFormaIndex = index;
        $('tr_inputs'+currentEditingFormaIndex).style.display = '';
        $('tr_text'+currentEditingFormaIndex).style.display = 'none';
        getForma( index).<%=NomConstants.COD_GRUPA%>.select();
    }

    function onRenunta( index){
        if( currentEditingFormaIndex != -1){
            $('tr_inputs'+currentEditingFormaIndex).style.display = 'none';
            $('tr_text'+currentEditingFormaIndex).style.display = '';
        }

        currentEditingFormaIndex = -1;
    }

    function onSalveaza( forma){        
        return isValidData( forma);            
    }

    function onDelete( index){
        if( confirm( 'Doriti sa stergeti intrarea ?')){
            var forma = getForma(index);
            forma.action = "<%=AppConstants.APP_NAME%>?action=delete-nomenclator";
            forma.submit();
        }
    }

    function isValidData( forma){
        return true;
    }

</script>