<%@ page import="ro.emid.Application" %>
<%@ page import="ro.emid.common.constants.AppConstants" %>
<%@ page import="ro.emid.common.mesg.Message" %>
<%@ page import="java.util.List" %>
<%@ page import="ro.emid.administrare.action.message.MessageService" %>



<%
    /**
     * @see ro.emid.administrare.action.message.LoadMessagesAction
     */
    List messages = (List) request.getAttribute("messages");
%>

<fieldset>
    <legend><b>Trimite mesaj nou:</b></legend>
    <form action="<%=AppConstants.APP_NAME%>?action=save-message" method="post" name="zaforma">
        <table>
            <tr>
                <td>
                    <input style="float: right" type="submit" value="Trimite"/>

                    Catre:
                    <select name="application" id="appSelect">
                        <option value="<%=Message.FOR_ALL%>">Toti</option>
                        <option value="<%=Application.getIncidenteAppName()%>">Incidente</option>
                        <option value="<%=Application.getCallcenterAppName()%>">Callcenter/Deranjamente</option>
                        <option value="<%=Application.getLucrariProgramateAppName()%>">LPOD</option>
                    </select>&nbsp;&nbsp;

                    Tip Mesaj:
                    <input type="radio" id="msg1" name="type" class="typeMsg" value="<%=Message.TYPE_ALERT%>" checked="checked"><label for="msg1"> Alert </label>
                    <input type="radio" id="msg2" name="type" class="typeMsg" value="<%=Message.TYPE_PERMANENT%>"><label for="msg2"> Permanent </label>
                </td>
            </tr>
            <tr>
                <td>
                    <textarea rows="3" cols="10" style="width: 100%" id="message" name="message"></textarea>
                </td>
            </tr>
            <tr>
                <td>
                    <select id="predefined">
                        <option></option>
                        <option>Atentie, in intervalul 10:00 si 10:30 aplicatia va fi oprita pentru mentenanta. Va rugam sa nu operati in acel interval de timp. Va multumim.</option>
                    </select>
                </td>
            </tr>
        </table>
    </form>
</fieldset>

<fieldset style="margin-top: 10px">
    <legend><b>Mesaje trimise:</b></legend>

    <table cellpadding=2 cellspacing=1 width="100%" class="table">
        <thead>
        <tr>
            <th width="10px">#</th>
            <th width="60px">Tip</th>
            <th width="130px">Applicatie</th>
            <th>Text</th>
            <th width="1%">&nbsp;</th>
        </tr>
        </thead>
        <tbody>
        <%if( messages == null || messages.size() == 0){%>
        <tr>
            <td colspan="10" class="no-data">Nu sunt mesaje</td>
        </tr>
        <%}else for (int i = 0; i < messages.size(); i++) {
            Message message = (Message) messages.get(i);
            List readBy = MessageService.getReadUsersForMessage(message);
        %>
        <tr>
            <td><%=message.getId()%></td>
            <td title="<%=readBy != null && readBy.size() > 0 ? String.valueOf(readBy) : ""%>">
                <%=message.getType() == Message.TYPE_ALERT ? "Alert" : message.getType() == Message.TYPE_PERMANENT ? "Permanent" : String.valueOf(message.getType())%>
            </td>
            <td><%=message.getApplication()%></td>
            <td><%=message.getText()%></td>
            <td>
                <a class="link" onclick="return alert('Doriti sa stergeti mesajul ?');" href="<%=AppConstants.APP_NAME%>?action=delete-message&id=<%=message.getId()%>">Şterge</a>
            </td>
        </tr>
        <%}%>
        </tbody>
    </table>
</fieldset>

<script type="text/javascript">
    jQuery(function(){
        $("predefined").onchange=function(){
            var selected = this.options[ this.selectedIndex].text;
            $("message").value = selected;
        };
    });
</script>

