<%@ page import="ro.emid.callcenter.db.LucrareProgramataAccessDB" %>
<%@ page import="ro.emid.common.constants.AppConstants" %>
<%@ page import="ro.emid.common.db.CategoryAccessDB" %>
<%@ page import="ro.emid.common.types.Adresa" %>
<%@ page import="ro.emid.common.types.Category" %>
<%@ page import="ro.emid.common.types.LucrareProgramata2" %>
<%@ page import="ro.emid.common.types.Reclamatie" %>
<%@ page import="ro.emid.common.utils.pagination.PaginationController" %>
<%@ page import="ro.emid.common.utils.pagination.PaginationData" %>
<%@ page import="ro.emid.common.utils.sort.SortController" %>
<%@ page import="ro.emid.common.utils.sort.SortData" %>
<%@ page import="java.util.List" %>
<%@ page import="ro.emid.common.utils.*" %>
<%@ page import="java.util.LinkedList" %>
<%@ page import="java.util.Date" %>

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

<%

    /**
     * @see ro.emid.callcenter.action.LoadLucrariProgramateCallcenterAction
     */

    //
    // Filters
    //
    final String durataInterval = (String) request.getAttribute("durataInterval");
    final Date dataStart = (Date) request.getAttribute("dataStart");
    final Date dataStop  = (Date) request.getAttribute("dataStop");
    final long idLocatieSelectata = (Long) request.getAttribute("idLocatieSelectata");
    final String stare = (String) request.getAttribute("stare");


    //
    // Action result
    //
    final SortData sd = (SortData) request.getAttribute("sd");
    final PaginationData pd = (PaginationData) request.getAttribute("pd");
    final List<LucrareProgramata2> list = (List<LucrareProgramata2>) request.getAttribute("list");
    final Category.ArboreView[] categs = (Category.ArboreView[]) request.getAttribute("categs");
%>


<div class="page-title">Lucrări programate</div>

