<%@ page import="ro.emid.callcenter.CallcenterSessionUtils" %>
<%@ page import="ro.emid.callcenter.obj.FiltruDeranjamente" %>
<%@ page import="ro.emid.common.constants.AppConstants" %>
<%@ page import="ro.emid.common.constants.DeranjamentConstants" %>
<%@ page import="ro.emid.common.types.Deranjament" %>
<%@ page import="ro.emid.common.types.Reclamatie" %>
<%@ page import="ro.emid.common.types.User" %>
<%@ page import="ro.emid.common.utils.ObjectUtils" %>
<%@ page import="ro.emid.common.utils.SessionUtils" %>
<%@ page import="ro.emid.common.utils.UserUtils" %>
<%@ 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" %>
<%@ taglib prefix="emid" tagdir="/WEB-INF/tags" %>

<%
    /**
     * @see ro.emid.callcenter.action.deranjamente.ShowDeranjamenteAction
     */
    final User user = SessionUtils.getUserLogat(session);

    final SortData sd = (SortData) request.getAttribute("sortData");
    final PaginationData pd = (PaginationData) request.getAttribute("paginationData");

    final FiltruDeranjamente filter = CallcenterSessionUtils.getFiltruDeranjamente(session);
    final Deranjament[] deranjamente = (Deranjament[]) request.getAttribute("deranjamente");

    final boolean firstShow = request.getParameter("first") != null;
    final long selectedId = ObjectUtils.getLongValue(request.getParameter("selectedId"), -1);
%>

<link rel="stylesheet" type="text/css" href="<%=AppConstants.WEB_RESOURCE_PATH%>/scripts/jquery/jquery.splitter.css">
<script type="text/javascript" src="<%=AppConstants.WEB_RESOURCE_PATH%>/scripts/jquery/jquery.splitter.js"></script>

<style type="text/css">
    #splitter-container {
    }

    .main-table{
        width: 100%;
    }

    .main-table .headeru{
        height: 20px;
    }

    .main-table .headeru fieldset{
        margin: 0;
        padding-top:0;
        padding-bottom: 5px;
    }

    #zeframe{
        border: 1px solid #ccc;
        height: 100%;
    }

    #form-table{
        height: 100%;
        width: 100%;
        display: none;
    }

    .right{
        border: 1px solid #ccc;
        overflow: auto;
    }

    .bottomu{
        text-align: right;
        border-top: none;
        padding: 3px;;
    }
</style>

<table class="main-table">
    <tr class="headeru"><td>
        <fieldset>
            <legend><b>Filtru:</b></legend>
                <span style="float: left;">
                    <%=filter.getText()%>
                </span>
                <span style="float: right;">
                    <input type="button" onclick="showFiltruDeranjamente()" title="Configurare filtru deranjamente" value="Configurare filtru"/>
                    &nbsp;
                    <input type="button" data-href="<%=AppConstants.APP_NAME%>?action=reset-filtru-deranjamente" title="Reset filtru curent" value="Reset filtru"/>
                    <% if (AppConstants.runAtFiliala() && (UserUtils.hasRoleAdminCallcenter(user) || (UserUtils.hasRoleUtilizatorCallcenter(user) && UserUtils.hasCcRightEditareDeranjamente(user)))) {%>
                        &nbsp;<input type=button title="Adauga deranjament nou" data-href="<%=AppConstants.APP_NAME%>?action=edit-deranjament&refreshParent=true" value="Adaugă deranjament"/>
                    <% } %>
                </span>
        </fieldset>
    </td></tr>
    <tr><td valign="top">
    <div id="splitter-container">
        <iframe name="zeframe" id="zeframe" scrolling="auto" frameborder="0" src="<%=AppConstants.APP_NAME%>?action=view-deranjament-help" style="overflow-y: hidden">
        </iframe>
        <form id="form-table" action="<%=AppConstants.APP_NAME%>?action=show-deranjamente" method="post">
            <%=SortController.initSort(sd)%>
            <div class="right">
                <table cellpadding=2 cellspacing=1 width="100%" class="table">
                    <thead>
                        <tr>
                            <th nowrap width="1%" colspan="2" title="Ordonare dupa numar deranjament" class="align-center">
                                <%=SortController.printSortableColumn("Nr.", DeranjamentConstants.NUMAR, sd)%>
                            </th>
                            <th nowrap title="Ordonare dupa data anuntarii deranjamentului">
                                <%=SortController.printSortableColumn("Titlu deranjament", DeranjamentConstants.DATA_ANUNTARE, sd)%>
                            </th>
                            <th nowrap width="1%" title="Colectiv/Individual">C/I</th>
                            <% if (UserUtils.hasRoleAdminCallcenter(user) || (UserUtils.hasRoleUtilizatorCallcenter(user) && UserUtils.hasCcRightEditareDeranjamente(user))) {%>
                                <th nowrap width="1%">&nbsp;</th>
                            <%}%>
                        </tr>
                    </thead>
                    <tbody>
                    <% if (deranjamente == null || deranjamente.length == 0) {%>
                        <tr>
                            <td colspan="5">
                                Nu exista date ...
                            </td>
                        </tr>
                    <% } else {
                        // trying = 0, alea nerezolvate,
                        //        = 1, alea rezolvate
                        for (int i = 0; i < deranjamente.length; i++) {
                            Deranjament deranjament = deranjamente[i];
                            Reclamatie primaReclamatie = deranjament.getPrimaReclamatie();
                    %>

                    <tr id="tr<%=i%>" style="color:<%=deranjament.getColorForDeranjament()%>" valign="top" title="<%=deranjament.getStatusText()%>"
                        onclick="onClickDeranjament(<%=deranjament.getId()%>)"
                        <%=selectedId == deranjament.getId() ? "class=\"selected\"" : ""%>
                    >
                        <td nowrap align="left" valign="top" style="padding-left: 4px;">
                            <span title="<%=deranjament.getCountReclamatii() == 0 ? "Fara reclamatii atasate" : deranjament.getCountReclamatii() == 1 ? "O reclamatie atasata, click pentru vizualizare" : deranjament.getCountReclamatii() + " reclamatii atasate, click pentru vuzualizare"%>" id="plus<%=deranjament.getId()%>">
                            </span>
                        </td>
                        <td>
                            <%=deranjament.getNumar()%>
                        </td>
                        <td>
                            <%=deranjament.getTitlu()%>&nbsp;<%=primaReclamatie != null ? primaReclamatie.getAdresaReclamatie() : ""%>
                        </td>
                        <td align="center">
                            <%=deranjament.getNiceTipDeranjament()%>
                        </td>
                        <td width="10">
                            <% if (AppConstants.runAtAparatCentral() || deranjament.getCountReclamatii() > 0) {%>

                            <% } else if (UserUtils.hasRoleAdminCallcenter(user) || (UserUtils.hasRoleUtilizatorCallcenter(user) && UserUtils.hasCcRightEditareDeranjamente(user))) {%>
                                <a class="link" id="linkDeleteDeranjament<%=i%>"
                                   onclick="return confirm('Stergeti deranjamentul ?')"
                                   href="<%=AppConstants.APP_NAME%>?action=delete-deranjament&id=<%=deranjament.getId()%>">Sterge</a>
                            <%}%>
                        </td>
                    </tr>

                    <% if (deranjament.getCountReclamatii() > 0) { %>
                    <tr id="reclamatii<%=deranjament.getId()%>">
                        <td></td>
                        <td colspan="6" id="con-reclamatii-<%=deranjament.getId()%>">
                            Se incarca ...
                        </td>
                    </tr>

                    <script type="text/javascript">
                        new Plus('plus<%=deranjament.getId()%>', 'reclamatii<%=deranjament.getId()%>', function () {
                            if (this.getStatus() == PlusConstants.STATUS_MINUS) {
                                loadReclamatii(<%=deranjament.getId()%>, <%=deranjament.getIdPrimaReclamatie()%>);
                            }
                        });
                    </script>
                    <% } %>
                    <%}%>
                    </tbody>
                    <%}%>
                </table>
            </div>
            <div class="bottomu paginationRow bordered">
                <%=PaginationController.printPaginationData(pd)%>
            </div>
        </form>
    </div>
