import { computed, ref } from 'vue'

export default function useSelection(rowKey = 'id') {
    const isSelected = ref(false)
    const selectedList = ref([])

    // 当前选中的所有id
    const selectedListIds = computed(() => {
        const ids: number | string[] = []
        selectedList.value.forEach(item => ids.push(item[rowKey]))
        return ids
    })
    // 多选操作
    const selectionChange = (rowArr = []) => {
        isSelected.value = rowArr.length ? true : false
        selectedList.value = rowArr
    }
    return {
        isSelected,
        selectedList,
        selectedListIds,
        selectionChange
    }
}
