import { api_financialRepeatVerify, api_financialVerifyRecordDetail, api_financialVerifyRecordList } from '@/api/financial/financialVerifyList/financialVerifyList';
import { FinancialVerifyRecordDetailDto, FinancialVerifyRecordParam } from '@/api/financial/financialVerifyList/financialVerifyListModel';
import type { BasicTableProps } from '@/components/VxeTable';
import { useDropDownData } from '@/hooks/useDropDownData';
import { useEleFormValidate } from '@/hooks/useEleFormValidate';
import { useMessage } from '@/hooks/useMessage';
import { useQueryList } from '@/hooks/useQueryList';
import { useStateFinancialType } from '@/store/list_financial';
import { BaseFinancialFinancialVerifyList } from '@/type/BaseFinancial';
import { resCodeSuccess, useReturnData } from '@/utils/useReturnData';
import { cloneDeep } from 'lodash';
import { ref, reactive, unref, onMounted } from 'vue';

const { resData } = useReturnData()
const { createConfirm } = useMessage()
/**
 * 核账结果
 */
function useFinancialVerifyList(financialVerifyListOptions: BasicTableProps) {
    const storeFinancialType = useStateFinancialType()
    const { storeDropDownData, allProvidersList } = useDropDownData()
    onMounted(() => {
        storeFinancialType.addBillCodeTypeList()
        storeFinancialType.addDifferenceTypeList()
        storeDropDownData.addAllProvidersList()
    })
    const financialVerifyListState = reactive({
        loading: false
    })
    // 查询参数
    const financialVerifyListParams = reactive<FinancialVerifyRecordParam>({
        idList: [],
        billCode: undefined,
        providerId: undefined,
        billCodeType: undefined,
        code: undefined,
        isDifference: undefined,
        pageNum: 1,
        pageSize: 10,
        total: 0
    })
    const financialVerifyListFormRef = ref()
    // 费用明细 ref
    const financialVerifyCategoryListRef = ref<{ show: Fn }>()

    // 初始化数据
    const initList = async () => {
        try {
            financialVerifyListState.loading = true
            const res = await api_financialVerifyRecordList(listParams)
            if (resCodeSuccess(res)) {
                financialVerifyListOptions.data = res.data.records
                financialVerifyListParams.total = res.data.total
            }
        } catch (error) {
        } finally {
            financialVerifyListState.loading = false
        }
    }
    // 费用明细
    const getFinancialVerifyRecordDetail = async (data: FinancialVerifyRecordDetailDto) => {
        const res = await api_financialVerifyRecordDetail({
            verifyRecordId: data.id, 
            summaryId: data.summaryId
        })
        if (resCodeSuccess(res)) {
            unref(financialVerifyCategoryListRef)?.show(res.data)
        }
    }
    // 账单重算
    const getRepeatVerify = async (summaryId: number) => {
        const res = await api_financialRepeatVerify({ summaryId })
        const resCode = resData(res)
        if (resCode !== 500) initList()
    }
    // 校验表单
    const { resetForm } = useEleFormValidate();
    const resetParams = () => {
        resetForm(unref(financialVerifyListFormRef))
    }
    const { sizeChange, currentChange, searchBtn, resetBtn, listParams } = useQueryList<FinancialVerifyRecordParam>(initList, resetParams, financialVerifyListParams)
    // 操作
    const handleClickChange = (row) => {
        const data = cloneDeep(row)
        switch (data.types) {
            case BaseFinancialFinancialVerifyList.BILL_DETAILS:
                getFinancialVerifyRecordDetail(data)
                break;
            case BaseFinancialFinancialVerifyList.BILL_RERUN:
                createConfirm({
                    type: "warning",
                    title: "系统提示",
                    message: "是否进行账单重算?",
                    cancelButtonText: "取消",
                    confirmButtonText: "确定",
                    beforeClose: (action, instance, done) => {
                        if (action === "confirm") {
                            // 删除
                            getRepeatVerify(data.summaryId)
                        }
                        done()
                    },
                })
                break;
            default:
                break;
        }
    }

    return {
        financialVerifyListState,
        financialVerifyListParams,
        financialVerifyListFormRef,
        financialVerifyCategoryListRef,
        sizeChange,
        currentChange,
        searchBtn,
        resetBtn,
        handleClickChange,
        storeFinancialType,
        allProvidersList
    }
}

export { useFinancialVerifyList }