layui.use(["layer", "form", "upload"], function(){
    $ = layui.$;
    form = layui.form;
    layer = layui.layer;
    upload = layui.upload;

    //注销 退出
    $(".head_portrait").on("click", (e) => {
        localStorage.clear(); //清空缓存
        conn.close();
        window.location.href="http://116.62.173.33:3033/login?type=manager";
    });

    $(".chat__room-info-input textarea").keydown(function (e){
        // 当 keyCode 是13时,是回车操作
        if (e.keyCode == 13){
            var textVal = $(".chat__room-info-input textarea").val();
            sendPrivateText(textVal);
            e.preventDefault();
        }
    });

    $(".chat__room-info-control").on("click", (e) => {
        $("#uploadImg").click();
    });

    const input = document.querySelector("#uploadImg");
    input.addEventListener('change',function(){
        // 通过onchange事件获取files,函数要使用function定义,箭头函数this指向父级作用域
        if(userID == null){
            layer.msg("暂未选择聊天对象", {icon: 2});
            return false;
        }

        let id = conn.getUniqueId();                 // 生成本地消息id
        let msg = new WebIM.message('img', id);      // 创建图片消息

        var input = document.getElementById('uploadImg');  // 选择图片的input
        var fileVal = WebIM.utils.getFileUrl(input);      // 将图片转化为二进制文件
        var allowType = {
            'jpg': true,
            'gif': true,
            'png': true,
            'bmp': true
        };
        var imgUrl = fileVal.url; //用户上传的图片
        if (fileVal.filetype.toLowerCase() in allowType) {
            var option = {
                file: fileVal,
                length: '3000',                       // 视频文件时，单位(ms)
                ext: {
                    file_length: fileVal.data.size       // 文件大小
                },
                to: userID,                       // 接收消息对象
                chatType: 'singleChat',               // 设置为单聊
                onFileUploadError: function () {      // 消息上传失败
                    console.log('onFileUploadError');
                },
                onFileUploadProgress: function (e) { // 上传进度的回调
                    console.log(e)
                },
                onFileUploadComplete: function (e) {   // 消息上传成功
                    console.log('onFileUploadComplete');
                    console.log(e)
                },
                success: function (e) {                // 消息发送成功
                    console.log('Success');
                    console.log(e)
                    $(".right_el_scrollbar").append("<div class=\"chat__room-message chat__room-message-reverse\"\n" +
                        "                                     ><img\n" +
                        "                                        src=\""+ headImg +"\"\n" +
                        "                                        alt=\"avatar\" class=\"avatar\"><!---->\n" +
                        "                                    <div class=\"chat__room-message-img\">\n" +
                        "                                        <div class=\"el-image\"><img onclick='clickImg(this)' alt=\"fileImage\"\n" +
                        "                                                                   src=\""+ imgUrl +"\"\n" +
                        "                                                                   class=\"el-image__inner el-image__preview\"\n" +
                        "                                                                   style=\"object-fit: cover;\"><!--v-if--></div>\n" +
                        "                                        <span>"+ formatNotss(parseInt(Date.parse(new Date()))) +"</span></div>\n" +
                        "                                </div>")
                },
                fail: function(e){
                    console.log("Fail");              //如禁言、拉黑后发送消息会失败
                },
                flashUpload: WebIM.flashUpload
            };
            msg.set(option);
            conn.send(msg.body);
        }

    },false);

    registerUser();//登录即时通讯
});