<template>
  <el-form
    ref="CROForm"
    :model="form"
    :rules="rules"
    class="demo-ruleForm"
    size="small"
    label-width="120px"
  >
    <div class="base-dialog-body">
      <el-form-item label="CRO Name: " prop="CROName">
        <el-input v-model="form.CROName" />
      </el-form-item>
      <el-form-item label="CRO NameCN: " prop="CRONameCN">
        <el-input v-model="form.CRONameCN" />
      </el-form-item>
      <el-form-item label="CRO Code: " prop="CROCode">
        <el-input v-model="form.CROCode" />
      </el-form-item>
    </div>
    <div class="base-dialog-footer" style="text-align:right;margin-top:10px;">
      <el-form-item>
        <el-button :disabled="btnLoading" type="primary" @click="handleCancel">Cancel</el-button>
        <el-button type="primary" :loading="btnLoading" @click="handleSave">Save</el-button>
      </el-form-item>
    </div>
  </el-form>
</template>
<script>
import { addOrUpdateCro } from '@/api/dictionary'
export default {
  name: 'CroForm',
  props: {
    data: {
      type: Object,
      default() {
        return {}
      }
    }
  },
  data() {
    return {
      btnLoading: false,
      form: {
        Id: '',
        CROName: '',
        CRONameCN: '',
        CROCode: ''
      },
      rules: {
        CROName: [{ required: true, message: 'Please specify', trigger: 'blur' }, { max: 50, message: 'The maximum length is 50' }],
        CRONameCN: [{ required: true, message: 'Please specify', trigger: 'blur' }, { max: 50, message: 'The maximum length is 50' }],
        CROCode: [{ required: true, message: 'Please specify', trigger: 'blur' }, { max: 50, message: 'The maximum length is 50' }]
      }
    }
  },
  mounted() {
    if (Object.keys(this.data).length && this.data.Id) {
      this.form = { ...this.data }
    }
  },
  methods: {
    handleSave() {
      this.$refs.CROForm.validate(valid => {
        if (!valid) return
        this.btnLoading = true
        addOrUpdateCro(this.form).then(res => {
          this.btnLoading = false
          if (res.IsSuccess) {
            this.$message.success('Saved successfully')
            this.$refs['CROForm'].resetFields()
            this.$emit('getList')
            this.$emit('close')
          }
        }).catch(() => {
          this.btnLoading = false
        })
      })
    },
    handleCancel() {
      this.$emit('close')
    }
  }

}
</script>
