<template>
  <el-dialog :title="title" :close-on-click-modal="false" :visible.sync="open" width="400px" append-to-body>
    <el-form ref="form" :model="form" :rules="rules" label-width="200px">
      <el-form-item label="是否忽略大小写" prop="ignoreCase" >
        <el-switch
            v-model="form.ignoreCase"
            inactive-color="#ff4949"
            :active-value="true"
            :inactive-value="false"
        ></el-switch>
      </el-form-item>
      <el-form-item label="是否忽略全角、半角" prop="ignoreWidth" >
        <el-switch
            v-model="form.ignoreWidth"
            inactive-color="#ff4949"
            :active-value="true"
            :inactive-value="false"
        ></el-switch>
      </el-form-item>
      <el-form-item label="是否忽略数字样式" prop="ignoreNumStyle" >
        <el-switch
            v-model="form.ignoreNumStyle"
            inactive-color="#ff4949"
            :active-value="true"
            :inactive-value="false"
        ></el-switch>
      </el-form-item>
      <el-form-item label="是否忽略中文样式" prop="ignoreChineseStyle" >
        <el-switch
            v-model="form.ignoreChineseStyle"
            inactive-color="#ff4949"
            :active-value="true"
            :inactive-value="false"
        ></el-switch>
      </el-form-item>
      <el-form-item label="是否忽略重复" prop="ignoreRepeat" >
        <el-switch
            v-model="form.ignoreRepeat"
            inactive-color="#ff4949"
            :active-value="true"
            :inactive-value="false"
        ></el-switch>
      </el-form-item>
      <el-form-item label="启用数字检测" prop="enableNumCheck" >
        <el-switch
            v-model="form.enableNumCheck"
            inactive-color="#ff4949"
            :active-value="true"
            :inactive-value="false"
        ></el-switch>
      </el-form-item>
      <el-form-item label="检测数字时的长度" prop="numCheckLen" v-if="form.enableNumCheck">
        <el-input-number v-model="form.numCheckLen" :step="1" :min="0"></el-input-number>
      </el-form-item>
      <el-form-item label="启用邮箱检测" prop="enableEmailCheck" >
        <el-switch
            v-model="form.enableEmailCheck"
            inactive-color="#ff4949"
            :active-value="true"
            :inactive-value="false"
        ></el-switch>
      </el-form-item>
      <el-form-item label="启用URL检测" prop="enableUrlCheck" >
        <el-switch
            v-model="form.enableUrlCheck"
            inactive-color="#ff4949"
            :active-value="true"
            :inactive-value="false"
        ></el-switch>
      </el-form-item>
      <el-form-item label="启用敏感词检测" prop="enableWordCheck" >
        <el-switch
            v-model="form.enableWordCheck"
            inactive-color="#ff4949"
            :active-value="true"
            :inactive-value="false"
        ></el-switch>
      </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 { getSensitiveConfig, updateSensitiveConfig } from '@/api/sensitive/word'

export default {
  components: {},
  data() {
    return {
      systemName: process.env.VUE_APP_SYSTEM_HOME,
      open: false,
      title: '',
      form: {
        ignoreCase: true,
        ignoreWidth: true,
        ignoreNumStyle: true,
        ignoreChineseStyle: true,
        ignoreEnglishStyle: true,
        ignoreRepeat: false,
        enableNumCheck: true,
        enableEmailCheck: true,
        enableUrlCheck: true,
        enableWordCheck: true,
        numCheckLen: 8,
      },
      // 表单校验
      rules: {
        sourceText: [
          { required: true, message: '数据不能为空', trigger: 'blur' }
        ]
      },
      buttonLoading: false
    }
  },
  created() {
  },
  methods: {
    init() {
      this.open = true
      this.title = '敏感词配置'
      this.$nextTick(() => {
        this.$refs['form'].resetFields();
        getSensitiveConfig().then(response => {
          if(response.data){
            this.form = response.data;
          }
        });
      })
    },
    // 表单提交
    submitForm() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.buttonLoading = true
          updateSensitiveConfig(this.form).then(data => {
            this.open = false
            this.buttonLoading = false
          }).finally(() => {
            this.buttonLoading = false
          })
        }
      })
    }
  }
}
</script>
