<template>
  <div class="enterprise-inspection">
    <el-form
      ref="ruleFormRef"
      :model="ruleForm"
      :rules="rules"
      label-position="top"
      label-width="150px"
      class="rule-form-orange"
      :style="formStyle"
    >
      <el-form-item :label="formlabel.company" prop="corporateName" class="two-form-item">
        <custom-select
          :value.sync="ruleForm.corporateName"
          :disabled="disabled"
          :placeholder="placeholder"
          @search-click="searchClick"
          @search-change="searchChange"
        >
          <el-option
            v-for="item in listData"
            :key="item.corporateName"
            :label="item.corporateName"
            :value="item.corporateName"
          />
        </custom-select>
      </el-form-item>
      <el-form-item label="统一社会信用代码">
        <el-input
          v-model="ruleForm.socialCreditCode"
          placeholder="请输入统一社会信用代码"
          disabled
        />
      </el-form-item>
      <el-form-item label="法人代表：" class="one-form-item">
        <el-input
          v-model="ruleForm.legalRepresentative"
          placeholder="请输入法人代表"
          disabled
        />
      </el-form-item>
      <el-form-item label="成立日期" class="one-form-item">
        <el-input
          v-model="ruleForm.startSate"
          placeholder="请输入成立日期"
          disabled
        />
      </el-form-item>
      <!-- <el-form-item label="注册资本">
        <el-input
          v-model="ruleForm.registeredCapital"
          placeholder="请输入注册资本"
          disabled
        />
      </el-form-item> -->
      <el-form-item label="注册地址" style="width: 100%; border-bottom: 0px">
        <el-input
          v-model="ruleForm.address"
          placeholder="请输入注册地址"
          disabled
        />
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { getEnterpriseInspectionInfo } from '@/api/myBusiness/supplierEnterprise'
export default {
  components: {
    CustomSelect: () => import('@/components/CustomSelect/index.vue')
  },
  props: {
    // 表单样式
    'formStyle': {
      type: Object,
      default: () => {}
    },
    // 字段映射配置
    inspectionData: {
      type: [Object],
      default: () => {}
    },
    // 来源
    source: {
      type: String,
      default: ''
    },
    // 是否禁用
    disabled: {
      type: Boolean,
      default: false
    },
    // 详情信息
    detailsInfo: {
      type: Object,
      default: () => {}
    },
    placeholder: {
      type: String,
      default: '请选择公司名称'
    }
  },
  data() {
    return {
      formlabel: {
        company: '企业名称',
        key: 'corporateName'
      },
      listData: [],
      ruleForm: {
        corporateName: '',
        socialCreditCode: '',
        legalRepresentative: '',
        startSate: '',
        registeredCapital: '',
        address: ''
      },
      rules: {
        corporateName: [
          { required: true, message: `请输入${this.inspectionData?.company?.name || '企业名称'}`, trigger: 'change' }
        ]
      }
    }
  },
  watch: {
    inspectionData: {
      handler(newV) {
        if (newV && Object.keys(newV).length) {
          const { company } = newV
          if (company) {
            this.formlabel.company = company.name
            this.formlabel.key = company.key
          }
        }
      },
      immediate: true,
      deep: true
    },
    detailsInfo: {
      handler(newV) {
        if (newV && Object.keys(newV).length) {
          const { corporateName, socialCreditCode, legalRepresentative, startSate, registeredCapital, address } = newV
          this.ruleForm = {
            ...this.ruleForm,
            corporateName,
            socialCreditCode,
            legalRepresentative,
            startSate,
            registeredCapital,
            address
          }
        }
      },
      immediate: true,
      deep: true
    }
  },
  methods: {
    searchClick(val) {
      getEnterpriseInspectionInfo({ keyword: val }).then(({ data }) => {
        this.listData = data
      })
    },
    searchChange(val) {
      const obj = this.listData.find(item => item.corporateName === val)
      if (!obj) return
      const { socialCreditCode, legalRepresentative, startSate, registeredCapital, address } = obj
      this.ruleForm = {
        ...this.ruleForm,
        socialCreditCode,
        legalRepresentative,
        startSate: startSate ? startSate.split(' ')[0] : '',
        registeredCapital,
        address
      }

      if (this.source !== '' && this.source === 'projectAdd') {
        console.log('send')
        window.$bus.$emit('sendCoreName', this.ruleForm.corporateName)
      }
    },
    validate() {
      return new Promise((resolve, reject) => {
        this.$refs.ruleFormRef.validate(valid => {
          if (valid) {
            return resolve(this.ruleForm)
          } else {
            reject()
            return false
          }
        })
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.enterprise-inspection {
}
</style>
