import { ref, reactive, computed, unref } from 'vue';
import { BaseFeesetAction } from '@/type/BaseFeesetAction';
import { api_getLadingBillFeeSetByPId } from '@/api/lading/lading';
import { BillPayableDto, BillReceivableDto, LadingBillFeeSetDto } from '@/api/lading/ladingModle';
import { resCodeSuccess } from '@/utils/useReturnData';
import { deal_SetObjectData } from '@/utils/dealData';
import { LadingBillMoneySetTabs } from '@/type/BaseLading';

/**
 * 
 * @returns 
 */
function useLadingBillMoneySet(
    onClose: Fn,
    onShow: Fn,
    emit
) {
    // 应收费用设置
    const receivableContentRef = ref<{ reset: Fn, getConfirmData: Fn }>()
    // 应付费用设置
    const payableContentRef = ref<{ reset: Fn, getConfirmData: Fn }>()
    // 基础数据
    const ladingBillMoneySetState = reactive({
        activeBillType: LadingBillMoneySetTabs.RECEIVABLE,//应收/应付
        type: -1,
        ladingBillFeeSetDetail: {}
    })
    // 提单费用设置参数
    const ladingBillFeeSetForm = reactive<LadingBillFeeSetDto>({
        id: "",
        ladingBillMainId: 0,
        billReceivable: {} as BillReceivableDto,
        payableModel: {} as BillPayableDto
    })
    const isShowView = computed(() => {
        return ladingBillMoneySetState.type !== BaseFeesetAction.VIEW
    })

    // 初始化数据
    const initData = async (id: number) => {
        // 提单主单编号
        const res = await api_getLadingBillFeeSetByPId({ id })
        if (resCodeSuccess(res)) {
            const data = res.data
            if (!data) return
            const newData = deal_SetObjectData(ladingBillFeeSetForm, data)
            // 参数数据源
            Object.assign(ladingBillFeeSetForm, newData)
        }
    }
    // 显示
    const show = (data: Recordable) => {
        // 提单主单编号
        ladingBillFeeSetForm.ladingBillMainId = data.id
        if (data.id) {
            ladingBillMoneySetState.ladingBillFeeSetDetail = data
            initData(data.id)
        }
        onShow()
    }
    // 隐藏
    const onclose = () => {
        ladingBillMoneySetState.activeBillType = LadingBillMoneySetTabs.RECEIVABLE
        unref(receivableContentRef)?.reset()
        unref(payableContentRef)?.reset()
        onClose()
    }
    // 校验-切换
    const changeValidate = (results: Recordable[]) => {
        const [receivable, payable] = results
        switch (ladingBillMoneySetState.activeBillType) {
            case LadingBillMoneySetTabs.RECEIVABLE:
                if (!receivable) {
                    ladingBillMoneySetState.activeBillType = LadingBillMoneySetTabs.RECEIVABLE
                    return false
                }
                if (!payable) ladingBillMoneySetState.activeBillType = LadingBillMoneySetTabs.PAYABLE
                break;
            case LadingBillMoneySetTabs.PAYABLE:
                if (!payable) {
                    ladingBillMoneySetState.activeBillType = LadingBillMoneySetTabs.PAYABLE
                    return false
                }
                if (!receivable) ladingBillMoneySetState.activeBillType = LadingBillMoneySetTabs.RECEIVABLE
                break;
        }
    }
    // 试算
    const confirmFeeCalculate = () => {
        emit("saveFeeCalculate", ladingBillMoneySetState.ladingBillFeeSetDetail)
    }
    // 确定
    const confirm = async () => {
        const receivableContent = unref(receivableContentRef)
        const payableContent = unref(payableContentRef)
        Promise.all([receivableContent?.getConfirmData(), payableContent?.getConfirmData()])
            .then((results) => {
                changeValidate(results)
                debugger
                const isResults = results.every(item => item)
                if (!isResults) return
                const [billReceivable, payableModel] = results
                // 应收数据
                ladingBillFeeSetForm.billReceivable = billReceivable
                // 应付数据
                ladingBillFeeSetForm.payableModel = payableModel
                emit("saveBillSet", ladingBillFeeSetForm)
            })
    }

    return {
        show,
        onclose,
        confirm,
        confirmFeeCalculate,
        ladingBillFeeSetForm,
        ladingBillMoneySetState,
        receivableContentRef,
        payableContentRef,
        isShowView
    }
}

export {
    useLadingBillMoneySet
}