<%@ page import="ro.emid.callcenter.obj.Lucrare" %>
<%@ page import="ro.emid.callcenter.obj.binder.LucrareBinder" %>
<%@ page import="ro.emid.callcenter.obj.LucrareConstants" %>
<%@ page import="ro.emid.common.types.Adresa" %>
<%@ page import="ro.emid.common.db.CategoryAccessDB" %>
<%@ page import="ro.emid.common.constants.*" %>

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

<%
    Lucrare bean = null;
    final LucrareBinder binder = (LucrareBinder) session.getAttribute(LucrareConstants.BEAN);
    bean = (binder != null ? (Lucrare) binder.getBean() : new Lucrare());


    final Adresa[] adrese = bean.getAdrese();
%>



<TABLE cellSpacing=1 cellPadding=5 width="100%">
    <TR>
        <TD>
            <TABLE class=gSAPTHEAD cellSpacing=1 cellPadding=2 width="100%" border=0>
                <tr>
                    <td align="left">
                        <span style="FONT-WEIGHT: bold; FONT-SIZE: 16px;"> Editor Lucrare programata:</span>
                    </td>
                </tr>
            </TABLE>
            <TABLE  border=0 cellPadding=2 cellSpacing=1 width="100%" class="gSAPTHEAD">


<form name="zaforma" action="<%=GlobalAppConstants.APP_NAME%>" method="post" >
    <input type="hidden" name="action" value="saveLucrare">
    <INPUT TYPE="hidden" NAME="<%=LucrareConstants.ID%>" VALUE="<%=bean.getId()%>" />
    <tr class="compartiment"><td colspan="2">Date lucrare:</td></tr>
    <tr>
        <td style="width: 70px"></td>
        <td>
            <table width="100%">
                <tr>
                    <td>Nume lucrare:</td>
                    <td align="left" >
                        <input class="gsapinputtext" type="text" size="50" name="<%=LucrareConstants.NUME%>"  value="<%=AppConstants.format( bean.getNume())%>">
                    </td>
                </tr>
                <tr>
                    <td>Descriere lucrare:</td>
                    <td align="left">
                        <textarea rows="2" cols="80" name="<%=LucrareConstants.DESCRIERE%>"><%=AppConstants.format(bean.getDescriere())%></textarea>
                    </td>
                </tr>
                <tr>
                    <td>Data start:</td>
                    <td align="left">
                        <input class="gsapinputtext" type="text" name="<%=LucrareConstants.DATA_START%>"  value="<%=AppConstants.format( bean.getDataStart())%>">
                        <a id="trigger<%=LucrareConstants.DATA_START%>" href="#"><img src="<%=AppConstants.WEB_RESOURCE_PATH%>/images/cal.gif" width="16" height="16" border="0" alt="Apasa pt. a selecta data"></a>
                        <script type="text/javascript">
                            Calendar.setup(
                              {
                                inputField  : "<%=LucrareConstants.DATA_START%>",         // ID of the input field
                                ifFormat    : "%d.%m.%Y %H:%M",    // the date format
                                button      : "trigger<%=LucrareConstants.DATA_START%>"       // ID of the button
                              }
                            );
                          </script>
                    </td>
                </tr>
                <tr>
                    <td>Durata estimata:</td>
                    <td>
                        <input class="gsapinputtext" type="text" style="text-align:right;" size="4" name="<%=LucrareConstants.DURATA%>"  value="<%=AppConstants.format( bean.getDurata())%>"> (ore)
                    </td>
                </tr>
            </table>
        </td>
    </tr>
    <tr class="compartiment"><td colspan="2">Localizare/Adresa (strazi afectate de lucrarea programata):</td></tr>
    <tr>
        <td></td>
        <td>
            <table width="100%">
                <tr>
                    <td>
                        Filiala:
                         <SELECT name="<%=LucrareConstants.FILIALA_ID%>" onchange="fill_sucursala_select()" class="gSAPSelect" >
                             <OPTION value="-1">ELECTRICA SA:</OPTION>
                             <% final Category[] filiale = CategoryAccessDB.getAllCategoriesByParentId(-1);
                                 if (filiale != null) for (int i = 0; i < filiale.length; i++) {%>
                               <OPTION <%=(filiale[i].getId() == bean.getFilialaId()) ? "selected=\"true\" " : ""%>value="<%=filiale[i].getId()%>"><%=filiale[i].getName()%></OPTION>
                             <%} %>
                         </SELECT>&nbsp;&nbsp;

                         Sucursala:
                         <span id="sucursalaDiv">
                         <SELECT name="<%=LucrareConstants.SUCURSALA_ID%>" class="gSAPSelect" onchange="fill_centru_select()" >
                               <option value="-1">Selectati:</option>
                         </SELECT></span>&nbsp;&nbsp;

                         C.E.:
                         <span id="centruDiv">
                             <SELECT name="<%=LucrareConstants.SECTIE_ID%>" class="gSAPSelect">
                              <OPTION value="-1">Selectati:</OPTION>
                             </SELECT>
                         </span>&nbsp;&nbsp;

                         <script type="text/javascript">

                             function fill_sucursala_select(selectedValue, disabled, firstShow){
                                 resetComboLeaveFirstOption( document.zaforma.<%=LucrareConstants.SUCURSALA_ID%>);
                                 resetComboLeaveFirstOption( document.zaforma.<%=LucrareConstants.SECTIE_ID%>);

                                 var filialaId = document.zaforma.<%=LucrareConstants.FILIALA_ID%>[ document.zaforma.<%=LucrareConstants.FILIALA_ID%>.selectedIndex].value;
                                 if( filialaId > -1){
                                     var params = "parentId="+filialaId + "&selectName=<%=LucrareConstants.SUCURSALA_ID%>" + "&selectedValue=" + selectedValue;
                                     //alert( params);

                                     getCategoriesFromServer(params, function(originalRequest){
                                         //alert( getResponseMesg (originalRequest.responseText));
                                         $( 'sucursalaDiv').innerHTML = getResponseMesg (originalRequest.responseText);

                                         document.zaforma.<%=LucrareConstants.SUCURSALA_ID%>.onchange=fill_centru_select;

                                         if( disabled){
                                             removeOptionsLeaveThisOne( document.zaforma.<%=LucrareConstants.SUCURSALA_ID%>, selectedValue);
                                          }


                                         if( firstShow){
                                             fill_centru_select(<%=bean.getSectieId()%>, <%=user.getIeLevel() == ro.emid.common.constants.RoleConstants.NIVEL_CENTRU || user.getIeLevel() == RoleConstants.NIVEL_FORMATIE%>);
                                         }
                                     });
                                 }
                             }

                             function fill_centru_select(selectedValue, disabled){
                                 resetComboLeaveFirstOption( document.zaforma.<%=LucrareConstants.SECTIE_ID%>);

                                 var sucursalaId = document.zaforma.<%=LucrareConstants.SUCURSALA_ID%>[ document.zaforma.<%=LucrareConstants.SUCURSALA_ID%>.selectedIndex].value;
                                 if( sucursalaId > -1){
                                     var params = "parentId="+sucursalaId + "&selectName=<%=LucrareConstants.SECTIE_ID%>" +  "&selectedValue=" + selectedValue;
                                     //alert( params);

                                     getCategoriesFromServer(params,function(originalRequest){
                                         //alert( getResponseMesg (originalRequest.responseText));
                                         $( 'centruDiv').innerHTML = getResponseMesg (originalRequest.responseText);

                                         if( disabled){
                                             removeOptionsLeaveThisOne( document.zaforma.<%=LucrareConstants.SECTIE_ID%>, selectedValue)
                                         }
                                     });
                                 }
                             }

                             function removeOptionsLeaveThisOne(oCombo, valueToLeave){
                                 for (var i = 0; i < oCombo.options.length;) {
                                    if( oCombo.options[i].value == valueToLeave){ i++; }else{ oCombo.options[i] = null;}
                                 }
                             }


                             <%if( user.getIeLevel() != RoleConstants.NIVEL_FULL){%>
                                 removeOptionsLeaveThisOne( document.zaforma.<%=LucrareConstants.FILIALA_ID%>, <%=bean.getFilialaId()%>);
                             <%}%>
                             fill_sucursala_select(<%=bean.getSucursalaId()%>, <%=user.getIeLevel() == RoleConstants.NIVEL_SUCURSALA || user.getIeLevel() == RoleConstants.NIVEL_CENTRU || user.getIeLevel() == RoleConstants.NIVEL_FORMATIE%>, true);

                         </script>
                    </td>
                </tr>
                <tr>
                    <td>
                        <a href="javascript: void(null)" class="link" onclick="adaugaAdresa(); return false;">Adauga adresa</a>
                    </td>
                </tr>
                <tr>
                    <td >
                        <table width="100%">
                            <tr>
                                <TH class="gsapthead" >Municipiu/Comuna (Judet)</TH>
                                <TH class="gsapthead" colspan="2">Localitate/Sat</TH>
                                <TH class="gsapthead">Denumire</TH>
                                <TH class="gsapthead">Nr</TH>
                                <TH class="gsapthead">Bloc</TH>
                                <TH class="gsapthead">Op.</TH>
                            </tr>

                            <% for (int i = 0; i < adrese.length; i++) {
                                final Adresa adresa = adrese[i];%>
                                <tr id="tr<%=i%>">
                                    <input type="hidden" name="visiblex" value="1">
                                    <td>
                                        <input class="gsapinputtext" style="width: 200px" id="<%=AdresaConstants.LOCALITATE + i%>" name="<%=AdresaConstants.LOCALITATE%>" value="<%=AppConstants.format( adresa.getLocalitate())%>">
                                        &nbsp;&nbsp;j. <input class="gsapinputtext" style="width: 160px" readonly id="<%=AdresaConstants.JUDET + i%>" name="<%=AdresaConstants.JUDET%>" value="<%=AppConstants.format( adresa.getJudet())%>">
                                    </td>
                                    <td colspan="2">
                                        <input class="gsapinputtext" style="width: 160px" id="<%=AdresaConstants.LOCALITATE2 + i%>" name="<%=AdresaConstants.LOCALITATE2%>" value="<%=AppConstants.format( adresa.getLocalitate2())%>">
                                    </td>
                                    <td>
                                        <input type="hidden" name="<%=AdresaConstants.COD_STRADA%>" id="<%=AdresaConstants.COD_STRADA%>"  value="<%=adresa.getCodStrada()%>">
                                        <input class="gsapinputtext" style="width: 200px" name="<%=AdresaConstants.DEN_STRADA%>" id="<%=AdresaConstants.DEN_STRADA + i%>" value="<%=AppConstants.format( adresa.getDenStrada())%>">
                                        <input class="gsapinputtext" style="width: 70px" type=text id="<%=AdresaConstants.TIP_ARTERA + i%>" name="<%=AdresaConstants.TIP_ARTERA%>" onchange="onChangeTipArtera( <%=i%>)" value="<%=AppConstants.format( adresa.getTipArtera()) %>" >
                                    </td>
                                    <td>
                                        <input class="gsapinputtext" type="text" id="<%=AdresaConstants.NR+i%>"  name="<%=AdresaConstants.NR%>" value="<%=AppConstants.format( adresa.getNr())%>">
                                    </td>
                                    <td>
                                        <input class="gsapinputtext" type="text" size="5" name="<%=AdresaConstants.BLOC%>" value="<%=AppConstants.format( adresa.getBloc())%>"  >
                                    </td>
                                    <td>
                                        <a href="javascript: void(null)" class="link" onclick="del( <%=i%>); return false;">Delete</a>
                                    </td>
                                </tr>
                            <%}%>

                            <% for( int i = adrese.length; i < adrese.length + 50; i++){ %>
                                <tr style="display: none" id="tr<%=i%>">
                                    <input type="hidden" name="visiblex" value="0">
                                    <td>
                                        <input class="gsapinputtext" style="width: 200px" id="<%=AdresaConstants.LOCALITATE + i%>" name="<%=AdresaConstants.LOCALITATE%>" value="">
                                        &nbsp;&nbsp;j.<input class="gsapinputtext" style="width: 160px" readonly id="<%=AdresaConstants.JUDET + i%>" name="<%=AdresaConstants.JUDET%>" value="">
                                    </td>
                                    <td colspan="2">
                                        <input class="gsapinputtext" style="width: 160px" id="<%=AdresaConstants.LOCALITATE2 + i%>" name="<%=AdresaConstants.LOCALITATE2%>" value="">
                                    </td>
                                    <td>
                                        <input type="hidden" name="<%=AdresaConstants.COD_STRADA%>" id="<%=AdresaConstants.COD_STRADA%>"  value="">
                                        <input class="gsapinputtext" style="width: 200px" name="<%=AdresaConstants.DEN_STRADA%>" id="<%=AdresaConstants.DEN_STRADA + i%>" value="">
                                        <input class="gsapinputtext" style="width: 70px" type=text id="<%=AdresaConstants.TIP_ARTERA + i%>" name="<%=AdresaConstants.TIP_ARTERA%>" onchange="onChangeTipArtera( <%=i%>)" value="" >
                                    </td>
                                    <td>
                                        <input class="gsapinputtext" type="text" name="<%=AdresaConstants.NR%>" id="<%=AdresaConstants.NR+i%>"  value="">
                                    </td>
                                    <td>
                                        <input class="gsapinputtext" type="text" size="5" name="<%=AdresaConstants.BLOC%>" value=""  >
                                    </td>
                                    <td>
                                        <a href="javascript: void( null)" onclick="del( <%=i%>); return false;">Delete</a>
                                    </td>
                                </tr>
                            <% } %>
                        </table>
                    </td>
                </tr>
            </table>
        </td>
    </tr>
    <TR>
        <td></td>
        <TD valign=top >
            <input type="button" value="Salveaza" onclick="if( validare()) this.form.submit()" /> &nbsp;&nbsp;&nbsp;
            <input type="button" onclick="window.location = '<%=GlobalAppConstants.APP_NAME%>?action=showLucrari'" value="Renunta">
        </TD>
    </TR>
