{% extends 'base.html' %}
{% block content %}
{% include 'nav_cat_bar.html' %}
<style>
    .bootstrap-dialog-body  {
        background-color: rgba(0, 0, 0, 0);
    }
    .bootstrap-dialog-message {
        background-color: rgba(0, 0, 0, 0);
    }
    .pre-class {
        background-color: rgba(0, 0, 0, 1);
    }
    .modal-content {
        background-color: rgba(0, 0, 0, 0.6);
    }
    .modal-dialog {
        background-color: rgba(0, 0, 0, 0);
        width: 800px;
    }
    .modal-body {
        background-color: rgba(0, 0, 0, 0);
    }
    .modal-header {
        background-color: #FFFFFF;
    }
</style>

<div class="wrapper wrapper-content animated fadeInRight">
    <div class="row">
        <div class="col-lg-12">
            <div class="ibox float-e-margins">
                <div id="ibox-content" class="ibox-title">
                    <h5> 用户日志详细信息列表 </h5>
                    <div class="ibox-tools">
                        <a class="collapse-link">
                            <i class="fa fa-chevron-up"></i>
                        </a>
                        <a class="dropdown-toggle" data-toggle="dropdown" href="#">
                            <i class="fa fa-wrench"></i>
                        </a>
                        <a class="close-link">
                            <i class="fa fa-times"></i>
                        </a>
                    </div>
                </div>

                <div class="ibox-content">
                    <div class="panel-options">
                        <ul class="nav nav-tabs">
                            <li class="active"><a href="/jlog/log_list/online/" class="text-center"><i class="fa fa-laptop"></i> 在线 </a></li>
                            <li><a href="/jlog/log_list/offline/" class="text-center"><i class="fa fa-bar-chart-o"></i> 历史记录 </a></li>
                        </ul>
                    </div>
                    <br/>
                    <div class="tab-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"> 来源IP </th>
                                    {% ifnotequal session_role_id 0 %}
                                        <th class="text-center"> 统计命令 </th>
                                        <th class="text-center"> 实时监控 </th>
                                        <th class="text-center"> 阻断 </th>
                                    {% endifnotequal %}
                                    <th class="text-center"> 登录时间 </th>

                                </tr>
                            </thead>
                            <tbody>
                            {% for post in contacts.object_list %}
                                <tr class="gradeX">
                                    <td id="username" class="text-center"> {{ post.user }} </td>
                                    <td id="ip" class="text-center"> {{ post.host }} </td>
                                    <td id="remote_ip" class="text-center"> {{ post.remote_ip }} </td>
                                    {% ifnotequal session_role_id 0 %}
                                        <td class="text-center"><a href="/jlog/history/?id={{ post.id }}" class="log_command"> 命令统计 </a></td>
                                        <td class="text-center"><a class="monitor" file_path="{{ post.log_path }}"> 监控 </a></td>
                                        <td class="text-center"><input type="button" id="cut" class="btn btn-danger btn-xs"  name="cut" value="阻断" onclick='cut("{{ post.pid }}")' /></td>
                                    {% endifnotequal %}
                                    <td class="text-center"> {{ post.start_time|date:"Y-m-d H:i:s" }} </td>
                                </tr>
                            {% endfor %}
                            </tbody>
                        </table>
                        <div class="row">
                            <div class="col-sm-6"></div>
                            {% include 'paginator.html' %}
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>


{#<script src="http://{{ web_socket_host }}/socket.io/socket.io.js"></script>#}
<script>
{#   $(document).ready(function(){#}
{#       $('.monitor').click(function(){#}
{#           window.open('/jlog/monitor/', '监控', 'height=500, width=910, top=89px, left=99px,toolbar=no,menubar=no,scrollbars=auto,resizeable=no,location=no,status=no');#}
{#        })#}
{#   });#}


    function init(obj){
        var file_path = obj.attr('file_path');
        console.log(file_path);
        var socket = new WebSocket('ws://j:8080/monitor?file_path='+file_path);
        socket.onopen = function(evt){
             socket.send(file_path)
        };

        window.onbeforeunload = function(){
            socket.close()
        };

        var username = obj.closest('tr').find('#username').text();
        var ip = obj.closest('tr').find('#ip').text();

        BootstrapDialog.show({message: function(){
            var tag = $('<pre id="log" style="height: 500px;overflow: auto;background-color: rgba(0, 0, 0, 0);line-height: 0.8; border: none"></pre>');
{#            document.cookie.split('; ').forEach(function(obj){#}
{#                var info = obj.split('=');#}
{#                if(info.length == 2 ){#}
{#                    if(info[0] == 'username'){#}
{#                        username = info[1];#}
{#                    }else if(info[0] == 'seed'){#}
{#                        seed = info[1];#}
{#                    }#}
{#                }#}
{#            });#}

            //服务器端认证
{#            socket.send('login', {userid:message.id, filename:message.filename,username:username,seed:seed});#}
            socket.onmessage = function(evt){
                var control_regx =  /\x1B\[([0-9]{1,3}((;[0-9]{1,3})*)?)?[m|K]/g;
                var newline_regx = /\n|\r|(\r\n)|(\u0085)|(\u2028)|(\u2029)/g;
{#                if (option == 'new') {#}
                tag.append(evt.data.replace(newline_regx, '<br />').replace(control_regx, ''));
{#                } else if (option == 'exist') {#}
{#                    tag.append('<pre>' + exsit_message + '</pre>');#}
{#                }#}
                tag.animate({ scrollTop: tag[0].scrollHeight}, 1);
            };

            tag[0].style.color = "#00FF00";
            return tag[0];
        } ,
            title:'Jumpserver实时监控 '+' 登录用户名: '+'<span class="text-info">'+username+'</span>'+' 登录主机: '+'<span class="text-info">'+ip,
            onhide:function(){
                socket.close();
        }});
    }

    $(document).ready(function(){
        $('.monitor').click(function(){
            init($(this))
        })
    });

    function log_search(){
        $.ajax({
            type: "GET",
            url: "/jlog/search/?env=online",
            data: $("#search_form").serialize(),
            success: function (data) {
                $(".tab-content").html(data);
            }
        });
     }

    $(document).ready(function(){
       $('.log_command').on('click',function(){
           var url = $(this).attr('href');
           var username = $('#username')[0].innerText;
           var ip = $('#ip')[0].innerText;
           var start_time = $('#start_time')[0].innerText;
           var end_time = $('#end_time')[0].innerText;
           var div_username = ' 用户名: '+'<span class="text-info">'+username+'' + '</span>';
           var div_ip = ' 主机: '+'<span class="text-info">' + ip + '</span>';
           var div_time = ' 开始时间: ' + '<span class="text-info">'+start_time +'</span>' + ' 结束时间: ' +'<span class="text-info">' + end_time + '</span>';
           var title = 'JumpServer命令统计 '+ div_username + div_ip + div_time;
            $.ajax({url:url,
                    success:function(data){
                        var tag = $('<div style="height: 500px;overflow: auto;background-color: rgba(0, 0, 0, 0);"></div>').html(data.replace(/\n/g,"<br />"));
                        BootstrapDialog.show({title: title, message:tag[0]});
            }});
           return false;
        });

       $("#search_input").keydown(function(e){
        if(e.keyCode==13){
            log_search()
            }
        });

       $('')
    });



   function cut(num){
      var g_url = "/jlog/log_kill/?id="+num;
       $.ajax({
           type: "GET",
           url: g_url,
           success: window.open("/jlog/log_list/online/", "_self")
//           error: window.open(g_url, "_self")
       });

}
</script>
{% endblock %}