<%@ page import="ro.emid.common.constants.ReclamatieConstants" %>
<%@ page import="ro.emid.common.types.ConsultareClient" %>
<%@ page import="ro.emid.common.constants.AppConstants" %>
<%@ page import="ro.emid.callcenter.i10n.Configuration" %>
<%@ page import="ro.emid.common.utils.StringUtils" %>
<%@ page import="java.util.List" %>

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

<%
        /**
         * @see ro.emid.callcenter.action.ConsultareClientAction
         */

    final boolean byIdentifier = request.getParameter("ByNLC") != null;
    final String strNlc = (String) request.getAttribute("strNLC");
    final String adresa = (String) request.getAttribute("adresa");
    final Configuration.SearchByIdLocConsumConfig[] searchByIdLocConsumConfig = (Configuration.SearchByIdLocConsumConfig[]) request.getAttribute("searchByIdLocConsumConfig");
    final List<ConsultareClient> clienti = (List<ConsultareClient>) request.getAttribute("clienti");
%>

<TABLE align=center border=0 cellPadding=5 cellSpacing=0 width="100%" style="position: absolute; height: 100%;">
<tr>
<td align="left" valign="top" style="height:10px">
    <fieldset>
        <% final String text = byIdentifier ? "NLC" : "Adresa/Clienti"; %>
        <% final String text2 = byIdentifier ? "Client:" : "Clienti corespunzatori adresei:"; %>
        <legend><b>&nbsp;<%=text%></b></legend>
        <table width="100%" border="0" height="100%">
            <tr style="height: 10px;"><td>
                <% if( byIdentifier){%>
                <form action="" method="post">
                    <input type="hidden" name="ByNLC" value="true" >
                    <% for (Configuration.SearchByIdLocConsumConfig byIdLocConsumConfig : searchByIdLocConsumConfig) { %>
                        <label>
                            <%=byIdLocConsumConfig.getLabel()%> : <input type="text" size="40"
                                                                         name="identifier-<%=byIdLocConsumConfig.getCode()%>"
                                                                         value="<%=StringUtils.getNullSafe(request.getParameter("identifier-"+byIdLocConsumConfig.getCode()))%>"/>
                        </label>
                    <% } %>

                    <%=strNlc != null && strNlc.trim().length() > 0 && clienti.size() == 0 ? "Nici-un rezultat ..." : ""%>

                    <button type="submit" id="buttonCautaClient">Caută</button>
                </form>
                <%}else{%>
                    Adresa: <input disabled size="80" value="<%=AppConstants.format( adresa)%>">
                <%}%>
            </td></tr>
            <tr>
                <td valign="top" >
                    <%=text2%>
                    <% final int selectedRow = -1;%>a
                    <div style="overflow: auto; height: 250px">

                        <TABLE border=0 cellPadding=2 cellSpacing=1 width="100%" class="gSAPTHEAD" style="BACKGROUND-COLOR: white; ">
                            <TR>
                                <TH class="gsapthead"  height=25 noWrap>&nbsp;Nume Client:&nbsp;</TH>
                                <TH class="gsapthead" noWrap>&nbsp;Adresa:&nbsp;</TH>
                                <TH class="gsapthead" noWrap>&nbsp;POD:&nbsp;</TH>
                                <TH class="gsapthead" noWrap>&nbsp;NLC Client:&nbsp;</TH>
                                <TH class="gsapthead" noWrap>&nbsp;Tensiune:&nbsp;</TH>
                                <TH class="gsapthead" noWrap>&nbsp;Client:&nbsp;</TH>
                                <TH class="gsapthead" noWrap>&nbsp;Stare instalatie:&nbsp;</TH>
                            </TR>
                            <% int i = 0; %>
                            <% for (final ConsultareClient client : clienti) { %>
                                <tr id="tr<%=client.hashCode()%>" onclick="selectRow(this, <%=i%>, <%=client.getCodClient()%>, '<%=ro.emid.common.utils.Util.escapeToJs(client.getNlc())%>', '<%=ro.emid.common.utils.Util.escapeToJs(client.getPOD())%>')" style="cursor: pointer;" duplicador="<%=client.getDuplicador()%>" cgv="<%=client.getCgv()%>">
                                    <td nowrap><%=client.getNumeClient()%></td>
                                    <td><%=client.getAdresaCompleta()%></td>
                                    <td><%=AppConstants.format(client.getPOD())%></td>
                                    <td><%=AppConstants.format(client.getNlc())%></td>
                                    <td><%=AppConstants.format(client.getTensiune())%></td>
                                    <td><%=AppConstants.format(client.getTipClient())%></td>
                                    <td id="stare-instalatie-<%=client.getPOD()%>" data-stare-instalatie-loaded="<%=!StringUtils.isZeroLength(client.getStareInstalatie())%>">
                                        <%if(StringUtils.isZeroLength(client.getStareInstalatie())){%>
                                            Click pentru afişare
                                        <%}else{%>
                                            <%=client.getStareInstalatie()%>
                                        <%}%>
                                    </td>
                                </tr>
                            <% i++;}%>
                        </table>

                    </div>
                </td>
            </tr>
            <tr style="height: 30px">
                <td align="right">
                    <button disabled id="buttonCompleteazaAdresa" onclick="completeazaAdresa()">Completează adresa client</button>
                    &nbsp;&nbsp;&nbsp;
                    <button onclick="window.close()">Închide fereastra</button>
                </td>
            </tr>            
        </table>
    </fieldset>
