<template>
  <el-dialog :title="title" :close-on-click-modal="false" :visible.sync="open" width="700px" append-to-body>
    <el-form ref="form" :model="form" :rules="rules" label-width="100px">
      <el-form-item label="文本" prop="sourceText">
        <el-input type="textarea" :rows="2" v-model="form.sourceText" placeholder="请输入"/>
      </el-form-item>
      <el-form-item label="替换后的文本" prop="targetText">
        <el-input type="textarea" :rows="2" v-model="form.targetText" readonly/>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="open = false">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { testWord } from '@/api/sensitive/word'

export default {
  components: {},
  data() {
    return {
      systemName: process.env.VUE_APP_SYSTEM_HOME,
      open: false,
      title: '',
      form: {
        sourceText: undefined,
        targetText: undefined,
      },
      // 表单校验
      rules: {
        sourceText: [
          { required: true, message: '数据不能为空', trigger: 'blur' }
        ]
      },
      buttonLoading: false
    }
  },
  created() {
  },
  methods: {
    init() {
      this.open = true
      this.title = '文本测试'
      this.$refs['form'].resetFields();
    },
    // 表单提交
    submitForm() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.buttonLoading = true
          testWord({
            word: this.form.sourceText
          }).then(data => {
            this.form.targetText = data.data.targetText
            this.buttonLoading = false
          }).finally(() => {
            this.buttonLoading = false
          })
        }
      })
    }
  }
}
</script>
