<%@ page import="ro.emid.common.constants.AppConstants" %>
<%@ page import="ro.emid.common.constants.CategoriiInstalatii" %>
<%@ page import="ro.emid.common.constants.GlobalAppConstants" %>
<%@ page import="ro.emid.common.constants.LucrareProgramataConstants" %>
<%@ page import="ro.emid.common.db.NomAccessDB" %>
<%@ page import="ro.emid.common.types.*" %>
<%@ page import="ro.emid.common.utils.ObjectUtils" %>
<%@ page import="ro.emid.common.utils.StringUtils" %>
<%@ 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="ro.emid.lpod.db.InstalatieDB" %>
<%@ page import="ro.emid.lpod.obj.CriteriuFiltruLucrareProgramata" %>
<%@ page import="ro.emid.lpod.obj.FiltruLucrareProgramata" %>
<%@ page import="ro.emid.lpod.obj.FiltruLucrareProgramataConstants" %>
<%@ page import="java.util.List" %>
<%@ page import="ro.emid.common.utils.SessionUtils" %>
<%@ page import="ro.emid.lpod.LpodSessionUtils" %>

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

<%
    /**
     * lucrari-programate
     * @see ro.emid.lpod.action.lucrariProgramate.LoadLucrariProgramateAction
     */

    User user = SessionUtils.getUserLogat(session);
    Category ce = LpodSessionUtils.getCentru(session);

    final SortData sd = (SortData) request.getAttribute("sd");
    final PaginationData pd = (PaginationData) request.getAttribute("pd");

    final FiltruLucrareProgramata filtru = (FiltruLucrareProgramata) request.getAttribute("filtru");

    final List<Integer> ani = (List<Integer>) request.getAttribute("ani");
    final List<LucrareProgramata> list = (List<LucrareProgramata>) request.getAttribute("list");
