import { enumOrderComponents } from '@/common/constants/enumOrderComponents';
import { initData } from '@/common/methods';
import APP from '@/services';
import { OperationTabMenu } from '@/services/go/commonService/interface';
import { ref } from 'vue';


// 单据菜单数据
const orderList = ref<OperationTabMenu[]>([])

// 选中的具体tab下的数据
const recordItemTab = ref<OperationTabMenu[]>([])
const isBottom = ref<boolean>(false)
// 处理页面下半部分数据
export function handleOrderData() {
    const componentId = ref<enumOrderComponents>()
    initData(() => {
        const menuList = APP.getRef('menus');
        const findResult = menuList.value.find((e: OperationTabMenu) => e.code === 'bottom' && e.type === 4);
        if (findResult) {
            isBottom.value = true
            const list = findResult.children;
            orderList.value = list
            if (list.length) {
                componentId.value = list[0].code
            }
        }
    });

    return { isBottom, orderList, componentId }
}
export function getHasBottom() {
    return isBottom
}

// 根据 code 获取单据对应子菜单配置数据
export function handleOrderDetailList(code: enumOrderComponents) {
    // 选中的组件，默认第一个
    const componentId = ref<enumOrderComponents>()
    const tabList = ref<OperationTabMenu[]>([])
    // 过滤，获取当前tab页配置数据
    function filerAction() {
        const temp = orderList.value.find(e => e.code === code)
        if (temp) {
            tabList.value = temp.children
            if (temp.children.length) {
                recordItemTab.value = temp.children[0].children
                componentId.value = temp.children[0].code as enumOrderComponents
            }
        }
    }
    // 切换tab组件
    function changeTab(index: number, item: OperationTabMenu) {
        recordItemTab.value = item.children
        console.log('当前底部组件名：', item.code)
        componentId.value = item.code as unknown as enumOrderComponents
    }
    filerAction()
    return { tabList, componentId, changeTab }
}

export function getRecordItemTab(): OperationTabMenu[] {
    return recordItemTab.value
}