<%@ page import="ro.emid.common.constants.*" %>
<%@ page import="ro.emid.common.db.CategoryAccessDB" %>
<%@ page import="ro.emid.common.db.VolumInstalatiiAccessDB" %>
<%@ page import="ro.emid.common.db.framework.ReportStatement" %>
<%@ page import="ro.emid.common.types.Category" %>
<%@ page import="ro.emid.common.types.User" %>
<%@ page import="ro.emid.common.types.VolumInstalatii" %>
<%@ page import="ro.emid.common.utils.ObjectUtils" %>
<%@ page import="ro.emid.common.utils.UserUtils" %>
<%@ page import="java.sql.Date" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.Vector" %>
<%@ page import="ro.emid.common.utils.SessionUtils" %>


<%
    User user = SessionUtils.getUserLogat(session);
    final VolumInstalatii newEntry = new VolumInstalatii();

    Category ieFilialaSelectata = new Category();
    Category ieSucursalaSelectata = new Category();
    Category filiala = new Category();

    final Long sSucursalaId = null;
    long sucursalaId = (sSucursalaId != null ? sSucursalaId.longValue() : -1);

    if (sucursalaId == -1){
        sucursalaId = ObjectUtils.getLongValue(request.getParameter(UserConstants.IE_SUCURSALA_ID), -1);
        if (sucursalaId >0){
            ieSucursalaSelectata = CategoryAccessDB.getById(sucursalaId);
        }
    };

    final Long sFilialaId = null;
    long filialaId = (sFilialaId != null ? sFilialaId.longValue() : -1);

    if (filialaId == -1){
        filialaId = ObjectUtils.getLongValue(request.getParameter(UserConstants.IE_FILIALA_ID), -1);
        if (filialaId > 0){
            filiala = CategoryAccessDB.getById(filialaId);
            ieFilialaSelectata = CategoryAccessDB.getById(filialaId);
        }
    };

    Category sucursalaRaport = CategoryAccessDB.getById(sucursalaId);
    if (sucursalaRaport == null) sucursalaRaport = new Category();


    final boolean showSumeDeVolume = (filiala.getId() == -1 || sucursalaRaport.getId() == -1);

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


<%! protected double getSuma(final List list, final int i){
    double result = 0.0;

    try{
        final Object[] arr = (Object[])list.get(0);
        final Object obj = arr[ i];
        result = Double.parseDouble( obj.toString());
    }catch(Exception e){
    }
    return result;
}   %>





<style type="text/css">
    .yearRow{
        border-bottom-color: gray;
        border-bottom-style: solid;
        border-bottom-width: 1px;

        border-top-color: gray;
        border-top-style: solid;
        border-top-width: 1px;
    }
</style>


<form name="zaforma" action="<%=GlobalAppConstants.APP_NAME%>" method="post">
    <input type="hidden" name="action" value="show-volum-instalatii">

    <table border="0">
        <tr>
            <td>
                <label>Filiala:</label>
                <% final Category[] ie_filiale = CategoryAccessDB.getAllCategoriesByParentId(-1); %>
                <select class="gSAPSelect" name="<%=UserConstants.IE_FILIALA_ID%>"
                        onchange="ie_on_filiala_changed()">
                    <option value="-1">Selectati:</option>
                    <% for (int ie_i = 0; ie_i < ie_filiale.length; ie_i++) {
                        final Category ie_category = ie_filiale[ie_i]; %>
                    <option value="<%=ie_category.getId()%>" <%=ie_category.getId() == ieFilialaSelectata.getId() ? "selected" : ""%> ><%=ie_category.getName()%>
                    </option>
                    <% }%>
                </select>
                <label>Sucursala:</label>
             <span id="ie_sucursalaDiv">
             <select id="<%=UserConstants.IE_SUCURSALA_ID%>"
                     name="<%=UserConstants.IE_SUCURSALA_ID%>" class="gSAPSelect">
                 <option value="-1">Selectati:</option>
             </select>
             </span>

                <input type="submit" value="Afiseaza">
            </td>
        </tr>
    </table>
</form>


<script type="text/javascript">
    function ie_on_filiala_changed() {
        ie_fill_sucursala_select();
    }

    function ie_fill_sucursala_select(selectedValue, disabled) {
        resetComboLeaveFirstOption(document.zaforma.<%=UserConstants.IE_SUCURSALA_ID%>);

        var filialaId = document.zaforma.<%=UserConstants.IE_FILIALA_ID%>[ document.zaforma.<%=UserConstants.IE_FILIALA_ID%>.selectedIndex].value;
        var params = "parentId=" + filialaId + "&selectName=<%=UserConstants.IE_SUCURSALA_ID%>&disabled=" + disabled + (selectedValue ? "&selectedValue=" + selectedValue : "");
        //alert( params);

        if (filialaId == -1) return;

        getCategoriesFromServer(params, function (originalRequest) {
            //alert(getResponseMesg(originalRequest.responseText));
            $('ie_sucursalaDiv').innerHTML = getResponseMesg(originalRequest.responseText);
            document.zaforma.<%=UserConstants.IE_SUCURSALA_ID%>.onchange = ie_on_sucursala_changed;
        });
    }

    ie_fill_sucursala_select(<%=ieSucursalaSelectata.getId()%>);

