import { api_ladingBillMoneyDetailList, api_ladingBillMoneyList } from '@/api/lading/lading';
import { LadingBillMoneyDetailParam, LadingBillMoneyParam } from '@/api/lading/ladingModle';
import type { BasicTableProps } from '@/components/VxeTable';
import { useDropDownData } from '@/hooks/useDropDownData';
import { useEleFormValidate } from '@/hooks/useEleFormValidate';
import { useFeesetList } from '@/store/list_feeset';
import { FeeSetTypeListType } from '@/type/BaseLading';
import { resCodeSuccess } from '@/utils/useReturnData';
import { ref, reactive, unref, computed } from 'vue';
/**
 * 处理提单费用明细列表
 * @returns 
 */
function useLadingBillMoneyList(onClose: Fn, onShow: Fn, ladingDetailOptions: BasicTableProps) {
    const ladingBillDetailRef = ref()
    const ladingBillMoneyDetailListRef = ref<{ show: Fn }>()
    const ladingBillDetailState = reactive({
        loading: false,
    })
    // 查询参数
    const ladingBillDetailParams = reactive<LadingBillMoneyParam>({
        mainId: undefined,
        feeSetType: undefined,
        feeItemCategory: undefined,
        showFinal: 0
    })
    const storeFeesetList = useFeesetList()
    const { feeSetTypeList } = useDropDownData()
    const selectfeeSetTypeList = computed(() => {
        return unref(feeSetTypeList).filter((item) => {
            return item.value === FeeSetTypeListType.SETTLEMENT || //结算价
                   item.value === FeeSetTypeListType.CUSTOMER || //客户价
                   item.value === FeeSetTypeListType.PAYABLE //应付
        })
    })
    // 校验表单
    const { resetForm } = useEleFormValidate();
    // 初始化数据
    const initSingleData = async (params: LadingBillMoneyParam) => {
        ladingBillDetailState.loading = true
        try {
            const res = await api_ladingBillMoneyList(params)
            if (resCodeSuccess(res)) {
                ladingDetailOptions.data = res.data
            }
        } catch (error) {
            console.error(error);
        } finally {
            ladingBillDetailState.loading = false
        }
    }
    // 明细
    const hanleViewDetail = async (row) => {
        const params: LadingBillMoneyDetailParam = {
            mainId: row.mainId,
            billMoneyId: row.id
        }
        const res = await api_ladingBillMoneyDetailList(params)
        if (resCodeSuccess(res)) {
            unref(ladingBillMoneyDetailListRef)?.show(res.data)
        }
    }
    // 搜索
    const searchBtn = () => {
        initSingleData(ladingBillDetailParams)
    }
    // 显示
    const show = (data) => {
        ladingBillDetailParams.mainId = data.id
        initSingleData({ mainId: data.id, feeSetType: data.feeSetType, waybillNumber: data.waybillNumber })
        onShow()
    }
    // 关闭
    const onclose = () => {
        resetForm(ladingBillDetailRef.value)
        onClose()
    }

    return {
        ladingBillDetailRef,
        ladingBillDetailParams,
        ladingBillMoneyDetailListRef,
        ladingBillDetailState,
        show,
        onclose,
        searchBtn,
        hanleViewDetail,
        storeFeesetList,
        selectfeeSetTypeList
    }
}

export {
    useLadingBillMoneyList
}