<%@ page import="ro.emid.callcenter.obj.AlocareLocalitateCentru" %>
<%@ page import="ro.emid.callcenter.util.CallcenterHelper" %>
<%@ page import="ro.emid.common.utils.pagination.PaginationController" %>
<%@ page import="ro.emid.common.utils.pagination.PaginationData" %>
<%@ page import="ro.emid.common.utils.sort.SortController" %>
<%@ page import="ro.emid.common.utils.sort.SortData" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.Map" %>
<%@ page import="ro.emid.common.action.Action" %>
<%@ page import="ro.emid.common.utils.*" %>


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


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

    final String search = RequestUtils.getString(request, "search");

    final AlocareLocalitateCentru[] alocari = (AlocareLocalitateCentru[]) request.getAttribute("items");

    final SortData sd = (SortData) request.getAttribute("sd");
    final PaginationData pd = (PaginationData) request.getAttribute("pd");

    final Category[] sucursale = (Category[]) request.getAttribute("sucursale");
    final Map<Category, Category[]> centreMap = (Map<Category, Category[]>) request.getAttribute("centre");

    final long selectedCentru = ObjectUtils.getLongValue(request.getParameter("idCentru"), -1);
    final List<String> errorMessages = (List<String>) session.getAttribute(Action.KEY_ERROR_MESSAGES);
    if( errorMessages != null){
        session.removeAttribute(Action.KEY_ERROR_MESSAGES);
    }
%>

<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">
            <%@ 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%>" method="get" id="ze-form">
                <input type="hidden" name="action" id="ze-form-action" value="localitati-nealocate" />
                <%=SortController.initSort(sd)%>

                <div class="pull-left mb5">
                    <label>
                        Căutare: <input type="text" name="search" id="search" value="<%=search%>"/>
                    </label>
                    <input type="submit" value="Caută"/>
                    <input type="button" value="Reset filtru" id="reset-filtru"/>
                </div>

                <table id="table-adrese" cellpadding=2 cellspacing=1 width="100%" class="table">
                    <thead>
                        <tr>
                            <th width="50" >&nbsp;</th>
                            <th>
                                <%=SortController.printSortableColumn("Judet", "j.DENUMIRE", sd)%>
                            </th>
                            <th>
                                <%=SortController.printSortableColumn(CallcenterHelper.getConfiguration().getLabelLocalitate1(), "l.DENUMIRE", sd)%>
                            </th>
                            <th>
                                <%=SortController.printSortableColumn(CallcenterHelper.getConfiguration().getLabelLocalitate2(), "s.DENUMIRE", sd)%>
                            </th>
                        </tr>
                    </thead>
                    <tbody>
                        <% if( alocari == null || alocari.length == 0 ){ %>
                            <tr>
                                <td colspan="5" class="no-data">Nu exista date ... </td>
                            </tr>
                        <% } else for (int i = 0; i < alocari.length; i++) {
                            final AlocareLocalitateCentru alocare = alocari[i];

                            String str = alocare.getJudet() + '|' + StringUtils.getNullSafe(alocare.getMunicipiu()) + '|' + StringUtils.getNullSafe(alocare.getLocalitate());
                        %>

                            <tr data-index="<%=i%>">
                                <td align="center">
                                    <input type="checkbox" name="adrese" id="check<%=i%>" value="<%=str%>"  />
                                </td>
                                <td><%=AppConstants.format(alocare.getJudet())%></td>
                                <td><%=AppConstants.format(alocare.getMunicipiu())%></td>
                                <td><%=AppConstants.format(alocare.getLocalitate())%></td>
                            </tr>
                        <%}%>
                    </tbody>
                    <tfoot>
                        <tr><td colspan="4" valign="middle">
                            <div style="float: left; margin: 2px;">
                                <label for="idCentru">Alocă localitățile selectate la centrul:</label>

                                <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() == selectedCentru ? "selected" : ""%> value="<%=c.getId()%>">
                                            <%=c.getCodSap()%> - <%=ObjectUtils.getStringValue(c.getName())%>
                                        </option>
                                        <%}%>
                                    </optgroup>
                                    <% } %>
                                </select>
                                <input type="button" id="btn-aloca" value="Alocă" />

                            </div>
                            <div style="float: right">
                                <%=PaginationController.printPaginationData(pd)%>
                            </div>
                        </td></tr>
                    </tfoot>
                </table>
            </form>
        </td>
    </tr>
</table>

<script type="text/javascript">
    jQuery(function(){
        jQuery("#reset-filtru").click(function(){
            jQuery("#search").val("");
            jQuery("#ze-form").submit();
        });

        jQuery("#table-adrese tbody tr").click(function(){
            var row = this;
            var index = row.getAttribute("data-index");
            if( jQuery(row).hasClass("selected")){
                row.className = "";
                $("check"+index).checked = false;
            }else{
                row.className = 'selected';
                $("check"+index).checked = true;
            }
        });

        jQuery("#btn-aloca").click(function(){

            jQuery("#ze-form-action").val("alocare-localitati-centru");
            jQuery("#ze-form").submit();

        });
    });
</script>


