<template>
  <el-dialog
    custom-class="center-dialog"
    :title="title"
    top="0vh"
    :visible.sync="dialogFormVisible"
    width="500px"
    @close="close"
  >
    <el-form
      ref="form"
      label-position="left"
      label-width="100px"
      :model="form"
      :rules="rules"
      size="medium"
    >
      <el-form-item :label="__('源')" label-width="50px" prop="meta_ori">
        <el-input
          v-model="form.meta_ori"
          clearable
          :placeholder="__('请输入源')"
          :style="{ width: '100%' }"
        />
      </el-form-item>
      <el-form-item :label="__('值')" label-width="50px" prop="meta_value">
        <el-input
          v-model="form.meta_value"
          clearable
          :placeholder="__('请输入值')"
        />
      </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/lang/meta'

  export default {
    name: 'BaseLangMetaEdit',
    data() {
      return {
        form: {},
        rules: {
          meta_ori: [
            {
              required: true,
              message: this.__('请输入源'),
              trigger: 'blur',
            },
          ],
          meta_value: [
            {
              required: true,
              message: this.__('请输入值'),
              trigger: 'blur',
            },
          ],
        },
        title: '',
        dialogFormVisible: false,
      }
    },
    created() {},
    methods: {
      __,
      showEdit(row) {
        if (!row) {
          this.title = this.__('添加')
        } else {
          this.title = this.__('编辑')
          const obj = Object.assign({}, row)
          this.form = {
            meta_id: obj.meta_id,
            meta_ori: obj.meta_ori,
            meta_value: obj.meta_value,
          }
        }
        this.dialogFormVisible = true
      },
      close() {
        this.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>
