<%@ page import="ro.emid.callcenter.i10n.Configuration" %>
<%@ page import="ro.emid.callcenter.obj.LittleIncident" %>
<%@ page import="ro.emid.callcenter.obj.LittleIncidentConstants" %>
<%@ page import="ro.emid.callcenter.obj.binder.LittleIncidentBinder" %>
<%@ page import="ro.emid.callcenter.util.CallcenterHelper" %>
<%@ page import="ro.emid.common.action.browse.adrese.BrowseAdresaAction" %>
<%@ page import="ro.emid.common.components.sugest.base.SugestConstants" %>
<%@ page import="ro.emid.common.constants.AdresaConstants" %>
<%@ page import="ro.emid.common.constants.AppConstants" %>
<%@ page import="ro.emid.common.constants.WebConstants" %>
<%@ page import="ro.emid.common.types.Adresa" %>
<%@ page import="java.util.List" %>

<%@ taglib prefix="emid" tagdir="/WEB-INF/tags" %>

<%
    final LittleIncidentBinder binder = (LittleIncidentBinder) session.getAttribute(LittleIncidentConstants.BEAN);
    final LittleIncident bean = binder.getBean();

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

    Configuration configuration = CallcenterHelper.getConfiguration();
%>

<table cellpadding=2 cellspacing=0 width="100%" class="gSAPTHEAD">
<tr>
    <th class="gsapthead" colspan=2 height=25>Fișă Incident</th>
</tr>
<tr>
    <td>
        <emid:bind-messages messages="<%=(List)request.getAttribute(WebConstants.MESG_ERR)%>"/>
        <form name="zaforma" action="<%=AppConstants.APP_NAME%>?action=save-incident" method="post" >
            <input type="hidden" name="subaction" VALUE="" />
            <input type="hidden" name="<%=LittleIncidentConstants.ID%>" VALUE="<%=bean.getId()%>" />

            <table width="100%" class="ml10">
                <tr>
                    <td width="200">
                        <label for="<%=LittleIncidentConstants.NUME%>">
                            Denumire:
                        </label>
                    </td>
                    <td>
                        <input class="gsapinputtext" TYPE="TEXT" size="70" id="<%=LittleIncidentConstants.NUME%>" name="<%=LittleIncidentConstants.NUME%>" VALUE="<%=AppConstants.format( bean.getNume())%>"/>
                    </td>
                </tr>

                <tr>
                    <td valign=top>
                        Data producere:
                    </td>
                    <td valign="middle">
                        <emid:datetimefield name="<%=LittleIncidentConstants.DATA_ANUNTARE%>" width="120px" value="<%=bean.getDataAnuntare()%>" readonly="true" onchange="onDataAnuntareUpdate()" />
                    </td>
                </tr>
                <tr>
                    <td valign=top>
                        Data estimată remediere:
                    </td>
                    <td valign="middle">
                        <emid:datetimefield name="<%=LittleIncidentConstants.DATA_REZOLVARE%>" width="120px" value="<%=bean.getDataRezolvare()%>" readonly="true" onchange="onDataRezolvareUpdate()" />
                    </td>
                <tr>
            </table>

            <br/>
            <div class="pull-left">
                Localizare/Adresa (strazi afectate de incident):
                <a href="javascript: void( null)" class="link" onclick="adaugaAdresa(); return false;">Adaugă adresă</a>
                <%--&nbsp;|&nbsp;--%>
                <%--<a href="javascript: void( null)" class="link" onclick="adaugaAdreseMultiple(); return false;">Adauga adrese</a>--%>
            </div>
            <table width="100%" class="table">
                <thead>
                    <tr>
                        <th><%=configuration.getLabelLocalitate1()%> (Judet)</th>
                        <th colspan="2"><%=configuration.getLabelLocalitate2()%></th>
                        <th>Denumire arteră</th>
                        <th>Nr</th>
                        <th>Bloc</th>
                        <th>Op.</th>
                    </tr>
                </thead>
                <tbody>
                    <% 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 type="hidden" id="<%=AdresaConstants.COD_LOCALITATE + i%>" name="<%=AdresaConstants.COD_LOCALITATE%>" value="<%=AppConstants.format( adresa.getCodLocalitate())%>">
                                <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 type="hidden" id="<%=AdresaConstants.COD_LOCALITATE2 + i%>" name="<%=AdresaConstants.COD_LOCALITATE2%>" value="<%=AppConstants.format(adresa.getCodLocalitate2())%>">
                                <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;">Șterge</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 type="hidden" id="<%=AdresaConstants.COD_LOCALITATE + i%>" name="<%=AdresaConstants.COD_LOCALITATE%>" value="">
                                <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 type="hidden" id="<%=AdresaConstants.COD_LOCALITATE2 + i%>" name="<%=AdresaConstants.COD_LOCALITATE2%>" value="">
                                <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;" class="link">Șterge</a>
                            </td>
                        </tr>
                    <% } %>
                </tbody>
            </table>

            <div class="mt10">
                <input type="submit" value="Salvează" class="mr10" />
                <input type="button" value="Renunţă" onclick="window.location = '<%=AppConstants.APP_NAME%>?action=show-incidente'"/>
            </div>
        </form>
    </td>
