<%@ page import="ro.emid.callcenter.obj.Informatie" %>
<%@ page import="ro.emid.callcenter.db.InformatieAccessDB" %>
<%@ page import="ro.emid.common.constants.GlobalAppConstants" %>
<%@ page import="ro.emid.common.utils.ObjectUtils" %>
<%@ page import="ro.emid.common.constants.InformatieConstants" %>
<%@ page import="java.sql.Date" %>
<%@ page import="ro.emid.common.db.framework.IFilter" %>
<%@ page import="ro.emid.common.db.framework.Filter" %>
<%@ page import="java.sql.Timestamp" %>
<%@ page import="java.util.Calendar" %>
<%@ page import="ro.emid.common.db.IncidentAccessDB" %>
<%@ page import="ro.emid.common.constants.AppConstants" %>

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

<%
    String sortColumn = ObjectUtils.getStringValue(request.getParameter("sortColumn"));
    if (sortColumn == null || sortColumn.length() == 0) sortColumn = "ID";
    String sortType = ObjectUtils.getStringValue(request.getParameter("sortType"));
    if (sortType == null || sortType.length() == 0) sortType = "ASC";

    int zona = ObjectUtils.getIntValue( request.getParameter("zona"), -2);
    if( zona == -2){    // nu exista in request
        final Long  sessionZona = (Long) session.getAttribute("zona_selectata");
        if(sessionZona instanceof Long){
            zona = sessionZona.intValue();
        }
    }

    Date dataStart = ObjectUtils.getDateValue( request.getParameter("dataStart"), GlobalAppConstants.FORMAT_DATA_TIME);
    Date dataStop  = ObjectUtils.getDateValue( request.getParameter("dataStop"), GlobalAppConstants.FORMAT_DATA_TIME);

    if( dataStart == null){
        final Calendar c = Calendar.getInstance();
        c.set(Calendar.HOUR_OF_DAY, 7);
        c.set(Calendar.MINUTE, 0);
        dataStart = new Date(c.getTime().getTime());
    }

    if( dataStop == null){
        final Calendar c = Calendar.getInstance();
        c.set(Calendar.HOUR_OF_DAY, 19);
        c.set(Calendar.MINUTE, 0);
        dataStop = new Date( c.getTime().getTime());
    }

    session.setAttribute("zona_selectata", new Long(zona));

    final IFilter filter = new Filter();
    if( dataStart != null){
        filter.addOrSetCriteria("AND", "DATA", ">=", new Timestamp( dataStart.getTime()));
    }

    if( dataStop != null){
        filter.addOrSetCriteria("AND", "DATA", "<=", new Timestamp( dataStop.getTime()));
    }

    if( zona > 0){
        filter.addOrSetCriteria("AND", "ZONA", "<=", new Integer( zona));
    }



    //for pagination{
    if (!"alteReclamatii".equals(CallcenterSessionUtils.getPaginationJsp(session))) {
        CallcenterSessionUtils.resetPaginationData(session, "alteReclamatii");
    }

    long nrLines = ObjectUtils.getLongValue(request.getParameter("nrLines"), -1);
    if (nrLines == -1) nrLines = CallcenterSessionUtils.getNrLines(session);

    long currentBeginIndex = ObjectUtils.getLongValue(request.getParameter("currentBeginIndex"), -1);
    if (currentBeginIndex == -1) currentBeginIndex = CallcenterSessionUtils.getBeginIndex(session);

    if( "true".equals( request.getParameter("onFormSubmit"))){
        currentBeginIndex = -1; 
    }


    final long nrLinesTotal = InformatieAccessDB.getCount( filter);
    final String direction = ObjectUtils.getStringValue(request.getParameter("direction"));
    if ("backward".equals(direction)) {
        currentBeginIndex -= nrLines;
    } else if ("forward".equals(direction)) {
        currentBeginIndex += nrLines;
    } else if( "first".equals( direction)){
        currentBeginIndex = 1;
    } else if( "last".equals( direction)){
        currentBeginIndex =  nrLines * nrLinesTotal/nrLines + 1;
    }

    if (currentBeginIndex > nrLinesTotal && nrLinesTotal > 1)
        currentBeginIndex -= nrLines; //cazul cind se sterge ultimul element de pe ultima pagina si este singurul pe pagina
    if (currentBeginIndex < 1) currentBeginIndex = 1;

    CallcenterSessionUtils.setNrLines(session, nrLines);
    CallcenterSessionUtils.setBeginIndex(session, currentBeginIndex);
    //}for pagination

    final Informatie[] informatii = InformatieAccessDB.getAll( filter, sortColumn, sortType, currentBeginIndex, currentBeginIndex + nrLines - 1);
    final int nrLinesSelected = informatii.length;
