<%@ page import="ro.emid.common.db.framework.ReportStatement" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.Calendar" %>
<%@ page import="java.sql.Date" %>
<%@ page import="java.sql.Timestamp" %>
<%@ page import="ro.emid.common.types.Reclamatie" %>
<%@ page import="ro.emid.common.constants.*" %>
<%@ page import="ro.emid.common.utils.*" %>

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

    FSC fscRaport = FSCUtils.getFsc(request);

    Date dataStart = ObjectUtils.getDateValue(request.getParameter("data_start"), AppConstants.FORMAT_DATA_TIME);
    Date dataStop = ObjectUtils.getDateValue(request.getParameter("data_stop"), AppConstants.FORMAT_DATA_TIME);

    final Calendar c = Calendar.getInstance();
    c.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DATE), 7, 0, 0);
    if (dataStart == null) dataStart = new Date(c.getTime().getTime());

    if (dataStop == null) {
        c.add(Calendar.DATE, 1);
        dataStop = new Date(c.getTime().getTime());
    }

    final String value_data_start = AppConstants.formatx(dataStart);
    final String value_data_stop = AppConstants.formatx(dataStop);

    final Timestamp dataStartx = new Timestamp(dataStart.getTime());
    final Timestamp dataStopx = new Timestamp(dataStop.getTime());


    final ReportStatement raportReclamatii = new ReportStatement(
            " SELECT r.CONTINUT_SESIZARE1, r.CONTINUT_SESIZARE2, r.ALTA_SESIZARE, COUNT(*) FROM reclamatii r",
            " GROUP BY CONTINUT_SESIZARE1, r.CONTINUT_SESIZARE2, r.ALTA_SESIZARE ORDER BY  CONTINUT_SESIZARE1, r.CONTINUT_SESIZARE2, r.ALTA_SESIZARE")
            .addOrSetFilter("r.DATA", ">=", dataStartx)
            .addOrSetFilter("r.DATA", "<=", dataStopx);

    if (fscRaport.getFilialaId() != -1){
        raportReclamatii.addOrSetFilter("r.ID_FILIALAR", "=", new Long(fscRaport.getFilialaId()));
    }
    if (fscRaport.getSucursalaId() != -1) {
        raportReclamatii.addOrSetFilter("r.ID_SUCURSALAR", "=", new Long(fscRaport.getSucursalaId()));
    }
    if (fscRaport.getCentruId() != -1) {
        raportReclamatii.addOrSetFilter("r.ID_CENTRUR", "=", new Long(fscRaport.getCentruId()));
    }
%>

<TABLE border=0 cellPading=0 cellSpacing=0 width="100%">
    <TR>
        <TD>
            <SPAN style="FONT-SIZE: 12px; FONT-WEIGHT: bold">

               <A class="link" href="<%=GlobalAppConstants.APP_NAME%>?action=rapoarte">Rapoarte</A> - Raport reclamatii grupate dupa continut sesizare
            </span>
        </TD>
    </TR>
</TABLE>

<table><tr><td>
    <center>
        <br />
        <span style="font-size: 14px">Raport reclamatii grupate dupa continut sesizare</span>
    </center>
<div class="spacer"></div>
<table class="gsaptd" width="100%"><tr><td>
<form name="zaforma" action="<%=GlobalAppConstants.APP_NAME%>" method="post">
    <input type="hidden" name="action" value="raport-reclamatii-per-sesizare">
    <input type="hidden" name="run_report" value="1"/>


    <emid:filtru-localizare/>

    &nbsp;&nbsp;<input type="text" id="data_start" name="data_start" size="14" value="<%=value_data_start%>" /> <a href="#" id="trigger_data_start" ><img src="<%=AppConstants.WEB_RESOURCE_PATH + "/images/cal.gif"%>" width="16" height="16" border="0" alt="Apasa pt. a selecta data."></a> -
    <input type="text" id="data_stop" name="data_stop" size="14" value="<%=value_data_stop%>" /> <a href="#" id="trigger_data_stop" ><img src="<%=AppConstants.WEB_RESOURCE_PATH + "/images/cal.gif"%>" width="16" height="16" border="0" alt="Apasa pt. a selecta data."></a>
    <input type="submit" value="Aplica">
</form>
</td></tr></table>
<div class="spacer"></div>

<TABLE id="table" frame="hsides" rules="groups" border=1 cellPadding=3 cellSpacing=1 width="100%" class="gSAPTHEAD" style="BACKGROUND-COLOR: white">
    <col>
    <col>
    <col style="padding-right: 10px">
    <col style="padding-right: 10px; padding-left: 10px">
    <tr noSelect="true">
        <th class="gsapthead">Continut sesizare</th>
        <th class="gsapthead" nowrap>Nr. reclamatii</th>
    </tr>
    <%
        final List rows = raportReclamatii.getListOfArrays("Get Reclamatii");
        if( rows == null || rows.size() == 0){%>
        <tr noSelect="true">
            <td>Nu exista informatii...</td>
        </tr>
       <%} else {
            long total = 0;
            for (int i = 0; i < rows.size(); i++) {
                final Object[] values = (Object[]) rows.get(i);
                int continutSesizare1 = -1;
                int continutSesizare2 = -1; %>
                <tbody>
                <tr>
                    <td nowrap>
                       <%
                           continutSesizare1 = (int)ObjectUtils.getLongValue( values[ 0], -1);
                           continutSesizare2 = (int)ObjectUtils.getLongValue( values[ 1], -1);
                           final String altaSesizare = (String )values[2];

                           final Reclamatie r = new Reclamatie();
                           r.setContinutSesizare1( continutSesizare1);
                           r.setContinutSesizare2( continutSesizare2);
                           r.setAltaSesizare( altaSesizare);
                       %>
                       <%=ReclamatieConstants.getContinutSesizare( r, " ")%>
                    </td>
                    <%
                        final long value = ObjectUtils.getLongValue( values[3], 0);
                        total += value;
                    %>
                    <td align="right"><%=value%></td>
                </tr>
                </tbody>
            <%} %>
            <tr>
                <td><b>TOTAL:</b></td>
                <td align="right"><%=total%></td>
            </tr>
        <%}%>
</TABLE>
</td></tr></table>

<script type="text/javascript">
    var table = new Table("table", "table");
</script>
<script type="text/javascript">
      Calendar.setup(
        {
          inputField  : "data_start",         // ID of the input field
          ifFormat    : "%d.%m.%Y %H:%M",    // the date format
          button      : "trigger_data_start"       // ID of the button
        }
      );

      Calendar.setup(
        {
          inputField  : "data_stop",         // ID of the input field
          ifFormat    : "%d.%m.%Y %H:%M",    // the date format
          button      : "trigger_data_stop"       // ID of the button
        }
      );
</script>
