import { reactive, onMounted } from 'vue';
import { BaseTableProps } from '@/components/Table/src/types';
import { formattedData } from '@/utils/formattedData';
import { useReturnData } from '@/utils/useReturnData';
import { getAllCustomerIdsList, getDownloadReceivableBillOrderSealSumApi, getReceivableBillApi } from '@/api/receivablebill/receivablebill';
import { ReceivableBillParam } from '@/api/receivablebill/receivableBillModle';
import { ListIitem } from '@/api/base/BaseDataModel';
import { downloadByData } from '@/utils/download';
import { getYearMonthList } from '@/utils/getYearMonthList';
import { getMonth } from '@/utils/dateTimeStr';
import { useQueryList } from '@/hooks/useQueryList';

// 应收账单-初始值
type State = {
    exportDisabled: boolean
    customerList: ListIitem[]
    monthList: ListIitem[]
}
// TODO 处理：应收账单基础业务
/**
 * 处理应收账单基础业务数据
 * @param baseTable 表格
 */
export default function useReceivableBill(baseTable: BaseTableProps) {
    // 定义列表查询的参数
    const listParm = reactive<ReceivableBillParam>({
        customerId: undefined,
        month: '',
        pageNum: 1,
        pageSize: 10,
        total: 10,
    })
    // 客户列表
    const state: State = reactive({
        exportDisabled: false,
        customerList: [],
        monthList: []
    })

    // 数据处理
    const { resData } = useReturnData()

    // 初始化列表
    const initList = async () => {
        // 时间转换
        const currentDate = getMonth()
        if (!listParm.month) listParm.month = currentDate
        // 年-月列表
        state.monthList = getYearMonthList()
        const res = await getAllCustomerIdsList()
        const data = resData(res)
        state.customerList = data
    }
    // 初始化列表数据
    const receivableBillList = async () => {
        const res = await getReceivableBillApi(listParams)
        const { data, total } = formattedData(res)
        baseTable.dataSource = data
        listParm.total = total
    }
    // 重置
    const resetParams = () => {
        listParm.customerId = undefined;
        listParm.month = '';
    }
    // 初始化列表、分页数据
    const { sizeChange, currentChange, searchBtn, resetBtn, listParams } = useQueryList<ReceivableBillParam>(receivableBillList, resetParams, listParm, false)

    // 导出
    const exportBtn = async () => {
        state.exportDisabled = true
        // 导出参数
        const params = {
            month: listParm.month,//年月份
            customerId: listParm.customerId,//客户id
        }
        try {
            const res = await getDownloadReceivableBillOrderSealSumApi(params)
            if (res) {
                // 获取接口表格响应的头
                const header = res.headers['content-disposition'].split("''")
                // 转中文
                const fileName = decodeURIComponent(header[1])
                // 通过blod导出模板
                downloadByData(res.data, fileName)
                state.exportDisabled = false
            }
        } catch (error: any) {
            state.exportDisabled = false
            console.error(error);
        }
    }

    onMounted(() => {
        // 初始化数据
        initList()
        // receivableBillList()
    })

    return {
        searchBtn,
        resetBtn,
        exportBtn,
        sizeChange,
        currentChange,
        listParm,
        state
    }
}