</script>








<% String td_color = "BACKGROUND-COLOR: #D9D9E8"; %>
<% if (showSumeDeVolume){
        final ReportStatement sum = new ReportStatement(
                " SELECT TO_CHAR(sum(v.NR_CONSUMATORI)), TO_CHAR(sum(v.STATII)), TO_CHAR(sum(v.LEA_110)), TO_CHAR(sum(v.LES_110)), TO_CHAR(sum(v.STATII_MT)), TO_CHAR(sum(v.PA_PT)), TO_CHAR(sum(v.LEA_MT)), TO_CHAR(sum(v.LES_MT))   " +
                " FROM volumInstalatii v "
        );

        String headerText = "-";
        Category []categories = new Category[0];
        if (filiala.getId() > 0) {
            categories = CategoryAccessDB.getAllCategoriesByParentId( filiala.getId());
            headerText = "Sucursale";
        }else{
            categories = CategoryAccessDB.getAllCategoriesByParentId( -1);
            headerText = "Filiale";
        }
%>
<TABLE id="table" border=0 cellPadding=2 cellSpacing=1 width="100%" class="gSAPTHEAD" style="BACKGROUND-COLOR: white">
    <TR>
        <TH class="gsapthead" noWrap ><%=headerText%></TH>
        <TH class="gsapthead" noWrap >Nr. consumatori</TH>
        <TH class="gsapthead" noWrap >Statii</TH>
        <TH class="gsapthead" noWrap >LEA 110KV</TH>
        <TH class="gsapthead" noWrap >LES 110KV</TH>
        <TH class="gsapthead" noWrap >StatiiMT/MT</TH>
        <TH class="gsapthead" noWrap >PA/PT</TH>
        <TH class="gsapthead" noWrap >LEA MT</TH>
        <TH class="gsapthead" noWrap >LES MT</TH>
    </TR>

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

        <tr>
            <td class="yearRow"><%=an%></td>
            <td colspan="12" class="yearRow">&nbsp;</td>
        </tr><%
        sum.addOrSetFilter("AN", "=", an);
        for (int i = 0; i < categories.length; i++) {
            final Category category = categories[ i];

            if( category.getId_parent() == -1){//is Filiala
                sum.addOrSetFilter("FILIALA_ID", "=", new Long(category.getId()));
            }else{ // is Sucursala
                sum.addOrSetFilter("SUCURSALA_ID", "=", new Long(category.getId()));
            }

            if (i % 2 == 0) {
                td_color = "BACKGROUND-COLOR: #CACADF";
            } else {
                td_color = "BACKGROUND-COLOR: #D9D9E8";
            }


            final List resultSume = sum.getListOfArrays("Get Sume Volum Instalatii");%>
            <tr align="right" style="<%=td_color%>" >
                <td><%=category.getName()%></td>
                <td><%=AppConstants.format03(getSuma(resultSume, 0))%></td>
                <td><%=AppConstants.format03(getSuma(resultSume, 1))%></td>
                <td><%=AppConstants.format03(getSuma(resultSume, 2))%></td>
                <td><%=AppConstants.format03(getSuma(resultSume, 3))%></td>
                <td><%=AppConstants.format03(getSuma(resultSume, 4))%></td>
                <td><%=AppConstants.format03(getSuma(resultSume, 5))%></td>
                <td><%=AppConstants.format03(getSuma(resultSume, 6))%></td>
                <td><%=AppConstants.format03(getSuma(resultSume, 7))%></td>
            </tr>
        <%}%>
    <%} %>

</TABLE>

