<template>
  <div class="core-enterprise">
    <el-form
      ref="ruleFormRef"
      :model="ruleForm"
      :rules="rules"
      label-position="top"
      label-width="150px"
      class="rule-form-orange"
    >
      <el-form-item label="公司名称">
        <el-input v-model="ruleForm.corporateName" placeholder="请输入公司名称" readonly />
      </el-form-item>
      <el-form-item label="集采平台" prop="centralizedProcurementPlatform">
        <el-radio-group v-model="ruleForm.centralizedProcurementPlatform">
          <el-radio :label="1">是</el-radio>
          <el-radio :label="0">否</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="与供应商存在历史合作" prop="historicalCooperation">
        <el-radio-group v-model="ruleForm.historicalCooperation" @change="historicalCooperationChange">
          <el-radio :label="1">是</el-radio>
          <el-radio :label="0">否</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  props: {
    details: {
      type: Object,
      default: () => {}
    }
  },
  data() {
    return {
      ruleForm: {
        id: '',
        corporateName: '',
        cProjectInformationId: '',
        centralizedProcurementPlatform: '',
        historicalCooperation: ''
      },
      rules: {
        centralizedProcurementPlatform: [
          { required: true, message: '请选择是否是集采平台', trigger: 'change' }
        ],
        historicalCooperation: [
          { required: true, message: '请选择是否与供应商存在历史合作', trigger: 'change' }
        ]
      }
    }
  },
  watch: {
    details: {
      handler: function(newV) {
        if (newV && Object.keys(newV)) {
          const details = JSON.parse(JSON.stringify(newV))
          for (const k in this.ruleForm) {
            if (details[k] !== '' && details[k] !== null && details[k] !== undefined) {
              this.ruleForm[k] = details[k]
            }
          }
          this.ruleForm.corporateName = details.corporateName
        }
      },
      immediate: true,
      deep: true
    }
  },
  methods: {
    historicalCooperationChange() {
      this.$emit('isHistoricalCooperation', this.ruleForm.historicalCooperation)
    },
    validate(submitType) {
      return new Promise((resolve, reject) => {
        if (submitType === 'submit') {
          this.$refs.ruleFormRef.validate(valid => {
            if (valid) {
              return resolve(this.ruleForm)
            } else {
              reject()
              return false
            }
          })
        } else {
          resolve(this.ruleForm)
        }
      })
    }
  }

}
</script>

<style lang="scss" scoped>
.core-enterprise {
.el-form {
  padding: 10px 20px;
}
}
</style>
