<%@ page import="ro.emid.common.constants.AppConstants" %>
<%@ page import="ro.emid.common.types.Eveniment" %>
<%@ page import="ro.emid.common.types.UserEvenimentInfo" %>
<%@ page import="org.apache.log4j.Logger" %>
<%@ page import="ro.emid.common.utils.UserUtils" %>
<%@ page import="ro.emid.Application" %>

<%!
    private static final Logger LOG = Logger.getLogger("showEvenimente.jsp");
%>

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

<%
    /**
     * @see ro.emid.incidente.action.eveniment.ShowEvenimenteAction
     */

    UserEvenimentInfo[] legaturi = (UserEvenimentInfo[]) request.getAttribute("legaturi");
    boolean incidentParinteSetat = legaturi.length > 0;

    String sortColumn = (String) request.getAttribute("sortColumn");
    String sortType   = (String) request.getAttribute("sortType");

    Category[] formatii = (Category[]) request.getAttribute("formatii");
    Eveniment[] evenimente = (Eveniment[]) request.getAttribute("evenimente");
%>

<table border=0 cellpadding="0" cellspacing=0 width="100%">
    <tr>
        <td>
            <%@ include file="../header-localizare.jsp"%> <span class="slash">/</span> Evenimente
        </td>

        <% if ( incidentParinteSetat) {%> 
            <td><a class="link" href="<%=AppConstants.APP_NAME%>?action=clearUserEvenimentInfo">Clear</a></td>
        <%}%>

        <td align="right">
            <span style="font-size: 12px;">Mergi la:</span>
            <A title="Lista incidente" href="<%=AppConstants.APP_NAME%>?action=showIncidente" class="link">Incidente</A>&nbsp;
            <A title="Lista echipamente" href="<%=AppConstants.APP_NAME%>?action=showEchipamente" class="link">Echipamente</A>&nbsp;
            <A title="Lista evenimente arhiva" href="<%=AppConstants.APP_NAME%>?action=showEvenimenteArhiva" class="link">Arhiva</A>&nbsp;
        </td>
    </tr>
</table>

