<%@ page import="ro.emid.Application" %>
<%@ page import="ro.emid.common.constants.AppConstants" %>
<%@ page import="ro.emid.common.constants.RoleConstants" %>
<%@ page import="ro.emid.common.types.CategoryCheckData" %>
<%@ page import="ro.emid.common.types.CategoryOD" %>
<%@ page import="ro.emid.common.types.RaportOdRow" %>
<%@ page import="ro.emid.common.types.User" %>
<%@ page import="ro.emid.common.utils.ObjectUtils" %>
<%@ page import="ro.emid.common.utils.StringUtils" %>
<%@ page import="ro.emid.common.utils.Util" %>
<%@ page import="ro.emid.lpod.LpodSessionUtils" %>
<%@ page import="ro.emid.lpod.db.CategoryODAccessDB" %>
<%@ page import="java.util.Date" %>
<%@ page import="java.util.HashSet" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.Set" %>

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

<%
    /**
     * @see ro.emid.lpod.action.reports.RaportEvenimenteODAction
     */

    final User user = LpodSessionUtils.getUserLogat(session);
    final CategoryOD filiala = LpodSessionUtils.OD.getFiliala(session);
    final CategoryOD sucursala = LpodSessionUtils.OD.getSucursala(session);
    final CategoryOD centruExploatare = LpodSessionUtils.OD.getCentru(session);

    request.setAttribute("ctx", AppConstants.APP_NAME);
%>

<emid:template-od title="Raport evenimente">
    <jsp:attribute name="breadcrumb">
        <emid:localizare-od>
            <span class="slash">/</span> <a class="link" href="${ctx}?action=rapoarte-od">Rapoarte</a>
            <span class="slash">/</span> Raport Evenimente
        </emid:localizare-od>
    </jsp:attribute>
    <jsp:attribute name="body">

<%
    if( Application.runAsFiliala() ) {
        if( filiala == null || filiala.getId() < 0){
            %>

            <br/>
            Va rugam sa selectati o filiala din <a class="link" href="<%=AppConstants.APP_NAME%>?action=home" >pagina principala.</a>
            <br/>
            <br/>

            <%
            return ;
        }
    }else if ( Application.runAsAparatCentral() ){
        //  do nothing yet
    }

    if( user.getOdLevel() > RoleConstants.NIVEL_FILIALA && ((sucursala == null || sucursala.getId() <= 0) && ( centruExploatare == null || centruExploatare.getId() <= 0))
    ){  %>
    <br>
         Va rugam sa selectati un DED / DED-L din
         <a class="link" href="<%=AppConstants.APP_NAME%>?action=home" >pagina principala.</a>
    <br/>
    <br/>
    <%
        return;
    }
    List result = (List) request.getAttribute("result");
    Date dataStart = (Date) request.getAttribute("dataStart");
    Date dataStop  = (Date) request.getAttribute("dataStop");

    Boolean error = (Boolean) request.getAttribute("error");


    final boolean checkBifaDedl = (sucursala != null && sucursala.getId() > 0) && (centruExploatare == null || centruExploatare.getId() <= 0);

    boolean faraDEDL;

    if (checkBifaDedl) {
        faraDEDL = 1 == ObjectUtils.getIntValue(request.getParameter("fara_dedl"), 0);
    } else {
        faraDEDL = true;
    }


    final CategoryCheckData checkData = LpodSessionUtils.getCategoriiSelectate(session);
%>

<style type="text/css">
    .bordered td{
        border : 1px solid #999999;
        background-color: white;
    }

</style>

