<template>
  <div>
    <el-select
      class="select"
      :style="{ width: `${width}px` }"
      filterable
      v-model="props.selectValue"
      @change="onChange"
      clearable
      :placeholder="props.placeholder"
      size="large"
    >
      <el-option
        v-if="props.type == '999'"
        v-for="item in props.options"
        :key="item[fieldName.value]"
        :label="item[fieldName.label]"
        :value="item[fieldName.value]"
      />
      <el-option
        v-if="props.type == '1'"
        v-for="item in arr.arr"
        :key="item.companyId"
        :label="item.companyName"
        :value="item.companyId"
      />
    </el-select>
  </div>
</template>

<script setup lang="ts">
  import { getAuthorizeCompany } from '@/api/deviceAuthorize'
  import { useRequest } from '@/utils/hooks'
  const arr: any = reactive({
    arr: []
  })
  const emits = defineEmits(['update:selectValue', 'update:selectName'])
  const onChange = (e: any) => {
    if (props.type == 1) {
      const obj = arr.arr.find((item: any) => item.companyId == e)
      emits('update:selectName', obj?.companyName)
    }
    emits('update:selectValue', e)
  }
  onMounted(async () => {
    if (props.type == 1) {
      await company.getData()
      arr.arr = company.data
    }
  })

  const company = useRequest(() => getAuthorizeCompany())
  const props: any = defineProps({
    fieldName: {
      type: Object,
      default: () => ({
        label: 'label',
        value: 'value'
      })
    },
    options: {
      type: Array,
      default: () => []
    },
    placeholder: {
      type: String,
      default: '请选择'
    },
    selectValue: {
      type: [String, Number],
      default: ''
    },
    selectName: {
      type: String,
      default: ''
    },
    width: {
      type: [String, Number],
      default: '475'
    },
    type: {
      type: [String, Number],
      default: '999'
    }
  })
</script>

<style lang="less" scoped>
  :deep(.select) {
    background: #ffffff;
    border-radius: 10px;
    .el-input__wrapper {
      height: 50px;
      border-radius: 10px;
    }
    .el-icon svg {
      color: #5f7cd6;
    }
  }
</style>
