<%@ page language="java" pageEncoding="UTF-8"
         contentType="text/html; charset=UTF-8"%>
<%@ page import="org.apache.commons.lang.StringEscapeUtils"%>
<%@ page isELIgnored="false"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%
    String basePath = request.getContextPath();
%>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>兰亭集智知识管理系统-消息队列管理</title>
    <jsp:include page="../../common/head.jsp"></jsp:include>
</head>
<body>
<div  class="easyui-layout"  data-options="fit:true">
    <div data-options="region:'west',split:true,title:'服务器列表'" style="width:230px;padding:0px 0px;">
        <table id="dgQueueServer"  class="easyui-datagrid" style="width:220px;height:100%;"
               data-options="emptyMsg:'没有队列服务器',toolbar:'#tbActivemq',loadMsg:'正在加载请稍后...',singleSelect:true,pagination:false,fitColumns:true,url:'<%=basePath%>/activemq/json/server.action',method:'get'">
            <thead>
            <tr>
                <th data-options="field:'serverip',width:400">服务器IP</th>
            </tr>
            </thead>
        </table>
    </div>
    <div data-options="region:'center',title:'消息队列'" style="padding:0px 0px;">
        <table id="dgActivemq"  class="easyui-datagrid" style="height:100%;"
               data-options="emptyMsg:'没有符合检索条件的结果',loadMsg:'正在检索请稍后...',singleSelect:true,pagination:false,fitColumns:true,url:'<%=basePath%>/activemq/json/broker.action',method:'get'">
            <thead>
            <tr>
                <th data-options="field:'name',width:400">队列名称</th>
                <th data-options="field:'size',width:200">消息数量</th>
                <th data-options="field:'consumers',width:200">消费者</th>
                <th data-options="field:'enqueue',width:200">消费数量</th>
                <th data-options="field:'dequeue',width:200">生产数量</th>
            </tr>
            </thead>
        </table>
    </div>
</div>

<div id="tbActivemq" style="padding-left: 10px;">
    <input class="easyui-searchbox" id="searchHost" style="width: 145px;" data-options="prompt:'请输入检索关键词'">
    <a href="javascript:void(0);" onclick="loadServerQueue()" class="easyui-linkbutton" data-options="iconCls:'fa fa-search-plus',plain:true">检索</a>
</div>

<div id="queueMessageInfo" class="easyui-window" title="队列消息" data-options="modal:true,closed:true,collapsible:false,resizable:false,minimizable:false,maximizable:false,iconCls:'icon-save'"
     style="width:800px;height:600px;padding:10px;">
    <div class="easyui-layout" data-options="fit:true">
        <div data-options="region:'north'" style="height:150px">
            <p id="msgContent">暂无数据</p>
        </div>
        <div data-options="region:'center',split:true" style="height:448px;">
            <table id="dgActiveMQMessage"  class="easyui-datagrid" style="height:100%;"
                   data-options="emptyMsg:'暂时没有消息',loadMsg:'正在检索请稍后...',singleSelect:true,pagination:true,fitColumns:true,url:'<%=basePath%>/activemq/json/broker/destination.action',method:'get'">
                <thead>
                <tr>
                    <th data-options="field:'messageid',width:400">消息编号</th>
                    <th data-options="field:'priority',width:200">优先级</th>
                    <th data-options="field:'redelivered',width:200,formatter:function(lock){
                if(lock==true)
                    return '是';
                else
                    return '否';
            }">是否被重发</th>
                    <th data-options="field:'timestamp',width:200">创建时间</th>
                    <th data-options="field:'_operation',width:180,formatter:optFormatter">操作</th>
                </tr>
                </thead>
            </table>
        </div>
    </div>
</div>
<input type="hidden" id="current-server-row" value="">
<input type="hidden" id="current-broker-row" value="">
<script type="text/javascript">
    function optFormatter(value, row, index) {
        var optStr="<a href='javascript:void(0);' style='text-decoration:none' onclick='return viewMessage(" + index + ");'>查看</a>&nbsp;&nbsp;&nbsp;<a href='javascript:void(0);' style='text-decoration:none' onclick='return deleteMessage(" + index + ");'>删除</a>";
        return optStr;
    }

    function deleteMessage(index){
        var item=$('#dgActiveMQMessage').datagrid('getRows')[index];
        var queueRowIndex=$("#current-server-row").val();
        if(queueRowIndex==undefined || queueRowIndex==null || queueRowIndex==""){
            return false;
        }
        var queueRow=$('#dgQueueServer').datagrid('getRows')[queueRowIndex];
        var brokerRowIndex=$("#current-broker-row").val();
        if(brokerRowIndex==undefined || brokerRowIndex==null || brokerRowIndex==""){
            return false;
        }
        var brokerRow=$('#dgActivemq').datagrid('getRows')[brokerRowIndex];
        $.messager.confirm('温馨提示', '您确定要删除消息'+item.messageid+"?", function(r){
            if (r){
                var win = null;
                $.ajax({
                    type: "GET",
                    url: "<%=basePath%>/activemq/json/message/delete.action",
                    data: {host:queueRow.serverip,port:queueRow.port,broker:queueRow.broker,queue:brokerRow.name,message:item.messageid,token:queueRow.userproperties},
                    dataType: "json",
                    beforeSend:function(XMLHttpRequest){
                        win = $.messager.progress({
                            title: '消息管理',
                            msg: '正在处理中，请稍后...'
                        });
                    },
                    success: function(data){
                        $.messager.alert('温馨提示',data.message,function(){
                            if(data.status==1){
                                $('#dgActiveMQMessage').datagrid('reload');
                                $('#dgActivemq').datagrid('reload');
                            }
                        });
                    },
                    complete:function(XMLHttpRequest, textStatus){
                        $.messager.progress('close');
                    },
                    error:function(XMLHttpRequest, textStatus, errorThrown){
                        $.messager.alert('温馨提示', '请求出错！','error');
                    }
                });
            }
        });
    }
    $(document).ready(function(){
        $('#dgQueueServer').datagrid({
            onClickRow: function(index,row){
                $("#current-server-row").val(index);
                $('#dgActivemq').datagrid('load',{host:row.serverip,port:row.port,broker:row.broker,token:row.userproperties});
            },
            onUnselect:function(index,row){
                $("#current-server-row").val("");
            }
        });
        $('#dgActivemq').datagrid({
            onClickRow: function(index,row){
                var queueRowIndex=$("#current-server-row").val();
                if(queueRowIndex==undefined || queueRowIndex==null || queueRowIndex==""){
                    return false;
                }
                var queueRow=$('#dgQueueServer').datagrid('getRows')[queueRowIndex];
                $("#current-broker-row").val(index);
                $("#msgContent").html("暂无数据");
                $('#queueMessageInfo').window('open');
                $('#dgActiveMQMessage').datagrid('load',{host:queueRow.serverip,port:queueRow.port,broker:queueRow.broker,queue:row.name,token:queueRow.userproperties});
            },
            onUnselect:function(index,row){
                $("#current-broker-row").val("");
            }
        });
        var pager = $('#dgActiveMQMessage').datagrid().datagrid('getPager');
        pager.pagination({
            showPageList:false
        });
    });
       function viewMessage(index) {
           var row=$('#dgActiveMQMessage').datagrid('getRows')[index];
           $("#msgContent").html(row.message);
       }
    function loadServerQueue() {
        $('#dgQueueServer').datagrid('load', {host: $("#searchHost").textbox("getValue")});
        $('#dgActivemq').datagrid('reload');
    }
</script>
</body>
</html>