<table border=0 width="80%" style="margin-top: 10px;">

    <tr>
        <td align="center" style="font-weight:bold;">RAPORT EVENIMENTE</td>
    </tr>

    <tr>
        <td>
            <form name="zaforma" action="<%=AppConstants.APP_NAME%>?action=raport-evenimente-od" method="post" style="margin-top: 10px">
                <input type="hidden" name="toExport" value="0">

                <table>
                    <tr valign="top">
                        <td align="left">

                            <A href="#" title="Click pentru a selecta mai multe categorii" onClick="openCheckCategorii(); return false" class="link"><IMG alt="Click pentru a selecta mai multe sucursale/C.E." border=0 width=12 height=13 hspace=3 src="<%= AppConstants.WEB_RESOURCE_PATH + "/images/icon_mini_search.gif" %>"></A>
                            &nbsp;
                            Data :
                            <emid:datetimefield name="data_start" value="<%=dataStart%>" hourFirst="true"/>
                            -
                            <emid:datetimefield name="data_stop" value="<%=dataStop%>" hourFirst="true"/>


                            <% if ( checkBifaDedl) { %>

                                &nbsp;

                                <input type="checkbox" name="fara_dedl" value="1" <%=faraDEDL ? "checked" :"" %> id="fara_dedl">
                                <label for="fara_dedl">Fara DED-L subordonate</label>

                            <% }  %>

                        </td>

                        <td align="right">
                            &nbsp;<input type="submit" value="Ruleaza raport" />
                            &nbsp;<input type="button" value="Export" onclick="doExport()" />

                            <script type="text/javascript">
                                 function doExport(){
                                     document.zaforma.toExport.value = '1';
                                     document.zaforma.submit();
                                     setTimeout(function(){
                                         document.zaforma.toExport.value = '0';
                                     }, 300);
                                 }
                            </script>
                        </td>
                    </tr>
                    <tr>
                    <% if (error.booleanValue()){%>
                      <tr>
                         <td>
                             <span id="spanMaxPeriod" style="color: red;">
                                Va rugam selectati o perioada de maxim 90 de zile.
                             </span>
                         </td>
                      </tr>
                      <%}%>

                    <% if( checkData.someDataChecked()){
                            final StringBuffer strCategorii = new StringBuffer("");
                            final long[] idCentre = checkData.getIdCentre();
                            final Set sucursaleSelectate = new HashSet();
                            final Set filialeSelectate = new HashSet();
                            for (int i = 0; i < idCentre.length; i++) {
                                final long id = idCentre[i];
                                final CategoryOD c = CategoryODAccessDB.getById(id);
                                if (c != null) {
                                    final CategoryOD s = CategoryODAccessDB.getById(c.getIdParent());
                                    if (s != null) {
                                        final CategoryOD f = CategoryODAccessDB.getById(s.getIdParent());
                                        if (f != null) {
                                            if (strCategorii.length() > 0) strCategorii.append("<br>");
                                            strCategorii.append(f.getName()).append(", ").append(s.getName()).append(", ").append(c.getName());

                                            sucursaleSelectate.add(new Long(s.getId()));
                                            filialeSelectate.add(new Long(f.getId()));
                                        }
                                    }
                                }
                            }

                            final long[] idSucursale = checkData.getIdSucursale();
                            for (int i = 0; i < idSucursale.length; i++) {
                                final long id = idSucursale[i];
                                if( !sucursaleSelectate.contains( new Long( id))){
                                    final CategoryOD s = CategoryODAccessDB.getById( id );
                                    if( s != null){
                                        final CategoryOD f = CategoryODAccessDB.getById( s.getIdParent());
                                        if( f != null){
                                            if( strCategorii.length() > 0) strCategorii.append("<br>");
                                            strCategorii.append(f.getName()).append(", ").append(s.getName());
                                        }
                                    }
                                }
                            }

                            final long[] idFiliale = checkData.getIdFiliale();
                            for (int i = 0; i < idFiliale.length; i++) {
                                final long id = idFiliale[i];
                                if( !filialeSelectate.contains( new Long( id))){
                                    final CategoryOD f = CategoryODAccessDB.getById( id);
                                    if( f != null){
                                        if( strCategorii.length() > 0) strCategorii.append("<br>");
                                        strCategorii.append(f.getName());
                                    }
                                }
                            }
                        %>

                        <%=strCategorii%>

                    <% } %>
                </table>

                <table border="1" style="margin-top: 10px; font-size:12px" cellspacing="1" cellpadding="3" width="100%">

                    <% if( !error.booleanValue() && (result == null || result.size() == 0)){ %>
                        <tr>
                            <td >Fara evenimente ...</td>
                        </tr>

                    <% } else for (int i = 0; i < result.size(); i++) {
                        final RaportOdRow row = (RaportOdRow) result.get(i); %>

                        <tr>
                            <td>
                                <table width="100%">
                                    <tr>
                                        <td>
                                            <nobr><b><%=row.getLocatie()%> EVENIMENT: <%=AppConstants.format(row.getDataEveniment())%></b> </nobr>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>
                                            <nobr>
                                                <%=AppConstants.format(row.getDenLocatie(), "")%>
                                                <% if( !StringUtils.isZeroLength( row.getDenLocatie2())) {%>
                                                    ,<%=AppConstants.format(row.getDenLocatie2())%>
                                                <% } %>
                                            </nobr>

                                            <br/> <%=Util.formatForHtml( row.getDescriereEveniment())%>

                                            <% if ( !StringUtils.isZeroLength( row.getDefect() )) { %>
                                                <br/>
                                                Defect: <%=Util.formatForHtml(row.getDefect())%>
                                            <% } %>

                                            <% if ( !StringUtils.isZeroLength( row.getSemnalizari() )) { %>
                                                <br/>
                                                Semnalizari: <%=Util.formatForHtml( row.getSemnalizari())%>
                                            <% } %>

                                            <br/>
                                            <nobr>
                                                <%
                                                    final double pi = ObjectUtils.getDoubleValue( row.getPi(), -1)/1000;

                                                    final long durataOre    = row.getDurataEveniment()/60;
                                                    final long durataMinute = row.getDurataEveniment()%60;
                                                %>
                                                P.intrerupta = <%=pi != -1 ? AppConstants.format2(pi) + " MW" : row.getPi() + " kW (conform fisa)"%>  &nbsp;&nbsp;t = <%=durataOre > 0 ? durataOre + " ore " : ""%> <%=durataMinute%> minute
                                            </nobr>

                                            <% if ( !StringUtils.isZeroLength( row.getObservatii() )) { %>
                                                <br/>
                                                Observatii: <%=Util.formatForHtml( row.getObservatii())%>
                                            <% } %>
                                        </td>
                                    </tr>
                                </table>
                            </td>
                        </tr>

                    <% } //for %>

                </table>
            </form>
        </td>
    </tr>


</table>


    </jsp:attribute>
</emid:template-od>

<script type="text/javascript">
    function categoriesChecked(){
        document.zaforma.submit();
    }
</script>