<%}else{%>

<TABLE id="table" border=0 cellPadding=2 cellSpacing=1 width="100%" class="gSAPTHEAD" style="BACKGROUND-COLOR: white">
    <TR>
        <TH class="gsapthead" noWrap >An</TH>
        <th class="gsapthead" title="Data ultimei actualizari" width="50">Data actualizare</th>
        <TH class="gsapthead" title="Centrul de Exploatare" height=25 noWrap>C.E.</TH>
        <TH class="gsapthead" noWrap >Nr. consumatori</TH>
        <TH class="gsapthead" noWrap >Statii</TH>
        <TH class="gsapthead" noWrap >LEA 110KV</TH>
        <TH class="gsapthead" noWrap >LES 110KV</TH>
        <TH class="gsapthead" noWrap >StatiiMT/MT</TH>
        <TH class="gsapthead" noWrap >PA/PT</TH>
        <TH class="gsapthead" noWrap >LEA MT</TH>
        <TH class="gsapthead" noWrap >LES MT</TH>

        <%--todo:--%>
        <%if( ro.emid.common.utils.UserUtils.hasAccessAdministrare(user) ){%>
            <TH class="gsapthead" noWrap width=1%>Operatii</TH>
         <%}%>
    </TR>
<%


    int formIndex = 0;
    for (int k = 0; k < ani.length; k++) {
        final Integer an = ani[ k];

        final Vector entries = new Vector();
        final Category[] centre = CategoryAccessDB.getAllCategoriesByParentId( sucursalaRaport.getId());

        for (int i = 0; i < centre.length; i++) {
            final Category centru = centre[i];
            VolumInstalatii volum = VolumInstalatiiAccessDB.getEntryByCentruIdAndYear( centru.getId(), an.intValue());

            if (volum == null) volum = new VolumInstalatii(filiala.getId(), sucursalaRaport.getId(), centru.getId(), an.intValue());

            volum.setCentru(centru);
            entries.add( volum);
        }
%>

    <tr>
        <td class="yearRow"><%=an%></td>
        <td colspan="12" class="yearRow">&nbsp;</td>
    </tr>

    <% for (int i = 0; i < entries.size(); i++) {
        final VolumInstalatii volum = (VolumInstalatii)entries.get(i);
        if (i % 2 == 0) {
            td_color = "BACKGROUND-COLOR: #CACADF";
        } else {
            td_color = "BACKGROUND-COLOR: #D9D9E8";
        }%>
        <% final Date dataCurenta = new Date(System.currentTimeMillis());%>
        <form name="<%=WebConstants.FORM%><%=formIndex%>" action="<%=AppConstants.APP_NAME%>" method="post" onsubmit="return onSalveaza( this)">
            <input type="hidden" name="action" value="saveVolumInstalatii">
            <input type="hidden" name="<%=VolumInstalatiiConstants.ID%>" value="<%=volum.getId()%>">
            <input type="hidden" name="<%=VolumInstalatiiConstants.FILIALA_ID%>" value="<%=volum.getFilialaId()%>">
            <input type="hidden" name="<%=VolumInstalatiiConstants.SUCURSALA_ID%>" value="<%=volum.getSucursalaId()%>">
            <input type="hidden" name="<%=VolumInstalatiiConstants.SECTIE_ID%>" value="<%=volum.getSectieId()%>">
            <input type="hidden" name="<%=VolumInstalatiiConstants.AN%>" value="<%=volum.getAn()%>">

            <input type="hidden" name="<%=UserConstants.IE_FILIALA_ID%>" value="<%=filialaId%>">
            <input type="hidden" name="<%=UserConstants.IE_SUCURSALA_ID%>" value="<%=sucursalaId%>">

            <tr align="center" id="tr_inputs<%=formIndex%>" style="display:none">
                <td style="<%=td_color%>" align="left">
                    &nbsp;
                </td>
                <td style="<%=td_color%>" align="left" >
                    <%=AppConstants.format(volum.getDataSave())%>
                </td>
                <td style="<%=td_color%>" align="left">
                    <%=volum.getCentru().getName()%>
                </td>
                <td style="<%=td_color%>" align="right">
                    <input class="gsapinputtext" style="text-align:right;" type="text" size="5" onkeyup="if (/[^0-9.]/g.test(this.value)) this.value = this.value.replace(/[^0-9.]/g,'')" name="<%=VolumInstalatiiConstants.NR_CONSUMATORI%>"  value="<%=AppConstants.format( volum.getNrConsumatori())%>">
                </td>
                <td style="<%=td_color%>" align="right">
                    <input class="gsapinputtext" style="text-align:right;" type="text" size="5" onkeyup="if (/[^0-9.]/g.test(this.value)) this.value = this.value.replace(/[^0-9.]/g,'')" name="<%=VolumInstalatiiConstants.STATII%>"  value="<%=AppConstants.format03( volum.getStatii())%>">
                </td>
                <td style="<%=td_color%>" align="right">
                    <input class="gsapinputtext" style="text-align:right;" type="text" size="5" onkeyup="if (/[^0-9.]/g.test(this.value)) this.value = this.value.replace(/[^0-9.]/g,'')" align="left" name="<%=VolumInstalatiiConstants.LEA_110%>"  value="<%=AppConstants.format03( volum.getLea110())%>">
                </td>
                <td style="<%=td_color%>" align="right">
                    <input class="gsapinputtext" style="text-align:right;" type="text" size="5" onkeyup="if (/[^0-9.]/g.test(this.value)) this.value = this.value.replace(/[^0-9.]/g,'')" name="<%=VolumInstalatiiConstants.LES_110%>"  value="<%=AppConstants.format03( volum.getLes110())%>">
                </td>
                <td style="<%=td_color%>" align="right">
                    <input class="gsapinputtext" style="text-align:right;" type="text" size="5" onkeyup="if (/[^0-9.]/g.test(this.value)) this.value = this.value.replace(/[^0-9.]/g,'')" name="<%=VolumInstalatiiConstants.STATII_MT%>"  value="<%=AppConstants.format03( volum.getStatiiMt())%>">
                </td>
                <td style="<%=td_color%>" align="right">
                    <input class="gsapinputtext" style="text-align:right;" type="text" size="5" onkeyup="if (/[^0-9.]/g.test(this.value)) this.value = this.value.replace(/[^0-9.]/g,'')" name="<%=VolumInstalatiiConstants.PA_PT%>"  value="<%=AppConstants.format03( volum.getPa_pt())%>">
                </td>
                <td style="<%=td_color%>" align="right">
                    <input class="gsapinputtext" style="text-align:right;" type="text" size="5" onkeyup="if (/[^0-9.]/g.test(this.value)) this.value = this.value.replace(/[^0-9.]/g,'')" name="<%=VolumInstalatiiConstants.LEA_MT%>"  value="<%=AppConstants.format03( volum.getLea_mt())%>">
                </td>
                <td style="<%=td_color%>" align="right">
                    <input class="gsapinputtext"  style="text-align:right;" type="text" size="5" onkeyup="if (/[^0-9.]/g.test(this.value)) this.value = this.value.replace(/[^0-9.]/g,'')" name="<%=VolumInstalatiiConstants.LES_MT%>"  value="<%=AppConstants.format03( volum.getLes_mt())%>">
                </td>
                <td style="<%=td_color%>" nowrap>
                    <a href="javascript: void( null)" class="sapBtnStd"  onclick="document.<%=WebConstants.FORM%><%=formIndex%>.submit(); return false;">Salveaza</a>
              </td>
            </tr>
            <tr align="center" id="tr_text<%=formIndex%>">
               <td style="<%=td_color%>" align="left">
                    &nbsp;
               </td>
               <td style="<%=td_color%>" align="left">
                    <%=AppConstants.format(volum.getDataSave())%>
               </td>
               <td style="<%=td_color%>" align="left">
                    <%=volum.getCentru().getName()%>
               </td>                
               <td style="<%=td_color%>" align="right">
                    <%=AppConstants.format(volum.getNrConsumatori())%>
                </td>
                <td style="<%=td_color%>" align="right">
                    <%=AppConstants.format03(volum.getStatii())%>
                </td>
                <td style="<%=td_color%>" align="right">
                    <%=AppConstants.format03(volum.getLea110())%>
                </td>
                <td style="<%=td_color%>" align="right">
                    <%=AppConstants.format03(volum.getLes110())%>
                </td>
                <td style="<%=td_color%>" align="right">
                    <%=AppConstants.format03(volum.getStatiiMt())%>
                </td>
                <td style="<%=td_color%>" align="right">
                    <%=AppConstants.format03(volum.getPa_pt())%>
                </td>
                <td style="<%=td_color%>" align="right">
                    <%=AppConstants.format03(volum.getLea_mt())%>
                </td>
                <td style="<%=td_color%>" align="right">
                    <%=AppConstants.format03(volum.getLes_mt())%>
                </td>

                <%--todo:--%>
                <%if( UserUtils.hasAccessAdministrare(user) ){%>
                    <td style="<%=td_color%>" nowrap>
                        <a href="javascript: void( null)" class="sapBtnStd"  onclick="onEdit(<%=formIndex%>); return false;">Editeaza</a>
                    </td>
                <%}%>
            </tr>
        </form>

        <% formIndex ++; %>
    <%}%>    
<%}%>

</TABLE>
    

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

    function getForma(index){
        return  eval( 'document.<%=WebConstants.FORM%>' + index);
    }

    function onEdit( index){
        if( currentEditingFormaIndex != -1){
            $('tr_inputs'+currentEditingFormaIndex).style.display = 'none';
            $('tr_text'+currentEditingFormaIndex).style.display = '';
        }

        currentEditingFormaIndex = index;
        $('tr_inputs'+currentEditingFormaIndex).style.display = '';
        $('tr_text'+currentEditingFormaIndex).style.display = 'none';
        getForma( index).<%=VolumInstalatiiConstants.NR_CONSUMATORI%>.select();
    }

    function onSalveaza( forma){        
        return isValidData( forma);            
    }

    function isValidData( forma){
        return true;
    }

</script>

<%}%>
