<script lang="ts" setup>
import { onBeforeMount, ref } from 'vue'
import type { FormItemRule, FormRules } from 'naive-ui'
import { useDictionary } from '@/store/modules/dictonary'

const props = defineProps<{
  value: string[] | null
  label: string
}>()

const emit = defineEmits<{
  (e: 'update:value', value: string[]): void
}>()

const formValue = ref({
  plans: props.value,
})

const rules: FormRules = {
  plans: [
    {
      required: false,
      type: 'array',
      trigger: ['blur', 'change'],
    },
  ],
}

const options = ref([])
const configStore = useDictionary()
const labStyle = {
  fontWeight: 'bold',
}

onBeforeMount(async () => {
  const list = await configStore.fetchIzfirmList()
  options.value = list
})

function onChange(value: Array<string>) {
  emit('update:value', value)
}
</script>

<template>
  <n-form :model="formValue" style="padding: 0px 10px;" :rules="rules">
    <n-form-item :label="label" path="plans" :label-style="labStyle">
      <n-select filterable
        v-model:value="formValue.plans" :max-tag-count="2" placeholder="请选择厂商" multiple
        :options="options" @update:value="onChange"
      />
    </n-form-item>
  </n-form>
</template>

<style lang="less" scoped></style>
