<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="Code: " prop="Code">
        <el-input v-model="form.Code" />
      </el-form-item>
      <el-form-item label="模板: " prop="Name">
        <el-input v-model="form.Name" />
      </el-form-item>
      <el-form-item label="签名内容(EN): " prop="Value">
        <el-input v-model="form.Value" type="textarea" rows="5" />
      </el-form-item>
      <el-form-item label="签名内容(CN): " 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">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: 'Please specify', trigger: ['blur'] }],
        Name: [{ required: true, message: 'Please specify', trigger: ['blur'] }],
        Value: [{ required: true, message: 'Please specify', trigger: ['blur'] }],
        ValueCN: [{ required: true, message: 'Please 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('Saved successfully')
        }).catch(() => {
          this.saveBtnLoading = false
        })
      })
    }
  }
}
</script>
