<%@ page import="ro.emid.callcenter.obj.AlocareLocalitateCentru" %>
<%@ page import="ro.emid.callcenter.util.CallcenterHelper" %>
<%@ page import="ro.emid.common.constants.ReclamatieConstants" %>
<%@ page import="ro.emid.common.types.Localitate" %>
<%@ page import="ro.emid.common.utils.ObjectUtils" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.Map" %>

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


<%
    /**
     * @see ro.emid.callcenter.action.localitatice.EditAlocareAction
     * @see SaveAlocareAction
     */

    AlocareLocalitateCentru alocare = (AlocareLocalitateCentru) request.getAttribute("alocare");

    final Category[] sucursale = (Category[]) request.getAttribute("sucursale");
    final Map<Category, Category[]> centreMap = (Map<Category, Category[]>) request.getAttribute("centre");
    final Localitate[] judete = (Localitate[]) request.getAttribute("judete");
    final List<String> errorMessages = (List<String>) request.getAttribute("_errorMessage");
%>

<div class="spacer"></div>
<table width="100%" border="0" cellpadding="0" cellspacing="0">
    <tr>
        <td  width="180" valign="top">
            <%@ include file="menu.jsp"%>
        </td>
        <td class="pl5">
            <b>Alocare localitate la Centru Exploatare</b>
            <div class="spacer"></div>

            <%@ include file="../../layout/messages.jspf"%>

            <% if( errorMessages != null && errorMessages.size() > 0){ %>
                <div style="color: red">
                    <% for (String em : errorMessages) { %>
                    <%=em%><br/>
                    <% } %>
                </div>
            <% } %>

        <form action="<%=AppConstants.APP_NAME%>?action=save-alocare-localitate-ce" method="post">
            <input type="hidden" name="id" value="<%=alocare.getId()%>"/>
            <input type="hidden" name="referer" value="${referer}"/>
            <table>
                <tr>
                    <td>
                        <label for="<%=ReclamatieConstants.MUNICIPIU%>">
                            <%=CallcenterHelper.getConfiguration().getLabelLocalitate1()%>:
                        </label>
                    </td>
                    <td>
                        <input type="hidden" id="<%=ReclamatieConstants.COD_MUNICIPIU%>" name="<%=ReclamatieConstants.COD_MUNICIPIU%>" value="<%=ObjectUtils.getLongValue( request.getParameter(ReclamatieConstants.COD_MUNICIPIU), -1)%>">
                        <input style="width: 100%" type="text" id="<%=ReclamatieConstants.MUNICIPIU%>" name="<%=ReclamatieConstants.MUNICIPIU%>" value="<%=ObjectUtils.getStringValue( alocare.getMunicipiu())%>" sugestWidth="250">
                    </td>
                </tr>
                <tr>
                    <td>
                        <label for="<%=ReclamatieConstants.LOCALITATE2%>">
                            <%=CallcenterHelper.getConfiguration().getLabelLocalitate2()%>:
                        </label>
                    </td>
                    <td>
                        <input type="hidden" id="<%=ReclamatieConstants.COD_LOCAL%>" name="<%=ReclamatieConstants.COD_LOCAL%>" value="<%=ObjectUtils.getLongValue( request.getParameter( ReclamatieConstants.COD_LOCAL), -1)%>" >
                        <input style="width: 100%" type=text id="<%=ReclamatieConstants.LOCALITATE2%>" name="<%=ReclamatieConstants.LOCALITATE2%>" value="<%=ObjectUtils.getStringValue( alocare.getLocalitate())%>" sugestWidth="200" >
                    </td>
                </tr>
                <tr>
                    <td>
                        <label for="<%=ReclamatieConstants.JUDET%>">Judeţ:</label>
                    </td>
                    <td>
                        <select id="<%=ReclamatieConstants.JUDET%>" name="<%=ReclamatieConstants.JUDET%>" style="width: 100%">
                            <option value="-1">Selectaţi:</option>
                            <% for (Localitate localitate : judete) { %>
                                <option value="<%=localitate.getDenumireJudet()%>" <%=localitate.getDenumireJudet().equalsIgnoreCase(alocare.getJudet()) ? "selected" : ""%>><%=localitate.getDenumireJudet()%></option>
                            <% } %>
                        </select>
                    </td>
                </tr>
                <tr>
                    <td>
                        <label for="idCentru">
                            La Centrul Exploatare:
                        </label>
                    </td>
                    <td>
                        <select id="idCentru" name="idCentru">
                            <option value="-1">Selectaţi:</option>
                            <% for (final Category suc : sucursale) { %>
                            <optgroup label="<%=ObjectUtils.getStringValue(suc.getCodSap())%> - <%=ObjectUtils.getStringValue(suc.getName())%>">
                                <% final Category[] centre = centreMap.get(suc); %>
                                <% for (final Category c : centre) { %>
                                    <option <%=c.getId() == alocare.getIdCentru() ? "selected" : ""%> value="<%=c.getId()%>">
                                        <%=c.getCodSap()%> - <%=ObjectUtils.getStringValue(c.getName())%>
                                    </option>
                                <%}%>
                            </optgroup>
                            <% } %>
                        </select>
                    </td>
                </tr>
                <tr>
                    <td>&nbsp;</td>
                </tr>
                <tr>
                    <td colspan="2">
                        <input type="submit" value="Salvează alocare" />
                        <input type="button" value="Renunță" data-href="${referer}" />
                    </td>
                </tr>
            </table>
        </form>
    </td>
</tr>
</table>

<script type="text/javascript">
    //municipiu sugest
    var municipiuAjaxDataProvider = new AjaxDataProvider( function(){
        return "<%=AppConstants.APP_NAME%>?action=callcenter_cautMunicipiu&q=";
    });

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

    function initSugests(){
        new InputSugest( $("<%=ReclamatieConstants.MUNICIPIU%>"), $("<%=ReclamatieConstants.COD_MUNICIPIU%>"), municipiuAjaxDataProvider, {
            onSelect : function( item){
                $("<%=ReclamatieConstants.MUNICIPIU%>").value = item.denMunicipiu;
                $('<%=ReclamatieConstants.COD_LOCAL%>').value ='';
                $('<%=ReclamatieConstants.LOCALITATE2%>').value ='';
                $('<%=ReclamatieConstants.LOCALITATE2%>').focus();
                $('<%=ReclamatieConstants.JUDET%>').value = item.denJudet;
            }
        });

        new InputSugest( $("<%=ReclamatieConstants.LOCALITATE2%>"), $("<%=ReclamatieConstants.COD_LOCAL%>"), localitate2AjaxDataProvider, {
            onSelect : function( item){
                $('<%=ReclamatieConstants.LOCALITATE2%>').value = item.denLocalitate;
                $('<%=ReclamatieConstants.COD_MUNICIPIU%>').value = item.codMunicipiu;
                $('<%=ReclamatieConstants.MUNICIPIU%>').value = item.denMunicipiu;
                $('<%=ReclamatieConstants.JUDET%>').value = item.denJudet;
            }
        });
    }

    setTimeout("initSugests()", 1000);
</script>

