<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="_sendChat">发送</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: ''
    };
  },
  computed: {
    ...mapGetters({
      channelInfo: 'channelInfo'
    }),
    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;
    },
    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.content.trim()
      });
      if (res.code === 200 && res.errorCode === 0) {
        this.content = '';
        this._close();
        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: #fff;
  .box {
    flex: 1;
    padding-left: 0.1rem;
    height: 0.36rem;
    input {
      padding: 0 0.1rem;
      width: 100%;
      height: 100%;
      font-size: 0.16rem;
      color: #333;
      background: #f6f6f6;
      border-radius: 0.05rem;
    }
  }
  .btn {
    flex: 0 0 auto;
    font-size: 0.16rem;
    padding: 0.1rem;
    color: #409eff;
  }
}
</style>
