import { generateUUID } from '@/utils'
import { localeProps, makeRequiredRule } from '@/components/FormCreate/src/utils'
import selectRule from '@/components/FormCreate/src/config/selectRule'
import { dictTypeListAll } from '@/api/system-boot/dictType'
import { ref, onMounted } from 'vue'


export const useDictSelectRule = () => {
    const label = '字典选择器'
    const name = 'DictSelect'
    const dictOptions = ref<{ label: string; value: string }[]>([]) // 字典类型下拉数据
    onMounted(async () => {
        let data: any = []
        await dictTypeListAll().then(res => {
            data = res.data
        })
        if (data.length === 0) {
            return
        }
        dictOptions.value =
            data.map((item: any) => ({
                label: item.name,
                value: item.id
            })) ?? []
    })
    return {
        icon: 'icon-select',
        label,
        name,
        rule() {
            return {
                type: name,
                field: generateUUID(),
                title: label,
                info: '',
                $required: false
            }
        },
        props(_, { t }) {
            return localeProps(t, name + '.props', [
                makeRequiredRule(),
                {
                    type: 'select',
                    field: 'dictType',
                    title: '字典类型',
                    value: '',
                    options: dictOptions.value
                },
                {
                    type: 'select',
                    field: 'valueType',
                    title: '字典值类型',
                    value: 'str',
                    options: [
                        { label: '数字', value: 'int' },
                        { label: '字符串', value: 'str' },
                        { label: '布尔值', value: 'bool' }
                    ]
                },
                ...selectRule
            ])
        }
    }
}
