<template>
  <div>
    <el-dialog :title="!tmpWrong.ID ? '新建易错词' : '编辑易错词'" :visible.sync="isShow" class="wrong-word-dialog">
      <el-form :model="tmpWrong" ref="wrongForm" label-width="100px">
        <el-form-item  prop="word" label="易错词：" :verify="['NotNull']">
          <el-input type="text" v-model="tmpWrong.word" auto-complete="off"></el-input>
        </el-form-item>
        <div style="text-align:center;padding-bottom: 12px;" v-if="!tmpWrong.ID">(多个易错词之间用逗号隔开)</div>
        <el-form-item prop="replaceWord" label="替换词：" :verify="['NotNull']">
          <el-input type="text" v-model="tmpWrong.replaceWord" auto-complete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="isShow = false">取 消</el-button>
        <el-button type="primary" @click="confirmWrongClick">确 定</el-button>
      </div>
    </el-dialog>
</div>
</template>

<script>
import util from '@src/common/util.js'

export default {
  data() {
    return {
      // 易错词搜索
      searchWrong: {
        word: ''
      }
    }
  },
  computed: {
    isShow: {
      get() {
        return this.addWrongDialog
      },
      set(val) {
        this.$emit('update:addWrongDialog', val)
      }
    }
  },
  methods: {
    keywordsChange(val) {
      this.tmpWrong.word = val
      if (this.tmpWrong.word) {
        this.$refs.wrongForm.validateField('word')
      }
    },
    // 获取字符长度，一个中文3个字节
    getLength(str){
      let realLength = 0
      let len = str.length
      let charCode = -1
      for (var i = 0; i < len; i++) {
        charCode = str.charCodeAt(i)
        if (charCode >= 0 && charCode <= 128) {
          realLength += 1
        } else {
          realLength += 3
        }
      }
      return realLength
    },
    // 校验字符长度
    checkStrLength(str, strLength) {
      let length = this.getLength(str)
      return length > strLength
    },
    // 验证易错词事件
    async confirmWrongClick() {
      try {
        await util.validateForm(this.$refs['wrongForm'])
      } catch (e) {
        util.showErrorMessageBox(e)
        return
      }

      if (!this.tmpWrong.word || !this.tmpWrong.word.trim()) {
        util.showErrorMessageBox('易错词不能为空 ！')
        return
      }

      if (!this.tmpWrong.replaceWord || !this.tmpWrong.replaceWord.trim()) {
        util.showErrorMessageBox('替换词不能为空 ！')
        return
      }

      if (!this.tmpWrong.ID) {
        if (this.checkStrLength(this.tmpWrong.word, 50) || this.checkStrLength(this.tmpWrong.replaceWord, 50)) {
          util.showErrorMessageBox('易错词或替换词最大长度为50')
          return
        }
        let res = await axios.post('/ui/wrongwords', this.tmpWrong)
        if (res.data.status === 1) {
          util.showResponseMessage(res.data)
          this.isShow = false
          this.$emit('callback')
        } else {
          util.showErrorMessageBox(res.data.message)
        }
      } else {
        if (this.checkStrLength(this.tmpWrong.word, 50) || this.checkStrLength(this.tmpWrong.replaceWord, 50)) {
          util.showErrorMessageBox('易错词或替换词最大长度为50')
          return
        }
        let res = await axios.put(`/ui/wrongwords/${this.tmpWrong.ID}`, this.tmpWrong)
        if (res.data.status === 1) {
          util.showResponseMessage(res.data)
          this.isShow = false
          this.$emit('callback')
        } else {
          util.showErrorMessageBox(res.data.message)
        }
      }
    }
  },
  props: {
    addWrongDialog: {
      type: Boolean,
      required: true
    },
    tmpWrong: {
      type: Object,
      required: true
    }
  }
}
</script>

<style>
/* 在一般情况下dialog宽为450像素，在逻辑分辨率480像素以下dialog宽为96% */
.wrong-word-dialog .el-dialog {
  width: 450px;
}
@media (max-width: 480px) {
  .wrong-word-dialog .el-dialog {
    width: 96%;
  }
}
.wrong-word-dialog .el-dialog__body {
  height: 180px;
}
</style>
<style scoped>

</style>
