<%@ page import="ro.emid.callcenter.db.AdresaAccesDB" %>
<%@ page import="ro.emid.callcenter.db.LittleIncidentAccessDB" %>
<%@ page import="ro.emid.callcenter.db.LucrareAccessDB" %>
<%@ page import="ro.emid.callcenter.db.LucrareProgramataAccessDB" %>
<%@ page import="ro.emid.callcenter.obj.*" %>
<%@ page import="ro.emid.common.constants.GlobalAppConstants" %>
<%@ page import="ro.emid.common.constants.NomConstants" %>
<%@ page import="ro.emid.common.constants.SessionConstants" %>
<%@ page import="ro.emid.common.db.DeranjamentAccessDB" %>
<%@ page import="ro.emid.common.db.NomAccessDB" %>
<%@ page import="ro.emid.common.db.framework.Filter" %>
<%@ page import="ro.emid.common.db.framework.IFilter" %>
<%@ page import="ro.emid.common.types.*" %>
<%@ page import="ro.emid.common.utils.ObjectUtils" %>
<%@ page import="ro.emid.common.utils.pagination.PaginationController" %>
<%@ page import="ro.emid.common.utils.pagination.PaginationData" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.LinkedList" %>
<%@ page import="ro.emid.common.utils.IntervalLucrare" %>
<%@ page import="ro.emid.callcenter.CallcenterSessionUtils" %>
<%@ page import="ro.emid.common.constants.AppConstants" %>

<%@include file="../declaratii.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"));
    String sortType = ObjectUtils.getStringValue(request.getParameter("sortType"));

    int trimiteLa = ObjectUtils.getIntValue(request.getParameter("trimiteLa"), -1);
    if (trimiteLa == -1) {
        Object o = session.getAttribute(SessionConstants.TRIMITE_LA_TIP);
        if (o != null) trimiteLa = ((Integer) o).intValue();
    }

    if (trimiteLa == -1) { //default value
        trimiteLa = Reclamatie.LEGAT_LA_INCIDENT;
    }
    session.setAttribute(SessionConstants.TRIMITE_LA_TIP, new Integer(trimiteLa));

    long checkedID = -1;
%>

