<%@ page import="java.util.List" %>
<%@ page import="ro.emid.common.components.sugest.base.SugestConstants" %>
<%@ page import="ro.emid.common.types.TipInstalatie" %>
<%@ page import="ro.emid.common.types.InstalatieAfectata" %>
<%@ page import="ro.emid.common.utils.BrowseData" %>
<%@ page import="ro.emid.common.utils.BrowseController" %>
<%@ page import="ro.emid.common.constants.AppConstants" %>


<%@ include file="../../../layout/includes-emid.jspf"%>


<%
    final BrowseData data = BrowseController.getBrowseData(session);

    final List editList = BrowseController.getListaInstalatiiSelectate( session);
%>

<style type="text/css">
    html,body{
        background-color: #ffffff !important;
    }
</style>


<TABLE cellPadding=0 cellSpacing=5 style="margin: 10px;">
    <TBODY>
        <tr>
            <td align="left" valign="top">
                <form name="zaforma" action="<%=AppConstants.APP_NAME%>" method="post" style="margin:0">
                    <input name="mode" type="hidden" value="search" />

                    <fieldset style="margin:0; padding-top:0">
                        <legend>Cautare instalatie:</legend>

                        <input type="radio" name="<%=BrowseData.TIP_LF%>" id="statie" value="<%=TipInstalatie.STATIE%>" /> <label for="statie" >Statie</label>
                        <input type="radio" name="<%=BrowseData.TIP_LF%>" id="linie" value="<%=TipInstalatie.LINIE%>" /> <label for="linie" >Linie</label>
                        <input type="radio" name="<%=BrowseData.TIP_LF%>" id="post" checked value="<%=TipInstalatie.PA_PT%>" /> <label for="post" >Post</label>
                        <br />
                        <input type="hidden" name="<%=BrowseData.ID_LF_SELECTAT%>" id="<%=BrowseData.ID_LF_SELECTAT%>" />
                        <input type="text" id="inputSearch" name="inputSearch" class="gsapinputtext" style="width:400px" />
                    </fieldset>

                    <div class="spacer">&nbsp;</div>

                    Lista instalatii selectate: <br>
                    <table id="tableInstalatii" class="gSAPTHEAD" style="BACKGROUND-COLOR: white" width="100%" cellspacing="1">
                        <tr noSelect="true">
                            <TH class="gsapthead" >Instalatie</th>
                            <TH class="gsapthead" width="5%" >Durata Intrerupere</th>
                            <TH class="gsapthead" width="1%">&nbsp;</th>
                        </tr>
                        <% for (int i = 0; i < editList.size(); i++) {
                            final InstalatieAfectata info = (InstalatieAfectata) editList.get( i);

                            final long oreToShow = (info.getDurataIntrerupere() == 0 ? data.getDurataParentOre() : info.getDurataIntrerupere()/60);
                            final long minuteToShow = (info.getDurataIntrerupere() == 0 ? data.getDurataParentMinute() : info.getDurataIntrerupere()%60);
                        %>
                            <tr onclick="showDetaliiLF('<%=info.getCodSapLF()%>')">
                                <td> <%=info.getCodSapLF()%> </td>
                                <td nowrap>
                                    <input type="hidden" name="<%=BrowseData.COD_LF%>" value="<%=info.getCodSapLF()%>" />
                                    <input type="text" class="gsapinputtext" style="width: 30px;text-align:right;" name="durataIntrerupereOre<%=info.getCodSapLF()%>"  value="<%=oreToShow%>"  />h
                                    <input type="text" class="gsapinputtext" style="width: 30px;text-align:right;" name="durataIntrerupereMinute<%=info.getCodSapLF()%>" value="<%=minuteToShow%>" />&nbsp;'
                                </td>
                                <td>
                                    <a class="link" title="Sterge instalatia din lista" href="javascript:void( null)" onclick="deleteInstalatie('<%=info.getCodSapLF()%>'); return false;" >Delete</a>
                                </td>
                            </tr>
                        <%}%>
                    </table>

                    <script type="text/javascript">
                        function deleteInstalatie( codLf){
                            if( confirm('Stergeti instalatia din lista ?')){
                                document.zaforma.action = '<%=AppConstants.APP_NAME%>?action=removeInstalatie&codLfToDelete='+codLf;
                                document.zaforma.submit();
                            }
                        }
                    </script>
                </form>
            </td>
        </tr>
    </TBODY>
</TABLE>

<script type="text/javascript">

    function save(){
        document.zaforma.action = '<%=AppConstants.APP_NAME%>?action=saveInstalatiiAfectate';
        document.zaforma.submit();
    }

    function showDetaliiLF( codInstalatie){
        window.parent.BrowseWindow.showDetaliiInstalatie( codInstalatie, <%=data.getAn()%>);
    }

    //suggest
    var ajaxDataProvider = new AjaxDataProvider( function(){
        var tipLF = ( $("statie").checked ? $("statie").value :
                        ( $("linie").checked ? $("linie").value :
                                ( $("post").checked ? $("post").value :
                                    -1)));
        return "<%=AppConstants.APP_NAME%>?action=suggestInstalatie&tip=" + tipLF + "&<%=SugestConstants.SUGEST_Q_QUERY%>=";
    });

    var ajaxSugest = new InputSugest( $("inputSearch"), $("<%=BrowseData.ID_LF_SELECTAT%>"), ajaxDataProvider, {
        onSelect : function( item){
            document.zaforma.action='<%=AppConstants.APP_NAME%>?action=addInstalatie';
            document.zaforma.submit();
        }
    });

    ajaxSugest.maxCount = 20;

    $( "inputSearch").focus();

    var tableInstalatii = new Table('tableInstalatii', "tableInstalatii");

    document.body.className = "";
</script>
