<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-select v-model="form.companyName" :remote-method="getData" :loading="loading" clearable filterable remote placeholder="请输入企业名称" @change="onChange">
            <el-option v-for="(item, index) in companyOptions" :key="index" :label="item" :value="item"/>
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="原责任人姓名" prop="corporationName">
          <el-select v-model="form.corporationName" placeholder="请选择原责任人">
            <el-option v-for="(item, index) in corporationOptions" :key="index" :label="item" :value="item"/>
          </el-select>
        </el-form-item>
      </el-col>
    </el-row>
  </el-form>
</template>
<script>
import { queryEnterprise } from '@/api/person-change'
export default {
  name: 'EnterpriseInfo',
  props: {
    data: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    return {
      form: {
        companyName: '',
        corporationName: ''
      },
      loading: false,
      options: [],
      rules: {
        companyName: [
          { required: true, message: '请输入企业名称', trigger: 'blur' }
        ],
        corporationName: [
          { required: true, message: '请选择原责任人姓名', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
    /**
     * 公司下拉选项
     */
    companyOptions() {
      const options = []
      // 遍历所有的接口返回的数据
      this.options.forEach(option => {
        if (!options.some(companyName => companyName === option.companyName)) {
          options.push(option.companyName)
        }
      })
      return options
    },

    /**
     * 责任人下拉列表
     */
    corporationOptions() {
      const options = []
      const { companyName } = this.form
      this.options.forEach(option => {
        if (option.companyName === companyName) {
          options.push(option.corporationName)
        }
      })
      return options
    }
  },
  watch: {
    data() {
      this.updateInternalData()
    }
  },
  created() {
    this.init()
  },
  methods: {
    init() {
      this.updateInternalData()
    },

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

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

    /**
    * 获取数据
    * @time 2022-04-23 20:38:03
    */
    getData(companyName) {
      // 如果当前输入的不正确，则直接清空数据
      if (!companyName) {
        this.options = []
        return
      }
      this.loading = true
      return queryEnterprise({ companyName }).then(data => {
        this.loading = false
        this.options = data.list || []
      }).catch(() => {
        this.loading = false
        this.options = []
      })
    },

    /**
    * change事件
    * @time 2022-04-23 20:37:35
    */
    onChange() {
      this.$nextTick(() => {
        this.form.corporationName = this.corporationOptions[0]
      })
    },

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