<%@ page import="ro.emid.Application" %>
<%@ page import="ro.emid.common.constants.AppConstants" %>
<%@ page import="ro.emid.common.db.CategoryAccessDB" %>
<%@ page import="ro.emid.common.types.Category" %>
<%@ page import="ro.emid.common.utils.ObjectUtils" %>
<%@ page import="ro.emid.common.utils.RequestUtils" %>


<%
    final boolean update = RequestUtils.existParameter( request, "update");
    if( update){

        final String []arr = request.getParameterValues("idCategorie");

        if( arr != null && arr.length > 0){

            for (int i = 0; i < arr.length; i++) {
                final String idCateg = arr[i];

                final long id = ObjectUtils.getLongValue( idCateg);

                final int it = RequestUtils.getInt(  request, "IT" + idCateg, 0);
                final int mt = RequestUtils.getInt(  request, "MT" + idCateg, 0);
                final int jt = RequestUtils.getInt(  request, "JT" + idCateg, 0);

                CategoryAccessDB.updateNiveleTensiune( id, it == 1, mt == 1, jt == 1);
            }

            CategoryAccessDB.reload();
        }
    }

    final Category[] categorii = CategoryAccessDB.getAllCategories( " AND length(id)=3");
%>

<form action="<%=AppConstants.APP_NAME%>?action=showUpdateCategoriiScreen" method="post">
    <input type="hidden" name="update" value="true">

    <table border="0" width="600">
        <tr>
            <td>
                <b>Repartizare Centre Exploatare pe nivele tensiune</b>
            </td>

            <td align="right">
                <%
                    if(!Application.runAsAparatCentral()){
                %>
                <input type="submit" value="Salveaza">
                <%
                    }
                %>

            </td>
        </tr>

        <tr>
            <td colspan="2">
                <table class="table" width="100%">
                    <thead>
                        <tr>
                            <th>&nbsp;</th>
                            <th>C.E.</th>
                            <th class="align-center">IT</th>
                            <th class="align-center">MT</th>
                            <th class="align-center">JT</th>
                        </tr>
                    </thead>
                    <tbody>

                    <% for (int i = 0; i < categorii.length; i++) {  %>
                    <%    final Category category = categorii[i]; %>

                    <tr align="center">
                        <td>
                            <%
                                if(!Application.runAsAparatCentral()){
                            %>
                            <input type="checkbox" name="idCategorie" value="<%=category.getId()%>" id="idCategorie<%=category.getId()%>">
                            <%
                                }else{
                            %>
                                <input type="checkbox" name="idCategorie" disabled="disabled" value="<%=category.getId()%>" id="idCategorie<%=category.getId()%>">
                            <%
                                }
                            %>

                        </td>
                        <td align="left">

                            <%=category.getName()%>

                        </td>

                        <%
                            if(!Application.runAsAparatCentral()){
                        %>
                        <td>
                            <input type="checkbox" name="IT<%=category.getId()%>" value="1" <%=category.isIT() ? "checked" : ""%>>
                        </td>
                        <td>
                            <input type="checkbox" name="MT<%=category.getId()%>" value="1" <%=category.isMT() ? "checked" : ""%>>
                        </td>
                        <td>
                            <input type="checkbox" name="JT<%=category.getId()%>" value="1" <%=category.isJT() ? "checked" : ""%>>
                        </td>
                        <%
                        }else{
                        %>
                        <td>
                            <input type="checkbox" disabled="disabled" name="IT<%=category.getId()%>" value="1" <%=category.isIT() ? "checked" : ""%>>
                        </td>
                        <td>
                            <input type="checkbox" disabled="disabled" name="MT<%=category.getId()%>" value="1" <%=category.isMT() ? "checked" : ""%>>
                        </td>
                        <td>
                            <input type="checkbox" disabled="disabled" name="JT<%=category.getId()%>" value="1" <%=category.isJT() ? "checked" : ""%>>
                        </td>
                        <%
                            }
                        %>



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

</form>

