<template>
  <div class="autoBox box">
    <div class="service" style="display: inline-block;">
      <div class="service_title">
          在线客服
      </div> 
     <div style="width: 100%;">
          <div class="form-group" style="min-height: 600px;">
              <div class="chat-list form-control">
                <p :style="item.replyingPersonId==null?'float:left;':'float:right;'" 
                v-for="item in list">
                <span v-if="item.replyingPersonId==null">{{item.questionerName}} :{{item.questionerContext}}</span></br>
                <span v-if="item.replyingPersonId!=null"> {{item.replyingPersonName}} : {{item.replyingContext}}</span></br>
              </p>
              </div>
          </div>
          <div class="down" style="width:100%;">
              <!-- <input type="text" ref="sendMsg" v-model="contentText" style="height: 130px;border: none;"/> -->
              <el-input
                  type="textarea"
                  placeholder="请输入内容"
                  v-model="contentText"
                  
              >
              </el-input>
              <button ref="btn" @click="sendText">发送</button>
          </div>
     </div>
    </div>
    <div style="display: inline-block;width: 300px;vertical-align: top;
    margin-left: 20px;border: 1px solid #ccc;height: 683px;">
      <div style="width: 100%;height: 30px;">
          <span class="problem">常见问题</span>
          <span class="ChangeTheBatch">换一批></span>
      </div>
      <div class="management">
          <div  v-for="(i,j) in management" >{{i.menuName}}</div>
      </div>
    </div>
  </div>
</template>
<script>
  import { getDicts } from "@/api/dict";
  import { getAllInformationByCustomer, getUmsUserOnlineServiceExchangeInformations } from "@/api/customer/umsUserOnlineServiceExchangeInformation";
  import Base from "@/views/base/Base";
  import { getToken, resetToken } from "@/utils/auth";
export default {
  extends: Base,
  data () {
    return {
      event: {},
      ws: null,
      count: 0,
      userId: null,
      list: [],
      contentText: '',// input输入的值
      management:[],
    }
  },
  // created () {
  //   this.getUserId()
  // },
  mounted () {
    this.initWebsocket()
    // this.$refs.sendMsg.focus()
    this.$nextTick(() => {
      setInterval(this.look, 30000);
    })
  },
  updated () {
  },
  methods: {
    look () {
      let user = window.localStorage.getItem("user");
      var users = JSON.parse(user);
      if (users == null) {
        alert("请先登录")
        return;
      }
      getAllInformationByCustomer(users.userId).then(result => {
          this.list = result.data.allInformationByCustomer;
        })
    },

    // 发送聊天消息
    sendText () {
        
        // Listen for messages
        debugger
        this.ws.addEventListener('message', function (event) {
            console.log('Message from server ', event.data);
            debugger
        });
        debugger
      let user = window.localStorage.getItem("user");
      var users = JSON.parse(user);
      if (users == null) {
        alert("请先登录")
        return;
      }
      let _this = this
      // _this.$refs.sendMsg.focus()
      if (!_this.contentText) {
        return false
      }
      let params = {
        userName: users.username,
        userId: users.userId,
        msg: _this.contentText,
        flag: "CustomerOnlineService",
        status: "离线"
      }
      _this.ws.send(JSON.stringify(params)) // 调用WebSocket send()发送信息的方法

      _this.contentText = ''

      getAllInformationByCustomer(users.userId).then(result => {
          this.list = result.data.allInformationByCustomer;
        })
    },
    initWebsocket () {
      let user = window.localStorage.getItem("user");
      var users = JSON.parse(user);
      var userId = users.userId;
        let _this = this
      // 判断页面有没有存在websocket连接
        if (window.WebSocket) {
          let ws = new WebSocket("ws://localhost:7028/robot/websocket/"+userId+",1")
          _this.ws = ws
          console.log(_this.ws+"_this.ws")
          ws.onopen = function (e) {
            console.log('服务器连接成功')
          }
          ws.onclose = function () {
            console.log('服务器连接关闭')
          }
          ws.onerror = function () {
            console.log('服务器连接出错')
          }
          ws.onmessage = function (e) {
            
            // 接收服务器返回的数据
            // console.log(e+"e")
            // let resData = JSON.parse(e.data)
            // console.log(resData+"resData")
            // console.log(e.text()+"e.text()")
            // if (resData.funName === 'userCount') {
            //   _this.count = resData.users
            //   _this.list = resData.chat
            //   console.log(_this.list, '1234')
            // } else {
            //   _this.list = [
            //     ..._this.list,
            //     { userId: resData.userId, content: resData.msg }
            //   ]
            // }
          }
          ws.addEventListener('open', function (event) {
          });

        
        }
        getAllInformationByCustomer(userId).then(result => {
          this.list = result.data.allInformationByCustomer;
        })
        var infoParams = {
          pageNo: 0,
          pageSize: 10,
          language: this.$i18n.locale.toUpperCase(),
          reserveOne: "n",
        }
        getUmsUserOnlineServiceExchangeInformations(infoParams).then((result) => {
            this.management = result.data.umsUserOnlineServiceMenuInformations;
        })
      }
  },
  watch: {
    // list () {
    //   this.$refs.scorll.refresh()
    // }
  }
}
</script>
<style scoped>
.message {
  padding-top: 60px;
  margin-bottom: 60px;
  height: 100%;
  overflow: hidden;
}
.box .service>>>.el-textarea__inner {
  min-height: 130px !important;
  max-height: 130px !important;
}
.problem{
    float: left;
}
.ChangeTheBatch{
  float: right;
}
.box {
  background: #fff;
  padding: 50px 0;
}
.service {
  width: 800px;
  margin-left: 50px;
  border: 1px solid #ccc;
}
.down {
  display: flex;
  align-items: center;
  
}
.service input {
  flex: 1;
  height: 40px;
  font-display: 18px;
}
.service button {
  border: none;
  width: 70px;
  height: 44px;
  background: blue;
  color: #fff;
  line-height: 40px;
  text-align: center;
  border-radius: 5px;
  margin-left: 10px;
}
.msg-box {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  width: 100%;
  margin: 20px 10px 0px 0px;
}
.service_title {
    line-height: 60px;
    background:#f2f2f2;
    border: 1px solid rgba(228, 228, 228, 1);
    font-size: 26px;
    text-indent: 20px;
    color: #999;
}
.avtive {
  display: inline-block;
  background: blue;
  color: #fff;
  padding: 10px;
  font-size: 16px;
  border-radius: 6px;
  margin-right: 10px;
  flex-wrap: wrap;
  word-break: break-all;
  max-width: 220px;
}
.header-avtor {
  width: 40px !important;
  height: 40px;
  border-radius: 50%;
  background: #ccc;
}
.avtive-left {
  justify-content: flex-start;
}
.getClass1 {
      color:#0050d8
  }
.getClass2 {
    color: #303133;
}
.management div{
      line-height: 50px;
      /* text-indent: 30px; */
      font-size: 14px;
}
.management div:hover {
    color: #ff0000;
    cursor: pointer;
}
</style>