<form action="<%=AppConstants.APP_NAME%>?action=show-lucrari" method="post" name="zaforma">
    <%=SortController.initSort( sd)%>
    <input type="hidden" name="toExport" value=""/>
    <div class="filter-bar">
        <select name="durataInterval">
            <option value="durata_progr" <%=durataInterval.equals("durata_progr")?"selected":""%>>Data Progr.</option>
            <option value="durata_lucrare" <%=durataInterval.equals("durata_lucrare")?"selected":""%>>Data Lucr.</option>
            <option value="durata_intr" <%=durataInterval.equals("durata_intr")?"selected":""%>>Data Intr.</option>
        </select>
        : <emid:datetimefield name="data-start" value="<%=dataStart%>"/> - <emid:datetimefield name="data-stop" value="<%=dataStop%>"/>
        &nbsp;&nbsp;

        Locație:
        <select class="gSAPSelect" name="idLocatieLucrare">
            <option>Selectati:</option>
            <% for (int k = 0; k < categs.length; k++) {
            final Category.ArboreView categ = categs[k]; %>

            <option <%=categ.getId() == idLocatieSelectata ? "selected" : ""%> class="<%=categ.isF()?"f":categ.isS()?"s":categ.isC()?"c":""%>" value="<%=categ.getId()%>"><%=categ.getFullPath(" - ")%></option>
            <% } %>
        </select>
        &nbsp;&nbsp;

        Stare:
        <select name="stare">
            <option value="toate" <%="toate".equals( stare) ? "selected" : ""%> >Toate</option>
            <option value="programate" class="programata"  <%="programate".equals( stare) ? "selected" : ""%>>Programate</option>
            <option value="inlucru" class="inLucru" <%="inlucru".equals( stare) ? "selected" : ""%> >In lucru</option>
            <option value="terminate" class="terminata" <%="terminate".equals( stare) ? "selected" : ""%>>Finalizate</option>
        </select>

        <input type="submit" value="Aplică"/>&nbsp;
        <input type="button" value="Export" id="btn-export"/>
    </div>

    <table id="lucrari" cellPadding=2 cellSpacing=1 width="100%" class="table">
    <thead>
        <tr>
            <th class="gsapthead"  nowrap width="10">#</th>
            <th class="gsapthead"  nowrap width="40"><%=SortController.printSortableColumn("Număr", "NUMAR", sd)%></th>
            <th class="gsapthead" width="30">NT</th>
            <th class="gsapthead">Descriere</th>
            <th title="Data programarii lucrarii" class="gsapthead" width="1%" ><%=SortController.printSortableColumn("Data programare", "DATA_PROGRAMARE_START", sd)%></th>
            <th title="Data programarii lucrarii" class="gsapthead" width="1%" ><%=SortController.printSortableColumn("Data incepere realizarii lucrarii", "DATA_START", sd)%></th>
            <th title="Data programarii lucrarii" class="gsapthead" width="1%" >Durata intreruperii consumatorilor</th>
            <th class="gsapthead" width="60">Reclamații</th>
        </tr>
    </thead>
    <tbody>
        <% if( list == null || list.size() == 0 ) { %>
            <tr>
                <td colspan="10" class="no-data" >
                    Nu exista date ...
                </td>
            </tr>
        <% } else for (final LucrareProgramata2 lp : list) {
                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 valign="top">

                <td align="center" title="<%=lp.getStatus()%>" style="<%=style%>" >&nbsp;</td>

                <td align="center">
                    <%=lp.getNumar()%>
                </td>
                <td>
                    <%=lp.getNivelTensiune()%>
                </td>
                <td>
                    <a class="link" href="#" onclick=" Element.toggle('adrese<%=lp.getId()%>'); return false;" title="Click pentru a afisa adresa lucrare" >
                        <%=lp.getParametriTitlu()%>
                    </a>

                    <div id="adrese<%=lp.getId()%>" style="display: none; padding-left: 20px">
                        <%  final List adrese = lp.getAdreseLucrare();
                            if ( adrese == null || adrese.size() == 0){ %>
                                Fara adrese selectate ...;
                        <%  } else for (int j = 0; j < adrese.size(); j++) {
                                final Adresa adresa = (Adresa) adrese.get(j);  %>
                                <% String judet = adresa.getJudet(); %>
                                <% if( StringUtils.isZeroLength( judet)){
                                    Category s = CategoryAccessDB.getById( lp.getSucursalaId());
                                    if( s == null) s = new Category();
                                    judet = s.getName();
                                } %>
                                j.<%=StringUtils.getNullSafe( judet, "-")%>,
                                <%=StringUtils.getNullSafe( adresa.getLocalitate(), "-")%>,
                                <%=StringUtils.getNullSafe( adresa.getLocalitate2(), "-")%>,
                                <%=StringUtils.getNullSafe( adresa.getTipArtera(), "-")%>
                                <%=StringUtils.getNullSafe( adresa.getDenStrada(), "-")%>
                                <%=StringUtils.isZeroLength( adresa.getNr()) ? "" : ", nr. " + adresa.getNr() %>
                                <%=StringUtils.isZeroLength( adresa.getBloc()) ? "" : ", bl. " + adresa.getBloc() %>

                                <br/>
                        <%  } %>
                    </div>
                </td>

                <td nowrap align="left" style="font-size:12px"
                    title="Durata: <%=lp.getDurataProgramare()/60%>h <%=lp.getDurataProgramare()%60%>m"
                >
                    <%
                        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);
                        final long difZile = ( il.getDataStop().getTime() - il.getDataStart().getTime() ) / ( 1000 * 60 * 60 * 24);

                        if( j >= 1){ %> <br/> <% }%>
                        <% if( difZile == 0){ %>
                            <%=AppConstants.formatDate(il.getDataStart())%>
                            &nbsp;[<%=AppConstants.formatOraMinute(il.getDataStart())%> - <%=AppConstants.formatOraMinute(il.getDataStop())%>]
                        <% } else {%>
                            [<%=AppConstants.format(il.getDataStart())%> - <%=AppConstants.format(il.getDataStop())%>]
                        <% } %>
                    <% } %>
                </td>


                <td nowrap style="font-size:12px" title="Durata lucrare: <%=lp.getDurataLucrare()/60%>h <%=lp.getDurataLucrare()%60%>m">
                    <%
                        List intervaleLucrare = new LinkedList();

                        IntervalLucrare i2Default = new IntervalLucrare();
                        i2Default.setDataStart( lp.getDataLucrareStart());
                        i2Default.setDataStop( lp.getDataLucrareStop());
                        intervaleLucrare.add( i2Default);

                        if( lp.getCountIntervaleLucrare() > 0){
                            List _intervale = LucrareProgramataAccessDB.getIntervaleLucrare( lp.getId());
                            for (int idx=0; idx<_intervale.size(); idx++)  {
                                if( ( (IntervalLucrare)_intervale.get(idx)).getTip().equals("IL")){
                                    intervaleLucrare.add(_intervale.get(idx));
                                }
                            }
                        }

                        for (int j = 0; j < intervaleLucrare.size(); j++) {

                            IntervalLucrare il = (IntervalLucrare) intervaleLucrare.get(j);
                            if( il.getDataStart() != null && il.getDataStop() != null){

                                long difZile = ( il.getDataStop().getTime() - il.getDataStart().getTime() ) / ( 1000 * 60 * 60 * 24);
                                if( j >= 1){ %> <br/> <% }%>
                    <% if( difZile == 0){ %>
                    <%=AppConstants.formatDate( il.getDataStart())%>
                    &nbsp;[<%=AppConstants.formatOraMinute( il.getDataStart())%> - <%=AppConstants.formatOraMinute( il.getDataStop())%>]
                    <% } else {%>
                    [<%=AppConstants.format( il.getDataStart())%> - <%=AppConstants.format( il.getDataStop())%>]
                    <% } %>
                    <% } else{ %>
                    -
                    <% } %>
                    <% } %>

                </td>

                <td nowrap style="font-size:12px" title="Durata lucrare: <%=lp.getDurataLucrare()/60%>h <%=lp.getDurataLucrare()%60%>m">
                    <%
                        List intervaleIntreruperi = new LinkedList();

                        IntervalLucrare i3Default = new IntervalLucrare();
                        i3Default.setDataStart(lp.getDataIntrerupereStart());
                        i3Default.setDataStop( lp.getDataIntrerupereStop());
                        intervaleIntreruperi.add( i3Default);

                        if( lp.getCountIntervaleLucrare() > 0){
                            List _intervale = LucrareProgramataAccessDB.getIntervaleLucrare( lp.getId());

                            for (int idx=0; idx<_intervale.size(); idx++)  {
                                if( ( (IntervalLucrare)_intervale.get(idx)).getTip().equals("II")){
                                    intervaleIntreruperi.add(_intervale.get(idx));
                                }
                            }
                        }
                        for (int j = 0; j < intervaleIntreruperi.size(); j++) {
                            IntervalLucrare il = (IntervalLucrare) intervaleIntreruperi.get(j);

                            if( il.getDataStart() != null && il.getDataStop() != null){

                                long difZile = ( il.getDataStop().getTime() - il.getDataStart().getTime() ) / ( 1000 * 60 * 60 * 24);
                                if( j >= 1){ %> <br/> <% }%>
                    <% if( difZile == 0){ %>
                    <%=AppConstants.formatDate( il.getDataStart())%>
                    &nbsp;[<%=AppConstants.formatOraMinute( il.getDataStart())%> - <%=AppConstants.formatOraMinute( il.getDataStop())%>]
                    <% } else {%>

                    [<%=AppConstants.format( il.getDataStart())%> - <%=AppConstants.format( il.getDataStop())%>]

                    <% } %>
                    <% } else{ %>
                    -
                    <% } %>
                    <% } %>

                </td>

                <td align="center">
                    <% if( lp.getCountReclamatii() > 0) { %>
                        <a id="linkReclamatii<%=lp.getId()%>" class="link" href="#" onclick="showReclamatii( <%=lp.getId()%>); return false;"> <%=lp.getCountReclamatii()%> Reclamatii</a>
                    <% }else{ %>
                        Fara reclamatii atasate
                    <% } %>
                </td>

            </tr>

            <tr id="trReclamatii<%=lp.getId()%>" style="display: none;" noSelect="true">
                <td></td>
                <td></td>
                <td colspan="6" nowrap id="containerReclamatii<%=lp.getId()%>">

                </td>
            </tr>


        <% } %>
    </tbody>
    <tfoot>
        <tr>
            <td colspan="15" align="right"  >
                <%=PaginationController.printPaginationData(pd)%>
            </td>
        </tr>
    </tfoot>
    </table>