</tr>
</table>


<script type="text/javascript">
    function getDate( inputId){
        var strDate = $( inputId).value;
        if( strDate.length == 0) return null;
        return Date.parseDate(strDate, "%d.%m.%Y %H:%M");
    }

    function onDataAnuntareUpdate(){
        var dataAnuntare  = getDate( '<%=LittleIncidentConstants.DATA_ANUNTARE%>');
        var dataRezolvare = getDate( '<%=LittleIncidentConstants.DATA_REZOLVARE%>');
        if( dataRezolvare == null || ( dataAnuntare != null && dataRezolvare != null && dataAnuntare < dataRezolvare)){

        } else {
            //not good
            $('<%=LittleIncidentConstants.DATA_ANUNTARE%>').value = '';
        }
    }

    function onDataRezolvareUpdate(){
        var dataAnuntare  = getDate( '<%=LittleIncidentConstants.DATA_ANUNTARE%>');
        var dataRezolvare = getDate( '<%=LittleIncidentConstants.DATA_REZOLVARE%>');

        if( dataAnuntare == null || ( dataAnuntare != null && dataRezolvare != null && dataAnuntare < dataRezolvare)){
            // OK, lasam completarea datei din calendar
        }else{
            //not good
            $('<%=LittleIncidentConstants.DATA_REZOLVARE%>').value = '';
        }
    }

    function initSuggesters(){
        var municipiuAjaxDataProvider = new AjaxDataProvider( function(){
            return "<%=AppConstants.APP_NAME%>?action=suggest-localitate&<%=SugestConstants.SUGEST_Q_QUERY%>=";
        });

        //tip artera sugest
        var tipArteraAjaxDataProvider = new AjaxDataProvider( function(){
            return "<%=AppConstants.APP_NAME%>?action=suggest-tip-artera&<%=SugestConstants.SUGEST_Q_QUERY%>=";
        });


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

            //municipiu sugest
            new InputSugest( $("<%=AdresaConstants.LOCALITATE + i%>"), $("<%=AdresaConstants.COD_LOCALITATE + i%>"), 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 if (item.countCopii == 0){
                        //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 "<%=AppConstants.APP_NAME%>?action=suggest-localitate2"
                        +"&mun=" + $("<%=AdresaConstants.LOCALITATE+i%>").value
                        +"&codMun=" + $("<%=AdresaConstants.COD_LOCALITATE+i%>").value
                        + "&<%=SugestConstants.SUGEST_Q_QUERY%>=";
            });

            new InputSugest( $("<%=AdresaConstants.LOCALITATE2+i%>"), $("<%=AdresaConstants.COD_LOCALITATE2+i%>"), 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 codMun = $('<%=AdresaConstants.COD_LOCALITATE + i%>').value;
                var mun = $('<%=AdresaConstants.LOCALITATE+i%>').value;
                var codLoc = $('<%=AdresaConstants.COD_LOCALITATE2 + i%>').value;
                var loc = $('<%=AdresaConstants.LOCALITATE2+i%>').value;
                var tipArtera = $('<%=AdresaConstants.TIP_ARTERA+i%>').value;

                if( mun != null && mun.trim().length > 0)
                    return "<%=AppConstants.APP_NAME%>?action=suggest-artera"
                            + "&codLocalitate=" + codMun
                            + "&localitate=" + mun
                            + "&codLocalitate2=" + codLoc
                            + "&localitate2=" + loc
                            + "&tipArtera=" + tipArtera
                            + "&<%=SugestConstants.SUGEST_Q_QUERY%>=";
                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)}
    }


    var browseGEOWindow = null;
    function browseGEO(){
        if( browseGEOWindow != null) browseGEOWindow.close();
        browseGEOWindow = window.open('<%=AppConstants.APP_NAME%>?action=callcenter_browseGeo&m=<%=BrowseAdresaAction.MODE_CHECK%>&<%=BrowseAdresaAction.P_JSCBF%>=onAdreseSelected()'
                , 'browseGeo', 'top=50,left=50,width=850 height=600,scrollbars=yes, status=yes,resizable=yes,dependent=yes,alwaysRaised=yes');
    }

    function adaugaAdreseMultiple(){
        browseGEO();
    }

    function onAdreseSelected( ){
        document.zaforma.subaction.value = "addAdrese";
        document.zaforma.submit();
    }

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

    setTimeout(function(){
        initSuggesters();
    }, 200);
</script>

