<template>

  <el-form
    ref="sponsorForm"
    :model="form"
    :rules="rules"
    class="demo-ruleForm"
    size="small"
    label-width="150px"
  >
    <div class="base-dialog-body">
      <el-form-item label="Sponsor Name: " prop="SponsorName">
        <el-input :maxLength="$inputMax" v-model="form.SponsorName" />
      </el-form-item>
      <el-form-item label="Sponsor NameCN: " prop="SponsorName">
        <el-input :maxLength="$inputMax" v-model="form.SponsorNameCN" />
      </el-form-item>
      <el-form-item label="Sponsor Code: " prop="SponsorName">
        <el-input :maxLength="$inputMax" v-model="form.SponsorCode" />
      </el-form-item>
    </div>
    <div class="base-dialog-footer" style="text-align:right;margin-top:10px;">
      <el-form-item>
        <el-button :disabled="btnLoading" size="small" type="primary" @click="handleCancel">Cancel</el-button>
        <el-button size="small" type="primary" :loading="btnLoading" @click="handleSave">Save</el-button>
      </el-form-item>
    </div>
  </el-form>
</template>
<script>
import { addOrUpdateSponsor } from '@/api/dictionary'
export default {
  name: 'SponsorForm',
  props: {
    data: {
      type: Object,
      default() {
        return {}
      }
    }
  },
  data() {
    return {
      btnLoading: false,
      form: {
        Id: '',
        SponsorName: '',
        SponsorNameCN: '',
        SponsorCode: '',
      },
      rules: {
        SponsorName: [{ required: true, message: '请注明', trigger: 'blur' }, { max: 50, message: '最大长度为 50' }],
        SponsorNameCN: [{ required: true, message: '请注明', trigger: 'blur' }, { max: 50, message: '最大长度为 50' }],
        SponsorCode: [{ required: true, message: '请注明', trigger: 'blur' }, { max: 50, message: '最大长度为 50' }]
      }
    }
  },
  mounted() {
    if (Object.keys(this.data).length && this.data.Id) {
      this.form = { ...this.data }
    }
  },
  methods: {
    handleSave() {
      this.$refs.sponsorForm.validate(valid => {
        if (!valid) return
        this.btnLoading = true
        addOrUpdateSponsor(this.form).then(res => {
          this.btnLoading = false
          if (res.IsSuccess) {
            this.$message.success('Saved successfully')
            this.$refs['sponsorForm'].resetFields()
            this.$emit('getList')
            this.$emit('close')
          }
        }).catch(() => {
          this.btnLoading = false
        })
      })
    },
    handleCancel() {
      this.$emit('close')
    }
  }

}
</script>
