<%@ page import="ro.emid.common.action.dispecer.JSP_OD" %>
<%@ page import="ro.emid.common.constants.GlobalAppConstants" %>
<%@ page import="ro.emid.common.db.CategoryODAccessDB" %>

<%@ page import="ro.emid.common.db.sqlBuilder.Filter" %>
<%@ page import="ro.emid.common.db.sqlBuilder.MatchCriteria" %>
<%@ page import="ro.emid.common.types.CategoryOD" %>
<%@ page import="ro.emid.common.types.CategoryODConstants" %>
<%@ page import="java.util.List" %>


<%
    List filiale = null;
    //incarc lista filiale
    request.setAttribute( JSP_OD.RECORDS,
            filiale = CategoryODAccessDB.getAllCategories(
                    new Filter().
                            addCriteria(new MatchCriteria(CategoryODConstants.SQL.ID_PARENT, MatchCriteria.EQUALS, -1))
            )
    );

    for (int i = 0; i < filiale.size(); i++) {
        final CategoryOD filiala = (CategoryOD) filiale.get(i);

        filiala.setChildren(CategoryODAccessDB.getChildCategs(filiala.getId()));

        final List sucursale = filiala.getChildren();
        if( sucursale != null) for (int j = 0; j < sucursale.size(); j++) {
            final CategoryOD sucursala = (CategoryOD) sucursale.get(j);

            sucursala.setChildren(CategoryODAccessDB.getChildCategs(sucursala.getId()));
        }
    }
%>

<table width="99%">
    <tr>
        <td colspan="2">
            <b>Alocare Centre exploatare pe Dispceri</b>
        </td>
    </tr>
    <tr valign="top">
        <td width="50%">
            <table cellpadding=2 cellspacing=1 width="100%" class="table" >
                <thead>
                    <tr>
                        <th colspan=2 height=25 nowrap>Structura dispeceri:</th>
                    </tr>
                </thead>
                <tbody>
                <%
                    if (null == filiale || filiale.size() == 0) {%>
                        <TR>
                            <TD>
                                No Sunt filiale definite ...
                            </TD>
                        </TR>
                <%} else {
                    for( int i = 0; i < filiale.size(); i++) {
                        final CategoryOD itemFiliala = (CategoryOD) filiale.get( i); %>
                        <tr class="filiala" valign="middle" onclick=" showCentreDed( -1);">
                            <td></td>
                            <TD width="100%">
                                <%=itemFiliala.getName()%>
                            </TD>
                        </tr>

                        <%
                            final List sucursale = itemFiliala.getChildren();
                            if( sucursale != null && sucursale.size() > 0) for (int j = 0; j < sucursale.size(); j++) {
                                final CategoryOD itemSucursala = (CategoryOD) sucursale.get(j); %>
                                <tr class="sucursala" onclick=" showCentreDed( <%=itemSucursala.getId()%>); ">
                                    <td></td>
                                    <td style="padding-left:20px">
                                        <%=itemSucursala.getName()%>
                                    </td>
                                </tr>

                                <%
                                    final List centre = itemSucursala.getChildren();
                                    if( centre != null && centre.size() > 0) for (int k = 0; k < centre.size(); k++) {
                                        final CategoryOD itemCentru = (CategoryOD) centre.get( k);

                                %>
                                <tr class="centru" onclick=" showCentreDed( <%=itemCentru.getId()%>); ">
                                    <td></td>
                                    <td style="padding-left:40px">
                                        <%=itemCentru.getName()%>
                                    </td>
                                </tr>
                                <%
                                        }
                                %>

                                <%
                                        }
                                %>
                                <%}%>
                <%}%>
                </tbody>
            </table>
        </td>

        <td id="centrele" >

            
        </td>



    </tr>
</table>


<script type="text/javascript">
    function showCentreDed( idDed){

        $("centrele").innerHTML = "";

        if( idDed == -1){
            $("centrele").innerHTML = "Fara centre alocate";
        }else{
            var params = "idDed="+idDed;
            new Ajax.Request('<%=GlobalAppConstants.APP_NAME%>?action=getCentreForDed',{
                method: 'post',
                parameters: params,
                onComplete: function ( originalRequest){
                    $("centrele").innerHTML = getResponseMesg( originalRequest.responseText);
                }
            });
        }
    }
</script>
