<%@ page import="ro.emid.common.db.ActionLogger" %>
<%@ page import="ro.emid.common.utils.ObjectUtils" %>
<%@ page import="ro.emid.common.constants.AppConstants" %>
<%@ page import="java.sql.Date" %>
<%@ page import="java.util.Calendar" %>
<%@ page import="ro.emid.common.types.LogEntry" %>
<%@ page import="java.util.Hashtable" %>
<%@ page import="ro.emid.common.db.CategoryAccessDB" %>
<%@ page import="ro.emid.common.db.framework.DatabaseException" %>
<%@ page import="ro.emid.common.types.Category" %>
<%@ page import="ro.emid.common.constants.AppConstants" %>


<SPAN style="FONT-SIZE: 12px; FONT-WEIGHT: bold">
    Log-uri
    &nbsp;&nbsp;&nbsp;<a class="link" href="<%=AppConstants.APP_NAME%>?action=showLogs" >Incidente</a>
    &nbsp;&nbsp;&nbsp;<a class="link" href="<%=AppConstants.APP_NAME%>?action=showLogs" >Callcenter / Deranjamente</a>
    &nbsp;&nbsp;&nbsp;<a class="link" href="<%=AppConstants.APP_NAME%>?action=showLogsLPD" >Lucrari Programate</a>
    &nbsp;&nbsp;&nbsp;<a class="link" href="<%=AppConstants.APP_NAME%>?action=showLogsLPD" >Operatiuni Dispecer</a>
</SPAN>
<br />
<%

    final String selectedAutor = ObjectUtils.getStringValue(request.getParameter("autor"));
    final int selectedAppObiect = ObjectUtils.getIntValue(request.getParameter("app_obiect"), -100);
    Date dataStart = ObjectUtils.getDateValue(ObjectUtils.getStringValue(request.getParameter("dataStart")), AppConstants.FORMAT_DATA_TIME);
    Date dataStop = ObjectUtils.getDateValue(ObjectUtils.getStringValue(request.getParameter("dataStop")), AppConstants.FORMAT_DATA_TIME);


    if (dataStart == null) {
        final Calendar c = Calendar.getInstance();
        c.set(Calendar.HOUR_OF_DAY, 7);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MILLISECOND, 0);
        dataStart = new Date(c.getTime().getTime());
    }
    if (dataStop == null) dataStop = new Date(System.currentTimeMillis());

    String condSql = "";
    if (selectedAutor != null && selectedAutor.trim().length() > 0) {
        condSql += " AND autor = '" + ro.emid.common.utils.Util.escapeToSql(selectedAutor) + '\'';
    }

    if (selectedAppObiect != -100) {
        if (selectedAppObiect == -1) {    // App. Incidente
            condSql +=
                    " AND object_type IN ("
                            + ActionLogger.OBJECT_TYPE_INCIDENT + ", "
                            + ActionLogger.OBJECT_TYPE_ECHIPAMENT + ", "
                            + ActionLogger.OBJECT_TYPE_EVENIMENT + ", "
                            + ActionLogger.OBJECT_TYPE_NOMENCLATOR + ", "
                            + ActionLogger.OBJECT_TYPE_USER
                            + ") ";
        } else if (selectedAppObiect == -2) {  //App. deranjamente
            condSql +=
                    " AND object_type IN ("
                            + ActionLogger.OBJECT_TYPE_DERANJAMENT + ", "
                            + ActionLogger.OBJECT_TYPE_LITTLE_INCIDENT + ", "
                            + ActionLogger.OBJECT_TYPE_LUCRARE_PROGRAMATA + ", "
                            + ActionLogger.OBJECT_TYPE_CLIENT_ELIGIBIL
                            + ") ";
        } else if (selectedAppObiect == -3) {   //App. Call center
            condSql +=
                    " AND object_type IN ("
                            + ActionLogger.OBJECT_TYPE_RECLAMATIE
                            + ") ";
        } else if (
                selectedAppObiect == ActionLogger.OBJECT_TYPE_INCIDENT ||
                        selectedAppObiect == ActionLogger.OBJECT_TYPE_ECHIPAMENT ||
                        selectedAppObiect == ActionLogger.OBJECT_TYPE_EVENIMENT ||
                        selectedAppObiect == ActionLogger.OBJECT_TYPE_USER ||
                        selectedAppObiect == ActionLogger.OBJECT_TYPE_NOMENCLATOR ||
                        selectedAppObiect == ActionLogger.OBJECT_TYPE_DERANJAMENT ||
                        selectedAppObiect == ActionLogger.OBJECT_TYPE_CLIENT_ELIGIBIL ||
                        selectedAppObiect == ActionLogger.OBJECT_TYPE_LITTLE_INCIDENT ||
                        selectedAppObiect == ActionLogger.OBJECT_TYPE_LUCRARE_PROGRAMATA
                ) {
            condSql += " AND object_type =  " + selectedAppObiect;
        }
    }
%>


<% final LogEntry[] entries = ActionLogger.getLogEntries(dataStart, dataStop, condSql);%>

