<%@ page import="ro.emid.common.utils.ObjectUtils" %>
<%@ page import="ro.emid.common.constants.GlobalAppConstants" %>
<%@ page import="ro.emid.common.types.Reclamatie" %>
<%@ page import="ro.emid.common.db.ReclamatieAccessDB" %>
<%@ page import="ro.emid.common.types.Nomenclator" %>
<%@ page import="ro.emid.common.db.NomAccessDB" %>
<%@ page import="ro.emid.common.constants.NomConstants" %>
<%@ page import="ro.emid.common.constants.ReclamatieConstants" %>
<%@ page import="java.util.Map" %>
<%@ page import="ro.emid.common.db.AccessDB" %>
<%@ page import="ro.emid.common.utils.UserUtils" %>
<%@ page import="org.apache.log4j.Logger" %>
<%@ page import="ro.emid.common.constants.AppConstants" %>
<%@ page import="ro.emid.callcenter.util.CallcenterHelper" %>
<%@ page import="ro.emid.common.utils.StringUtils" %>

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

<%
    final Logger LOG = Logger.getLogger("getTableReclamatiiNoi_jsp");
    //stabilire coloana, tip sortare
    //daca in request nu-s acesti parametri, se iau din sesiune, dac in sesiune tot nu-s se iau cei default
    String sortColumn = ObjectUtils.getStringValue(request.getParameter("sortColumn"));
    if (sortColumn == null || sortColumn.length() == 0) {
        sortColumn = (String) session.getAttribute("legareReclamatiiSortColumn");
    }
    if (sortColumn == null || sortColumn.length() == 0) sortColumn = "DATA";

    String sortType = ObjectUtils.getStringValue(request.getParameter("sortType"));
    if (sortType == null || sortType.length() == 0) {
        sortType = (String) session.getAttribute("legareReclamatiiSortType");
    }
    if (sortType == null || sortType.length() == 0) sortType = "DESC";

    session.setAttribute("legareReclamatiiSortColumn", sortColumn);
    session.setAttribute("legareReclamatiiSortType", sortType);

    StringBuffer condLocalizare = new StringBuffer();

    long idZonaSelectata = ObjectUtils.getLongValue(request.getParameter("zonaSelectata"), -1);

    LOG.debug("idZonaSelectata" + idZonaSelectata);

    if (idZonaSelectata > 0) {    //o locatie selectata

        String strIdZona = String.valueOf(idZonaSelectata);
        if (strIdZona.length() == 1) {           //este filiala
            condLocalizare.append(" AND ID_FILIALAR = ").append(idZonaSelectata);
        } else if (strIdZona.length() == 2) {     //este sucursala
            condLocalizare.append(" AND ID_SUCURSALAR = ").append(idZonaSelectata);
        } else if (strIdZona.length() == 3) {     //este centru
            condLocalizare.append(" AND ID_CENTRUR = ").append(idZonaSelectata);
        } else {
            throw new RuntimeException("Lungime id categorie necunoscuta");
        }

    } else if (idZonaSelectata == -1) { //toate locatiile la care userul este arondat

        Long[] ids = UserUtils.getIdLocatiiFull(user);
        condLocalizare.append(" AND (0=1 ");
        if (ids != null) for (int i = 0; i < ids.length; i++) {
            Long idZona = ids[i];
            String strIdZona = String.valueOf(idZona);
            if (strIdZona.length() == 1) {           //este filiala
                condLocalizare.append(" OR ID_FILIALAR = ").append(idZona);
            } else if (strIdZona.length() == 2) {     //este sucursala
                condLocalizare.append(" OR ID_SUCURSALAR = ").append(idZona);
            } else if (strIdZona.length() == 3) {      //este centru
                condLocalizare.append(" OR ID_CENTRUR = ").append(idZona);
            } else {
                throw new RuntimeException("Lungime id categorie necunoscuta");
            }
        }

        condLocalizare.append(" ) ");

    } else {
        throw new RuntimeException("idZoneSelectata are valoare necunoscuta :" + idZonaSelectata);
    }

    LOG.debug("condLocalizare " + condLocalizare);

    if (GlobalAppConstants.CIR_ACTIV && user.getCcCodCir() > 0) {
        condLocalizare.append(" AND COD_CIR = ").append(user.getCcCodCir());
    }

    String conditionReclamatii = new StringBuffer(" AND (LEGAT_OBJECT_ID = -1 OR OBJECT_TYPE = -1) AND ARHIVATA=0 ").append(condLocalizare).toString();
    String conditionReclamatiiSplit1 = new StringBuffer(" AND LEGAT_OBJECT_ID = -1 AND ARHIVATA=0 ").append(condLocalizare).toString();
    String conditionReclamatiiSplit2 = new StringBuffer(" AND OBJECT_TYPE   = -1   AND ARHIVATA=0 ").append(condLocalizare).toString();


    //alea checkuite sunt salvate in sesiune
    Map checkedIds = (Map) session.getAttribute("checkedReclamatiiIds");
    //}


    int tipCountReclamatii = -1;
    int _rTipCountReclamatii = ObjectUtils.getIntValue(request.getParameter("tipCountReclamatii"), -1);
    if (_rTipCountReclamatii > 0) {
        tipCountReclamatii = _rTipCountReclamatii;
        session.setAttribute("tipCountReclamatii", String.valueOf(tipCountReclamatii));
    } else {
        tipCountReclamatii = ObjectUtils.getIntValue(session.getAttribute("tipCountReclamatii"), 1);  //ultimele 100
    }

    Reclamatie[] reclamatii = null;
    int countReclamatii = 0;
    if (tipCountReclamatii == 1) {
        reclamatii = ReclamatieAccessDB.getAllMaxCount(conditionReclamatiiSplit1, conditionReclamatiiSplit2, sortColumn, sortType, 100);
        countReclamatii = AccessDB.getCountIndexed("reclamatii", conditionReclamatii, "get count reclamatii", "/*+ INDEX(r CHECKRECLAMATIINOI_IDX) */").intValue();
    } else {
        reclamatii = ReclamatieAccessDB.getAllIndexed(conditionReclamatiiSplit1, conditionReclamatiiSplit2, sortColumn, sortType);
        countReclamatii = reclamatii.length;
    }


    session.setAttribute("conditionReclamatii", conditionReclamatii);
    session.setAttribute("countReclamatiiVizualizate", new Integer(countReclamatii));
