<%@ page import="ro.emid.common.constants.AppConstants" %>
<%@ page import="ro.emid.common.db.AdEntryAccessDB" %>
<%@ page import="ro.emid.common.db.IncidentAccessDB" %>
<%@ page import="ro.emid.common.types.AdEntry" %>
<%@ page import="ro.emid.common.types.Category" %>
<%@ taglib prefix="emid" tagdir="/WEB-INF/tags" %>
<%

    //todo: filtru dupa filiala
    Category filiala = new Category();

    Integer ani[] = IncidentAccessDB.getAniProducereIncident();
    ani = ro.emid.common.utils.Util.addCurrentYear(ani);
%>

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

<%! private AdEntry getEntry(final AdEntry[] entries, final int luna){
    for (int i = 0; i < entries.length; i++) {
        final AdEntry entry = entries[i];
        if( entry.getLuna() == luna) return entry;
    }
    return null;
}%>

<div class="spacer"></div>
<TABLE id="table" border=0 cellPadding=2 cellSpacing=1 width="100%" class="gSAPTHEAD" style="BACKGROUND-COLOR: white">
    <col align="center">
    <col align="right">
    <col align="right">
    <col align="right">
    <col align="right">
    <col align="right">
    <col align="right">
    <col align="right">
    <col align="right">
    <col align="right">
    <col align="right">
    <col align="right">
    <col align="right">
    <TR>
        <TH class="gsapthead" noWrap >An</TH>
        <%for (int i = 0; i < AppConstants.DENUMIRI_LUNI.length; i++) {
            final String luna = AppConstants.DENUMIRI_LUNI[i];%>
            <TH class="gsapthead" noWrap align="center"><%=luna%></TH>
        <%}%>
    </TR>

    <% for (int k = 0; k < ani.length; k++) {
        final Integer an = ani[ k];
        String td_color = "";

        if (k % 2 == 0) {
            td_color = "BACKGROUND-COLOR: #CACADF";
        } else {
            td_color = "BACKGROUND-COLOR: #D9D9E8";
        }%>
        <tr style="<%=td_color%>">
            <td><%=an%></td>
            <%final AdEntry []forAnEntries = AdEntryAccessDB.getAll(" AND AN=" + an +  (filiala.getId() > 0 ? " AND FILIALA_ID=" + filiala.getId() : "")); %>
            <%for (int i = 0; i < AppConstants.DENUMIRI_LUNI.length; i++) {
                final AdEntry entry = getEntry( forAnEntries, i+1);
                final String value = entry != null ? String.valueOf(entry.getValue()) : "";%>
                <td title="Dublu click pentru editare" ondblclick="inlineStartEditValue('newValue<%=k%><%=i%>', 'spanText<%=k%><%=i%>')">
                    <span>
                        <input class="gsapinputtext" AN="<%=an%>" LUNA="<%=(i+1)%>" size="5" type="text" id="newValue<%=k%><%=i%>" style="display: none" />
                        <span id="spanText<%=k%><%=i%>"><%=value%></span>
                    </span>
                </td>
            <%}%>
        </tr>
        <!--<div class="spacer"></div>-->
<%}%>

</TABLE>
<div class="spacer"></div>

<script type="text/javascript">
    function inlineStartEditValue( inputId, spanTextId){
        var oInput    = $( inputId);
        var oSpanText = $(spanTextId);

        oInput.value = oSpanText.innerText.trim();
        oInput.onkeyup = function( ev){
            var ev = ev || window.event;
//            alert( ev.keyCode);
            switch( ev.keyCode){
                case Event.KEY_ESC : escapeEditValue(inputId, spanTextId); break;
                case Event.KEY_RETURN : changeValue( inputId, spanTextId); break;
            }
        }

        oInput.onblur = function(){
            escapeEditValue(inputId, spanTextId);
        }

        Element.hide( oSpanText);
        Element.show( oInput);
        oInput.select();
        oInput.focus();
    }

    function escapeEditValue( inputId, spanTextId){
        Element.hide( inputId);
        Element.show( spanTextId);
    }

    function changeValue( inputId, spanTextId){
        var AN = $(inputId).getAttribute("AN");
        var LUNA = $(inputId).getAttribute("LUNA");
        var newValue = $(inputId).value;

        new Ajax.Request(
                    "<%=AppConstants.APP_NAME%>?action=changeAdValue",
                    {
                        method : "post",
                        parameters: "AN=" + AN + "&LUNA=" + LUNA + "&newValue="+ newValue+"&inputId="+inputId + "&spanTextId="+spanTextId,
                        onComplete : onChangeValueResponse
                    });
    }

    function onChangeValueResponse( originalRequest){
        var responseText = getResponseMesg( originalRequest.responseText).trim();
        var responseData = null;
        try{
            responseData = eval("("+responseText+")");
        }catch(e){
//            alert("Eroare la parsare Server Reponse :"+ e.message);
        }

        if( responseData != null){
//            alert( "inputId = " + responseData.inputId);
//            alert( "spanId  = " + responseData.spanTextId);
//            alert( "codEMID  = " + responseData.codEMID);

            $( responseData.spanTextId).innerText = responseData.value;
            Element.hide( responseData.inputId);
            Element.show( responseData.spanTextId);
        }else{
            alert("Mesaj Erorare: " + responseText);
        }
    }


</script>
