<%@ page import="ro.emid.common.db.framework.ReportStatement" %>
<%@ page import="ro.emid.common.utils.FSC" %>
<%@ page import="ro.emid.common.utils.FSCUtils" %>
<%@ page import="ro.emid.common.utils.ObjectUtils" %>
<%@ page import="java.text.MessageFormat" %>
<%@ page import="java.util.List" %>
<%@ include file="../incidente/declaratii.jsp" %>

<table border=0 cellpading=0 cellspacing=0 width="100%">
    <tr>
        <td>
            <%@ include file="../header-localizare.jsp"%>
            <span class="slash">/</span> <A class="link" href="<%=AppConstants.APP_NAME%>?action=rapoarte">Rapoarte</A>
            <span class="slash">/</span> Intreruperi scurte / lungi
        </td>
    </tr>
</table>


<%
    final StringBuffer buffer = new StringBuffer();
    buffer.append("SELECT A.TIP_DURATA, COUNT(*)  FROM (\n").append(
            "  SELECT i.numar_incident, i.durata_incident, \n").append(
            "      CASE\n").append(
            "        WHEN 0 <= i.durata_incident AND i.durata_incident <= 3 THEN ''SD''    \n").append(
            "        WHEN i.durata_incident > 3 THEN ''LD''    \n").append(
            "        ELSE ''N/A''\n").append(
            "      END AS TIP_DURATA\n").append(
            "  FROM incidente i\n").append(
            "  WHERE 0=0 {0}\n").append(
            ") A\n").append(
            "GROUP BY A.TIP_DURATA");


    final String[] filter = new String[]{""};

    FSC fscRaport = FSCUtils.getFsc(request);

    if (fscRaport.getFilialaId() > 0) {
        filter[0] += " AND i.FILIALA_ID = " + fscRaport.getFilialaId();
    }

    if (fscRaport.getSucursalaId() > 0) {
        filter[0] += " AND i.SUCURSALA_ID = " + fscRaport.getSucursalaId();
    }

    if (fscRaport.getCentruId() > 0) {
        filter[0] += " AND i.SECTIE_ID = " + fscRaport.getCentruId();
    }

    final ReportStatement raport = new ReportStatement(
            MessageFormat.format(buffer.toString(), filter)
    );
%>

<br/>

<form action="<%=AppConstants.APP_NAME%>?action=raport-intreruperi" method="post">
    <input type="hidden" name="run_report" value="1"/>
    <table>
        <tr>
            <td>
                <emid:filtru-localizare/>
            </td>
            <td>&nbsp;&nbsp; <input type="submit" value="Aplica"></td>
        </tr>
    </table>
    <TABLE id="table" frame="hsides" rules="groups" border=1 cellPadding=3 cellSpacing=1 class="gSAPTHEAD"
           style="BACKGROUND-COLOR: white">
        <col>
        <col>
        <tr noSelect="true">
            <th class="gsapthead">Tip intrerupere</th>
            <th class="gsapthead">Numar incidente</th>
        </tr>

        <%
            final List rows = raport.getListOfArrays("Run raport intreruperi scurte/lungi");
            if (rows == null || rows.size() == 0) {%>
        <tr noSelect="true">
            <td>Nu exista informatii...</td>
        </tr>
        <%
        } else for (int i = 0; i < rows.size(); i++) {
            final Object[] values = (Object[]) rows.get(i);

            int k = 0;
            String tipIntrerupere = (String) values[k++];
            final long nrIncidente = ObjectUtils.getLongValue(values[k++], -1);


            if (tipIntrerupere == null || tipIntrerupere.equals("N/A")) {
                tipIntrerupere = "-";
            } else if (tipIntrerupere.equals("SD")) {
                tipIntrerupere = "Incidente de scurta durata, (1s < durata incident <= 3m)";
            } else if (tipIntrerupere.equals("LD")) {
                tipIntrerupere = "Incidente de lunga durata, durata incident > 3m ";
            }
        %>
        <tr>
            <td><%=tipIntrerupere%>
            </td>
            <td nowrap align="right"><%=nrIncidente%>&nbsp;</td>
        </tr>
        <% } %>
    </TABLE>
</form>