<respmesg>
<%try {%>

<%if (trimiteLa == Reclamatie.LEGAT_LA_LUCRARE2) {%>
<%
    if (sortColumn == null || sortColumn.length() == 0) sortColumn = "NUMAR" /*"DATA_ANUNTARE"*/;
    if (sortType == null || sortType.length() == 0) sortType = "DESC" /*ASC*/;

    FiltruLucrariProgramate uiFiltruLucrari = CallcenterSessionUtils.getFiltruLucrariProgramate(session);

    Filter sqlFilter = new Filter();

    if (uiFiltruLucrari.getDataStart() != null) {
        sqlFilter.addOrSetCriteria("AND", "DATA_PROGRAMARE_START", ">=", uiFiltruLucrari.getDataStart());
    }

    if (uiFiltruLucrari.getDataStop() != null) {
        sqlFilter.addOrSetCriteria("AND", "DATA_PROGRAMARE_START", "<=", uiFiltruLucrari.getDataStop());
    }

    if (uiFiltruLucrari.getNumarLucrare() > 0) {
        sqlFilter.addOrSetCriteria(" AND ", "NUMAR", "=", new Long(uiFiltruLucrari.getNumarLucrare()));
    }

    if (uiFiltruLucrari.getNumarReclamatie() > 0) {
        sqlFilter.addOrSetStringCriteria(" AND ", "1", " <= (SELECT COUNT(*) FROM reclamatii r WHERE r.LEGAT_OBJECT_ID = lp.ID AND r.OBJECT_TYPE=" + Reclamatie.LEGAT_LA_LUCRARE2 + " AND r.ID = " + uiFiltruLucrari.getNumarReclamatie() + ") ");
    }

    String value = "";
    boolean comma = false;
    if (uiFiltruLucrari.isStareInLucru()){
        value = " 'IN LUCRU' ";
        comma = true;
    }
    if (uiFiltruLucrari.isStareIncheiate()){
        value = value + (comma ? "," : "") + " 'INCHEIATA' ";
        comma = true;
    }
    if (uiFiltruLucrari.isStareProgramate()){
        value = value + (comma ? "," : "") + " 'PROGRAMATA' ";
    }
    
    if (uiFiltruLucrari.isStareInLucru() || uiFiltruLucrari.isStareIncheiate() || uiFiltruLucrari.isStareProgramate()) {
        sqlFilter.addOrSetStringCriteria(" AND", "STATUS", "IN ("+ value + ")");
    }

    PaginationData pd = PaginationController.getPaginationData(request, 100);

    List list = LucrareProgramataAccessDB.getList(sqlFilter, "STATUS ASC," + sortColumn, sortType, pd.getStartIndex(), pd.getQueryEndIndex(), false);
%>
<TABLE border=0 cellPadding=2 cellSpacing=1 class="gSAPTHEAD" style="BACKGROUND-COLOR: white;">
    <thead>
    <TH class="gsapthead" noWrap>&nbsp;</TH>
    <TH class="gsapthead" noWrap width="1%" title="Numar"><a title="Ordonare dupa numar" href="#"
                                                             onclick="loadDestinationObjectList( this)"
                                                             sortColumn="NUMAR"
                                                             sortType="<%=(sortColumn.equals("NUMAR") ? (sortType.equals("ASC") ? "DESC" : "ASC") : sortType)%>"
                                                             style="COLOR: #000000; FONT-SIZE: 13px; text-decoration: none;">Numar</a>&nbsp; <% if (sortColumn.equals("NUMAR")) 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;Descriere:&nbsp;</TH>
    <TH class="gsapthead" noWrap title="Numar"><a title="Ordonare dupa data programare" href="#"
                                                  onclick="loadDestinationObjectList( this)"
                                                  sortColumn="DATA_PROGRAMARE_START"
                                                  sortType="<%=(sortColumn.equals("DATA_PROGRAMARE_START") ? (sortType.equals("ASC") ? "DESC" : "ASC") : sortType)%>"
                                                  style="COLOR: #000000; FONT-SIZE: 13px; text-decoration: none;">Data
        progr.</a>&nbsp; <% if (sortColumn.equals("DATA_PROGRAMARE_START")) 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>
    </thead>
    <tbody>

    <% if (list.size() == 0) { %>
    <tr>
        <td colspan="4"> Nu exista date ...</td>
    </tr>
    <% } else for (int i = 0; i < list.size(); i++) {
        LucrareProgramata2 lp = (LucrareProgramata2) list.get(i);

        String titlu = lp.getParametriTitlu().toString();

        String style = "";
        if (lp.isProgramata()) {
            style = "background-color:#ffcccc;";
        } else if (lp.isInLucru()) {
            style = "background-color: #ffcc00;";
        } else if (lp.isIncheiata()) {
            style = "background-color: #ccff99;";
        }

    %>

    <tr title="<%=lp.getStatus()%>" valign="top">
        <td nowrap style="<%=style%>; border-top:#cccccc 1px solid;">
            <input type="radio" onclick="onObjectChecked( this);" id="lpd<%=lp.getId()%>" name="objectId"
                   value="<%=lp.getId()%>"
                   catre="lucrarea programata <%=titlu%>">
        </td>
        <td align="left" style="; border-top:#cccccc 1px solid;">
            <nobr>
                <% if (lp.getCountReclamatii() > 0) { %>
                <span title="Click pentru a vizualiza reclamatiile atasate" id="lp2_plusId<%=lp.getId()%>"></span>
                <% } %>
                <label for="lpd<%=lp.getId()%>" <%if (lp.getCountReclamatii() == 0) {%> style="padding-left:14px" <% }%>
                       style="width:100%">
                    <%=lp.getNumar()%>
                </label>
            </nobr>
        </td>
        <td style="; border-top:#cccccc 1px solid;">
            <label for="lpd<%=lp.getId()%>" style="width:100%">
                <%=titlu%>
            </label>
        </td>
        <td nowrap title="Data programare" style="; border-top:#cccccc 1px solid;">
            <%
               final List intervaleProgramare = new LinkedList();

               final IntervalLucrare ilDefault = new IntervalLucrare();
               ilDefault.setDataStart( lp.getDataProgramataStart());
               ilDefault.setDataStop( lp.getDataProgramataStop());
               intervaleProgramare.add( ilDefault);

               if( lp.getCountIntervaleProgramare() > 0){
                   final List _intervale = LucrareProgramataAccessDB.getIntervaleProgramare( lp.getId());
                   if( _intervale != null && _intervale.size() > 0){
                       intervaleProgramare.addAll( _intervale);
                   }
               }
             %>

            <%
                for (int j = 0; j < intervaleProgramare.size(); j++){
                  final IntervalLucrare il = (IntervalLucrare) intervaleProgramare.get(j);
            %>
                <%=AppConstants.format(il.getDataStart())%> -
                <%=AppConstants.format(il.getDataStop())%>
            <br/>
            <%}%>

            <%--<%=AppConstants.format(lp.getDataProgramataStart())%> ---%>
            <%--<%=AppConstants.format(lp.getDataProgramataStop())%>--%>
        </td>
    </tr>

    <% if (lp.getCountReclamatii() > 0) { %>
    <tr id="lp2_plusForId<%=lp.getId()%>">
        <td>&nbsp;</td>
        <td colspan="6" nowrap>
            <table width="100%" cellpadding="0" cellspacing="0">
                <tr>
                    <td id="lp2_reclamatii_plusForId<%=lp.getId()%>">
                        &nbsp;&nbsp;&nbsp;&nbsp;Se incarca ...
                        <!-- se incarca cu ajax aici ...-->
                    </td>
                </tr>
            </table>
        </td>
    </tr>
    <script type="text/javascript">
        new Plus('lp2_plusId<%=lp.getId()%>', 'lp2_plusForId<%=lp.getId()%>', function () {
            if (this.getStatus() == PlusConstants.STATUS_MINUS) {
                Element.addClassName('lp2_reclamatii_plusForId<%=lp.getId()%>', "loadingReclamatii");
                new Ajax.Request('<%=GlobalAppConstants.APP_NAME%>?action=get-reclamatii-atasate&idObj=<%=lp.getId()%>&objType=<%=Reclamatie.LEGAT_LA_LUCRARE2%>',
                        {
                            method: 'post',
                            onComplete: function (originalRequest) {
                                Element.removeClassName('lp2_reclamatii_plusForId<%=lp.getId()%>', "loadingReclamatii");

                                var response = getResponseMesg(originalRequest.responseText);

                                $('lp2_reclamatii_plusForId<%=lp.getId()%>').innerHTML = response;
                            }
                        });
            }
        });
    </script>
    <% } %>
    <% } %>
    </tbody>
</TABLE>
<% } else if (trimiteLa == Reclamatie.LEGAT_LA_LUCRARE) {%>
<%
    FiltruLucrariProgramate filtruLucrari = CallcenterSessionUtils.getFiltruLucrariProgramate(session);

    Lucrare[] lucrari = LucrareAccessDB.getAll(filtruLucrari.buildSqlFilter(), "ID", "ASC");
%>

<TABLE border=0 cellPadding=2 cellSpacing=1 width="100%" class="gSAPTHEAD" style="BACKGROUND-COLOR: white;">
    <thead>
    <TH class="gsapthead" height=25 noWrap>&nbsp;Nume:&nbsp;</TH>
    <TH class="gsapthead" align="left" height=25 noWrap width="400">&nbsp;Adresa:&nbsp;</TH>
    </thead>

    <% if (lucrari.length == 0) {%>
    <tr>
        <td colspan="3">Nu sunt lucrari...</td>
    </tr>
    <% } else for (int i = 0; i < lucrari.length; i++) {
        Lucrare lucrare = lucrari[i];

        boolean checked = lucrare.getId() == checkedID;
    %>
    <tr <%if (checked) {%> class="checked" <%}%>  <% if (lucrare.isRezolvat()) {%> style=" color: red"
                           title="Lucrare incheiata" <%} else {%> style=" color: black"
                           title="Lucrare in derulare " <%}%>>
        <td nowrap>
            <input type="radio" id="luc<%=i%>" name="objectId" value="<%=lucrare.getId()%>"
                   onclick="onObjectChecked( this);" <%if( checked){%> checked <%}%>
                   catre="lucrarea programata <%=AppConstants.format(ro.emid.common.utils.Util.escapeToHtml( lucrare.getNume()))%>">
            <label for="luc<%=i%>"><%=AppConstants.format(lucrare.getNume())%>
            </label>
        </td>
        <td>
            <% Adresa[] adrese = AdresaAccesDB.getAdreseLucrare(lucrare.getId());
                StringBuffer adresa = new StringBuffer("");
                for (int j = 0; j < adrese.length; j++) {
                    Adresa adresaLucrare = adrese[j];
                    adresa.append(adresaLucrare.getLocalitate())
                            .append(", ").append(adresaLucrare.getTipArtera())
                            .append(' ').append(adresaLucrare.getDenStrada())
                            .append(adresaLucrare.getNr() != null ? ", Nr. " + ObjectUtils.getStringValue(adresaLucrare.getNr()) : "")
                            .append(adresaLucrare.getBloc() != null ? ", Bl. " + ObjectUtils.getStringValue(adresaLucrare.getBloc()) : "")
                            .append("<br>");
                }
            %>
            <%=adresa.toString()%>
        </td>
    </tr>
    <% }%>
</TABLE>
<%} else if (trimiteLa == Reclamatie.LEGAT_LA_DERANJAMENT) {%>
<%
    if (sortColumn == null || sortColumn.length() == 0) sortColumn = "NUMAR" /*"DATA_ANUNTARE"*/;
    if (sortType == null || sortType.length() == 0) sortType = "DESC" /*ASC*/;

    FiltruDeranjamente uiFilter = CallcenterSessionUtils.getFiltruDeranjamente(session);
    if (uiFilter == null) {
        uiFilter = new FiltruDeranjamente(user, filiala, sucursala, centruExploatare);
        CallcenterSessionUtils.setFiltruDeranjamente(session, uiFilter);
    }

    StringBuffer filtruText = uiFilter.getText();
    IFilter sqlFilterNerezolvate = uiFilter.buildSqlFilterForNerezolvate();
    IFilter sqlFilterRezolvate = uiFilter.buildSqlFilterForRezolvate();

    Deranjament[] deranjamente = DeranjamentAccessDB.getAllDeranjamente2(sqlFilterNerezolvate, sqlFilterRezolvate, sortColumn, sortType, 0, 1000);
%>

<TABLE border=0 cellPadding=2 cellSpacing=1 class="gSAPTHEAD" style="BACKGROUND-COLOR: white; width:100%">
    <col align="center">
    <col align="left">
    <col>
    <col align="center">
    <thead>
    <th class="gsapthead" noWrap width="17px">&nbsp;</th>
    <TH class="gsapthead" noWrap width="1%"><a title="Ordonare dupa Nr." href="#"
                                               onclick="loadDestinationObjectList( this)" sortColumn="NUMAR"
                                               sortType="<%=(sortColumn.equals("NUMAR") ? (sortType.equals("ASC") ? "DESC" : "ASC") : sortType)%>"
                                               style="COLOR: #000000; FONT-SIZE: 13px; text-decoration: none;">Nr.</a>&nbsp; <% if (sortColumn.equals("NUMAR")) 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" noWrap align="left"><a title="Ordonare dupa Data Anuntare" href="#"
                                                 onclick="loadDestinationObjectList( this)" sortColumn="DATA_ANUNTARE"
                                                 sortType="<%=(sortColumn.equals("DATA_ANUNTARE") ? (sortType.equals("ASC") ? "DESC" : "ASC") : sortType)%>"
                                                 style="COLOR: #000000; FONT-SIZE: 13px; text-decoration: none;">Titlu
        deranjament.</a>&nbsp; <% if (sortColumn.equals("DATA_ANUNTARE")) 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 align="left">&nbsp;C/I:&nbsp;</TH>
    </thead>


    <% for (int i = 0; i < deranjamente.length; i++) {
        Deranjament deranjament = deranjamente[i];
        boolean checked = deranjament.getId() == checkedID;

        Reclamatie primaReclamatie = deranjament.getPrimaReclamatie();

        Nomenclator cirDer = null;
        if (primaReclamatie != null && primaReclamatie.getCodCIR() > 0) {
            cirDer = NomAccessDB.getNom(NomConstants.NOM_CIR, (int) primaReclamatie.getCodCIR());
        }
    %>
    <tr valign="top" <%if (checked) { %> class="checked" <%}%>
        style=" color: <%=deranjament.getColorForDeranjament()%>"
        title="<%=deranjament.getStatusText()%>">
        <td nowrap>
            <input type="radio" id="d<%=i%>" name="objectId" value="<%=deranjament.getId()%>"
                   onclick="onObjectChecked( this);" <%if( checked){%> checked <%}%>
                   catre="deranjamentul nr. <%=AppConstants.format( deranjament.getNumar())%>">
        </td>
        <td>
            <nobr>
                <% if (deranjament.getCountReclamatii() > 0) { %>
                <span id="der_plusId<%=deranjament.getId()%>"></span>
                <% } %>
                <label for="d<%=i%>" style="width: 100%"><%=deranjament.getNumar()%>
                </label>
            </nobr>
        </td>
        <td nowrap>
            <label for="d<%=i%>" style="width: 100%">
                <%=deranjament.getTitlu()%>
                <%if (primaReclamatie != null) {%>
                <%=AppConstants.format(primaReclamatie.getTipArtera())%> <%=AppConstants.format(primaReclamatie.getStrada())%>
                , nr. <%=AppConstants.format(primaReclamatie.getNr())%>
                <%}%>
                <%if (cirDer != null) {%>
                <br>
                <span style="font-size:10px"><%=AppConstants.format(cirDer.getDescriere())%></span>
                <%}%>
            </label>
        </td>
        <td>
            <%=deranjament.getNiceTipDeranjament()%>
        </td>
    </tr>

    <% if (deranjament.getCountReclamatii() > 0) { %>
    <tr id="der_plusForId<%=deranjament.getId()%>">
        <td>&nbsp;</td>
        <td colspan="6" nowrap>
            <table width="100%" cellpadding="0" cellspacing="0">
                <tr>
                    <td id="der_reclamatii_plusForId<%=deranjament.getId()%>">
                        Se incarca ...
                        <!-- se incarca cu ajax aici ...-->
                    </td>
                </tr>
            </table>
        </td>
    </tr>

    <script type="text/javascript">
        new Plus('der_plusId<%=deranjament.getId()%>', 'der_plusForId<%=deranjament.getId()%>', function () {
            if (this.getStatus() == PlusConstants.STATUS_MINUS) {
                Element.addClassName('der_reclamatii_plusForId<%=deranjament.getId()%>', "loadingReclamatii");
                new Ajax.Request('<%=GlobalAppConstants.APP_NAME%>?action=get-reclamatii-atasate&idObj=<%=deranjament.getId()%>&objType=<%=Reclamatie.LEGAT_LA_DERANJAMENT%>',
                        {
                            method: 'post',
                            onComplete: function (originalRequest) {
                                Element.removeClassName('der_reclamatii_plusForId<%=deranjament.getId()%>', "loadingReclamatii");

                                var response = getResponseMesg(originalRequest.responseText);

                                $('der_reclamatii_plusForId<%=deranjament.getId()%>').innerHTML = response;
                            }
                        });
            }
        });
    </script>
    <% } %>
    <% }%>
    <script type="text/javascript">
        try {
            $("linkFiltruDeranjamente").innerHTML = "<%=AppConstants.formatSimple( filtruText.toString())%>";
        } catch (e) {
        }
    </script>

    <tr>
        <td>
            <input type="radio" id="dnou" name="objectId" value="-2" onclick="onObjectChecked( this);"
                   catre="un deranjament nou">
        </td>
        <td colspan="3">
            <label for="dnou" style="width:100%"><b>Deranjament nou</b></label>
        </td>
    </tr>
</TABLE>
<%} else if (trimiteLa == Reclamatie.LEGAT_LA_INCIDENT) {%>
<TABLE border=0 cellPadding=2 cellSpacing=1 width="100%" class="gSAPTHEAD" style="BACKGROUND-COLOR: white;">
    <thead>
    <TH class="gsapthead" height=25 noWrap>&nbsp;Nume:&nbsp;</TH>
    </thead>

    <%
        FiltruLittleIncidente filtruLittleIncidente = CallcenterSessionUtils.getFiltruLittleIncidente(session);
    %>

    <%
        LittleIncident[] incidente = LittleIncidentAccessDB.getAll(filtruLittleIncidente.buildSqlFilter(), "ID", "ASC"); %>
    <% if (incidente.length == 0) {%>
    <tr>
        <td>Nu sunt incidente ...</td>
    </tr>
    <% } else for (int i = 0; i < incidente.length; i++) {
        LittleIncident incident = incidente[i];
        boolean checked = incident.getId() == checkedID;
    %>
    <tr <%if (checked) { %> class="checked" <%}%> <% if (incident.isRezolvat()) {%> style=" color: red"
                            title="Incident incheiat" <%} else {%> style=" color: black"
                            title="Incident in derulare " <%}%> >
        <td>
            <input type="radio" id="li<%=i%>" name="objectId" value="<%=incident.getId()%>"
                   onclick="onObjectChecked( this);"  <%if( checked){%> checked <%}%>
                   catre="incidentul <%=AppConstants.format( ro.emid.common.utils.Util.escapeToHtml( incident.getNume()))%>">
            <label for="li<%=i%>"><%=incident.getNume()%>
            </label>
        </td>
    </tr>
    <% }%>
</TABLE>
<%}%>
<%} catch (Exception t) {%>
<%=t.getMessage()%>
<%t.printStackTrace();%>
<%}%>
</respmesg>


