<template>
  <el-form ref="form" :rules="rules" :model="form" label-width="80px" label-position="top" size="small">
    <el-row :gutter="40">
      <el-col :span="12">
        <el-form-item label="企业名称" prop="companyName">
          <el-input v-model="form.companyName" placeholder="请输入企业名称" disabled />
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="企业证件类型" prop="companyCertType">
          <el-select v-model="form.companyCertType" placeholder="请选择证件类型" disabled>
            <el-option v-for="item of dict.companyCertType" :key="item.key" :label="item.label" :value="item.value"/>
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="企业证件号码" prop="companyCertNumber">
          <el-input v-model="form.companyCertNumber" placeholder="请输入企业证件号码" disabled />
        </el-form-item>
      </el-col>
    </el-row>
  </el-form>
</template>
<script>
import { mapGetters } from 'vuex'
export default {
  name: 'FlowEnterpriseInfo',
  props: {
    data: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    return {
      form: {
        companyName: '',
        companyCertType: '',
        companyCertNumber: ''
      },
      rules: {
        companyName: [{ required: true, message: '请输入企业名称', trigger: 'blur' }],
        companyCertType: [{ required: true, message: '请选择企业证件类型', trigger: 'blur, change' }],
        companyCertNumber: [{ required: true, message: '请输入企业证件号码', trigger: 'blur' }]
      }
    }
  },
  computed: {
    ...mapGetters(['dict'])
  },
  watch: {
    data() {
      this.updateInternalData()
    }
  },
  created() {
    this.init()
  },
  methods: {
    init() {
      this.updateInternalData()
    },

    updateInternalData() {
      if (typeof this.data === 'object' && this.data !== null) {
        const { companyName, companyCertType, companyCertNumber } = this.data
        this.form = {
          companyName,
          companyCertType,
          companyCertNumber
        }
      } else {
        this.form = {
          companyName: '',
          companyCertType: '',
          companyCertNumber: ''
        }
      }
    },

    getPendingCacheData() {
      return this.getFormData()
    },

    /**
    * 获取当前模块数据
    * @time 2022-04-23 15:50:05
    */
    async validate(isErrorAnchor = false) {
      try {
        await this.$refs.form.validate()
        return true
      } catch (e) {
        if (isErrorAnchor) {
          this.$refs.form.fields.filter(item => item.validateState === 'error')[0].$el.scrollIntoView(true)
        }
        let temp = {
          anchor: () => {
            this.$refs.form.fields.filter(item => item.validateState === 'error')[0].$el.scrollIntoView(true)
            temp = null
          }
        }
        throw temp
      }
    },

    /**
    * 获取表单数据
    * @time 2022-04-23 18:02:02
    */
    getFormData() {
      return this.form
    }
  }
}
</script>
