<%@ page import="ro.emid.common.constants.AppConstants" %>
<%@ page import="ro.emid.common.constants.GlobalAppConstants" %>
<%@ page import="ro.emid.common.db.AccessDB" %>
<%@ page import="ro.emid.common.db.CategoryAccessDB" %>
<%@ page import="ro.emid.common.db.LucrareProgramataAccessDB" %>
<%@ page import="ro.emid.common.db.framework.DatabaseException" %>
<%@ page import="ro.emid.common.types.Category" %>
<%@ page import="ro.emid.common.types.LucrareProgramata" %>
<%@ page import="ro.emid.common.utils.SessionUtils" %>
<%@ page import="ro.emid.lpod.LpodSessionUtils" %>
<%@ page import="java.sql.Timestamp" %>

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

<%
    LpodSessionUtils.removeFiliala(session);
    LpodSessionUtils.removeSucursala(session);
    LpodSessionUtils.removeCentru(session);
    LpodSessionUtils.removeFormatia(session);

    final String _currentApp = SessionUtils.getCurrentLpodApp(session);
%>


<emid:template-lp title="Filiale">
    <jsp:attribute name="breadcrumb">
        <emid:localizare-lp/>
    </jsp:attribute>
    <jsp:attribute name="body">
        <TABLE border=0 cellPadding=2 cellSpacing=1 width="100%" class="gSAPTHEAD" style="BACKGROUND-COLOR: white">
            <TR>
                <TH class="gsapthead" colSpan=2 height=25 noWrap>Filiale Electrica:</TH>
                <TH class="gsapthead" noWrap width=80><%=getNumarHeader(LpodSessionUtils.getCurrentLpodApp(session))%>

                </TH>
                <TH class="gsapthead" noWrap width=150>Data ultimei editari</TH>
            </TR>

            <% final Category[] filiale = CategoryAccessDB.getAllCategoriesByParentId(-1);
                long total = 0;
                if (null == filiale) {%>
            <TR>
                <TD>
                    No Sunt filiale definite ...
                </TD>
            </TR>
            <%
            } else {

                for (int i = 0; i < filiale.length; i++) { %>
            <TR class="row<%=i%2%>">
                <TD height=50 align="center" valign="middle">
                    <IMG height=25 src="<%= AppConstants.WEB_RESOURCE_PATH + "/images/folder_big.gif" %>"
                         width=46>
                </TD>
                <TD height=50 width="100%">
                    <%=AppConstants.format(filiale[i].getCodSap())%>&nbsp;<A title="Lista sucursale <%=filiale[i].getName()%>"
                                                                             class="link"
                                                                             href="<%=GlobalAppConstants.APP_NAME%>?action=showSucursale&filialaId=<%=filiale[i].getId()%>"><%=filiale[i].getName()%>

                </A>&nbsp;
                    <%=(filiale[i].getDescription() == null) ? "" : '-' + filiale[i].getDescription()%><BR>
                            <SPAN style="font-size: 10px;"><%
                                final Category[] sucs = CategoryAccessDB.getAllCategoriesByParentId(filiale[i].getId());
                                if (sucs != null) for (int j = 0; j < sucs.length; j++) {%>
                                    <%=sucs[j].getName() + ", "%>
                                <%}%>
                            </span>
                </TD>

                <TD height=50 valign="middle" align="center">
                    <%
                        final long nrItems = getNumarOfItems(_currentApp, filiale[i].getId()); %>

                    <% total += nrItems; %>
                    <SPAN class=gensmall><%=nrItems%></SPAN>
                </TD>
                <TD height=50 noWrap valign="middle" align="center">
                        <SPAN class=gensmall>

                            <%=getDataLastEdit(_currentApp, filiale[i].getId())%>
                       </SPAN>
                </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-lp>



<%! private String getNumarHeader(final String app) {

    if (app.equals(GlobalAppConstants.APP_LUCRARI_PROGRAMATE)) {
        return "Nr. lucrari";
    } else if (app.equals(GlobalAppConstants.APP_OPERATIUNI_DISPECER)) {
        return "Nr. evenimente";
    } else {
        return "-";
    }

}%>

<%! private long getNumarOfItems(final String app, final long idFiliala) throws DatabaseException {

    if (app.equals(GlobalAppConstants.APP_LUCRARI_PROGRAMATE)) {
        long count = AccessDB.getCount("LPD_LUCRARE_PROGRAMATA", " AND FILIALA_ID=" + idFiliala, "Get Numar Lucrari programate").longValue();
        if (count <= 0) {
            count = 0;
        }
        return count;
    } else if (app.equals(GlobalAppConstants.APP_OPERATIUNI_DISPECER)) {
        return AccessDB.getCount("LPD_EVENIMENT_DISPECER", " AND FILIALA_ID=" + idFiliala, "Get Numar Evenimente dispecer").longValue();
    }
    return -1;
}%>

<%! private String getDataLastEdit(final String app, final long idFiliala) throws DatabaseException {
    Timestamp data = null;
    if (app.equals(GlobalAppConstants.APP_LUCRARI_PROGRAMATE)) {
        final LucrareProgramata lucrare = LucrareProgramataAccessDB.getLastLucrare(" AND FILIALA_ID = " + idFiliala);
        if (lucrare != null) {
            data = lucrare.getDataUpdate() != null ? lucrare.getDataUpdate() : lucrare.getDataInsert();
        }
    }
    return GlobalAppConstants.formatZiSaptaminaData(data);
}%>

