<%@ page import="ro.emid.callcenter.obj.Lucrare" %>
<%@ page import="ro.emid.callcenter.db.LucrareAccessDB" %>
<%@ 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.callcenter.db.AdresaAccesDB" %>
<%@ page import="ro.emid.common.constants.RoleConstants" %>
<%@ page import="ro.emid.common.types.Adresa" %>
<%@ page import="ro.emid.common.constants.AppConstants" %>
<%@ page import="ro.emid.common.utils.*" %>

<%@include file="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";


    String condLucrari = "";
    if (user.getIeLevel() == RoleConstants.NIVEL_FILIALA) {
        condLucrari = " AND FILIALA_ID =" + filiala.getId();
    } else if (user.getIeLevel() == RoleConstants.NIVEL_SUCURSALA) {
        condLucrari = " AND SUCURSALA_ID =" + sucursala.getId();
    } else if (user.getIeLevel() == RoleConstants.NIVEL_CENTRU) {
        condLucrari = " AND SECTIE_ID =" + centruExploatare.getId();
    }

    final Lucrare[] lucrariActive = LucrareAccessDB.getAllActive( condLucrari, sortColumn, sortType);
%>


<%@ include file="../layout/messages.jspf"%>

<style type="text/css">

    .tabButtons{
        margin-bottom: 10px;
        margin-top: 10px;
    }

    .tabButtons span{
        border-color: gray;
        border-style:solid;
        border-width: 1px;
        padding:3px;
        cursor: pointer;
    }

    .selectedTab{
        background-color: #ffffcc;
        cursor: default;
    }

    .tabButton{

    }
</style>

