import type { BaseTableProps } from "@/components/Table/src/types"
import { isArrayLength, isNonNegativeInteger } from "@/utils/is"
import { Ref, unref } from 'vue';
/**
 * 抽离费用设置公用的方法
 */
function useDealFeeItemData(baseData: Recordable, allFeeDetailsList: Ref<Recordable[]>, detailTable: BaseTableProps) {
    // 计抛系数模型
    const weightRateModelList = [
        { remoteFlag: 0, weightRatio: undefined },//否 偏远
        { remoteFlag: 1, weightRatio: undefined },//是 偏远
    ]
    // 根据费用类型过滤数据显示
    const currentFilterFeeItemData = (feeItemComputeDetailParams, feeItemId) => {
        const allFeeItemData = feeItemComputeDetailParams.filter(item => item.feeItemId === feeItemId)
        detailTable.dataSource = allFeeItemData
        // 没有默认是选择默认包装类型
        baseData.packageType = allFeeItemData.length > 0 ? allFeeItemData[0].packageType : 0
    }

    // 改变当前数据费用类型 《==》包装类型
    const hanleChangePackageType = (val: number) => {
        if (!isArrayLength(detailTable.dataSource)) return
        // 当前费用类型id
        const feeItemId = detailTable.dataSource[0].feeItemId
        // 改变当费用明细表格数据的包装类型
        detailTable.dataSource.map(item => {
            if (isNonNegativeInteger(val) && isNonNegativeInteger(feeItemId)) item.packageType = val
            return item
        })
    }
    // 根据第一条数据统一改变计量单位
    const hanleChangeCalculateUnitList = () => {
        if (!isArrayLength(detailTable.dataSource)) return
        // 第一条费用明细信息
        const feeItem = detailTable.dataSource[0]
        if (!isNonNegativeInteger(feeItem.originalCalculateUnit)) return
        // 改变当费用明细表格数据的计量单位
        detailTable.dataSource.map(item => {
            if (isNonNegativeInteger(feeItem.feeItemId)) item.originalCalculateUnit = feeItem.originalCalculateUnit
            return item
        })
    }
    // 根据第一条数据统一改变算法
    const hanleChangeCountTypeList = () => {
        if (!isArrayLength(detailTable.dataSource)) return
        // 第一条费用明细信息
        const feeItem = detailTable.dataSource[0]
        if (!isNonNegativeInteger(feeItem.countModel.countType)) return
        // 改变当费用明细表格数据的计量单位
        detailTable.dataSource.map(item => {
            if (isNonNegativeInteger(feeItem.feeItemId) && item.countModel) item.countModel.countType = feeItem.countModel.countType
            return item
        })
    }
    // 分区费用根据第一条数据设置
    const hanleInitRefresh = (field: string) => {
        if (!isArrayLength(detailTable.dataSource)) return
        // 当前费用类型id-唯一标识关联
        const feeItem = detailTable.dataSource[0]
        if (feeItem[field] == '') return
        // 刷新费用明细数据
        detailTable.dataSource.map(item => {
            if (isNonNegativeInteger(feeItem.feeItemId)) item[field] = feeItem[field]
            return item
        })
    }

    // 处理费用明细初始化的值
    const feesetSetingDetailFilterEmpty = (detailList, meterHeadList: Ref) => {
        // 过滤费用明细（起始重量、结束重量、计量单位、算法）都为空 ==》 费用明细是响应式数据
        const dataFilter = detailList.filter(flItem =>
            "originalBeginWeight" in flItem &&
            "originalEndWeight" in flItem &&
            "countModel" in flItem &&
            "originalCalculateUnit" in flItem)
        // 分区的键分离处理
        const headProps = meterHeadList.value.map(item => item.prop)
        // debugger
        // 初始化分区的费用
        // 当前的费用明细数据-初始化值
        initFeesetDetails(dataFilter, headProps)
        // 全部费用明细数据-初始化值
        initFeesetDetails(allFeeDetailsList.value, headProps)
        return dataFilter
    }

    // 分区的key的费用初始值
    const initFeesetDetails = (feesetDetailList, headProps: string[]) => {
        // 费用明细
        const dataFilterLength = feesetDetailList.length
        if (dataFilterLength === 0) return
        // 分区编号
        const headPropsLength = headProps.length
        for (let i = 0; i < dataFilterLength; i++) {
            for (let j = 0; j < headPropsLength; j++) {
                const key = headProps[j];
                if (!feesetDetailList[i].hasOwnProperty(key)) { // 判断分区的键属性是否已经存在
                    feesetDetailList[i][key] = 0; // 初始化属性值为0
                }
            }
        }
    }

    // 更新比对费用选中的值
    const getUpdatefeeItemIdIndex = (feeItemIdList: number[], feeItemId: number): number => {
        if(!isArrayLength(feeItemIdList)) return -1
        const findIndex = feeItemIdList.findIndex((currentId) => currentId === feeItemId)
        return findIndex || -1
    }

    return {
        weightRateModelList,
        feesetSetingDetailFilterEmpty,
        currentFilterFeeItemData,
        hanleChangePackageType,
        hanleChangeCalculateUnitList,
        hanleChangeCountTypeList,
        hanleInitRefresh,
        getUpdatefeeItemIdIndex,
        initFeesetDetails
    }
}

/**
 * 刷新表头
 * @param xDetailTableRef 
 * @param clearHea 
 * @returns 
 */
const refreshTableColumn = (xDetailTableRef: Ref, clearHea: Fn) => {
    if (!unref(xDetailTableRef)) return
    clearHea()
    // 刷新费用明细表头
    xDetailTableRef.value?.refreshColumn()
}

export { useDealFeeItemData, refreshTableColumn }