<?php /*a:1:{s:65:"D:\phpstudy_pro\WWW\tp5.1\application\index\view\index\index.html";i:1609731525;}*/ ?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <!-- <script src="/jquery-3.5.1.min.js"></script> -->
    <!--the js for jquery-->
    <script src="http://cdn.bootcss.com/jquery/1.11.3/jquery.js"></script>
    <link rel="stylesheet" type="text/css" href="/css/chat.css"/>
    <link rel="stylesheet" href="/lib/css/jquery.mCustomScrollbar.min.css"/>
    <link rel="stylesheet" href="/dist/css/jquery.emoji.css"/>
    <script src="/js/flexible.js"></script>
</head>
<body>
    <header class="header">
        <a class="back" href="/index.php/index/index/tc"></a>
        <h5 class="tit">猪头</h5>
        <!-- <div class="right">资料</div> -->
    </header>
    <div id="message">

    </div>
    <div id="footer">
        <img src="/images/hua.png" alt=""/>
        <img class="xiaolian" src="/images/xiaolian.png" alt=""/>
        <div contenteditable="true" class="my-input"></div>
        <input type="hidden" class="user-name" value="" />
        <p class="send">发送</p>
    </div>
    <!-- <script src="/js/chat.js" type="text/javascript" charset="utf-8"></script> -->
    <script src="/lib/script/jquery.mousewheel-3.0.6.min.js"></script>
    <!--the js for jquery.mCustomScrollbar-->
    <script src="/lib/script/jquery.mCustomScrollbar.min.js"></script>
    <!--the js for this plugin-->
    <script src="/src/js/jquery.emoji.js"></script>
    <script>
       $(".my-input").emoji({
                button: ".xiaolian",
                showTab: false,
                animation: 'slide',
                icons: [{
                name: "贴吧表情",
                path: "/dist/img/tieba/",
                maxNum: 50,
                file: ".jpg",
                placeholder: ":{alias}:",
            },{
                path: "/dist/img/qq/",
                name:"QQ表情",
                maxNum: 91,
                excludeNums: [41, 45, 54],
                file: ".gif",
                placeholder: "#qq_{alias}#"
            }]
        });
        
    </script>
    <script>
        ws = new WebSocket("ws://192.168.5.7:2348");
        //服务端主动推送消息时会触发这里的onmessage
        ws.onmessage = function(e){
           // json数据转换成js对象
           if(isJSON(e.data)){
                var data = eval("("+e.data+")");
            }else{
                var data = e.data;
            }
            // console.log(e);
            var type = data.type || '';
            switch(type){
                // Events.php中返回的init类型的消息，将client_id发给后台进行uid绑定
                case 'init':
                    // 利用jquery发起ajax请求，将client_id发给后端进行uid绑定
                    $.post("/index/index/bind", {client_id: data.client_id}, function(data){$('.user-name').val(data);ws.send(data);}, 'json');
                    break;
                case 'dl':
                    console.log(data.msg);
                    tsy(data.msg);
                    break;
                case 'tc':
                    console.log(data.msg);
                    tsy(data.msg);
                    break;
                case 'fxx':
                    console.log(data.msg);
                    if(data.user != $('.user-name').val()){
                        reply("/images/user_avatar.jpg",data.msg,data.user);
                    }
                    break;
                case 'ping':
                    break;
                // 当mvc框架调用GatewayClient发消息时直接alert出来
                default :
                    console.log(data.msg);
            }
        };
        //提示语
        function tsy(str) {
            var html = "<div class='ask1'><div class='time'>" + str + "</div></div>";
            return upView(html);
        }
        function reply(headSrc, str,name) {
            var html = "<div class='reply'><div class='msg'><img src=" + headSrc + " /><span class='name'>"+name+"</span><div class='conte'>" + str + "</div></div></div>";
            return upView(html);
        }
        function ask(headSrc, str) {
            var html = "<div class='ask'><div class='msg'><img src=" + headSrc + " />" + "<div class='conte'>" + str + "</div></div></div>";
            return upView(html);
        }


        let canSend = true;
        $(function() {
            $('#footer').on('keyup', '.my-input', function() {
                if ($(this).html().length > 0) {
                    $(this).next().css('background', '#114F8E').prop('disabled', true);
                    canSend = true;
                } else {
                    $(this).next().css('background', '#ddd').prop('disabled', false);
                    canSend = false;
                }
            })
            $('#footer .send').click(send)
            $("#footer .my-input").keydown(function(e){
                if(e.keyCode == 13){
                    return send();
                }
            });
        })

        
        function upView(html) {
            let message = $('#message');
            message.append(html);
            window.scrollTo(0, document.body.scrollHeight)
        }
        function send(msg){
            if(canSend){
                let input = $("#footer .my-input");
                ask("/images/user_avatar.jpg", input.html());
                $.post("/index/index/isn",{
                    name:$('.user-name').val(),
                    content:input.html()
                },function(res){

                })
                input.html('');
            }
        }
        function sj() {
            return parseInt(Math.random() * 10)
        }

        function isJSON(str) {
            if (typeof str == 'string') {
                try {
                    var obj=JSON.parse(str);
                    if(typeof obj == 'object' && obj ){
                        return true;
                    }else{
                        return false;
                    }
                } catch(e) {
                    return false;
                }
            }
        }
    </script>
</body>
</html>