<%!
    private final java.text.SimpleDateFormat sdf1 = new java.text.SimpleDateFormat("dd.MM.yyyy k:mm");

    private String getLogEntryText(final LogEntry entry) throws DatabaseException {
        final Hashtable hSucursale = CategoryAccessDB.getHashAllSucursale();
        final StringBuffer item = new StringBuffer("");
        final long date = entry.getData().getTime();
        int action = entry.getAction();
        final String autor = entry.getAutor();
        final long sucursala_id = entry.getSucursalaId();
        final String objectId = entry.getObjectId();
        final int objectType = entry.getObjectType();
        item.append(sdf1.format(new Date(date)));
        String saction = "[not defined]";
        if (action == ActionLogger.ACTION_DELETE) saction = "sters";
        if (action == ActionLogger.ACTION_EDIT) saction = "modificat";
        if (action == ActionLogger.ACTION_INSERT) saction = "inserat";
        if (action == ActionLogger.ACTION_DEZARHIVAT) saction = "dezarhivat";
        if (action == ActionLogger.ACTION_ARHIVAT) saction = "arhivat";
        if (action == ActionLogger.ACTION_CONVERTIT_INCIDENT) saction = "trimis la incidente";
        if (action == ActionLogger.ACTION_CONVERTIT_ECHIPAMENT) saction = "trimis la echipamente";
        if (action == ActionLogger.ACTION_TRIMIS_LA_ALT_CENTRU) saction = "trimis";
        if (action == ActionLogger.ACTION_ATASAT_RECLAMATIE) saction = "atasat";
        if (action == ActionLogger.ACTION_SCOS_RECLAMATIE) saction = "scos";
        if (action == ActionLogger.ACTION_TRIMIS_APARAT_CENTRAL) saction = "a trimis la aparat central";

        Category sucursala = null;
        try {
            sucursala = (Category) hSucursale.get(new Long(sucursala_id));
        } catch (Exception e) {
        }

        if (action == ActionLogger.ACTION_LOGIN) {
            item.append(" useru ").append(autor).append(" s-a autentificat");
        } else if (
                objectType == ActionLogger.OBJECT_TYPE_USER ||
                        objectType == ActionLogger.OBJECT_TYPE_CLIENT_ELIGIBIL ||
                        objectType == ActionLogger.OBJECT_TYPE_NOMENCLATOR
                ) {
            item.append(" userul ").append(autor).append(" a ").append(saction).append(' ').append(ActionLogger.getObjectNameByType(objectType)).append(' ').append(objectId);
        } else if (
                objectType == ActionLogger.OBJECT_TYPE_INCIDENT ||
                        objectType == ActionLogger.OBJECT_TYPE_ECHIPAMENT ||
                        objectType == ActionLogger.OBJECT_TYPE_DERANJAMENT

                ) {
            item.append(" userul ").append(autor).append(" a ").append(saction).append(' ').append(ActionLogger.getObjectNameByType(objectType)).append(" nr. ").append(objectId).append(" in sucursala ").append(sucursala == null ? "[null]" : sucursala.getName());
        } else if (
                objectType == ActionLogger.OBJECT_TYPE_EVENIMENT ||
                        objectType == ActionLogger.OBJECT_TYPE_LITTLE_INCIDENT ||
                        objectType == ActionLogger.OBJECT_TYPE_LUCRARE_PROGRAMATA ||
                        objectType == ActionLogger.OBJECT_TYPE_RECLAMATIE
                ) {
            item.append(" userul ").append(autor).append(" a ").append(saction).append(' ').append(ActionLogger.getObjectNameByType(objectType)).append(" nr.").append(objectId);
        } else {
            item.append(" TIP OBIECT NECUNOSCUT !!! ");
        }
        return item.toString();
    }%>




        <table width="100%" class="gsaptd">
            <tr>
                <td valign="middle">
                    <form action="<%=AppConstants.APP_NAME%>" method="post">
                        <input type="hidden" name="action" value="showLogs">
                        Utilizator:
                        <select class="gSAPSelect" name="autor">
                            <option value="">Selectati:</option>
                            <% final String[] autori = ActionLogger.getDistinctUsers(); %>
                            <% for (int i = 0; i < autori.length; i++) {%>
                            <option value="<%=autori[i]%>" <%=autori[i] != null && autori[i].equals(selectedAutor) ? "selected" : ""%>><%=autori[i]%>
                            </option>
                            <%}%>
                        </select>
                        &nbsp;&nbsp;&nbsp;
                        Applicatie/Obiect:
                        <select class="gSAPSelect" name="app_obiect">
                            <option value="">Selectati:</option>
                            <option value="-1" <%=selectedAppObiect == -1 ? "selected" : ""%> >Ap. Incidente</option>
                            <option value="<%=ActionLogger.OBJECT_TYPE_INCIDENT%>"  <%=selectedAppObiect == ActionLogger.OBJECT_TYPE_INCIDENT ? "selected" : ""%>  >
                                &nbsp;&nbsp;Incidente
                            </option>
                            <option value="<%=ActionLogger.OBJECT_TYPE_ECHIPAMENT%>" <%=selectedAppObiect == ActionLogger.OBJECT_TYPE_ECHIPAMENT ? "selected" : ""%>>
                                &nbsp;&nbsp;Echipamente
                            </option>
                            <option value="<%=ActionLogger.OBJECT_TYPE_EVENIMENT%>" <%=selectedAppObiect == ActionLogger.OBJECT_TYPE_EVENIMENT ? "selected" : ""%>>
                                &nbsp;&nbsp;Evenimente
                            </option>
                            <option value="<%=ActionLogger.OBJECT_TYPE_USER%>" <%=selectedAppObiect == ActionLogger.OBJECT_TYPE_USER ? "selected" : ""%> >
                                &nbsp;&nbsp;User
                            </option>
                            <option value="<%=ActionLogger.OBJECT_TYPE_NOMENCLATOR%>" <%=selectedAppObiect == ActionLogger.OBJECT_TYPE_NOMENCLATOR ? "selected" : ""%> >
                                &nbsp;&nbsp;Nomenclator
                            </option>
                            <option value="-2" <%=selectedAppObiect == -2 ? "selected" : ""%>  >Ap. Deranjamente
                            </option>
                            <option value="<%=ActionLogger.OBJECT_TYPE_LUCRARE_PROGRAMATA%>" <%=selectedAppObiect == ActionLogger.OBJECT_TYPE_LUCRARE_PROGRAMATA ? "selected" : ""%>>
                                &nbsp;&nbsp;Lucrari programate
                            </option>
                            <option value="<%=ActionLogger.OBJECT_TYPE_LITTLE_INCIDENT%>" <%=selectedAppObiect == ActionLogger.OBJECT_TYPE_LITTLE_INCIDENT ? "selected" : ""%>>
                                &nbsp;&nbsp;Incidente
                            </option>
                            <option value="<%=ActionLogger.OBJECT_TYPE_DERANJAMENT%>" <%=selectedAppObiect == ActionLogger.OBJECT_TYPE_DERANJAMENT ? "selected" : ""%>>
                                &nbsp;&nbsp;Deranjamente
                            </option>
                            <option value="<%=ActionLogger.OBJECT_TYPE_CLIENT_ELIGIBIL%>" <%=selectedAppObiect == ActionLogger.OBJECT_TYPE_CLIENT_ELIGIBIL ? "selected" : ""%> >
                                &nbsp;&nbsp;Clienti eligibili
                            </option>
                            <option value="-3" <%=selectedAppObiect == -3 ? "selected" : ""%> >Ap. Call Center</option>
                        </select>
                        &nbsp;&nbsp;&nbsp;
                        Data start:
                        <INPUT class="gsapinputtext" size="17" id="dataStart" name="dataStart"
                               value="<%=AppConstants.formatx( dataStart)%>">
                        <a id="dataStartTrigger" href="#"><img src="<%=AppConstants.WEB_RESOURCE_PATH%>/images/cal.gif" width="16"
                                                               height="16" border="0"
                                                               alt="Apasa pt. a selecta data"></a>
                        &nbsp;&nbsp;
                        Data stop:
                        <INPUT class="gsapinputtext" size="17" id="dataStop" name="dataStop"
                               value="<%=AppConstants.formatx(dataStop)%>">
                        <a id="dataStopTrigger" href="#"><img src="<%=AppConstants.WEB_RESOURCE_PATH%>/images/cal.gif" width="16"
                                                              height="16" border="0" alt="Apasa pt. a selecta data"></a>
                        &nbsp;&nbsp;&nbsp;
                        <input type="submit" value="Aplica">
                    </form>
                </td>
            </tr>
        </table>
        <script type="text/javascript">
            Calendar.setup(
                    {
                        inputField:"dataStart", // ID of the input field
                        ifFormat:"%d.%m.%Y %H:%M", // the date format
                        button:"dataStartTrigger", // ID of the button
                        showsTime:true
                    }
            );

            Calendar.setup(
                    {
                        inputField:"dataStop", // ID of the input field
                        ifFormat:"%d.%m.%Y %H:%M", // the date format
                        button:"dataStopTrigger", // ID of the button
                        showsTime:true
                    }
            );
        </script>

        <div class="spacer"></div>
        <TABLE border=1 cellPadding=2 cellSpacing=1 width="100%" class="gSAPTHEAD">
            <TR>
                <TH class="gsapthead" colSpan=2 height=25 noWrap align="left">&nbsp;Log&nbsp;</TH>
            </TR>
            <TR>
                <TD>
                    <% if (entries.length == 0) {%>
                    Nu exista informatii...
                    <%} else {%>
                    <% for (int i = 0; i < entries.length; i++) {%>
                    <%=getLogEntryText(entries[i])%><BR>
                    <%}%>
                    <%}%>
                </TD>
            </TR>
        </TABLE>

