<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="请输入企业名称" clearable />
        </el-form-item>
      </el-col>
    </el-row>
    <el-row v-if="type === 'enterprise'" :gutter="40">
      <el-col :span="12">
        <el-form-item label="企业性质" prop="companyType">
          <el-select v-model="form.companyType" placeholder="请选择企业性质" clearable>
            <el-option v-for="item of dict.companyType" :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="industryType">
          <el-select v-model="form.industryType" placeholder="请选择行业类型" clearable>
            <el-option v-for="item of dict.industryType" :key="item.key" :label="item.label" :value="item.value"/>
          </el-select>
        </el-form-item>
      </el-col>
    </el-row>
  </el-form>
</template>
<script>
import { mapGetters } from 'vuex'
export default {
  name: 'BaseInfo',
  props: {
    type: {
      type: String,
      default: 'enterprise' // enterprise: 企业, car-enterprise: 车企
    },
    data: {
      type: Object,
      default() {
        return {}
      }
    }
  },
  data() {
    return {
      form: {
        companyName: '',
        companyType: '',
        industryType: ''
      },
      rules: {
        companyName: [
          { 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) {
        this.form = {
          companyName: this.data.companyName || '',
          companyType: this.data.companyType || '',
          industryType: this.data.industryType || ''
        }
      } else {
        this.form = {
          companyName: '',
          companyType: '',
          industryType: ''
        }
      }
    },

    /**
    * 获取当前模块数据
    * @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
    },

    /**
    * 获取待缓存数据
    * @time 2022-04-24 19:43:57
    */
    getPendingCacheData() {
      return this.getFormData()
    }
  }
}
</script>
