<template>
  <div>
    <el-dialog :title="!tmpBad.ID ? '新建敏感词' : '编辑敏感词'" :visible.sync="isShow" v-if="isShow" class="bad-word-dialog">
      <el-form :model="tmpBad" ref="badForm" label-width="100px">
        <el-form-item prop="word" label="敏感词：" :verify="['NotNull']">
          <el-input type="text" v-model="tmpBad.word" auto-complete="off"></el-input>
        </el-form-item>
        <div style="text-align:center;padding-bottom: 12px;" v-if="!tmpBad.ID">(多个敏感词之间用逗号隔开)</div>
    <el-form-item prop="replaceWord" label="替换词：">
      <el-input type="text" v-model="tmpBad.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="confirmBadClick">确 定</el-button>
    </div>
    </el-dialog>
</div>
</template>

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

export default {
  data() {
    return {
    }
  },
  computed: {
    isShow: {
      get() {
        return this.addBadDialog
      },
      set(val) {
        this.$emit('update:addBadDialog', val)
      }
    }
  },
  methods: {
    keywordsChange(val) {
      this.tmpBad.word = val
      if (this.tmpBad.word) {
        this.$refs.badForm.validateField('word')
      }
    },
    // 验证敏感词事件
    async confirmBadClick() {
      try {
        await util.validateForm(this.$refs['badForm'])
      } catch (e) {
        util.showErrorMessageBox(e)
        return
      }
      if (!this.tmpBad.ID) {
        let res = await axios.post('/ui/badwords', this.tmpBad)
        util.showResponseMessage(res.data)
        if (res.data.status === 1) {
          this.isShow = false
          this.$emit('callback')
        }
      } else {
        if (this.tmpBad.word.length > 50) {
          util.showErrorMessageBox('敏感词最大长度为50')
          return
        }
        let res = await axios.put(`/ui/badwords/${this.tmpBad.ID}`, this.tmpBad)
        util.showResponseMessage(res.data)
        if (res.data.status === 1) {
          this.isShow = false
          this.$emit('callback')
        }
      }
    }
  },
  props: {
    addBadDialog: {
      type: Boolean,
      required: true
    },
    tmpBad: {
      type: Object,
      required: true
    }
  }
}
</script>

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

</style>