<form name="zaforma" style="padding:0; margin:0" action="<%=AppConstants.APP_NAME%>" method="post">
    <input type="hidden" name="evenimentId" value="-1">
    <input type="hidden" name="action" value="">
    <input type="hidden" name="saveEvenimenteSelectate" value="">
    <table width="100%" border=0>
        <tr><td>
            <table border=0 cellpadding=2 cellspacing=1 width="100%" class="table" id="table">
            <thead>
                <tr>
                    <th width="1%"></th>
                    <th height=25 nowrap>&nbsp;<a href="<%=AppConstants.APP_NAME%>?action=showEvenimente&sortColumn=NR_COMANDA&sortType=<%=(sortColumn.equals("NR_COMANDA") ? (sortType.equals("ASC") ? "DESC" : "ASC") : sortType)%>" style="COLOR: #000000; FONT-SIZE: 13px; text-decoration: none;">Nr Comanda:</a>&nbsp; <% if(sortColumn.equals("NR_COMANDA")) 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 height=25 nowrap>&nbsp;<a href="<%=AppConstants.APP_NAME%>?action=showEvenimente&sortColumn=TEXT&sortType=<%=(sortColumn.equals("TEXT") ? (sortType.equals("ASC") ? "DESC" : "ASC") : sortType)%>" style="COLOR: #000000; FONT-SIZE: 13px; text-decoration: none;">Text:</a>&nbsp; <% if(sortColumn.equals("TEXT")) 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 height=25 nowrap>&nbsp;<a href="<%=AppConstants.APP_NAME%>?action=showEvenimente&sortColumn=ID_OBIECT&sortType=<%=(sortColumn.equals("ID_OBIECT") ? (sortType.equals("ASC") ? "DESC" : "ASC") : sortType)%>" style="COLOR: #000000; FONT-SIZE: 13px; text-decoration: none;"><%=isCuSectiiSiCentre ? "C.E." : "F.L."%></a>&nbsp; <% if(sortColumn.equals("ID_OBIECT")) 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 nowrap>&nbsp;<a href="<%=AppConstants.APP_NAME%>?action=showEvenimente&sortColumn=DATA_COM || TIME_COM&sortType=<%=(sortColumn.equals("DATA_COM || TIME_COM") ? (sortType.equals("ASC") ? "DESC" : "ASC") : sortType)%>" style="COLOR: #000000; FONT-SIZE: 13px; text-decoration: none;">Data comanda:</a>&nbsp; <% if(sortColumn.equals("DATA_COM || TIME_COM")) 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>
                    <% if( UserUtils.hasRoleAdminIncidente(user) || UserUtils.hasRoleAdminIncidente( user)  || UserUtils.hasRoleUtilizatorIncidente( user)){%>
                        <th colspan="2" nowrap width="2%">
                            <div class="pull-left">
                                Operatie:
                            </div>
                            <div class="pull-right" id="operatiiMultiple">
                                <select id="maiMulteOparatii" class="gSAPSelect" onchange="onMaiMulteOperatiiChanged( this)">
                                    <option value="">mai multe...</option>
                                    <%if ( UserUtils.hasRoleAdminIncidente( user) || UserUtils.hasIeRightArhivare(user)){%>
                                    <option value="arhivareGrup">arhivare grup</option>
                                    <%}%>
                                </select>
                            </div>
                        </th>
                    <% }%>
                </tr>
            </thead>
            <tbody>
            <% if( evenimente.length == 0){%>
                <tr>
                    <td class="no-data" colspan="5">
                        Nu sunt evenimente ...
                    </td>
                </tr>
            <%} else for (int i = 0; i < evenimente.length; i++) {
                    final Eveniment eveniment = evenimente[i];
                    final String parametri_titlu = eveniment.getText();%>
                    <tr <% if( eveniment.isTipIA()){%> style="font-weight: bold; <%if( incidentParinteSetat){%> color:#cccccc; <%}%> " <%}%>
                        <% if (Eveniment.isCheckuit(legaturi, eveniment.getNrComanda())) {%> class="checked"<%}%> >
                        <td>
                            <% if( ! UserUtils.hasRoleViewIncidente( user)){%>
                                <input id="check<%=eveniment.getNrComanda()%>" type="checkbox" <%=Eveniment.isCheckuit( legaturi, eveniment.getNrComanda()) ? "checked" : ""%> onclick="onclickcheck(this)" name="checkbox" tip="<%=eveniment.getTip()%>" value="<%=eveniment.getNrComanda()%>" <%=incidentParinteSetat && eveniment.isTipIA() ? "disabled" : ""%>  >
                            <% }%>
                        </td>
                        <td title="Tip <%=eveniment.getTip()%>" nowrap <%if(sortColumn.equals("NR_COMANDA")){%> style="background-color:#f2efef;"<%}%>>
                            <label for="check<%=eveniment.getNrComanda()%>"><%=eveniment.getNrComanda()%></label>
                        </td>
                        <td <%if(sortColumn.equals("TEXT")){%> style="background-color:#f2efef;"<%}%> ><label for="check<%=eveniment.getNrComanda()%>"><%=parametri_titlu%></label></td>
                        <td nowrap <%if(sortColumn.equals("ID_OBIECT")){%> style="background-color:#f2efef;"<%}%>>
                            <%--<%=eveniment.getLocFunctional()%>--%>
                            <%--<% Category formatia = CategoryAccessDB.getFormatieByCodSAP( eveniment.getIdObiect()); %>--%>
                            <% Category formatia = null;
                             for (int j = 0; j < formatii.length; j++) {
                                 final Category f = formatii[j];
                                 final String codFormatie = eveniment.getCodSapFormatie() != null ? eveniment.getCodSapFormatie() : eveniment.getIdObiect();
                                 if( f.getCodSap() != null && f.getCodSap().indexOf( codFormatie) >= 0){
                                    formatia = f;
                                    break;
                                }
                             } %>
                            <%=formatia != null ? formatia.getName() : "-"%>
                        </td>
                        <td align="center" nowrap <%if(sortColumn.equals("DATA_COM || TIME_COM")){%> style="background-color:#f2efef;"<%}%>><%=AppConstants.format(eveniment.getDataComanda())%></td>
                        <%--<td align="center" nowrap><%=AppConstants.format(eveniment.getDataPIF())%></td>--%>
                        <td style="<%= UserUtils.hasRoleViewIncidente( user) ? "display: none" : ""%>" nowrap>
                            <select name="select<%=eveniment.getNrComanda()%>" class="gSAPSelect" style="width: 90px" <%=incidentParinteSetat && eveniment.isTipIA() ? "disabled": ""%> >
                                <%if( eveniment.isTipIA()){%>
                                    <option value="editIncident">Incidente</option>
                                    <option value="editEchipament">Echipamente</option>
                                    <option value="toSend"><%=isCuSectiiSiCentre ? "Alta Sectie" : "Alt C.E."%></option>
                                <%}else if( incidentParinteSetat){%>
                                    <option value="editEchipament">Echipamente</option>
                                    <option value="toSend"><%=isCuSectiiSiCentre ? "Alta Sectie" : "Alt C.E."%></option>
                                <%}else{%>
                                    <option value="editIncident">Incidente</option>
                                    <option value="editEchipament">Echipamente</option>
                                    <option value="toSend"><%=isCuSectiiSiCentre ? "Alta Sectie" : "Alt C.E."%></option>
                                <%}%>
                            </select>
                        </td>

                        <% if(!Application.runAsAparatCentral() && ( UserUtils.hasRoleAdminIncidente( user) || UserUtils.hasRoleUtilizatorIncidente( user) )){%>
                            <td nowrap >
                                <%if( incidentParinteSetat && eveniment.isTipIA()){%>
                                <%}else{
                                    LOG.debug("User has access Trimite I/E.");
                                %>
                                    <a href="javascript: void( null)" class="link" onclick="trimite(<%=eveniment.getNrComanda()%>); return false;">Trimite</a>&nbsp;&nbsp;
                                    <% if ( UserUtils.hasRoleAdminIncidente( user) || UserUtils.hasIeRightArhivare(user)){
                                        LOG.debug("User has access Arhivare I/E.");
                                    %>
                                        <a class="link" href="<%=AppConstants.APP_NAME%>?action=arhivareEveniment&id=<%=eveniment.getNrComanda()%>" onclick="return confirm('Arhivati evenimentul ?')">Arhiveaza</a>
                                    <%}%>
                                <%}%>
                            </td>
                        <%}%>
                    </tr>
                <%}%>
            </tbody>
            <tfoot>
                <tr>
                    <td></td>
                    <td></td>
                    <td colspan="5"><%=evenimente.length%>&nbsp;<%=evenimente.length == 1 ? "eveniment" : "evenimente"%></td>
                </tr>
            </tfoot>
        </table>
    </td></tr>
