<%@ page import="ro.emid.callcenter.obj.LittleIncident" %>
<%@ page import="ro.emid.callcenter.obj.LittleIncidentConstants" %>
<%@ page import="ro.emid.common.constants.AppConstants" %>
<%@ page import="ro.emid.common.types.Adresa" %>
<%@ 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" %>
<%@ page import="java.util.Date" %>
<%@ page import="java.util.List" %>

<%@ taglib prefix="emid" tagdir="/WEB-INF/tags" %>

<%
    /**
     * @see ro.emid.callcenter.action.ShowIncidenteCallcenterAction
     */

    User user = SessionUtils.getUserLogat(session);

    //
    // Filters
    //
    final Date dataStart = (Date) request.getAttribute("filter.data-start");
    final Date dataStop  = (Date) request.getAttribute("filter.data-stop");
    final int stare = ((Integer) request.getAttribute("filter.stare"));


    //
    // Result
    //
    final SortData sd = (SortData) request.getAttribute("sd");
    final PaginationData pd = (PaginationData) request.getAttribute("pd");
    final List<LittleIncident> incidente = (List<LittleIncident>) request.getAttribute("incidente");
%>

<div class="page-title">Incidente</div>
<form id="form-table" action="<%=AppConstants.APP_NAME%>?action=show-incidente" method="post">
    <%=SortController.initSort(sd)%>
    <div class="filter-bar">
        <label>
            Data producere:
            <emid:datetimefield name="dataStart" value="<%=dataStart%>"/>
        </label>
        - <emid:datetimefield name="dataStop" value="<%=dataStop%>"/>

        <label class="ml10 mr5">
            Stare:
            <select name="status" class="gSAPSelect">
                <option value="1" <%=stare == 1 ? "selected" : ""%> >in derulare</option>
                <option value="2" <%=stare == 2 ? "selected" : ""%> >incheiate</option>
            </select>
        </label>

        <input type="submit" value="Aplică" />
    </div>

    <table cellpadding=2 cellspacing=1 width="100%" class="table">
        <thead>
        <tr>
            <th nowrap width="1%" colspan="2" class="align-center">
                <%=SortController.printSortableColumn("Nr.", LittleIncidentConstants.ID, sd)%>
            </th>
            <th nowrap>
                <%=SortController.printSortableColumn("Denumire", LittleIncidentConstants.NUME, sd)%>
            </th>
            <th nowrap width=70>
                <%=SortController.printSortableColumn("Autor", LittleIncidentConstants.AUTOR, sd)%>
            </th>
            <th nowrap width=130>
                <%=SortController.printSortableColumn("Data producere", LittleIncidentConstants.DATA_ANUNTARE, sd)%>
            </th>
            <th nowrap width=130>
                <%=SortController.printSortableColumn("Data estimata pt remediere", LittleIncidentConstants.DATA_REZOLVARE, sd)%>
            </th>
            <% if(UserUtils.hasRoleAdminCallcenter(user) || (UserUtils.hasRoleUtilizatorCallcenter(user) &&  UserUtils.hasCcRightEditareLittleIncidente(user))){%>
                <th nowrap width="70">&nbsp;</th>
            <%}%>
        </tr>
        </thead>
        <tbody>
        <% if( incidente == null || incidente.size() == 0){%>
            <tr>
                <td colspan="7" class="no-data">
                    Nu exista date ...
                </td>
            </tr>
        <%} else for (final LittleIncident incident : incidente) { %>
            <tr valign="top">
                <td align="left" width="10">
                    <span title="<%=incident.getCountReclamatii() == 0 ? "Fara reclamatii atasate" : incident.getCountReclamatii() == 1 ? "O reclamatie atasata, click pentru vizualizare" : incident.getCountReclamatii() + " reclamatii atasate, click pentru vuzualizare"%>"
                          id="plus<%=incident.getId()%>">
                    </span>
                </td>
                <td width="20">
                    <%=incident.getId()%>
                </td>
                <td align="left">
                    <% if( incident.getCountAdrese() > 0){%>
                        <a href="#" class="link bold" style="color: black" title="Click pentru a vizualiza adresa" onclick="Element.toggle('adrese<%=incident.getId()%>') ; return false;">
                            <%=AppConstants.format(incident.getNume())%>
                        </a>
                        <div id="adrese<%=incident.getId()%>" class="ml5" style="font-size: 10px; display: none">
                            <% for (final Adresa adresa : incident.getAdrese()) { %>
                                <%=adresa.getLocalitate()%>
                                <%=ObjectUtils.isEmpty(adresa.getLocalitate2()) ? "" : ", sat " + ObjectUtils.getStringValue(adresa.getLocalitate2())%>,
                                <%=adresa.getTipArtera()%>&nbsp;<%=adresa.getDenStrada()%>
                                <%=adresa.getNr() != null ? ", Nr. " + ObjectUtils.getStringValue(adresa.getNr()) : ""%>
                                <%=adresa.getBloc() != null ? ", Bl. " + ObjectUtils.getStringValue(adresa.getBloc()) : ""%>
                                <br/>
                            <% } %>
                        </div>
                    <% }else{ %>
                        <%=AppConstants.format(incident.getNume())%>
                    <% } %>
                </td>
                <td><%=ObjectUtils.getStringValue( incident.getAutor())%></td>
                <td align="center"><%=AppConstants.format(incident.getDataAnuntare())%></td>
                <td align="center"><%=AppConstants.format(incident.getDataRezolvare())%></td>
                <% if(UserUtils.hasRoleAdminCallcenter(user) || (UserUtils.hasRoleUtilizatorCallcenter(user) &&  UserUtils.hasCcRightEditareLittleIncidente(user))){%>
                <td align="left">
                    <nobr>
                        <a class="link" title="Editeaza Incident" href="<%=AppConstants.APP_NAME%>?action=edit-incident&id=<%=incident.getId()%>">Modifică</a>

                        <% if( incident.getCountReclamatii() == 0){%>
                            <a class="link" title="Sterge Incident" onclick="return confirm('Stergeti incidentul <%=incident.getId()%>?')" href="<%=AppConstants.APP_NAME%>?action=delete-incident&id=<%=incident.getId()%>">Șterge</a>
                        <% } %>
                    </nobr>
                </td>
                <%}%>
            </tr>

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

            <script type="text/javascript">
                new Plus('plus<%=incident.getId()%>', 'reclamatii<%=incident.getId()%>', function () {
                    if (this.getStatus() == PlusConstants.STATUS_MINUS) {
                        loadReclamatii(<%=incident.getId()%>);
                    }
                });
            </script>
        <% } %>
        <%}%>
        </tbody>
        <tfoot>
            <tr><td colspan="10" valign="middle">
                <div class="pull-left">
                    <% if( UserUtils.hasRoleAdminCallcenter(user) || (UserUtils.hasRoleUtilizatorCallcenter(user) &&  UserUtils.hasCcRightEditareLittleIncidente(user))){%>
                        <input type="button" data-href="<%=AppConstants.APP_NAME%>?action=edit-incident" title="Adaugă incident" value="Adaugă incident"/>
                    <%}%>
                </div>
                <div class="pull-right">
                    <%=PaginationController.printPaginationData(pd)%>
                </div>
            </td>
        </tfoot>
    </table>
</form>

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

    function scoateReclamatieCuAjax(idReclamatie, idIncident, isPrimaReclamatie, countReclamatii){
        if( confirm( "Doriti sa scoateti reclamatia selectata ?")){
            new Ajax.Request('<%=AppConstants.APP_NAME%>?action=scoate-reclamatia2&id=' + idReclamatie,{
                method: 'post',
                onComplete: function(){
                    if( countReclamatii > 1){
                        loadReclamatii(idIncident);
                    }else{
                        jQuery("#form-table").submit();
                    }
                }
            });
        }
    }
</script>