<template>
  <autocompleteUI
    v-model="value"
    :item="item"
    @change="valueChange"
  ></autocompleteUI>
</template>

<script setup>
import { ref } from 'vue'
import autocompleteUI from '@/components/FormUI/autocomplete'
defineOptions({ name: 'enterpriseLogin-companySelect' })
const props = defineProps({
  modelValue: String,
  defaultSelect: {
    type: Boolean,
    default: false
  },
  list: {
    type: Array,
    default: () => []
  }
})
const emit = defineEmits(['update:modelValue'])

const value = ref(props.modelValue)

// 企业登录
// const companyList = JSON.parse(localStorage.getItem('companyInfo'))
const companyList = props.list?.length ? [...props.list] : []
if (props.defaultSelect && companyList?.length) {
  const id = companyList[0].enterpriseId
  value.value = id
  emit('update:modelValue', id)
}
// onMounted(() => {})

const item = ref({
  // type: 'autocomplete',
  // width: 250,
  itemText: 'enterpriseName',
  itemValue: 'enterpriseId',
  label: '请选择要登录的企业',
  items: companyList
})

const valueChange = (val) => {
  if (val) emit('update:modelValue', val)
}
</script>