<template>
  <el-form
    ref="signTempForm"
    v-loading="loading"
    :model="form"
    label-width="160px"
    size="small"
    :rules="rules"
  >
    <div class="base-dialog-body">
      <el-form-item :label="$t('dictionary:sign:label:code')" prop="Code">
        <el-input v-model="form.Code" />
      </el-form-item>
      <!-- 模板 -->
      <el-form-item :label="$t('dictionary:sign:label:name')" prop="Name">
        <el-input v-model="form.Name" />
      </el-form-item>
      <!-- 签名内容(EN) -->
      <el-form-item :label="$t('dictionary:sign:label:value')" prop="Value">
        <el-input v-model="form.Value" type="textarea" rows="5" />
      </el-form-item>
      <!-- 签名内容(CN) -->
      <el-form-item :label="$t('dictionary:sign:label:valueCN')" prop="ValueCN">
        <el-input v-model="form.ValueCN" type="textarea" rows="5" />
      </el-form-item>
    </div>
    <div class="base-dialog-footer" style="text-align:right;margin-top:10px;">
      <el-form-item style="text-align:right;">
        <el-button size="small" type="primary" :disabled="form.Type === '' || form.Name === ''" :loading="saveBtnLoading" @click="handleSave">{{ $t('common:button:save') }}</el-button>
      </el-form-item>
    </div>
  </el-form>
</template>
<script>
import { addOrUpdateSystemBasicData } from '@/api/dictionary'
export default {
  props: {
    data: {
      type: Object,
      default() { return {} }
    }
  },

  data() {
    return {
      form: {
        Id: '',
        Code: '',
        Name: '',
        Value: '',
        ValueCN: ''
      },
      rules: {
        Code: [{ required: true, message: this.$t('common:ruleMessage:specify'), trigger: ['blur'] }],
        Name: [{ required: true, message: this.$t('common:ruleMessage:specify'), trigger: ['blur'] }],
        Value: [{ required: true, message: this.$t('common:ruleMessage:specify'), trigger: ['blur'] }],
        ValueCN: [{ required: true, message: this.$t('common:ruleMessage:specify'), trigger: ['blur'] }]
      },
      scenarioOption: [],
      saveBtnLoading: false,
      loading: false
    }
  },
  mounted() {
    this.initForm()
  },
  methods: {
    async initForm() {
      this.loading = true
      for (const k in this.form) {
        if (this.data.hasOwnProperty(k)) {
          this.form[k] = this.data[k]
        }
      }
      this.loading = false
    },
    // 保存
    handleSave() {
      this.$refs.signTempForm.validate(valid => {
        if (!valid) return
        this.saveBtnLoading = true
        addOrUpdateSystemBasicData(this.form).then(res => {
          this.saveBtnLoading = false
          this.$emit('closeDialog')
          this.$emit('getList')
          this.$message.success(this.$t('common:message:savedSuccessfully'))
        }).catch(() => {
          this.saveBtnLoading = false
        })
      })
    }
  }
}
</script>
