<template>
  <el-dialog
    custom-class="center-dialog"
    :title="title"
    top="0vh"
    :visible.sync="dialogFormVisible"
    width="500px"
    @close="close"
  >
    <el-form ref="form" label-width="120px" :model="form" :rules="rules">
      <el-form-item :label="__('查找敏感词')" prop="filter_find">
        <el-input v-model="form.filter_find" :placeholder="__('查找敏感词')" />
      </el-form-item>
      <!--      <el-form-item :label="__('替换内容')" prop="filter_replace">-->
      <!--        <el-input v-model="form.filter_replace" :placeholder="__('替换内容')" />-->
      <!--      </el-form-item>-->

      <el-form-item :label="__('是否启用')" prop="filter_enable">
        <el-radio v-model="form.filter_enable" :label="1">
          {{ __('已启用') }}
        </el-radio>
        <el-radio v-model="form.filter_enable" :label="0">
          {{ __('未启用') }}
        </el-radio>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="close">{{ __('取消') }}</el-button>
      <el-button type="primary" @click="save">{{ __('确定') }}</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { translateTitle as __ } from '@/utils/i18n'

  import { doEdit } from '@/api/base/filter/keyword'

  export default {
    name: 'BaseFilterKeywordEdit',
    data() {
      return {
        form: {
          filter_enable: 0,
        },
        rules: {},
        title: '',
        dialogFormVisible: false,
      }
    },
    created() {},
    mounted() {
      this.rules = {
        filter_id: [
          { required: true, trigger: 'blur', message: this.__('敏感词编号') },
        ],
        filter_find: [
          { required: true, trigger: 'blur', message: this.__('查找敏感词') },
        ],
        // filter_replace: [
        //   { required: true, trigger: 'blur', message: this.__('替换内容') },
        // ],
        filter_type: [
          { required: true, trigger: 'blur', message: this.__('过滤类型') },
        ],
        filter_time: [
          { required: true, trigger: 'blur', message: this.__('添加时间') },
        ],
        filter_buildin: [
          { required: true, trigger: 'blur', message: this.__('是否系统内置') },
        ],
        user_id: [
          { required: true, trigger: 'blur', message: this.__('用户编号') },
        ],
        filter_enable: [
          { required: true, trigger: 'blur', message: this.__('是否启用') },
        ],
      }

      this.$forceUpdate()
    },
    methods: {
      __,
      showEdit(row) {
        if (!row) {
          this.title = this.__('添加')
        } else {
          this.title = this.__('编辑')
          this.form = Object.assign({}, row)
        }
        this.dialogFormVisible = true
      },
      close() {
        this.form = this.$options.data().form
        this.$refs['form'].resetFields()
        this.dialogFormVisible = false
      },
      save() {
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            const { msg, status } = await doEdit(this.form)
            if (200 == status) {
              this.$baseMessage(msg, 'success')
            } else {
              this.$baseMessage(msg, 'error')
            }
            this.$emit('fetch-data')
            this.close()
          } else {
            return false
          }
        })
      },
    },
  }
</script>