</table>
</form>


<script type="text/javascript">

    function getNrEvenimenteSelectateTipIA(){
        var result = 0; 
        var els = Form.getElements( document.zaforma);
        for( var i = 0; i < els.length; i++){
            if( els[i].type && els[i].type=="checkbox" && els[i].checked && els[i].getAttribute("tip") == "<%=Eveniment.TIP_IA%>"){
                result++;
            }
        }
        //alert( result);
        return result;
    }

    function getNrEvenimenteSelectate(){
        var result = 0;
        var els = document.getElementsByTagName("input");
        for( var i = 0; i < els.length; i++){
            if( els[i].type && els[i].type=="checkbox" && els[i].checked ){
                result++;
            }
        }
        return result;
    }

    function onclickcheck(oCheck){
        var nrSelected = getNrEvenimenteSelectate();
        var nrOptions  = $( "maiMulteOparatii").options.length;

        if( nrSelected > 1 && nrOptions > 1){
            Element.show( "operatiiMultiple");
        }else{
            Element.hide( "operatiiMultiple");
        }

        hightLightRow(oCheck, 'selected', 'normal')
    }

    function setFormActionAndEvenimentIdAndSubmit( evenimentId){
        var selectu = eval( "document.zaforma.select" + evenimentId );
        document.zaforma.action.value = selectu.options[ selectu.selectedIndex].value;
        document.zaforma.evenimentId.value = evenimentId;
        document.zaforma.submit();
    }

    var centreDialog = null;
    function trimite(evenimentId){
        var selectu = eval( "document.zaforma.select" + evenimentId );
        if( selectu.options[ selectu.selectedIndex].value == "toSend"){

            if( centreDialog != null) centreDialog.close();

            centreDialog = window.open('<%=AppConstants.APP_NAME%>?action=selectareCentru&evenimentId='+evenimentId
                    , 'selectareCentru', 'top=300,left=100,width=650 height=140, status=yes,resizable=yes,dependent=yes,alwaysRaised=yes');
            return;
        }
        //alert('trimite eveniment ' + evenimentId);
        if( !confirm("Trimiteti evenimentul ?")) return;

        if( getNrEvenimenteSelectate() == 0){
            setFormActionAndEvenimentIdAndSubmit( evenimentId);

            return;
        }else{
            var checku = $("check"+evenimentId);

            if( checku == null) { alert( 'Nu gasesc checkul cu valoarea ' + evenimentId); return;}

            if( !checku.checked){
                alert( 'Trimiteti unul din evenimentele selectate !');
                return;
            }

            var incidentParinteSetat = <%=incidentParinteSetat%>;
            if( !incidentParinteSetat){
                var selectatEvenimentTipIA = getNrEvenimenteSelectateTipIA();
                if( selectatEvenimentTipIA && checku.getAttribute("tip") != "<%=Eveniment.TIP_IA%>"){
                    alert( "Trimiteti evenimentul tip IA selectat !");
                    return;
                }
            }
            document.zaforma.saveEvenimenteSelectate.value = true;
            setFormActionAndEvenimentIdAndSubmit( evenimentId);
            return;
        }
    }

    function onMaiMulteOperatiiChanged(oSelect){
        var operation = oSelect.options[ oSelect.selectedIndex].value;
        switch ( operation){
            case "arhivareGrup" : {
                var arr = document.zaforma.checkbox;
                var message = "Ati selectat comenzile:\n";
                for( var i = 0; i < arr.length; i++){
                    if( arr[i].checked){
                        message += "     " + arr[i].value + "\n";
                    }
                }

                message+="Doriti sa arhivati aceste comenzi ?";
                if( confirm(message)){
                    document.zaforma.action.value="arhivareGrup";
                    document.zaforma.submit();
                }
            }break;
        }

        oSelect.selectedIndex = 0;
    }


    try{
        Element.hide( "operatiiMultiple");
    }catch(e){}

    new Table("table");
</script>