</td></tr>
</table>


<script type="text/javascript">
    var currentId = -1;

    function onClickDeranjament(id) {
        if( currentId == id) return;
        currentId = id;
    <% if( AppConstants.runAtAparatCentral()){ %>
        $('zeframe').src = '<%=AppConstants.APP_NAME%>?action=view-deranjament&id=' + id;
    <% } else {  if( UserUtils.hasRoleViewCallcenter(user)){ %>
        $('zeframe').src = '<%=AppConstants.APP_NAME%>?action=view-deranjament&id=' + id;
    <% } else { %>
        $('zeframe').src = '<%=AppConstants.APP_NAME%>?action=edit-deranjament&refreshParent=true&id=' + id;
    <% }
     } %>
    }

    <% if( firstShow){ %>
        showFiltruDeranjamente();
    <%}%>

    <% if( selectedId > 0){%>
        onClickDeranjament(<%=selectedId%>);
    <%}%>

    function reloadDeranjamente() {
        $("form-table").submit();
    }

    function loadReclamatii(idDeranjament, idPrimaReclamatie) {
        $('con-reclamatii-' + idDeranjament).innerHTML = 'Se incarca ...';
        new Ajax.Request('<%=AppConstants.APP_NAME%>?action=get-reclamatii-atasate&idObj='+idDeranjament+'&objType=<%=Reclamatie.LEGAT_LA_DERANJAMENT%>&idPrimaReclamatie=' + idPrimaReclamatie,{
            method: 'post',
            onComplete: function (originalRequest) {
                Element.removeClassName('con-reclamatii-' + idDeranjament, "loadingReclamatii");
                $('con-reclamatii-' + idDeranjament).innerHTML = getResponseMesg(originalRequest.responseText);
            }
        });
    }

    function scoateReclamatieCuAjax(idReclamatie, idDeranjament, isPrimaReclamatie, countReclamatii){
        var msg = isPrimaReclamatie ? 'Atentie, este prima reclamatie care defineste adresa deranjamentului\nContinuati operatiunea de scoatere ?' : 'Doriti sa scoateti reclamatia selectata ?'
        if( confirm( msg)){
            new Ajax.Request('<%=AppConstants.APP_NAME%>?action=scoate-reclamatia2&id=' + idReclamatie,{
                method: 'post',
                onComplete: function( originalRequest){
                    if( countReclamatii > 1){
                        loadReclamatii(idDeranjament, isPrimaReclamatie ? idReclamatie : '')
                    }else{
                        reloadDeranjamente();
                    }
                }
            });
        }
    }

    jQuery(document).ready(function(){
        jQuery(".content").each(function(){
            jQuery('#splitter-container').height(jQuery(this).height() - jQuery(".main-table .headeru").height() - layoutOffset);
        });

        jQuery(".right").height( jQuery('#splitter-container').height() - jQuery(".bottomu").outerHeight() - 20);

        splitter = jQuery('#splitter-container').split({
            orientation:'vertical',
            limit: 320,
            position: '70%'
        });

        jQuery("#form-table").show();
    });
</script>




