<template>
  <div class="send-chat">
    <div class="box">
      <input type="text" v-model="content" @keyup.enter="_sendChat" ref="sendInput" v-iosfit />
    </div>
    <div class="btn" @click="sendCountDown === 0 && _sendChat()" :class="sendCountDown === 0 ? '' : 'unable'">
      {{ sendCountDown === 0 ? '发送' : `${sendCountDown}s` }}
    </div>
  </div>
</template>
<script>
import { sendChat } from '@/plugins/API/apiAll';
import { mapGetters } from 'vuex';
export default {
  name: 'vertical-send-chat',
  props: {
    value: {}
  },
  data() {
    return {
      sendDate: 0,
      content: '',
      sendCountDown: 0
    };
  },
  computed: {
    ...mapGetters({
      channelInfo: 'channelInfo',
      uuid: 'uuid'
    }),
    isShow: {
      get() {
        return this.value;
      },
      set(nVal) {
        this.$emit('input', nVal);
      }
    }
  },
  mounted() {
    this.$Bus.$on('bus-remSendChay', this._close);
    this._focus();
  },
  methods: {
    _focus() {
      if (this.isShow) {
        this.$nextTick(() => {
          this.$refs.sendInput.focus();
        });
      }
    },
    _close() {
      this.isShow = false;
    },
    _resetChat() {
      clearInterval(this.sendCountDownTimer)
      this.sendCountDown = 3
      this.sendCountDownTimer = setInterval(() => {
        if (this.sendCountDown === 0) {
          clearInterval(this.sendCountDownTimer)
        } else if (this.sendCountDown > 0) {
          this.sendCountDown--
        }
      }, 1000);
      this._close();
      this.content = '';
    },
    async _sendChat() {
      if (new Date().getTime() - this.sendDate <= 3000) {
        this.$layer.msg('您的操作太频繁了');
        return false;
      }
      if (!this.content.trim() || this.content.trim().length > 100) {
        this.$layer.msg('评论不能为空，且不超过100个字！');
        return;
      }
      this.sendDate = new Date().getTime();
      const res = await sendChat({
        uin: this.channelInfo.uin,
        channelId: this.channelInfo.id,
        replyId: '',
        content: this.$filterXSS(this.content.trim()),
        uuid: this.uuid
      });
      if (res.code === 200 && res.errorCode === 0) {
        this._resetChat()
        if (res.data.chatAuthModel === 1) {
          this.$layer.msg('已提交审核，请耐心等待');
        }
      }
    }
  },
  watch: {
    isShow() {
      this._focus();
    }
  },
  beforeDestroy() {
    this.$Bus.$off('bus-remSendChay', this._close);
  }
};
</script>
<style lang="less" scoped>
.send-chat {
  display: flex;
  align-items: center;
  height: 100%;
  background:  rgba(0, 0, 0, 0.1);;
  .box {
    flex: 1;
    padding-left: 0.1rem;
    height: 0.32rem;
    input {
      padding: 0 0.1rem;
      width: 100%;
      height: 0.32rem;
      line-height: 0.32rem;
      border-radius: 0.16rem;
      background: rgba(0, 0, 0, 0.4);
      font-size: 0.13rem;
      color: #fff;
      &:focus{
        outline:none;
      }
    }
  }
  .btn {
    margin: 0 0.1rem;
    padding: 0 0.1rem;
    height: 0.28rem;
    width: 0.5rem;
    text-align: center;
    line-height: 0.28rem;
    border-radius: 0.05rem;
    background: #409eff;
    color: #fff;
    font-size: 0.12rem;
  }
  .unable {
    color:#fff;
    background-color: #999;
  }
}
</style>