</FORM>
</TABLE>
</td></tr></TABLE>

<script type="text/javascript">

    var municipiuAjaxDataProvider = new AjaxDataProvider( function(){
        return "<%=GlobalAppConstants.APP_NAME%>?action=callcenter_cautMunicipiu&q=";
    });

    //tip artera sugest
    var tipArteraAjaxDataProvider = new AjaxDataProvider( function(){
        return "<%=GlobalAppConstants.APP_NAME%>?action=callcenter_cautTipArtera2&q=";
    });
    

    <% for(int i = 0; i < adrese.length + 50; i++) { %>
        //municipiu suggest

        new InputSugest( $("<%=AdresaConstants.LOCALITATE + i%>"), null, municipiuAjaxDataProvider, {
            onSelect : function( item){
                $("<%=AdresaConstants.LOCALITATE+ i%>").value = item.denMunicipiu;
                <%--$('<%=AdresaConstants.COD_LOCAL%>').value ='';--%>
                $('<%=AdresaConstants.LOCALITATE2+ i%>').value ='';
                $('<%=AdresaConstants.DEN_STRADA+ i%>').value ='';
                $('<%=AdresaConstants.TIP_ARTERA+ i%>').value ='';
    
                $('<%=AdresaConstants.JUDET+ i%>').value = item.denJudet;
    
                if( item.countCopii == 1 ){
                    //completam denumirea si codul localitatii data este doar una singura
                    $("<%=AdresaConstants.LOCALITATE2+ i%>").value = item.denCopil;

                    //setam focusul direct pe strada
                    $('<%=AdresaConstants.DEN_STRADA+ i%>').value = '';
                    $('<%=AdresaConstants.TIP_ARTERA+ i%>').value = '';
                    $('<%=AdresaConstants.DEN_STRADA+ i%>').focus();
                }else{
                    $('<%=AdresaConstants.LOCALITATE2+ i%>').focus();
                }
            }
        });

        //localitate sugest
        var localitate2AjaxDataProvider<%=i%> = new AjaxDataProvider( function(){
            return "<%=GlobalAppConstants.APP_NAME%>?action=callcenter_cautLocalitate22&mun=" + $("<%=AdresaConstants.LOCALITATE+i%>").value + "&q=";
        });

        new InputSugest( $("<%=AdresaConstants.LOCALITATE2+i%>"), null, localitate2AjaxDataProvider<%=i%>, {
            onSelect : function( item){
                $('<%=AdresaConstants.LOCALITATE2+i%>').value = item.denLocalitate;
                $('<%=AdresaConstants.LOCALITATE+i%>').value = item.denMunicipiu;
                $('<%=AdresaConstants.JUDET+i%>').value = item.denJudet;

                $('<%=AdresaConstants.DEN_STRADA+i%>').value = '';
                $('<%=AdresaConstants.TIP_ARTERA+i%>').value = '';
                $('<%=AdresaConstants.DEN_STRADA+i%>').focus();
            }
        });



        var tipArteraAjaxSugest = new InputSugest( $("<%=AdresaConstants.TIP_ARTERA+i%>"), null, tipArteraAjaxDataProvider, {
            onSelect : function( item){
                //item.key;
                //item.value;
                $('<%=AdresaConstants.DEN_STRADA+i%>').value ='';
                $('<%=AdresaConstants.DEN_STRADA+i%>').focus();
            }
        });


        //strada sugest
        var stradaAjaxDataProvider<%=i%> = new AjaxDataProvider( function(){
            var mun = $('<%=AdresaConstants.LOCALITATE+i%>').value;
            var loc = $('<%=AdresaConstants.LOCALITATE2+i%>').value;
            var tipArtera = $('<%=AdresaConstants.TIP_ARTERA+i%>').value;

            if( mun != null && loc != null && mun.trim().length > 0 && loc.trim().length > 0)
                return "<%=GlobalAppConstants.APP_NAME%>?action=callcenter_cautStrada2" +
                        "&mun="+ mun + "&loc="+ loc + "&tipArtera=" + tipArtera + "&q=";
            else return null;
        });

        new InputSugest( $("<%=AdresaConstants.DEN_STRADA+i%>"), null, stradaAjaxDataProvider<%=i%>, {
            onSelect : function( item){
                $('<%=AdresaConstants.TIP_ARTERA+i%>').value = item.tipArtera;
                $('<%=AdresaConstants.DEN_STRADA+i%>').value = item.denumire;
                
                $('<%=AdresaConstants.NR+i%>').value='';
                $('<%=AdresaConstants.NR+i%>').focus();

                //getStatusReclamatie();
            }
        });
    <% }%>


    function onChangeTipArtera( index){
        document.zaforma.<%=AdresaConstants.COD_STRADA%>[ index].value = '-1';
        document.zaforma.<%=AdresaConstants.DEN_STRADA%>[ index].value = '';
    }


    function adaugaAdresa(){
        try{
            var n = document.zaforma.visiblex.length;
            for( var i = n-1; i >= 0 ; i--){
                if( document.zaforma.visiblex[i].value=="1") break;
            }

            i++;
            document.zaforma.visiblex[i].value="1";
            Element.show("tr"+i);
        }catch(e){alert( e.message)}
    }

    function del(index){
        document.zaforma.visiblex[ index].value="0";

        document.zaforma.<%=AdresaConstants.LOCALITATE%>[ index].value = "";
        document.zaforma.<%=AdresaConstants.TIP_ARTERA%>[ index].value = "";
        document.zaforma.<%=AdresaConstants.COD_STRADA%>[ index].value = "";
        document.zaforma.<%=AdresaConstants.DEN_STRADA%>[ index].value = "";
        document.zaforma.<%=AdresaConstants.NR%>[ index].value = "";
        document.zaforma.<%=AdresaConstants.BLOC%>[ index].value = "";
        Element.hide("tr"+index);
    }

    function validare(){

        return true;
    }

</script>



