<template>
    <a-select ref="selectRef" v-model:value="localValue" placeholder="请选择" @change="onChange" @select="onSelect"
        v-bind="$attrs">
        <a-select-option v-for="item in localOptions" :key="item[localfieldNames.value]"
            :value="item[localfieldNames.value]" v-bind="item">
            {{ item[localfieldNames.label] }}
        </a-select-option>
    </a-select>
</template>
<script setup lang="jsx">
import { isEmpty } from '@/utils/validate'

import { getOptions } from '@/api/common'
import { ref, defineProps, defineExpose, defineEmits, onMounted, onBeforeUnmount, watch, nextTick, computed } from 'vue';
const emits = defineEmits(['update:value', 'onChange', 'onSelect']);
const props = defineProps({
    value: {
        type: [String, Array, Number],
        default: '',
        required: true
    },
    url: {
        type: String,
        default: ''
    },
    tableName: {
        type: String,
        default: ''
    },
    colName: {
        type: String,
        default: ''
    },
    options: {
        type: Array,
        default: () => []
    },
    formItemProps: {
        type: Object,
        default: () => { }
    },
    selectType: {
        type: String,
        default: ''
    },
    pageInterfaceNo: {
        type: String,
        default: ''
    },
    sobNo: {
        type: String,
        default: ''
    },
    correct: {
        type: Boolean,
        default: false
    },
    cascadeFlag: {
        type: Boolean,
        default: false
    },
    fieldNames: {
        type: Object,
        default: () => {
            return {
                value: 'item_no',
                label: 'item_name'
            }
        }
    }
});
const selectRef = ref(null);
const localOptions = ref([]);
const localfieldNames = ref(props.fieldNames);
const localValue = computed({
    get: () => {
        console.log('wyq:pro-select-获取localValue值:', props.value);
        return props.value;
    },
    set: (value) => {
        console.log('wyq:pro-select-更改localValue值:', value);
        emits('update:value', value);
    }
});

watch(() => props.options, (newOptions) => {
    localOptions.value = newOptions;
});

if (props.selectType === 'accLvl') {
    watch(() => props.sobNo, async () => {
        // 监听
        console.log('wyq:pro-select-监听sobNo值:', props.sobNo);
        await getAccLvlOptionsData();
    });
}
const onSelect = (value, option) => {
    emits('onSelect', value, option);
}

const onChange = (value, option) => {
    console.log('wyq:pro-select-更改localValue值:', value, option);
    emits('onChange', value, option);
}

onMounted(async () => {
    await getLocalOptionsData();
});

async function getLocalOptionsData () {
    if (!isEmpty(props.options)) {
        localOptions.value = props.options;
    } else if (!isEmpty(props.selectType)) {
        if (props.selectType === 'relationNo') {
            await getDepartRelationOptionsData();
        } else if (props.selectType === 'accLvl') {
            await getAccLvlOptionsData();
        } else if (props.selectType === 'curr') {
            await getCurOptionsData();
        } else if (props.selectType === 'perSys') {
            await getPerSysOptionsData();
        } else if (props.selectType === 'periodic') {
            await getPeriodicOptionsData();
        }
    } else {
        let queyUrl = '';
        let params;
        if (!isEmpty(props.tableName) && !isEmpty(props.colName)) {
            queyUrl = '/system/select/options/loadTbl'
            params = {
                tableName: props.tableName,
                colName: props.colName
            }
        } else {
            queyUrl = props.url;
            params = {};
        }
        if (queyUrl) {
            await getOptions(queyUrl, params).then((response) => {
                if (response != null) {
                    localOptions.value = response
                }
            })
            if (!isEmpty(localOptions.value) && localOptions.value.length > 0) {
                const defaultOption = findOptionByField('is_default', 'Y');
                if (!isEmpty(defaultOption)) {
                    localValue.value = defaultOption[localfieldNames.value.value];
                    emits('onSelect', localValue.value, defaultOption);
                }
            }
        }
    }
}

