<%@ page import="ro.emid.common.action.dispecer.JSP_OD" %>
<%@ page import="ro.emid.common.constants.AppConstants" %>
<%@ page import="ro.emid.common.constants.RoleConstants" %>
<%@ page import="ro.emid.common.types.CategoryOD" %>
<%@ page import="ro.emid.common.types.User" %>
<%@ page import="ro.emid.common.utils.SessionUtils" %>
<%@ page import="java.util.Date" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.Map" %>

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


<%
    User user = SessionUtils.getUserLogat(session);

    /** @see ro.emid.lpod.action.dispecer.LoadStructuraOdEvenimenteAction */
    final List filiale = (List) request.getAttribute(JSP_OD.RECORDS);

    final Map<Long, Map<String, Object>> statistics = (Map<Long, Map<String, Object>>) request.getAttribute("statistics");
%>

<%!
    private long getCount(Map<Long, Map<String, Object>> statistics, CategoryOD categoryOD) {
        Map<String, Object> categoryStatistics = statistics.get(categoryOD.getId());
        if (categoryStatistics == null) return 0;

        Long count = (Long) categoryStatistics.get("count");
        if (count == null) return 0;

        return count;
    }

    private Date getDate(Map<Long, Map<String, Object>> statistics, CategoryOD categoryOD) {
        Map<String, Object> categoryStatistics = statistics.get(categoryOD.getId());
        if (categoryStatistics == null) return null;

        Date date = (Date) categoryStatistics.get("lastDate");
        if (date == null) return null;

        return date;
    }
%>

<emid:template-od title="Structura dispeceri">
    <jsp:attribute name="breadcrumb">
        <emid:localizare-od/>
    </jsp:attribute>
    <jsp:attribute name="body">

        <style type="text/css">
            .filiala {
                background-color: #CACADF;
                height: 30px;
            }

            .sucursala {
                /*background-color: #D9D9E8;*/
                background-color: #E9EEF4;
                height: 30px;
            }

            .centru {
                background-color: #f7faff;
                height: 30px;
            }

        </style>

        <table cellpadding=2 cellspacing=1 width="100%" class="gSAPTHEAD" style="background-color: white">
            <tr>
                <th class="gsapthead" colSpan=2 height=25 noWrap>Structura dispeceri:</th>
                <th class="gsapthead" nowrap width=120>Număr evenimente</th>
                <th class="gsapthead" nowrap width=200>Data ultimei editări</th>
            </tr>
            <%
                long total = 0;
                if (null == filiale || filiale.size() == 0) {%>
            <TR>
                <TD>
                    Nu sunt filiale definite ...
                </TD>
            </TR>
            <%
            } else {
                long itemCount = 0;
                final int nivelOD = user.getOdLevel();
                final String idCategOD = "" + user.getOdCategoryId();
                for (int i = 0; i < filiale.size(); i++) {
                    final CategoryOD itemFiliala = (CategoryOD) filiale.get(i);

                    final List sucursale = itemFiliala.getChildren();
            %>

            <% if (AppConstants.runAtAparatCentral()) { %>
            <tr class="filiala">
                <td></td>
                <td style="padding-left:2px">
                    <A title="Setare filiala curenta <%=itemFiliala.getName()%>" class="link"
                       href="<%=AppConstants.APP_NAME%>?action=setLocatieOD&idFiliala=<%=itemFiliala.getId()%>&redirect=structura-od-evenimente">
                        <%=itemFiliala.getName()%>
                    </A>&nbsp;

                </td>
                <td align="center">
                                <SPAN class=gensmall>
                                    <%= itemCount = getCount(statistics, itemFiliala)%>
                                </SPAN>
                </td>
                <td align="center">
                    <%=AppConstants.formatZiSaptaminaData(getDate(statistics, itemFiliala))%>
                </td>
            </tr>

            <% } %>

            <% if (sucursale != null && sucursale.size() > 0) for (int j = 0; j < sucursale.size(); j++) {
                final CategoryOD itemSucursala = (CategoryOD) sucursale.get(j);

            %>
            <tr class="sucursala">
                <td></td>
                <td style="padding-left:10px">

                    <% if (nivelOD == RoleConstants.NIVEL_FULL || nivelOD <= RoleConstants.NIVEL_DED && (String.valueOf(itemSucursala.getId())).indexOf(idCategOD) == 0) { %>
                    <A title="Lista operatiuni dispecer <%=itemSucursala.getName()%>" class="link"
                       href="<%=AppConstants.APP_NAME%>?action=setLocatieOD&idFiliala=<%=itemFiliala.getId()%>&idSucursala=<%=itemSucursala.getId()%>&redirect=lista-evenimente-dispecer">
                        <%=itemSucursala.getName()%>
                    </A>&nbsp;
                    <% } else { %>
                                    <span>
                                        <%=itemSucursala.getName()%>
                                    </span>
                    <% } %>

                </td>
                <td align="center">
                                <SPAN class=gensmall>
                                    <%= itemCount = getCount(statistics, itemSucursala)%>
                                    <% total += itemCount; %>
                                </SPAN>
                </td>
                <td align="center">
                    <%=AppConstants.formatZiSaptaminaData(getDate(statistics, itemSucursala))%>
                </td>
            </tr>

            <%
                final List centre = itemSucursala.getChildren();
                if (centre != null && centre.size() > 0) for (int k = 0; k < centre.size(); k++) {
                    final CategoryOD itemCentru = (CategoryOD) centre.get(k);

            %>
            <tr class="centru">
                <td></td>
                <td style="padding-left:30px">

                    <% if (nivelOD == RoleConstants.NIVEL_FULL || nivelOD <= RoleConstants.NIVEL_DED_L && (String.valueOf(itemCentru.getId())).indexOf(idCategOD) == 0) { %>
                    <A title="Lista operatiuni dispecer <%=itemCentru.getName()%>" class="link"
                       href="<%=AppConstants.APP_NAME%>?action=setLocatieOD&idFiliala=<%=itemFiliala.getId()%>&idSucursala=<%=itemSucursala.getId()%>&idCentru=<%=itemCentru.getId()%>&redirect=lista-evenimente-dispecer">
                        <%=itemCentru.getName()%>
                    </A>&nbsp;
                    <% } else { %>
                                            <span>
                                                <%=itemCentru.getName()%>
                                            </span>
                    <% } %>
                </td>
                <td align=center>
                                        <SPAN class=gensmall>
                                            <%= itemCount = getCount(statistics, itemCentru)%>
                                            <% total += itemCount; %>
                                        </SPAN>
                </td>
                <td align=center>
                    <%=AppConstants.formatZiSaptaminaData(getDate(statistics, itemCentru))%>
                </td>
            </tr>
            <%
                    }
            %>

            <%
                    }
            %>
            <%}%>
            <tr style="font-weight:bold;">
                <td class="gsapthead" colspan="2" align="right">&nbsp;Total:&nbsp;</td>
                <td class="gsapthead" align="center"><%=total%>
                </td>
                <td class="gsapthead">&nbsp;</td>
            </tr>
            <%}%>
        </table>

    </jsp:attribute>
</emid:template-od>

