import { FeeItemResultDto, LadingBillMoneyDetailDto } from '@/api/lading/ladingModle';
import type { BasicTableProps } from '@/components/VxeTable';
import { useMessage } from '@/hooks/useMessage';
import { cloneDeep } from '@/utils/cloneDeep';
import { isArrayLength } from '@/utils/is';
import { ref, reactive, unref, watch, nextTick } from 'vue';

const { createMessage } = useMessage()
/**
 * 提单费用试算结果
 * @returns 
 */
function useBillMoneyCountResult(onClose: Fn, onShow: Fn, emit, props) {
    // 明细
    const billMoneyDetailRef = ref<{ show: Fn }>()
    // 提单费用试算结果表格-配置
    const billMoneyCountResultOptions = reactive<BasicTableProps>({
        id: 'billMoneyCount_result_table',
        border: true,
        keepSource: true,
        // showOverflow: true,
        // height: 220,
        maxHeight: 800,
        loading: false,
        stripe: true,
        rowConfig: { isHover: true, keyField: 'id' },
        cellStyle: { height: '38px', padding: 0 },
        columnConfig: { resizable: true },
        columns: [
            { type: "seq", title: '序号', width: 65 },
            { field: 'feeSetTypeName', title: '结算类型', width: 90 },
            { field: 'providerName', title: '供应商', minWidth: 130 },
            { field: 'productName', title: '产品名称', minWidth: 120 },
            { field: 'feeItemCategoryName', title: '费项类型', width: 120 },
            { field: 'fee', title: '金额', width: 120 },
            { field: 'formulaDescription', title: '公式描述', minWidth: 200 },
            { field: 'message', title: '提示信息', minWidth: 100 },
            { title: '费用明细', slots: { default: 'default_actions' }, align: "center", width: 100, fixed: 'right' }
        ],
        data: []
    })
    const billMoneyDetailState = reactive({
        mainId: 0,//主单编号
        bookingNumber: "",//主-订舱号
        waybillNumber: "",//主-提单号
        feeWeight: 0,//主-receivableWeight
    })

    // 初始化数据
    watch(
        () => props.ladingBillFeeQueryResul,
        (val) => {
            nextTick(() => {
                if (isArrayLength(val)) {
                    billMoneyCountResultOptions.data = val
                }
            })
        }
    )

    // 显示/隐藏
    const show = (data) => {
        billMoneyDetailState.mainId = data.id
        billMoneyDetailState.bookingNumber = data.bookingNumber
        billMoneyDetailState.waybillNumber = data.waybillNumber
        billMoneyDetailState.feeWeight = data.receivableWeight
        onShow()
    }
    const onclose = () => {
        billMoneyCountResultOptions.data = []
        onClose()
    }
    // 打开明细
    const hanleViewDetail = (row) => {
        unref(billMoneyDetailRef)?.show(row)
    }
    // 获取提单费用明细参数
    const getLadingBillMoneyDetailDtoList = (feeItemDtoList: FeeItemResultDto[]) => {
        const feeItemList: Partial<LadingBillMoneyDetailDto>[] = []
        for (const item of feeItemDtoList) {
            feeItemList.push({
                feeItemId: item.feeItemId,
                feeItemName: item.feeItemName,
                fee: item.fee,
                currencyCode: item.oriCurrencyCode,
                originalFee: item.oriCurrencyFee,
                rate: item.exchangeRate
            })
        }
        return feeItemList
    }
    // 获取保存费用参数
    const getLadingBillMoneyDto = (data) => {
        const params = data.map((item) => {
            if (item.id) delete item.id
            if (isArrayLength(item.feeItemDtoList)) {
                item.ladingBillMoneyDetailDtoList = getLadingBillMoneyDetailDtoList(item.feeItemDtoList)
                delete item.feeItemDtoList
            }
            // 合并主数据部分参数
            return { ...item, ...billMoneyDetailState }
        })
        return params
    }
    // 保存费用
    const confirm = () => {
        const data = cloneDeep(billMoneyCountResultOptions.data)
        const isFee = data.some(item => !Number(item.fee))
        if (isFee) {
            createMessage.error("费用计算异常，请重新设置！")
            return
        }
        const params = getLadingBillMoneyDto(data)
        emit("saveBaseFee", params, onclose)
    }

    return {
        show,
        onclose,
        confirm,
        billMoneyCountResultOptions,
        hanleViewDetail,
        billMoneyDetailRef
    }
}

export {
    useBillMoneyCountResult,
}