async function getAccLvlOptionsData () {
    if (!isEmpty(props.sobNo)) {
        let queyUrl = '/system/common/action/serachAccLvl';
        let params = {
            sob_no: props.sobNo,
            addEndFlag: props.addEndFlag
        }
        await getOptions(queyUrl, params).then((response) => {
            if (response != null) {
                localOptions.value = response
            }
        })

        if (!isEmpty(localOptions.value) && localOptions.value.length > 0) {
            const defaultOption = localOptions.value[localOptions.value.length - 1];
            if (!isEmpty(defaultOption)) {
                localValue.value = defaultOption[localfieldNames.value.value];
                emits('onSelect', localValue.value, defaultOption);
            }
        }
    }
}

async function getDepartRelationOptionsData () {
    let queyUrl = '/system/select/options/relationInfoNo.do';
    let params = {
        pageInterfaceNo: props.pageInterfaceNo
    };
    await getOptions(queyUrl, params).then((response) => {
        if (response != null) {
            localOptions.value = response
        }
    })
    if (!isEmpty(localOptions.value) && localOptions.value.length > 0) {
        const option = findOption("AAAAAA");
        console.log('wyq:pro-select-获取departRelationOptionsData值:', localOptions.value, option);
        if (!isEmpty(option)) {
            localValue.value = option[localfieldNames.value.value];
            emits('onSelect', localValue.value, option);
        }
    }
}

async function getCurOptionsData () {
    let queyUrl = '/system/select/options/currencyLoad.do';
    let params = {
        correct: props.correct
    };
    await getOptions(queyUrl, params).then((response) => {
        if (response != null) {
            localOptions.value = response
        }
    })

    if (!isEmpty(localOptions.value) && localOptions.value.length > 0) {
        let defaultValue = '';
        queyUrl = '/im/dhc/zz/automake/zzmanage/report/GetForeignCurConttroller.do';
        params = {};
        await getOptions(queyUrl, params).then((response) => {
            if (response != null) {
                defaultValue = response.foreign_cur_no;
            }
        })
        const option = findOption(defaultValue);
        if (!isEmpty(option)) {
            localValue.value = option[localfieldNames.value.value];
            emits('onSelect', localValue.value, option);
        }
    }
}

async function getPerSysOptionsData () {
    let queyUrl = '/system/select/options/getSystemInfo.do';
    let params = {
        cascadeFlag: props.cascadeFlag
    }

    await getOptions(queyUrl, params).then((response) => {
        if (response != null) {
            localOptions.value = response
            localfieldNames.value = {
                value: 'ITEM_NO',
                label: 'ITEM_NAME'
            }
        }
    })
    console.log('wyq:pro-select-获取getPerSysOptionsData值:', localfieldNames.value);

    console.log('wyq:pro-select-获取getPerSysOptionsData值:', localOptions.value);

}

async function getPeriodicOptionsData () {
    let queyUrl = '/system/select/options/periodicComponent.do';
    let params = {
        cycTypeField: 'zcfzb'
    }
    await getOptions(queyUrl, params).then((response) => {
        if (response != null) {
            localOptions.value = response
        }
    })
    if (!isEmpty(localOptions.value) && localOptions.value.length > 0) {
        const defaultOption = findOptionByField('is_default', 'Y');
        if (!isEmpty(defaultOption)) {
            const defaultValue = defaultOption[localfieldNames.value.value];
            localValue.value = defaultValue;
            console.log('wyq:pro-select-获取CycType值:', defaultValue, defaultOption);
            emits('onSelect', defaultValue, defaultOption);
        }
    }
}

const findOption = (value) => {
    return findOptionByField(localfieldNames.value.value, value);
};

const findOptionByField = (field, value) => {
    const option = localOptions.value.find(option => option[field] === value);
    return { ...option }
};
</script>
