<%@ page import="ro.emid.Application" %>
<%@ page import="ro.emid.administrare.sync.ac.SyncItem" %>
<%@ page import="ro.emid.administrare.sync.ac.SyncItemType" %>
<%@ page import="ro.emid.common.constants.AppConstants" %>
<%@ page import="ro.emid.common.utils.ObjectUtils" %>
<%@ page import="ro.emid.common.utils.pagination.PaginationController" %>
<%@ page import="ro.emid.common.utils.pagination.PaginationData" %>
<%@ page import="java.util.List" %>
<%@ page import="ro.emid.administrare.sync.ac.SyncAcConstants" %>

<%
    /**
     * @see ro.emid.administrare.sync.ac.LoadSyncAcItemsAction
     */

    String tipSelectat = ObjectUtils.getStringValue(request.getParameter("tip"));
    if (tipSelectat == null || tipSelectat.length() == 0) tipSelectat = "i";

    String numar = ObjectUtils.getStringValue(request.getParameter("numar"));

    int status = ObjectUtils.getIntValue(request.getParameter("status"), 0);

    final PaginationData pd = (PaginationData) request.getAttribute("pd");
    final List result = (List) request.getAttribute("items");

    boolean existaDateDeSincronizat = result.size() > 0;

%>


<div class="mb5">
    <span class="page-title">Sincronizare date cu aparat central</span>
</div>

<form action="<%=AppConstants.APP_NAME%>?action=sync-ac-manager" method="post">
    Tip fisa:
    <select name="tip" id="selectTipFisa">
        <option value="<%=SyncItemType.ALL%>" <%=tipSelectat.equals(SyncItemType.ALL) ? "selected" : ""%> >Toate fisele</option>
        <option value="<%=SyncItemType.FI%>"  <%=tipSelectat.equals(SyncItemType.FI) ? "selected" : ""%> >Fise Incident</option>
        <option value="<%=SyncItemType.FE%>"  <%=tipSelectat.equals(SyncItemType.FE) ? "selected" : ""%> >Fise Echipament</option>
        <option value="<%=SyncItemType.FD%>"  <%=tipSelectat.equals(SyncItemType.FD) ? "selected" : ""%> >Fise Deranjament</option>
        <option value="<%=SyncItemType.FLP%>" <%=tipSelectat.equals(SyncItemType.FLP) ? "selected" : ""%> >Fise Lucrari Programate</option>
        <option value="<%=SyncItemType.FED%>" <%=tipSelectat.equals(SyncItemType.FED) ? "selected" : ""%> >Fise Eveniment Dispecer</option>
    </select>

    &nbsp;&nbsp;
    Stare:
    <select name="status">
        <option value="-1">Toate starile</option>
        <option value="1" <%=status == 1 ? "selected" : ""%>>Sincronizate</option>
        <option value="0" <%=status == 0 ? "selected" : ""%>>Nesincronizate</option>
    </select>

    &nbsp;&nbsp;Numar fisa:
    <input type="text" name="numar" value="<%=numar%>"/>

    <input type="submit" value="Aplică"/>

    <br/>

    <table width="100%" class="table mt5" cellpadding="2" cellspacing="1">
        <thead>
        <tr>
            <th width="50px">TIP</th>
            <th>ID</th>
            <th>Numar</th>
            <th>Filiala</th>
            <th>Sucursala</th>
            <th>Centru</th>
            <th width="100px" class="align-center">Sincronizat</th>
        </tr>
        </thead>
        <tbody>
        <%  if (result.size() == 0) { %>
            <tr>
                <td colspan="10" class="no-data">
                    Nu exista date.
                </td>
            </tr>
        <% } else for (int i = 0; i < result.size(); i++) { %>
        <% ro.emid.administrare.sync.ac.SyncItem item = (ro.emid.administrare.sync.ac.SyncItem) result.get(i);%>
        <tr>
            <td><%=item.getTip()%></td>
            <td><%=item.getId()%></td>
            <td><%=item.getNumar()%></td>
            <td><%=item.getFiliala()%></td>
            <td><%=item.getSucursala()%></td>
            <td><%=item.getCentru()%></td>
            <td align="center"><%=item.getStatus() == 1 ? "DA" : "NU"%></td>
        </tr>
        <% } %>
        </tbody>
        <tfoot>
        <tr>
            <td colspan="7">
                <div class="pull-left">
                    <% if(Application.runAsFiliala()){ %>
                    <input id="btnSyncFise"
                           type="button"
                           value="setat de functia initUI"
                           onclick="syncFise(this)"
                           class="mr5"
                           title="setat de functia initUI"
                            <%if(!existaDateDeSincronizat){%> disabled="disabled" <%}%>
                    />
                    <input id="btnSyncNom"
                           type="button"
                           value="Sincronizeaza nomenclatoare"
                           onclick="syncNomenclator( this)"
                           class="mr5"
                           title="Trimite toate nomenclatoarele catre Aparat Central"/>
                    <input type="button"
                           value="Istoric sincronizări"
                           data-href="<%=AppConstants.APP_NAME%>?action=sync-ac-history"/>
                    <% } %>
                </div>
                <div class="pull-right">
                    <%=PaginationController.printPaginationData(pd)%>
                </div>
            </td>
        </tr>
        </tfoot>
    </table>
</form>
<br/>


<script type="text/javascript">

    var btnSyncFise = null;
    var selectedTipText = null;
    var selectedType = null;

    function syncFise(btn) {
        if( !<%=existaDateDeSincronizat%>){
            alert("Nu exista date pentru sincronizare.");
            return;
        }

        if (confirm('Doriti sincronizare pentru (' + selectedTipText + ') cu serverul Aparat Central ?')) {
            window.location = "<%=AppConstants.APP_NAME%>?action=sync-fise-ac&type="+selectedType;
        }
    }

    function syncNomenclator(btn) {
        if (confirm('Doriti sincronizarea nomenclatoarelor cu serverul Aparat Central ?')) {
            window.location = "<%=AppConstants.APP_NAME%>?action=sync-nomenclator-ac";
        }
    }

    jQuery(function(){
        btnSyncFise = jQuery("#btnSyncFise");
        selectedTipText = jQuery("#selectTipFisa option:selected").text();
        selectedType = jQuery("#selectTipFisa option:selected").val();

        btnSyncFise.val("Sincronizeaza " + selectedTipText);
        btnSyncFise.attr("title", (selectedType == "<%=ro.emid.administrare.sync.ac.SyncItemType.ALL%>" ? "Trimite toate fisele " + selectedTipText + " catre Aparat Central" : "Trimite toate " + selectedTipText.toLocaleLowerCase() + " catre Aparat Central"));
    });

</script>
