<%@ page import="org.apache.log4j.Logger"%>
<%@ page import="ro.emid.common.db.OpenSCGAccessDB" %>
<%@ page import="ro.emid.common.utils.ObjectUtils" %>
<%@ page import="ro.emid.common.components.sugest.base.SugestConstants" %>
<%@ page import="ro.emid.callcenter.CallcenterSessionUtils" %>
<%@ page import="ro.emid.common.types.Localitate" %>
<%@ page import="ro.emid.common.db.adrese.AdreseHelper" %>

<%
    final Logger LOG = Logger.getLogger("cautLocalitateResp_jsp");
    final String whatToSearch = ObjectUtils.getStringValue(request.getParameter("q"));

    final int beginIndex = ObjectUtils.getIntValue( request.getParameter( SugestConstants.SUGEST_Q_BEGIN_INDEX), 0);
    final int maxCount = ObjectUtils.getIntValue( request.getParameter( SugestConstants.SUGEST_Q_MAX_COUNT), 10);

    LOG.debug(" whatToSearch=" + whatToSearch);
    try {
        final String ids = CallcenterSessionUtils.getSucursala(session).getStrIdsDepartamenteOpen();
        final String cond = ids == null ? "" : " AND d.COD_DEPTO IN (" + ids + ") ";
//        final Localitate[] localitati = OpenSCGAccessDB.searchMunicipiu( whatToSearch, cond, beginIndex, maxCount);
        final Localitate[] localitati = AdreseHelper.getAdreseDao().searchLocalitate(whatToSearch, cond, beginIndex, maxCount);

//        final long totalCount = OpenSCGAccessDB.searchMunicipiuCount( whatToSearch, cond);
        final long totalCount = AdreseHelper.getAdreseDao().getCountLocalitate(whatToSearch, cond);
        LOG.debug("GASIT " + localitati.length + " MUNICIPII");
%>

<respmesg>
    {
        totalResultCount: <%=totalCount%>,
        list: [
            <% for (int i = 0; i < localitati.length; i++) {  %>
                <%=(i==0?"":",") %>
                {
                    key : "<%=localitati[i].getCodMunic()%>",
                    value : "<%=ro.emid.common.utils.Util.escapeToJs(localitati[i].getDenumireMunicipiu())%> - j. <%=ro.emid.common.utils.Util.escapeToJs(localitati[i].getDenumireJudet())%>",
                    denMunicipiu : "<%=ro.emid.common.utils.Util.escapeToJs(localitati[i].getDenumireMunicipiu())%>",
                    denJudet : "<%=ro.emid.common.utils.Util.escapeToJs(localitati[i].getDenumireJudet())%>",
                    countCopii : "<%=localitati[i].getCountCopii()%>",
                    codCopil : "<%=localitati[i].getCodCopil()%>",
                    denCopil : "<%=ro.emid.common.utils.Util.escapeToJs(localitati[i].getDenCopil())%>"
                }
            <% } %>
        ]
    }
</respmesg>

<%} catch (Throwable t) {
    LOG.error("Eroare get lista municipii", t);
}%>