<template>
  <el-select :value="value" :placeholder="placeholder" clearable @change="handleCarCompanyChange">
    <el-option v-for="item in list" v-bind="item" :key="item.value" />
  </el-select>
</template>

<script>
import { queryOrgByList } from '@/api/management'

export default {
  name: 'CarCompany',
  props: {
    value: {
      type: [String, Number],
      default: ''
    },
    placeholder: {
      type: String,
      default: '请选择'
    },
    // 默认选中第一项
    defaultSelectFirst: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      list: []
    }
  },
  mounted() {
    this.queryOrgByList()
  },
  methods: {
    /**
     * 查询车厂列表
     */
    async queryOrgByList() {
      const data = await queryOrgByList()
      // 如果查询出来了车企
      if (data && data.length > 0) {
        this.list = data.map(item => {
          return {
            value: item.organCode,
            label: item.organName
          }
        })
      }
      // 如果当前需要默认选中第一项
      this.defaultSelectFirst && this.handleCarCompanyChange(this.list[0].value)
    },

    /**
     * 车厂切换方法
     * @param v 当前方法
     */
    handleCarCompanyChange(v) {
      this.$emit('input', v)
      this.$emit('change', v)
    }
  }
}
</script>