%>
<emid:template-lp title="Lucrari programate">
    <jsp:attribute name="breadcrumb">
        <emid:localizare-lp>
            <span class="slash">/</span> Lucrări programate
        </emid:localizare-lp>
    </jsp:attribute>
    <jsp:attribute name="body">
        <form name="searchForm" action="<%=AppConstants.APP_NAME%>?action=lucrari-programate" method="post"  >
            <%=SortController.initSort( sd)%>
            <table width="100%" border=0>
                <tr><td>
                    <%  if( filtru != null && filtru.hasCriteriiSelectate()){%>
                    <table width="100%" cellspacing="0" class="gsaptd" border=0>
                        <tr valign="top">
                            <td width="10"><b>Filtru:</b></td>
                            <td>
                                <% boolean newLine = false;%>
                                <%if( filtru.getDataStart() != null){ newLine = true;%>
                                De la data: <b><%=AppConstants.format(filtru.getDataStart())%></b>
                                <%}%>
                                <%if( filtru.getDataStop() != null){ newLine = true;%>
                                pana la data: <b><%=AppConstants.format(filtru.getDataStop())%></b>
                                <%}%>

                                <%if(newLine){ %><br><%}%>
                                <%
                                    final List criterii = filtru.getCriteriiFiltru();
                                    for (int i = 0; i < criterii.size(); i++) {
                                        final CriteriuFiltruLucrareProgramata criteriu = (CriteriuFiltruLucrareProgramata) criterii.get(i); %>
                                <%=criteriu.getText()%>
                                <%if( i < criterii.size() - 1 ){%><br><%}%>
                                <%}%>
                            </td>
                            <td align="right" width="80" nowrap>
                                <a title="Click pentru a reseta filtrul" class="link" href="<%=GlobalAppConstants.APP_NAME%>?action=resetFiltruIncidente">Reset Filtru</a>
                            </td>
                        </tr>
                    </table>
                    <div class="spacer"></div>
                    <%}%>

                    <div id="divFiltru">
                        Număr : <input class="gsapinputtext" name="<%=FiltruLucrareProgramataConstants.NUMAR_LUCRARE%>" type="text" value="<%=(filtru != null && filtru.getNumar() > 0 ? String.valueOf(filtru.getNumar()) : "")%>"/>&nbsp;

                        <% final int anSelectat = filtru != null && filtru.getAnul() > 0 ? filtru.getAnul() : -1; %>
                        Din anul: <select name="<%=FiltruLucrareProgramataConstants.AN%>" class="gSAPSelect">
                        <option value="-1">&nbsp;</option>
                        <% for (Integer an : ani) { %>
                        <option value="<%=an%>" <%=anSelectat == an ? "selected" : ""%> ><%=an%></option>
                        <% }%>
                    </select>&nbsp;
                        <input type="submit" style="font-size:10px" value="Caută" />
                    </div>
                    <div class="spacer"></div>

                    <table border=0 cellpadding=2 cellspacing=1 width="100%" class="table">
                        <thead>
                        <tr>
                            <th height=25 colspan="2" noWrap>&nbsp;Localizare lucrare:&nbsp;</th>
                            <th nowrap width=90>&nbsp;<%=SortController.printSortableColumn("Tensiune:", ro.emid.common.constants.LucrareProgramataConstants.SQL.NIVEL_TENSIUNE, sd)%></th>
                            <th nowrap width=120>&nbsp;<%=SortController.printSortableColumn("Programata la:", LucrareProgramataConstants.SQL.DATA_PROGRAMARE_START, sd)%></th>
                            <th nowrap width=90>&nbsp;<%=SortController.printSortableColumn("Autor:", LucrareProgramataConstants.SQL.USER_INSERT, sd)%></th>
                            <th nowrap width=90>&nbsp;<%=SortController.printSortableColumn("Modificat:", LucrareProgramataConstants.SQL.USER_UPDATE, sd)%></th>
                            <th nowrap width="1%">&nbsp;Operatie:&nbsp;</th>
                        </tr>
                        </thead>
                        <tbody>

                        <% if( list == null || list.size() == 0){%>
                        <tr>
                            <td class="no-data" colspan="10">
                                Nu există date ...
                            </td>
                        </tr>
                        <%} else {

                            for (int i = 0; i < list.size(); i++) {
                                final LucrareProgramata lp = (LucrareProgramata) list.get( i);
                                final StringBuffer parametriTitluLucrare = new StringBuffer();

                                if( NivelTensiune.is( lp.getNivelTensiune(), NivelTensiune.JT)){

                                    if ( lp.getInstalatia() > 0 ) {

                                        parametriTitluLucrare.append("<span title=\"Instalatia\">").append(CategoriiInstalatii.Helper.getDenumireInstalatie(lp.getInstalatia(), "[necompletat]")).append("</span>");

                                    } else if( lp.getCatInstalatii() > 0) {

                                        parametriTitluLucrare.append("<span title=\"Categoria de Instalatii\">").append(CategoriiInstalatii.Helper.getCategoriiInstalatie(lp.getCatInstalatii(), "[necompletat]")).append("</span>");

                                    } else {

                                        parametriTitluLucrare.append( "<span style=\"color: red\">Tip Instalatie nespecificat</span>");

                                    }
                                    parametriTitluLucrare.append(", ");

                                    parametriTitluLucrare.append("<span title=\"Nr./Denumire instalatie\">").append(StringUtils.getNullSafe(lp.getDenumireInstalatie(), "<span style=\"color:red\">Nr./Denumire necompletat</span>")).append("</span>");

                                } else if( NivelTensiune.is( lp.getNivelTensiune(), NivelTensiune.IT) || NivelTensiune.is( lp.getNivelTensiune(), NivelTensiune.MT)){
                                    if (lp.getAnsambluId() != -1) {
                                        final Nomenclator ansamblu = NomAccessDB.getNom(lp.getAnsambluId());
                                        if (ansamblu != null) parametriTitluLucrare.append("<span title=\"Ansamblul Functional\">").append( ansamblu.getDescriere()).append("</span>, ");
                                    }

                                    if (!StringUtils.isZeroLength(lp.getStatieDen())) {
                                        final Instalatie data = InstalatieDB.getById(lp.getStatieId(), TipInstalatie.Helper.isLocFunctional(TipInstalatie.STATIE));
                                        if (data != null) parametriTitluLucrare.append("<span title=\"Statia\">").append(data.getNume()).append("</span>, ");
                                    }

                                    if (!StringUtils.isZeroLength(lp.getCelulaDen())) {
                                        final Instalatie data = InstalatieDB.getById(lp.getCelulaId(), TipInstalatie.Helper.isLocFunctional(TipInstalatie.CELULA));
                                        if (data != null) parametriTitluLucrare.append("<span title=\"Celula\">") .append(data.getNume()).append("</span>, ");
                                    }

                                    if (!StringUtils.isZeroLength(lp.getBaraDen())) {
                                        final Instalatie data = InstalatieDB.getById(lp.getBaraId(), TipInstalatie.Helper.isLocFunctional(TipInstalatie.BARA));
                                        if (data != null) parametriTitluLucrare.append("<span title=\"Bara\">") .append(data.getNume()).append("</span>, ");
                                    }

                                    if (!StringUtils.isZeroLength(lp.getBobinaDen())) {
                                        final Instalatie data = InstalatieDB.getById(lp.getBobinaId(), TipInstalatie.Helper.isLocFunctional(TipInstalatie.BOBINA));
                                        if (data != null) parametriTitluLucrare.append("<span title=\"Bobina\">") .append(data.getNume()).append("</span>, ");
                                    }

                                    if (!StringUtils.isZeroLength(lp.getLinieDen())) {
                                        final Instalatie data = InstalatieDB.getById(lp.getLinieId(), TipInstalatie.Helper.isLocFunctional(TipInstalatie.LINIE));
                                        if (data != null) parametriTitluLucrare.append("<span title=\"Linia\">") .append(data.getNume()).append("</span>, ");
                                    }

                                    if (!StringUtils.isZeroLength(lp.getPaptDen())) {
                                        final Instalatie data = InstalatieDB.getById(lp.getPA_PT_Id(), TipInstalatie.Helper.isLocFunctional(TipInstalatie.PA_PT));
                                        if (data != null) parametriTitluLucrare.append("<span title=\"PA/PT\">") .append(data.getNume() ).append("</span>, ");
                                    }

                                    if (!StringUtils.isZeroLength(lp.getTrafoDen())) {
                                        final Instalatie data = InstalatieDB.getById(lp.getTrafoId(), TipInstalatie.Helper.isLocFunctional(TipInstalatie.TRAFO));
                                        if (data != null) parametriTitluLucrare.append("<span title=\"Trafo\">") .append(data.getNume() ).append("</span>, ");
                                    }
                                }

                        %>
                        <tr valign="top">
                            <td align="center" width="15" title="Numar Fisa Lucrare "><%=lp.getNumar()%></td>
                            <td><%=parametriTitluLucrare%></td>
                            <td align="center" title="Nivel tensiune"><%=lp.getNivelTensiune()%></td>
                            <td align="center" title="Data programare" nowrap><%=AppConstants.format(lp.getDataProgramataStart())%></td>
                            <td align="center" title="Autor" ><%=ObjectUtils.getStringValue( lp.getUserInsert())%></td>
                            <td align="center" title="Actualizata de" ><%=ObjectUtils.getStringValue( lp.getUserUpdate())%></td>
                            <td align="left" nowrap >
                                <% if( AppConstants.runAtFiliala()){ %>
                                <% if ( UserUtils.hasRoleUtilizatorLucrariProgramate(user) && ( UserUtils.hasAccessAdministrare(user) || user.getUsername().equals(lp.getUserInsert()) || UserUtils.isNivelSuperiorLP(user, lp.getUserInsert()))) { %>
                                <a class="link" title="Modifica fisa lucrare programata" href="<%=GlobalAppConstants.APP_NAME%>?action=edit-lucrare&id=<%=lp.getId()%>">Modifică</a>
                                <a class="link" title="Sterge fisa lucrare programata" onclick="return confirm('Stergeti lucrarea ?')" href="<%=GlobalAppConstants.APP_NAME%>?action=delete-lucrare&id=<%=lp.getId()%>">Șterge</a>
                                <% } %>

                                <a class="link" href="#" onclick="printLucrare( <%=lp.getId()%>)">
                                    <%= UserUtils.hasRoleViewLucrariProgramate(user) ? "Vizualizare": "Printează"%>
                                </a>
                                <% } else { %>
                                <a class="link" href="#" onclick="printLucrare( <%=lp.getId()%>)">Vizualizare</a>
                                <% } %>
                            </td>
                        </tr>
                        <%}%>
                        <%}%>
                        </tbody>
                        <tfoot>
                        <tr>
                            <td colspan="8" >
                                <div class="pull-left">
                                    <% if( AppConstants.runAtFiliala() && UserUtils.hasRoleUtilizatorLucrariProgramate(user) && ce != null && ce.getId()>0){%>
                                    &nbsp;<input type="button" data-href="<%=GlobalAppConstants.APP_NAME%>?action=edit-lucrare" value="Adaugă lucrare"/>
                                    <% } %>
                                </div>
                                <div class="pull-right">
                                    <%=PaginationController.printPaginationData( pd)%>
                                </div>
                            </td>
                        </tr>
                        </tfoot>
                    </table>
                </td></tr>
            </table>
        </form>

        <script type="text/javascript">
            function reloadData(){
                window.location = "<%=AppConstants.APP_NAME%>?action=lucrari-programate";
            }
        </script>


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







