{% extends 'base.html' %}
{% block content %}
{% include 'nav_cat_bar.html' %}
<style>
    .modal-dialog {
        width: 800px;
    }
    .modal-body {
        background-color: #000000;
    }
</style>

<div class="wrapper wrapper-content animated fadeInRight">
    <div class="row">
        <div class="col-lg-12">
            <div class="panel-heading">
                <div class="panel-title m-b-md"><h4> 用户日志详细信息列表 </h4></div>
                <div class="panel-options">

                    <ul class="nav nav-tabs">
                        <li class="active"><a data-toggle="tab" href="#tab-1" class="text-center"><i class="fa fa-laptop"></i> 在线 </a></li>
                        <li><a data-toggle="tab" href="#tab-2" class="text-center"><i class="fa fa-bar-chart-o"></i> 历史记录 </a></li>
                    </ul>
                </div>
            </div>
            <div class="panel-body">
                <div class="tab-content">
                    <div id="tab-1" class="ibox float-e-margins tab-pane active">
                        <div class="ibox-content">
                            <table class="table table-striped table-bordered table-hover ">
                                <thead>
                                    <tr>
                                        <th class="text-center"> 用户名 </th>
                                        <th class="text-center"> 登录主机 </th>
                                        <th class="text-center"> 实时监控 </th>
                                        <th class="text-center"> 阻断 </th>
                                        <th class="text-center"> 登录时间 </th>
                                        <th class="text-center"> 结束时间 </th>

                                    </tr>
                                </thead>
                                <tbody>
                                {% for post in online %}
                                    <tr class="gradeX">
                                        <td class="text-center"> {{ post.user.name }} </td>
                                        <td class="text-center"> {{ post.asset.ip }} </td>
                                        <td class="text-center"><a class="monitor" filename="{{ post.log_path }}"> 监控 </a></td>
                                        <td class="text-center"><a href="/jlog/log_kill/{{ post.pid }}"> 阻断 </a></td>
                                        <td class="text-center"> {{ post.start_time|date:"Y-m-d H:i:s" }} </td>
                                        <td class="text-center"> {{ post.end_time|date:"Y-m-d H:i:s" }} </td>
                                    </tr>
                                {% endfor %}
                                </tbody>
                            </table>
                        </div>
                    </div>
                    <div id="tab-2" class="ibox float-e-margins tab-pane">
                        <div class="ibox-content">
                            <table class="table table-striped table-bordered table-hover ">
                                <thead>
                                    <tr>
                                        <th class="text-center"> 用户名 </th>
                                        <th class="text-center"> 登录主机 </th>
                                        <th class="text-center"> 命令统计 </th>
                                        <th class="text-center"> 登录时间 </th>
                                        <th class="text-center"> 结束时间 </th>

                                    </tr>
                                </thead>
                                <tbody>
                                {% for post in offline %}
                                    <tr class="gradeX">
                                        <td class="text-center"> {{ post.user.name }} </td>
                                        <td class="text-center"> {{ post.asset.ip }} </td>
                                        <td class="text-center"> 命令统计 </td>
                                        <td class="text-center"> {{ post.start_time|date:"Y-m-d H:i:s"}} </td>
                                        <td class="text-center"> {{ post.end_time|date:"Y-m-d H:i:s" }} </td>
                                    </tr>
                                {% endfor %}
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>


<script src="http://{{ web_socket_host }}/socket.io/socket.io.js"></script>
<script>
   $.fn.webSocket = function(opt){
    var st = {};
    st = $.extend(st,opt);
    var message = {};
    var $this = $(this);
    var genUid = function(){
        return new Date().getTime()+""+Math.floor(Math.random()*899+100);
    };
    var init = function(e){
        var socket = io.connect('ws://'+globalConfig.SOCKET_HOST);
        var node = $(e.target);
        message.id = genUid();
        message.filename = node.attr('filename');
        BootstrapDialog.show({message:function(){
            var escapeString = function (html){
                var elem = document.createElement('div')
                var txt = document.createTextNode(html)
                elem.appendChild(txt)
                return elem.innerHTML;
            }
            var tag = $('<div id="log" style="height: 500px;overflow: auto;"></div>');
            //告诉服务器端有用户登录
            socket.emit('login', {userid:message.id, filename:message.filename});
            socket.on('message',function(obj){
                //去除log中的颜色控制字符
                var regx =  /\x1B\[([0-9]{1,3}((;[0-9]{1,3})*)?)?[m|K]/g;
//                tag.append('<p>'+escapeString(obj.content.replace(regx,''))+'</p>');
                tag.append('<p>'+escapeString(obj.content)+'</p>');
                tag.animate({ scrollTop: tag[0].scrollHeight}, 1);
            });
            tag[0].style.color = "#00FF00";
            return tag[0];
        } ,
            title:'Jumpserver实时监控:',
            onhide:function(){
                socket.emit('disconnect');
        }});
    }
    $this.on("click",function(e){
        init(e);
        return false;
    });
}
   $('.log_command').on('click',function(){
       var url = $(this).attr('href');
        $.ajax({url:url,success:function(data){
        BootstrapDialog.show({title:'命令统计',message:data});
        }});
       return false;
    })
   globalConfig = {
        SOCKET_HOST: "{{ web_socket_host }}"
   }
   $(".monitor").webSocket()
</script>
{% endblock %}