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

<%@ include file="../callcenter/declaratii.jsp"%>
<%
    long filialaId = CallcenterSessionUtils.getFilialaId(session);
    long sucursalaId = CallcenterSessionUtils.getSucursalaId(session);
    long centruId = CallcenterSessionUtils.getCentruId(session);

    if (0 < ObjectUtils.getLongValue(request.getParameter("filiala"), -2)) {
        filialaId = ObjectUtils.getLongValue(request.getParameter("filiala"));
    }

    if (0 < ObjectUtils.getLongValue(request.getParameter("sucursala"), -2)) {
        sucursalaId = ObjectUtils.getLongValue(request.getParameter("sucursala"));
    }

    if (0 < ObjectUtils.getLongValue(request.getParameter("centruId"), -2)) {
        centruId = ObjectUtils.getLongValue(request.getParameter("centruId"));
    }

    Category filialaRaport = CategoryAccessDB.getById(filialaId);
    if (filialaRaport == null) filialaRaport = new Category();

    Category sucursalaRaport = CategoryAccessDB.getById(sucursalaId);
    if (sucursalaRaport == null) sucursalaRaport = new Category();

    Category centruRaport = CategoryAccessDB.getById(centruId);
    if (centruRaport == null) centruRaport = new Category();

    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 raportMotive = new ReportStatement(
            " SELECT MOTIV1, MOTIV2, ALT_MOTIV, COUNT(*) FROM informatii",
            " GROUP BY MOTIV1, MOTIV2, ALT_MOTIV ORDER BY  MOTIV1, MOTIV2, ALT_MOTIV")
            .addOrSetFilter("DATA", ">=", dataStartx)
            .addOrSetFilter("DATA", "<=", dataStopx);
%>

<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 Motive solicitari informatii
            </span>
        </TD>
    </TR>
</TABLE>

<table><tr><td>
    <center>
        <br />
        <span style="font-size: 14px">Raport Motive solicitari informatii</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-motive-solicitari-informatii">
    &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">Motiv 1</th>
        <th class="gsapthead">Motiv 2</th>
        <th class="gsapthead">Motiv 3</th>
        <th class="gsapthead" nowrap>Nr. reclamatii</th>
    </tr>
    <%
        final List rows = raportMotive.getListOfArrays("Get Motive Informatii");
        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 motiv1 = -1;
                int motiv2 = -1; %>
                <tbody>
                <tr>
                    <%
                        motiv1 = ObjectUtils.getIntValue( values[ 0], -1);
                        motiv2 = ObjectUtils.getIntValue( values[ 1], -1);
                        final String altMotiv = (String) values[2];


                       final String strMotiv1 = InformatieConstants.getMotiv1( motiv1);
                       final String strMotiv2 = InformatieConstants.getMotiv2(motiv1, motiv2);
                    %>
                    <td nowrap><%=AppConstants.format(strMotiv1)%></td>
                    <td nowrap><%=AppConstants.format(strMotiv2)%></td>
                    <%
                        final long value = ObjectUtils.getLongValue( values[3], 0);
                        total += value;
                    %>
                    <td nowrap><%=AppConstants.format(altMotiv)%></td>
                    <td align="right"><%=value%></td>
                </tr>

                </tbody>
            <%} %>
            <tr>
                <td colspan="3"><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">
        function fill_sucursala_select(selectedValue, disabled, firstShow){
            resetComboLeaveFirstOption( document.zaforma.sucursala);
            resetComboLeaveFirstOption( document.zaforma.centruId);

            var filialaId = document.zaforma.filiala[ document.zaforma.filiala.selectedIndex].value;
            if( filialaId > -1){
                var params = "parentId="+filialaId + "&selectName=sucursala&disabled=" + disabled + (selectedValue ? "&selectedValue=" + selectedValue : "");
                //alert( params);

                getCategoriesFromServer(params, function(originalRequest){
                    //alert( getResponseMesg (originalRequest.responseText));
                    $( 'sucursalaDiv').innerHTML = getResponseMesg (originalRequest.responseText);

                    document.zaforma.sucursala.onchange=fill_centru_select;

                    if( firstShow){
                        fill_centru_select(<%=centruRaport.getId()%>, <%=user.getIeLevel() == ro.emid.common.constants.RoleConstants.NIVEL_CENTRU || user.getIeLevel() == RoleConstants.NIVEL_FORMATIE%>);
                    }
                });
            }
        }

        function fill_centru_select(selectedValue, disabled){
            resetComboLeaveFirstOption( document.zaforma.centruId);

            var sucursalaId = document.zaforma.sucursala[ document.zaforma.sucursala.selectedIndex].value;
            if( sucursalaId > -1){
                var params = "parentId="+sucursalaId + "&selectName=centruId&disabled=" + disabled + (selectedValue ? "&selectedValue=" + selectedValue : "");
                //alert( params);

                getCategoriesFromServer(params,function(originalRequest){
                    //alert( getResponseMesg (originalRequest.responseText));
                    $( 'centruDiv').innerHTML = getResponseMesg (originalRequest.responseText);
                });
            }
        }

   </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>
