import { api_getFinancialBillDetailShareShareList } from '@/api/financial/financialBillList/financialBillList';
import { FinancialBillDetailShareParam } from '@/api/financial/financialBillList/financialBillListModel';
import type { BasicTableProps } from '@/components/VxeTable';
import { useDropDownData } from '@/hooks/useDropDownData';
import { useEleFormValidate } from '@/hooks/useEleFormValidate';
import { useQueryList } from '@/hooks/useQueryList';
import { useFeesetList } from '@/store/list_feeset';
import { useStateFinancialType } from '@/store/list_financial';
import timeFiltering from '@/utils/timeFiltering';
import { resCodeSuccess } from '@/utils/useReturnData';
import { ref, reactive, onMounted, unref, nextTick } from 'vue';

/**
 * 分摊结果
 */
function useFinancialShareList(financialShareOptions: BasicTableProps) {
    const { storeDropDownData, allProvidersList } = useDropDownData()
    const storeFinancialType = useStateFinancialType()
    const storeFeesetList = useFeesetList()

    const financialShareListFormRef = ref()
    const financialShareListParams = reactive<FinancialBillDetailShareParam>({
        billCode: '',
        feeItemCategory: undefined,
        providerId: undefined,
        billCodeType: undefined,
        code: '',
        pageNum: 1,
        pageSize: 10,
        total: 10
    })
    const financialShareListState = reactive({
        loading: false
    })
    onMounted(async () => {
        storeDropDownData.addAllProvidersList()
        storeFinancialType.addBillCodeTypeList()
        storeFeesetList.addFeeItemCategoryList()
    })

    // 初始化
    const initList = async () => {
        financialShareListState.loading = true
        try {
            const res = await api_getFinancialBillDetailShareShareList(listParams)
            if (resCodeSuccess(res)) {
                financialShareOptions.data = res.data.records
                financialShareListParams.total = res.data.total
                financialShareListState.loading = false
            }
        } catch (error) {
            financialShareListState.loading = false
        }
    }

    // 校验表单
    const { resetForm } = useEleFormValidate();
    const resetParams = () => {
        resetForm(unref(financialShareListFormRef))
    }
    const { sizeChange, currentChange, searchBtn, resetBtn, listParams } = useQueryList<FinancialBillDetailShareParam>(initList, resetParams, financialShareListParams)
    // 下载
    const download = () => {

    }

    return {
        financialShareListParams,
        financialShareListFormRef,
        sizeChange, 
        currentChange, 
        searchBtn, 
        resetBtn,
        download,
        allProvidersList,
        storeFinancialType,
        storeFeesetList,
        financialShareListState
    }
}

/**
 * 分摊结果表格配置
 */
function useFinancialShareListModel() {
    const financialShareListOptions = reactive<BasicTableProps>({
        id: 'financial_shareList_table',
        border: true,
        keepSource: true,
        showOverflow: true,
        height: 520,
        loading: false,
        stripe: true,
        rowConfig: { isHover: true, keyField: 'id' },
        columnConfig: { resizable: true },
        columns: [
            { type: "seq", title: '序号', width: 60 },
            { field: 'billCode', title: '账单编号', minWidth: 110 },
            { field: 'billName', title: '账单名称 ', minWidth: 110 },
            { field: 'providerName', title: '渠道商', minWidth: 110 },
            {
                field: 'belongTime',
                title: '归账时间',
                minWidth: 110,
                formatter: ({ row }) => {
                    return timeFiltering(row.createTime)
                }
            },
            { field: 'waybillNumber', title: '提单号', minWidth: 110 },
            { field: 'orderId', title: '订单号', minWidth: 110 },
            { field: 'trackingId', title: '尾程单号', minWidth: 110 },
            { field: 'weight', title: '重量', minWidth: 90 },
            { field: 'volume', title: '体积', minWidth: 90 },
            { field: 'feeItemCategoryName', title: '费项类型', minWidth: 100 },
            { field: 'fee', title: '分摊费用', minWidth: 100 },
            { field: 'shareTypeName', title: '分摊方式', minWidth: 100, fixed: 'right' },
        ],
        data: []
    }) 

    return {
        financialShareListOptions
    }
}

export {
    useFinancialShareList,
    useFinancialShareListModel
}