%>


<respmesg>
    <%try {%>
    <TABLE border=0 cellPadding=2 cellSpacing=1 width="100%" class="gSAPTHEAD" style="BACKGROUND-COLOR: white;">
        <thead>
        <TH class="gsapthead" height=25 noWrap align="center">
            <input type="checkbox" name="checkAll" onclick="checkAllReclamatii(this)"/>
        </TH>
        <TH class="gsapthead" noWrap width=120>&nbsp;<a href="#" onclick="loadReclamatii( this)" sortColumn="DATA"
                                                        sortType="<%=(sortColumn.equals("DATA") ? (sortType.equals("ASC") ? "DESC" : "ASC") : sortType)%>"
                                                        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> <%}%></TH>
        <TH class="gsapthead" height=25 noWrap>&nbsp;<a href="#" onclick="loadReclamatii( this)"
                                                        sortColumn="NUME_CLIENT"
                                                        sortType="<%=(sortColumn.equals("NUME_CLIENT") ? (sortType.equals("ASC") ? "DESC" : "ASC") : sortType)%>"
                                                        style="COLOR: #000000; FONT-SIZE: 13px; text-decoration: none;">Reclamant:</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" height=25 noWrap>&nbsp;<a href="#" onclick="loadReclamatii( this)" sortColumn="LOCALITATE"
                                                        sortType="<%=(sortColumn.equals("LOCALITATE") ? (sortType.equals("ASC") ? "DESC" : "ASC") : sortType)%>"
                                                        style="COLOR: #000000; FONT-SIZE: 13px; text-decoration: none;"><%=CallcenterHelper.getConfiguration().getLabelLocalitate1()%>:</a>&nbsp;<% if (sortColumn.equals("LOCALITATE")) 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" height=25 noWrap>&nbsp;<a href="#" onclick="loadReclamatii( this)"
                                                        sortColumn="LOCALITATE2"
                                                        sortType="<%=(sortColumn.equals("LOCALITATE2") ? (sortType.equals("ASC") ? "DESC" : "ASC") : sortType)%>"
                                                        style="COLOR: #000000; FONT-SIZE: 13px; text-decoration: none;"><%=CallcenterHelper.getConfiguration().getLabelLocalitate2()%>:</a>&nbsp;<% if (sortColumn.equals("LOCALITATE2")) 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" height=25 noWrap>&nbsp;TipArtera&nbsp;</TH>
        <TH class="gsapthead" height=25 noWrap>&nbsp;<a href="#" onclick="loadReclamatii( this)" sortColumn="STRADA"
                                                        sortType="<%=(sortColumn.equals("STRADA") ? (sortType.equals("ASC") ? "DESC" : "ASC") : sortType)%>"
                                                        style="COLOR: #000000; FONT-SIZE: 13px; text-decoration: none;">Strada:</a>&nbsp;<% if (sortColumn.equals("STRADA")) 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" height=25 noWrap>&nbsp;Nr&nbsp;</TH>
        <TH class="gsapthead" height=25 noWrap>&nbsp;Bloc&nbsp;</TH>
        <TH class="gsapthead" height=25 noWrap>&nbsp;Scara&nbsp;</TH>
        <TH class="gsapthead" height=25 noWrap>&nbsp;Continut Sesizare&nbsp;</TH>
        </thead>


        <% if (reclamatii.length == 0) {%>
        <tr>
            <td colspan="5">Nu sunt reclamatii ...</td>
        </tr>
        <% } else for (int i = 0; i < reclamatii.length; i++) {
            Reclamatie reclamatie = reclamatii[i];
            Nomenclator zeCir = reclamatie.getCodCIR() > 0 ? NomAccessDB.getNom(NomConstants.NOM_CIR, (int) reclamatie.getCodCIR()) : null;
            if (zeCir == null) zeCir = new Nomenclator();

            boolean checked = checkedIds != null && checkedIds.get(new Long(reclamatie.getId())) != null;

        %>
        <tr valign="top" <%if (checked) {%> class="checked" <%}%> >
            <td align="center" <%=ReclamatieConstants.DB_REP_LOC_CONF_ZONA_OPERATOR.equals(ObjectUtils.getStringValue(reclamatie.getObservatii())) ? " style=\"background-color:#fbe2e2;\" title=\"Atentie! Reclamatie arondata pe C.E. conform zona operator, va rugam sa alocati localitatea " + reclamatie.getLocalitate() + " in cadrul modulului Localitati/Centre\"" : ""%>>
                <input type="checkbox" id="recl<%=i%>" name="reclamatieId" value="<%=reclamatie.getId()%>"
                       onclick="onReclamatieChecked( this, true)" <%if( checked){%> checked <%}%> >
            </td>
            <td align="center"
                onclick="onReclamatieChecked($('recl<%=i%>'))"><%=GlobalAppConstants.format(reclamatie.getData())%>
            </td>
            <td align="left"
                onclick="onReclamatieChecked($('recl<%=i%>'))"><%=GlobalAppConstants.format(reclamatie.getNumeClient())%>
            </td>
            <td nowrap onclick="onReclamatieChecked($('recl<%=i%>'))">
                <%=GlobalAppConstants.format(reclamatie.getLocalitate())%><br>
                <span style="font-size: 10px"><%=GlobalAppConstants.format(zeCir.getDescriere())%></span>
            </td>
            <td onclick="onReclamatieChecked($('recl<%=i%>'))">
                <%=GlobalAppConstants.format(reclamatie.getLocalitate2())%><br>
            </td>
            <td nowrap
                onclick="onReclamatieChecked($('recl<%=i%>'))"><%=GlobalAppConstants.format(reclamatie.getTipArtera())%>
            </td>
            <td nowrap
                onclick="onReclamatieChecked($('recl<%=i%>'))"><%=GlobalAppConstants.format(reclamatie.getStrada())%>
            </td>
            <td nowrap onclick="onReclamatieChecked($('recl<%=i%>'))"><%=GlobalAppConstants.format(reclamatie.getNr())%>
            </td>
            <td nowrap
                onclick="onReclamatieChecked($('recl<%=i%>'))"><%=GlobalAppConstants.format(reclamatie.getBloc())%>
            </td>
            <td nowrap
                onclick="onReclamatieChecked($('recl<%=i%>'))"><%=GlobalAppConstants.format(reclamatie.getScara())%>
            </td>
            <td onclick="onReclamatieChecked($('recl<%=i%>'))"><%=StringUtils.getNullSafe(ReclamatieConstants.getContinutSesizare(reclamatie, "<br>"))%>
            </td>
        </tr>
        <% }%>
    </TABLE>
    <%} catch (Exception t) {%>
    <%=t.getMessage()%>
    <%}%>
</respmesg>