</form>

<script type="text/javascript">

    var table = new Table('lucrari', "table");

    jQuery("#btn-export").click(function(){
        document.zaforma.toExport.value = '1';
        document.zaforma.submit();

        setTimeout(function(){
            document.zaforma.toExport.value = '0';
        }, 200);
    });

    function showReclamatii( idObj, reload){
        var trId = "trReclamatii" + idObj;
        var tdId = "containerReclamatii" + idObj;
        if( reload || !Element.visible( trId)){
            $(tdId).innerHTML = "&nbsp;&nbsp;&nbsp; Se incarca ...";
            Element.addClassName( tdId, "loadingReclamatii");

            Element.show( trId);

            new Ajax.Request('<%=AppConstants.APP_NAME%>?action=get-reclamatii-atasate&idObj=' + idObj + "&objType=<%=Reclamatie.LEGAT_LA_LUCRARE2%>",
            {
                method: 'post',
                onComplete: function( originalRequest){
                    Element.removeClassName( tdId, "loadingReclamatii");

                    var response = getResponseMesg( originalRequest.responseText);

                    $(tdId).innerHTML = response;
                }
            });
        }else{
            Element.hide( trId);
        }
    }

    function scoateReclamatieCuAjax(idReclamatie, idLucrare){
        if( confirm('Confirmati actiunea.'))
            new Ajax.Request('<%=AppConstants.APP_NAME%>?action=scoate-reclamatia2&id=' + idReclamatie,
            {
                method: 'post',
                onComplete: function( originalRequest){
                    document.zaforma.submit();
                    //showReclamatii( idLucrare, true);
                }
            });
    }

</script>



