import { ActionOptions } from '@/components/DropDownButtons/type';
import type { BasicTableProps, VxeTablePropTypes } from '@/components/VxeTable';
import { BaseFinancialFinancialVerifyList } from '@/type/BaseFinancial';
import timeFiltering from '@/utils/timeFiltering';
import { View, Refresh } from "@element-plus/icons-vue";
import { reactive } from 'vue';

/**
 * 核账结果表格配置
 */
function useFinancialVerifyListModel() {
    // 核账结果表格
    const financialVerifyListOptions = reactive<BasicTableProps>({
        id: 'financial_verify_list_table',
        border: true,
        keepSource: true,
        showOverflow: true,
        height: 520,
        loading: false,
        stripe: true,
        rowConfig: { isHover: true, keyField: 'id' },
        columnConfig: { resizable: true },
        toolbarConfig: { custom: true },
        columns: [
            { type: "seq", title: '序号', width: 65 },
            { field: 'billCode', title: '账单编号', minWidth: 140 },
            { field: 'billName', title: '账单名称', minWidth: 110 },
            { field: 'providerName', title: '渠道商', minWidth: 100 },
            {
                field: 'belongTime',
                title: '归账时间',
                minWidth: 100,
                formatter: ({ row }) => {
                    return timeFiltering(row.belongTime)
                }
            },
            { field: 'waybillNumber', title: '提单号', minWidth: 120 },
            { field: 'orderId', title: '订单号', minWidth: 120 },
            { field: 'trackingId', title: '尾程单号', minWidth: 120 },
            { field: 'feeItemCategoryName', title: '费项类型', minWidth: 90 },
            { field: 'weight', title: '重量', minWidth: 90 },
            { field: 'currencyCode', title: '币种', minWidth: 90 },
            { field: 'ladingFee', title: '产品费用', minWidth: 110 },
            { field: 'billFee', title: '账单费用', minWidth: 110 },
            { field: 'differenceFee', title: '差异(产品-账单)', minWidth: 140 },
            { field: 'billStateName', title: '状态', minWidth: 90 },
            { field: 'remark', title: '备注', minWidth: 120 },
            { title: '操作', slots: { default: 'actions' }, width: 135, fixed: 'right' }
        ],
        data: []
    })

    // TODO 权限字段暂未处理
    // 操作类别 
    const financialVerifyListActionOptions: ActionOptions[] = [
        {
            value: BaseFinancialFinancialVerifyList.BILL_DETAILS,
            label: "费用明细",
            icon: View,
            color: "#00C297",
            permissions: ['sys:financialBillSummary:delete']
        },
        {
            value: BaseFinancialFinancialVerifyList.BILL_RERUN,
            label: "账单重算",
            type: "success",
            icon: Refresh,
            permissions: ['sys:financialBillSummary:edit']
        }
    ]

    return {
        financialVerifyListOptions,
        financialVerifyListActionOptions
    }
}

/**
 * 核账费用明细表格配置
 */
function financialVerifyCategoryListModel() {
    // 汇总
    const sumNum = (list: any[], field: string) => {
        let count = 0
        list.forEach(item => {
            count += Number(item[field])
        })
        return count
    }
    // 表格尾部处理
    const footerMethod: VxeTablePropTypes.FooterMethod = ({ columns, data }) => {
        console.log(data,":data");
        const footerData = [
            columns.map((column, _columnIndex) => {
                if (_columnIndex === 1) {
                    return '汇总'
                }
                if (_columnIndex === 2) {
                    return sumNum(data, 'fee')
                }
                return '-'
            })
        ]
        return footerData
    }
    // 费用明细表格
    const financialVerifyCategoryListOptions = reactive<BasicTableProps>({
        id: 'financial_verifyCategory_list_table',
        border: true,
        keepSource: true,
        showOverflow: true,
        // height: 360,
        loading: false,
        stripe: true,
        rowConfig: { isHover: true, keyField: 'id' },
        columnConfig: { resizable: true },
        showFooter: true,
        columns: [
            { field: 'itemCategoryName', title: '费项类型', minWidth: 110 },
            { field: 'feeItemName', title: '费用类型', minWidth: 110 },
            { field: 'currencyCode', title: '币种', minWidth: 100 },
            { field: 'originalFee', title: '原币费用', minWidth: 100 },
        ],
        data: []
    })

    return {
        financialVerifyCategoryListOptions
    }
}

export { useFinancialVerifyListModel, financialVerifyCategoryListModel }