import { ElMessage, ElMessageBox } from 'element-plus';

const useDeleteMoreHook = (id: string, fetchCallback: (ids) => Promise<any>) => {
    const ids = ref<string[]>([]);
    const subtotal = ref(0 as any);
    const handleSelectionChange = (val: any[]) => {
        ids.value = val.map((item) => item[id] as any);
        let subArray = val.map((item) => item.money_total);
        subtotal.value = subArray.reduce((accumulator, currentValue) => accumulator + currentValue, 0);
        // subArray.forEach((res: any) => {
        //     subtotal.value += res.money_total;
        // });
        subtotal.value = Number(subtotal.value.toFixed(2));
    };
    const handleDeleteMore = async () => {
        try {
            await ElMessageBox.confirm('是否删除选中的数据？', '删除提示',
                { confirmButtonText: '确定', cancelButtonText: '取消', type: 'warning',});
            await fetchCallback(ids.value);
        } catch (err) {
            ElMessage({ type: 'info', message: '取消删除' });
        }
    };

    return { handleDeleteMore, ids, handleSelectionChange, subtotal };
};

export default useDeleteMoreHook;