</td>
</tr>
<tr>
    <td valign="top">
        <fieldset>
            <legend>Ordine de serviciu</legend>
            <div id="divOrdineServiciu" style="overflow: auto; height: 100%">

            </div>
        </fieldset>
    </td>
</tr>
</TABLE>

<span id="waitPopup" style="position: absolute; left: 0; top: 0; font-size: 12px; background-color: #ff6666; color: black; display: none">
    Va rugam asteptati ... 
</span>

<script type="text/javascript">
    var selectedRowIndex = <%=selectedRow%>;
    var selectedClientCode = -1;

    function completeazaAdresa(){
        var arrFunctiiCompletare = new Array();
        <% i = 0;%>
        <% for (final ConsultareClient client : clienti) { %>
            arrFunctiiCompletare[<%=i%>] = function(){
                opener.completeazaAdresa(
                        "<%=ro.emid.common.utils.Util.escapeToJs( AppConstants.format(  client.getJudet()))%>",
                        "<%=ro.emid.common.utils.Util.escapeToJs( AppConstants.format(  client.getCodMunicipiu()))%>",
                        "<%=ro.emid.common.utils.Util.escapeToJs( AppConstants.format(  client.getMunicipiu()))%>",
                        "<%=ro.emid.common.utils.Util.escapeToJs( AppConstants.format(  client.getCodLocalitate()))%>",
                        "<%=ro.emid.common.utils.Util.escapeToJs( AppConstants.format(  client.getLocalitate()))%>",
                        "<%=ro.emid.common.utils.Util.escapeToJs( AppConstants.format(  client.getTipArtera()))%>",
                        "<%=ro.emid.common.utils.Util.escapeToJs( AppConstants.format(  client.getDenStrada()))%>",
                        "<%=ro.emid.common.utils.Util.escapeToJs( AppConstants.format(  client.getNr()))%>",
                        "<%=ro.emid.common.utils.Util.escapeToJs( AppConstants.format(  client.getBloc()))%>",
                        "<%=ro.emid.common.utils.Util.escapeToJs( AppConstants.format(  client.getScara()))%>",
                        "<%=ro.emid.common.utils.Util.escapeToJs( AppConstants.format(  client.getEtaj()))%>",
                        "<%=ro.emid.common.utils.Util.escapeToJs( AppConstants.format(  client.getAp()))%>",
                        "<%=ro.emid.common.utils.Util.escapeToJs( client.getNlc())%>",
                        isClientEligibil
                );
                opener.document.zaforma.<%=ReclamatieConstants.NUME_CLIENT%>.value="<%=ro.emid.common.utils.Util.escapeToJs( client.getNumeClient())%>";
                window.close();
            };
        <% i++; }%>

        if( selectedRowIndex >=0 && arrFunctiiCompletare.length > 0){
            arrFunctiiCompletare[ selectedRowIndex]();
        }
    }


    var selectedRow = null;
    function selectRow(row, rowIndex, codClient, NLC, POD){
//        if(selectedRowIndex != -1) {
//            $('tr'+selectedRowIndex).className = "";
//        }
//        selectedRowIndex = rowIndex;
//        $('tr' + rowIndex).className = "checked";

        if(selectedRow){
            selectedRow.className = "";
        }
        selectedRowIndex = rowIndex;
        selectedRow = row;
        selectedRow.className = "checked";


        selectedClientCode = codClient;

        getOrdineServiciu( POD);

        getStareInstalatie( POD);

        getIsEligibil(NLC);
    }

    function getStareInstalatie(POD){
        var loaded = jQuery("#stare-instalatie-"+POD).data("stare-instalatie-loaded");
        if(!loaded || "false" == loaded){
            var ajaxOrdine = new Ajax.Request('<%=AppConstants.APP_NAME%>?action=get-stare-instalatie', {
                method: 'post',
                parameters: 'POD='+POD,
                onComplete: function(originalRequest){
                    var response = getResponseMesg( originalRequest.responseText);
                    if( response != null) {
                        jQuery("#stare-instalatie-"+POD)
                                .html(response)
                                .data("stare-instalatie-loaded", "true");
                    }
                }
            });
        }
    }

    function getOrdineServiciu( identifier){
        if( selectedRowIndex == -1){
            alert( 'Selectati un client !');
            return;
        }

        var ajaxOrdine = new Ajax.Request(
                    '<%=AppConstants.APP_NAME%>?action=callcenter_showOrdineServiciu&NLC='+identifier,
                    {
                        method: 'post',
                        parameters: '',
                        onComplete: onGetOrdineServiciu,
                        evalScripts: true
                    });
    }

    var tableOrdine = null;
    function onGetOrdineServiciu( originalRequest){
        var response = getResponseMesg( originalRequest.responseText);
        if( response != null){
            $('divOrdineServiciu').innerHTML = response;
            if( $('ordine')){
                tableOrdine = new Table('ordine', "tableOrdine");
            }
        }
    }

    function getFacturi( NLC){
        if( selectedRowIndex == -1){
            alert( 'Selectati un client !');
            return;
        }

        var ajaxFacturi = new Ajax.Request(
                    '<%=AppConstants.APP_NAME%>?action=callcenter_showFacturiNeplatite&codClient='+selectedClientCode + "&NLC="+NLC,
                    {
                        method: 'post',
                        parameters: '',
                        onComplete: onGetFacturi,
                        evalScripts: true
                    });
    }

    var tableFacturi = null;
    function onGetFacturi( originalRequest){
        var response = getResponseMesg( originalRequest.responseText);
        if( response != null){
            $('divFacturiNeplatite').innerHTML = response;
            if( $('facturi')){
                tableFacturi = new Table('facturi', "tableFacturi");
            }
        }
    }

    function getIsEligibil(NLC){
        if( selectedRowIndex == -1){
            alert( 'Selectati un client !');
            return;
        }

        var ajaxIsEligibil = new Ajax.Request(
                '<%=AppConstants.APP_NAME%>?action=callcenter_cautISClientEligibil&NLC='+NLC,
                {
                    method: 'post',
                    parameters: '',
                    onComplete: onGetIsEligibil,
                    evalScripts: true
                });

    }

    var isClientEligibil = false;
    function onGetIsEligibil(originalRequest){
        var response = getResponseMesg( originalRequest.responseText);
        //alert(">>>"+response+">>>");
        //alert( "false" == response);

        isClientEligibil = "true" == response;
    }

    var myGlobalHandlers = {
        onCreate: function(){

            Element.show( 'waitPopup');
            $('buttonCompleteazaAdresa').disabled = true;
        },

        onComplete: function() {
            if(Ajax.activeRequestCount == 0){
                Element.hide( 'waitPopup');

                $('buttonCompleteazaAdresa').disabled = false;
            }
        }
    };

    Ajax.Responders.register( myGlobalHandlers);
</script>

