<template>
  <a-checkbox-group v-model:value="checkValue" :options="staticOptions" />
</template>
<script lang="ts" setup>
  import { ref, watch, inject } from 'vue';
  import { getSaleCode } from '/@/api/erp/sale/order';

  const props = defineProps({
    staticOptions: Array,
    value: String,
  });

  const checkValue = ref('');
  const emit = defineEmits(['update:value']);
  const formModel = inject<any>('formModel', null); // 注入表单数据

  watch(
    () => props.value,
    (val) => {
      checkValue.value = val || '';
    },
    {
      immediate: true,
    },
  );
  watch(
    () => checkValue.value,
    async (val) => {
      let code = '';
      if (val.length) {
        code = await getSaleCode();
        setFormModel(code);
      } else {
        setFormModel('');
      }
      emit('update:value', val.toString());
    },
    {
      immediate: true,
    },
  );

  const setFormModel = (val) => {
    const fieldNames = ['applyNumber', 'apply_number', 'APPLY_NUMBER'];
    fieldNames.some((item) => {
      if (formModel[item] !== undefined) {
        formModel[item] = val;
        return true;
      }
    });
  };
</script>