<table width="100%" border=0>
<tr>
    <td>
        <div class="tabButtons" >
            <span id="tabButtonLucrariActive" class="selectedTab" onclick="onLucrariActiveClick()">Lista Lucrari programate in lucru</span>
            <span id="tabButtonLucrariArhivate" class="tabButton" onclick="onLucrariArhivateClick()">Lista Lucrari programate incheiate</span>
        </div>
        <div id="lucrariActiveTabPage" style="height:100%">
            <TABLE id="lucrariActive" border=0 cellPadding=2 cellSpacing=1 width="100%" class="gSAPTHEAD" style="BACKGROUND-COLOR: white;">
                <TR noSelect="true">
                    <th class="gsapthead" width="40" nowrap>
                        <a style="COLOR: #000000; FONT-SIZE: 13px; text-decoration: none;" href="#" onclick="sort('ID', '<%=(sortType.equals("ASC") ? "DESC" : "ASC")%>')">Nr.</a> <% 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> <%}%>
                    </th>
                    <th class="gsapthead"  nowrap>
                        <a style="COLOR: #000000; FONT-SIZE: 13px; text-decoration: none;" href="#" onclick="sort('NUME', '<%=(sortType.equals("ASC") ? "DESC" : "ASC")%>')">Nume:</a> <% if(sortColumn.equals("NUME")) 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 width="300">Adresa:</TH>
                    <TH class="gsapthead" noWrap >Descriere:</TH>
                    <TH class="gsapthead" noWrap >
                        <a style="COLOR: #000000; FONT-SIZE: 13px; text-decoration: none;" href="#" onclick="sort('DATA_START', '<%=(sortType.equals("ASC") ? "DESC" : "ASC")%>')">Data start:</a> <% if(sortColumn.equals("DATA_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>
                    <TH class="gsapthead" noWrap >Durata:<br><span style="font-size:10px">(ore)</span></TH>
                    <% if( UserUtils.hasRoleAdminCallcenter(user) || (UserUtils.hasRoleUtilizatorCallcenter(user) && UserUtils.hasCcRightEditareLucrariProgramate(user))){%>
                        <TH class="gsapthead" noWrap width="1%">Operatii:</TH>
                    <% }%>
                </TR>
                <%if( lucrariActive.length == 0){%>
                    <tr noSelect="true">
                        <td colspan="5">Nu exista informati...</td>
                    </tr>
                <%} else for (int i = 0; i < lucrariActive.length; i++) {
                    final Lucrare lucrare = lucrariActive[i];
                    lucrare.setAdrese(AdresaAccesDB.getAdreseLucrare( lucrare.getId()));
                    final Reclamatie[] reclamatii = ReclamatieAccessDB.getAll(" AND OBJECT_TYPE =" + Reclamatie.LEGAT_LA_LUCRARE + " AND LEGAT_OBJECT_ID = " + lucrare.getId(), "DATA", "ASC");

                    final StringBuffer adresa = new StringBuffer();
                    for (int j = 0; j < lucrare.getAdrese().length; j++) {
                        final Adresa adresaLucrare = lucrare.getAdrese()[j];
                        adresa
                                .append("<label title='Judet'>").append(ro.emid.common.utils.StringUtils.isZeroLength( adresaLucrare.getJudet()) ? "" : "j." + adresaLucrare.getJudet() + ", ").append("</label>")
                                .append(" <label title='Municipiu/Comuna'>").append( adresaLucrare.getLocalitate()).append("</label>")
                                .append(", <label title='Localitate/Sat'>").append(ro.emid.common.utils.StringUtils.isZeroLength( adresaLucrare.getLocalitate2()) ? "" : adresaLucrare.getLocalitate2()).append("</label>")
                                .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>");
                    }
                %>

                        <tr valign="top">
                            <td align="left" <%if(sortColumn.equals("ID")){%> class="sortColumn"<%}%>>
                                &nbsp;<%=lucrare.getId()%>
                                <img style="cursor:pointer; <% if(reclamatii.length == 0){%> visibility: hidden;<%}%>" id="img<%=i%>" src="<%=AppConstants.WEB_RESOURCE_PATH + "/images/plus.bmp"%>" border="0" alt="Click pentru a vizualiza reclamatiile" onclick="toggleReclamatii(<%=i%>)">&nbsp;
                            </td>
                            <td <%if(sortColumn.equals("NUME")){%> class="sortColumn"<%}%>>
                                <%=AppConstants.format(lucrare.getNume())%>
                            </td>
                            <td <%if(sortColumn.equals("ADRESA")){%> class="sortColumn" <%}%>>
                                <%=adresa%>
                            </td>
                            <td >
                                <%=AppConstants.format(lucrare.getDescriere())%>
                            </td>
                            <td align="center" <%if(sortColumn.equals("DATA_START")){%> class="sortColumn" <%}%>>
                                <%=AppConstants.format(lucrare.getDataStart())%>
                            </td>
                            <td align="center">
                                <%=AppConstants.format(lucrare.getDurata())%>
                            </td>
                            <% if( UserUtils.hasRoleAdminCallcenter(user) || (UserUtils.hasRoleAdminCallcenter(user) && UserUtils.hasCcRightEditareLucrariProgramate(user))){%>
                                <td nowrap>
                                    <a class="link" title="Editeaza Lucrare" href="<%=GlobalAppConstants.APP_NAME%>?action=editLucrare&id=<%=lucrare.getId()%>">Edit</a>
                                    <% if( reclamatii.length == 0){ %>
                                        <a title="Sterge Lucrare" class="link" href="<%=GlobalAppConstants.APP_NAME%>?action=deleteLucrare&id=<%=lucrare.getId()%>" onclick="return confirm('Sugur ?')">Delete</a>
                                    <%}%>
                                    <%--<a class="link" title="Arhiveaza Lucrare" href="<%=GlobalAppConstants.APP_NAME%>?action=arhivareLucrare&id=<%=lucrare.getId()%>" onclick="return confirm('Sugur ?')">Arhivare</a>--%>
                                </td>
                            <% }%>
                        </tr>
                        <tr id="reclamatii<%=i%>" style="display: none;" noSelect="true">
                                <td colspan="7" nowrap>
                                    <table width="100%" cellpadding="0" cellspacing="0">
                                        <tr>
                                            <td width="1%">&nbsp;&nbsp;&nbsp;</td>
                                            <td>
                                                <table class="gsapthead" width="100%" cellpadding="0" cellspacing="1" id="tablereclamatii<%=i%>">
                                                <%if( reclamatii.length == 0 ){%>
                                                    <tr><td> Nu sunt reclamatii atasate ...</td></tr>
                                                <%} else{%>
                                                    <%for (int j = 0; j < reclamatii.length; j++) {
                                                        final Reclamatie reclamatie = reclamatii[j];%>
                                                        <tr ondblclick="showDateReclamatie(<%=reclamatie.getId()%>)">
                                                            <td>
                                                                <label><%=reclamatie.getTitlu()%></label>
                                                            </td>
                                                            <% if( UserUtils.hasRoleAdminCallcenter(user) || (UserUtils.hasRoleUtilizatorCallcenter(user) && UserUtils.hasCcRightEditareLucrariProgramate(user))){%>
                                                                <td width="1%" nowrap>
                                                                    <a class="link" href="<%=GlobalAppConstants.APP_NAME%>?action=scoate-reclamatia&id=<%=reclamatie.getId()%>" onclick=" return confirm('Doriti sa scoateti reclamatia selectata ?')">Scoate</a>
                                                                </td>
                                                            <%}%>
                                                        </tr>
                                                    <%}%>
                                                <%}%>
                                                </table>
                                            </td>
                                        </tr>
                                    </table>
                                </td>
                            </tr>

                            <script type="text/javascript">
                                var table<%=i%> = new Table('tablereclamatii<%=i%>', "table<%=i%>");
                                table<%=i%>.setSelectedClassName('checked');
                            </script>
                <%}%>
            </TABLE>

            
            <% if( UserUtils.hasRoleAdminCallcenter(user) || (UserUtils.hasRoleUtilizatorCallcenter(user) && UserUtils.hasCcRightEditareLucrariProgramate(user))){%>
                <input type="button"  data-href="<%=AppConstants.APP_NAME%>?action=editLucrare" value="Adaugă lucrare">
            <% }%>
        </div>
        <div id="lucrariArhivateTabPage" style="display:none; height:100%">
            <iframe id="frameLucrariArhivate" frameborder="0" width="100%" height="500px">

            </iframe>
        </div>
    </td>
</tr>
</table>

<script type="text/javascript">
    function toggleReclamatii(index){
        if( $('reclamatii' + index).style.display == 'none'){
            $('img' +index).src = '<%=AppConstants.WEB_RESOURCE_PATH + "/images/minus.bmp"%>';
            Element.show('reclamatii' + index);
        }else{
            $('img' +index).src = '<%=AppConstants.WEB_RESOURCE_PATH + "/images/plus.bmp"%>';
            Element.hide('reclamatii' + index);
        }
    }

    function sort( column, type){
        window.location = "<%=GlobalAppConstants.APP_NAME%>?action=showLucrari&sortColumn="+column+"&sortType="+type;
    }

    function onLucrariActiveClick(){
        Element.show("lucrariActiveTabPage");
        Element.hide("lucrariArhivateTabPage");

        $("tabButtonLucrariActive").className = "selectedTab";
        $("tabButtonLucrariArhivate").className = "tabButton";
    }


    var lucrariArhivateLoaded = false;
    function onLucrariArhivateClick(){
        Element.hide("lucrariActiveTabPage");
        Element.show("lucrariArhivateTabPage");

        $("tabButtonLucrariActive").className = "tabButton";
        $("tabButtonLucrariArhivate").className = "selectedTab";

        if( !lucrariArhivateLoaded){
            $("frameLucrariArhivate").src = "<%=GlobalAppConstants.APP_NAME%>?action=showLucrariArhivate";
            lucrariArhivateLoaded = true;
        }
    }

    var tableLucrariActive = new Table('lucrariActive', "tableLucrariActive");
</script>