<%@ page import="ro.emid.common.db.CategoryAccessDB" %>
<%@ page import="java.sql.Date" %>
<%@ page import="java.util.Calendar" %>
<%@ page import="java.util.List" %>
<%@ page import="ro.emid.incidente.obj.*" %>
<%@ page import="ro.emid.common.db.NomAccessDB" %>
<%@ page import="ro.emid.common.db.DataDB" %>
<%@ page import="ro.emid.incidente.IncidenteSessionUtils" %>
<%@ page import="ro.emid.common.types.*" %>
<%@ page import="ro.emid.common.constants.*" %>


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

<% if (request.getAttribute("act") !=null) {%>
<script type="text/javascript">
    try{
        opener.reloadData();
    }catch( e){
    }
    window.close();
</script>
<%}else{%>

<%
    FiltruIncidente filtru = null;
    if( IncidenteSessionUtils.isFiltruIncidente(session)){
        filtru = IncidenteSessionUtils.getFiltruIncidente(session);
    }else if( IncidenteSessionUtils.isFiltruEchipamente(session)){
        filtru = IncidenteSessionUtils.getFiltruEchipamente(session);
    }

    final String value_data_start = AppConstants.format(filtru.getDataStart());
    final String value_data_stop  = AppConstants.format(filtru.getDataStop());
%>

<style type="text/css">
    legend{
        font-weight: bold;
    }
</style>

<TABLE class="gsapthead" align=center border=0 cellPadding=10 cellSpacing=0 width="100%">
    <TBODY>
        <TR>
            <TD>
                <TABLE align=center border=0 cellPadding=1 cellSpacing=0 width="100%" class="gsaptd">
                    <FORM NAME="zaforma" ACTION="<%=AppConstants.APP_NAME%>" METHOD=POST style="margin: 0">
                        <INPUT TYPE="hidden" NAME="action" VALUE="saveFiltruIncidente" />
                        <INPUT TYPE="hidden" NAME="refreshData" VALUE="false" />
                        <TBODY>
                            <TR>
                                <TD colspan="2">
                                    <CENTER><b>
                                        <% if( IncidenteSessionUtils.isFiltruIncidente(session)){%>
                                            Configurare FILTRU Incidente
                                        <%}else if( IncidenteSessionUtils.isFiltruEchipamente(session)){%>
                                            Configurare FILTRU Echipamente
                                        <%}%>
                                    </b></CENTER>
                                </TD>
                            </TR>
                            <tr>
                                <td>
                                    <fieldset style="margin-top: 0">
                                        <legend><input type="checkbox" name="<%=FiltruIncidenteConstants.PERIOADA_CHECKED%>" id="perioada_checked" value="1" onclick="enableDateInputs( this)" <%=filtru.isPerioadaChecked() ? "checked" : ""%> > <label for="perioada_checked">Perioada</label></legend>
                                        &nbsp;&nbsp;<input type="text" id="data_start" name="<%=FiltruIncidenteConstants.DATA_START%>" size="8" value="<%=value_data_start%>" <%=filtru.isPerioadaChecked() ? "" : "disabled"%>  /> <a href="#" id="trigger_data_start" ><img src="<%=AppConstants.WEB_RESOURCE_PATH + "/images/cal.gif"%>" width="16" height="16" border="0" alt="Apasa pt. a selecta data."></a> -
                                        <input type="text" id="data_stop" name="<%=FiltruIncidenteConstants.DATA_STOP%>" size="8" value="<%=value_data_stop%>" <%=filtru.isPerioadaChecked() ? "" : "disabled"%> /> <a href="#"  id="trigger_data_stop" ><img src="<%=AppConstants.WEB_RESOURCE_PATH + "/images/cal.gif"%>" width="16" height="16" border="0" alt="Apasa pt. a selecta data."></a>
                                    </fieldset>

                                    <script type="text/javascript">
                                        function enableDateInputs( check){
                                            $("data_start").disabled = !check.checked;
                                            $("data_stop").disabled = !check.checked;

                                            if( check.checked){
                                                $("trigger_data_start").style.visibility = '';
                                                $("trigger_data_stop").style.visibility = '';
                                            }else{
                                                $("trigger_data_start").style.visibility = 'hidden';
                                                $("trigger_data_stop").style.visibility = 'hidden';
                                            }
                                        }


                                         Calendar.setup(
                                            {
                                              inputField  : "data_start",         // ID of the input field
                                              ifFormat    : "%d.%m.%Y",    // the date format
                                              button      : "trigger_data_start"       // ID of the button
                                            }
                                          );

                                          Calendar.setup(
                                            {
                                              inputField  : "data_stop",         // ID of the input field
                                              ifFormat    : "%d.%m.%Y",    // the date format
                                              button      : "trigger_data_stop"       // ID of the button
                                            }
                                          );

                                          enableDateInputs($("perioada_checked"));
                                    </script>
                                </td>
                            </tr>
                            <% if( filtru.getCriteriiFiltru() != null && filtru.getCriteriiFiltru().size() > 0){%>
                            <tr>
                                <td>
                                    <fieldset>
                                        <legend>Lista criterii filtrare</legend>
                                        <table class="gSAPTHEAD" id="criteriiTable" width="100%" style="BACKGROUND-COLOR: white" cellspacing="1">
                                        <%
                                            final List listaCriterii = filtru.getCriteriiFiltru();
                                            for (int i = 0; i < listaCriterii.size(); i++) {
                                                final CriteriuFiltruIncidente criteriu = filtru.getCriteriu(i);


                                                final StringBuffer text = new StringBuffer();
                                                if (criteriu.getInclusivExclusiv() == CriteriuFiltruIncidenteConstants.VALUE_INCLUSIV) {
                                                    text.append("<b>Include</b> ");
                                                } else if (criteriu.getInclusivExclusiv() == CriteriuFiltruIncidenteConstants.VALUE_EXCLUSIV) {
                                                    text.append("<b>Exclude</b> ");
                                                }

                                                if( criteriu.getFormatiaId() > 0){
                                                    final Category formatia = CategoryAccessDB.getById( criteriu.getFormatiaId());
                                                    if( formatia != null) text.append("<span title=\"Incidentele din Formatia de lucru\">").append( formatia.getName()).append("</span>, ");
                                                }

                                                if ( criteriu.getAnsambluId() != -1) {
                                                    final Nomenclator ansamblu = NomAccessDB.getNom( criteriu.getAnsambluId());
                                                    if (ansamblu != null) text.append("<span title=\"Ansamblul Functional\">").append( ansamblu.getDescriere()).append("</span>, ");
                                                }

                                                if (criteriu.getStatieId() != -1) {
                                                    final ro.emid.common.types.DataObj data = DataDB.getById(criteriu.getStatieId(), DataDB.isLocFunctional(NomConstants.NOM_STATII));
                                                    if (data != null)
                                                        text.append("<span title=\"Statia\">").append(data.getNume()).append("</span>, ");
                                                }

                                                if (criteriu.getCelulaId() != -1) {
                                                    final DataObj data = DataDB.getById(criteriu.getCelulaId(), DataDB.isLocFunctional(NomConstants.NOM_CELULE));
                                                    if (data != null)
                                                        text.append("<span title=\"Celula\">").append(data.getNume()).append("</span>, ");
                                                }

                                                if (criteriu.getBaraId() != -1) {
                                                    final DataObj data = DataDB.getById(criteriu.getBaraId(), DataDB.isLocFunctional(NomConstants.NOM_BARE));
                                                    if (data != null)
                                                        text.append("<span title=\"Bara\">").append(data.getNume()).append("</span>, ");
                                                }

                                                if (criteriu.getBobinaId() != -1) {
                                                    final DataObj data = DataDB.getById(criteriu.getBobinaId(), DataDB.isLocFunctional(NomConstants.NOM_BOBINE));
                                                    if (data != null)
                                                        text.append("<span title=\"Bobina\">").append(data.getNume()).append("</span>, ");
                                                }

                                                if (criteriu.getLinieId() != -1) {
                                                    final DataObj data = DataDB.getById(criteriu.getLinieId(), DataDB.isLocFunctional(NomConstants.NOM_LINII));
                                                    if (data != null)
                                                        text.append("<span title=\"Linia\">").append(data.getNume()).append("</span>, ");
                                                }

                                                if (criteriu.getDerivatieId() != -1) {
                                                    final DataObj data = DataDB.getById(criteriu.getDerivatieId(), DataDB.isLocFunctional(NomConstants.NOM_DERIVATIE));
                                                    if (data != null)
                                                        text.append("<span title=\"Derivatia\">").append(data.getNume()).append("</span>, ");
                                                }

                                                if (criteriu.getPA_PT_Id() != -1) {
                                                    final DataObj data = DataDB.getById(criteriu.getPA_PT_Id(), DataDB.isLocFunctional(NomConstants.NOM_PA_PT));
                                                    if (data != null)
                                                        text.append("<span title=\"PA/PT\">").append(data.getNume()).append("</span>, ");
                                                }

                                                if (criteriu.getTrafoId() != -1) {
                                                    final DataObj data = DataDB.getById(criteriu.getTrafoId(), DataDB.isLocFunctional(NomConstants.NOM_TRAFO));
                                                    if (data != null)
                                                        text.append("<span title=\"Trafo\">").append(data.getNume()).append("</span>, ");
                                                }

                                                if ( criteriu.getCauzaGrupaId() != -1){
                                                    final Nomenclator nom = NomAccessDB.getNom( criteriu.getCauzaGrupaId());
                                                    if( nom != null)
                                                        text.append("<span title=\"Cauza Grupa\">").append(nom.getDescriere()).append("</span>, ");
                                                }

                                                if( criteriu.getCauzaId() != -1){
                                                    final Nomenclator nom = NomAccessDB.getNom( criteriu.getCauzaId());
                                                    if( nom != null)
                                                        text.append("<span title=\"Cauza\">").append(nom.getDescriere()).append("</span>, ");
                                                }

                                                if ( !ro.emid.common.utils.Util.isZeroLenght(criteriu.getEnOp())){
                                                    text.append("Energia nelivrata ").append(criteriu.getEnOp()).append(' ').append(criteriu.getEnergieNelivrata()).append(',');
                                                }

                                                if( criteriu.getModCompletareConsumatori() != -1){
                                                    text.append("Compl.consum. - ").append(
                                                            criteriu.getModCompletareConsumatori()==IncidentConstants.ConsumatoriCompletati.AUTOMAT?"automat":
                                                            criteriu.getModCompletareConsumatori()==IncidentConstants.ConsumatoriCompletati.MANUAL?"manual":"n/a"
                                                    ).append(',');
                                                }

                                                if( criteriu.getMediul() != -1){
                                                    text.append("mediu - ").append(
                                                            criteriu.getMediul()==IncidentConstants.Mediul.DIN_NOMENCLATOR?"conf.nomenclator":
                                                            criteriu.getMediul()==IncidentConstants.Mediul.URBAN?"urban":
                                                            criteriu.getMediul()==IncidentConstants.Mediul.RURAL?"rural":"n/a"
                                                    );
                                                }


                                                criteriu.setText( text.toString());%>
                                                <tr valign="top">
                                                    <td align="left">
                                                        <%=criteriu.getText()%>
                                                    </td>
                                                    <td width="1%" align="center" nowrap>
                                                        <a class="link" href="<%=AppConstants.APP_NAME%>?action=editCriteriuFiltrareIncidente&index=<%=i%>" target="frameEditCriteriu">editeaza</a>&nbsp;
                                                        <a class="link" href="<%=AppConstants.APP_NAME%>?action=removeCriteriuFiltrareIncidente&index=<%=i%>" onclick="return confirm('Stergeti criteriul ?')">sterge</a>
                                                    </td>
                                                </tr>
                                        <%}%>
                                        </table>

                                        <script type="text/javascript">
                                            var tableCriterii = new Table("criteriiTable", "tableCriterii");
                                        </script>
                                    </fieldset>
                                </td>
                            </tr>
                            <%}%>
                            <tr>
                                <td>
                                    <fieldset>
                                        <legend>Editare criteriu filtrare</legend>
                                        <iframe name="frameEditCriteriu" src="<%=AppConstants.APP_NAME%>?action=editCriteriuFiltrareIncidente" frameborder="0" width="100%" height="400"></iframe>
                                    </fieldset>
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    <input type="submit" value="Aplica Filtru" />
                                    <input type="button" value="Renunta" onclick="window.close()"/>
                                </td>
                            </tr>
                        </TBODY>
                    </FORM>
                </TABLE>
            </TD>
        </TR>
    </TBODY>
</TABLE>

<script type="text/javascript">
    function refreshData(){
        document.zaforma.refreshData.value = "true";
        document.zaforma.submit();
    }

    function resetComboLeaveFirstOption( oCombo){
        for (var i = 1; oCombo.options.length > 1;) {
            oCombo.options[i] = null;
        }
        
        alert( 'Local resetComboLeaveFirstOption');
    }
</script>
<%}%>