%>
<!--<TABLE class="gsapthead" align=center border=0 cellPadding=10 cellSpacing=0 width="100%">-->
<!--<TBODY>-->
<!--<TR>-->
<!--<TD>-->
<TABLE align=center border=0 cellPadding=5 cellSpacing=0 width="100%" class="gsaptd">
    <tr><td align="left" valign="top">
        <form action="<%=GlobalAppConstants.APP_NAME%>" method="post">
            <input type="hidden" name="action" value="callcenter_alteinformatii" />
            <input type="hidden" name="onFormSubmit" value="" />
            <fieldset>
                <legend><b>&nbsp;Filtru:</b></legend>
                <table cellspacing="0" cellpadding="0" width="100%" border="0">
                    <tr>
                        <td align="left" nowrap="">
                            <% if( filiala.isMunteniaSud()){  %>
                            Zona:
                                <select name="zona">
                                    <option value="-1">Selectati:</option>
                                    <option <%=zona == InformatieConstants.ZONA_MS_BUCURESTI ? "selected" : ""%> value="<%=InformatieConstants.ZONA_MS_BUCURESTI%>">Bucuresti</option>
                                    <option <%=zona == InformatieConstants.ZONA_MS_ILFOV ? "selected" : ""%> value="<%=InformatieConstants.ZONA_MS_ILFOV%>">Ilfov</option>
                                    <option <%=zona == InformatieConstants.ZONA_MS_GIURGIU ? "selected" : ""%> value="<%=InformatieConstants.ZONA_MS_GIURGIU%>">Giurgiu</option>
                                </select>
                            <%}%>

                            Din data: <input type="text" class="gsapinputtext" name="dataStart" id="data_start" value="<%=GlobalAppConstants.formatx( dataStart)%>" /><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" class="gsapinputtext" name="dataStop"  id="data_stop"  value="<%=GlobalAppConstants.formatx( dataStop)%>" /><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="button" value="Afiseaza" onclick=" this.form.onFormSubmit.value='true'; this.form.submit();"   />
                        </td>
                        <td align="right">

                            <span style="float: right;">
                                <button onclick="window.close()">Inchide Fereastra</button>
                            </span>
                        </td>
                    </tr>
                </table>
            </fieldset>

        <fieldset>
        <legend><b>&nbsp;Multime reclamatii telefonice</b></legend>
        <TABLE id="tableAlteReclamatii" border=0 cellPadding=2 cellSpacing=1 width="100%" class="gSAPTHEAD" style="BACKGROUND-COLOR: white; margin-top: 8px ">
            <TR noSelect="true">
                <TH class="gsapthead" height=25 noWrap>&nbsp;<a href="<%=GlobalAppConstants.APP_NAME%>?action=callcenter_alteinformatii&sortColumn=ID&sortType=<%=(sortType.equals("ASC") ? "DESC" : "ASC")%>" style="COLOR: #000000; FONT-SIZE: 13px; text-decoration: none;">Id</a>&nbsp; <% if(sortColumn.equals("ID")) if( sortType.equals("ASC")){%> <img src="<%=AppConstants.WEB_RESOURCE_PATH%>/images/sort_up_1.gif" border=0> <%}else{%> <img src="<%=AppConstants.WEB_RESOURCE_PATH%>/images/sort_dn_1.gif" border=0> <%}%>&nbsp;</TH>
                <TH class="gsapthead" noWrap >&nbsp;<a href="<%=GlobalAppConstants.APP_NAME%>?action=callcenter_alteinformatii&sortColumn=AUTOR&sortType=<%=(sortType.equals("ASC") ? "DESC" : "ASC")%>" style="COLOR: #000000; FONT-SIZE: 13px; text-decoration: none;">Nume Utilizator</a>&nbsp; <% if(sortColumn.equals("AUTOR")) if( sortType.equals("ASC")){%> <img src="<%=AppConstants.WEB_RESOURCE_PATH%>/images/sort_up_1.gif" border=0> <%}else{%> <img src="<%=AppConstants.WEB_RESOURCE_PATH%>/images/sort_dn_1.gif" border=0> <%}%>&nbsp;</TH>
                <TH class="gsapthead" noWrap >&nbsp;<a href="<%=GlobalAppConstants.APP_NAME%>?action=callcenter_alteinformatii&sortColumn=DATA&sortType=<%=(sortType.equals("ASC") ? "DESC" : "ASC")%>" style="COLOR: #000000; FONT-SIZE: 13px; text-decoration: none;">Data</a>&nbsp; <% if(sortColumn.equals("DATA")) if( sortType.equals("ASC")){%> <img src="<%=AppConstants.WEB_RESOURCE_PATH%>/images/sort_up_1.gif" border=0> <%}else{%> <img src="<%=AppConstants.WEB_RESOURCE_PATH%>/images/sort_dn_1.gif" border=0> <%}%>&nbsp;</TH>
                <TH class="gsapthead" noWrap >&nbsp;<a href="<%=GlobalAppConstants.APP_NAME%>?action=callcenter_alteinformatii&sortColumn=NUME_CLIENT&sortType=<%=(sortType.equals("ASC") ? "DESC" : "ASC")%>" style="COLOR: #000000; FONT-SIZE: 13px; text-decoration: none;">Nume Client</a>&nbsp; <% if(sortColumn.equals("NUME_CLIENT")) if( sortType.equals("ASC")){%> <img src="<%=AppConstants.WEB_RESOURCE_PATH%>/images/sort_up_1.gif" border=0> <%}else{%> <img src="<%=AppConstants.WEB_RESOURCE_PATH%>/images/sort_dn_1.gif" border=0> <%}%>&nbsp;</TH>
                <TH class="gsapthead" noWrap >&nbsp;Telefon:&nbsp;</TH>
                <TH class="gsapthead" noWrap >&nbsp;Motiv reclamatie 1:&nbsp;</TH>
                <TH class="gsapthead" noWrap >&nbsp;Motiv reclamatie 2:&nbsp;</TH>
            </TR>

            <%if( informatii.length == 0 ){%>
                <tr noSelect="true"><td>Nu exista informati...</tr>
            <%}else{%>

                <% for( int i = 0; i  < informatii.length; i++){
                    final Informatie info = informatii[i]; %>
                    <tr>
                        <td align="center" <%if(sortColumn.equals("ID")){%> class="sortColumn"  <%}%> ><%=info.getId()%></td>
                        <td align="center" <%if(sortColumn.equals("AUTOR")){%> class="sortColumn" <%}%> ><%=info.getAutor()%></td>
                        <td align="center" nowrap <%if(sortColumn.equals("DATA")){%> class="sortColumn" <%}%>><%=GlobalAppConstants.format(info.getData())%></td>
                        <td <%if(sortColumn.equals("NUME_CLIENT")){%> class="sortColumn" <%}%> ><%=GlobalAppConstants.format(info.getNumeClient())%></td>
                        <td><%=GlobalAppConstants.format(info.getTelefon())%></td>
                        <td><%=GlobalAppConstants.format(info.getTextMotiv1())%></td>
                        <td><%=GlobalAppConstants.format(info.getTextMotiv2())%></td>
                    </tr>
                <%}%>
                <tr class="paginationRow" noSelect="true">
                    <td colspan="8" >
                        <table width="100%" cellspacing="0" cellpadding="0">
                            <tr noSelect="true">
                                <td align="right">
                                        <input type="hidden" name="currentBeginIndex" value="<%=currentBeginIndex%>">
                                        <input type="hidden" name="direction" value="">
                                        <input type="hidden" name="sortColumn" value="<%=sortColumn%>">
                                        <input type="hidden" name="sortType" value="<%=sortType%>">
                                        Nr. linii:
                                        <select name="nrLines" onchange="this.form.submit()" style="font-size: 10px">
                                            <option value="10"  <%=nrLines == 10 ? "selected" : ""%> >10</option>
                                            <option value="15"  <%=nrLines == 15 ? "selected" : ""%> >15</option>
                                            <option value="20"  <%=nrLines == 20 ? "selected" : ""%> >20</option>
                                            <option value="30"  <%=nrLines == 30 ? "selected" : ""%> >30</option>
                                            <option value="40"  <%=nrLines == 40 ? "selected" : ""%> >40</option>
                                            <option value="50"  <%=nrLines == 50 ? "selected" : ""%> >50</option>
                                        </select>&nbsp;&nbsp;
                                        <%=currentBeginIndex%> - <%=currentBeginIndex + nrLinesSelected - 1%> din <%=nrLinesTotal%>&nbsp;&nbsp;&nbsp;
                                        <input <%=currentBeginIndex == 1 ? "disabled" : ""%>  title="Primele <%=nrLines%> linii" type="button" onclick="this.form.direction.value='first'; this.form.submit()" style="font-size: 8px; cursor: pointer" value="&lt&lt" />
                                        <input <%=currentBeginIndex == 1 ? "disabled" : ""%>  title="Precedentele <%=nrLines%> linii" type="button" onclick="this.form.direction.value='backward'; this.form.submit()" style="font-size: 8px; cursor: pointer" value="&lt" />
                                        <input <%=currentBeginIndex + nrLinesSelected - 1 >= nrLinesTotal ? "disabled" : ""%> title="Urmatoarele <%=nrLines%> linii" type="button" onclick="this.form.direction.value='forward'; this.form.submit()" style="font-size: 8px; cursor: pointer" value="&gt" />
                                        <input <%=currentBeginIndex + nrLinesSelected - 1 >= nrLinesTotal ? "disabled" : ""%> title="Ultimele <%=nrLines%> linii" type="button" onclick="this.form.direction.value='last'; this.form.submit()" style="font-size: 8px; cursor: pointer" value="&gt&gt" />
                                    
                                </td>
                            </tr>
                        </table>
                    </td>
                </tr>

            <%}%>
        </TABLE>
      </fieldset>
    </form>
</td></tr></TABLE>
<!--</td></tr></TBODY></TABLE>-->

<script type="text/javascript">
    var tableReclamatii = new Table('tableAlteReclamatii', "tableReclamatii");


    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>

