import { getBillMainDownloadSummaryApi, getBillMainPageSummaryListApi } from "@/api/billmain/billMain";
import { BaseTableProps, TableActionType } from "@/components/Table/src/types";
import { useReturnData } from "@/utils/useReturnData";
import { DialogModel } from "@/type/BastType";
import { cloneDeep } from "@/utils/cloneDeep";
import { getPresMonth } from "@/utils/dateTimeStr";
import { downloadByData } from "@/utils/download";
import { reactive } from "vue";

// TODO 处理：账单汇总
export default function useViewBillMainRate(dialog: DialogModel, onClose: Function, onShow: Function, tableData: BaseTableProps) {
    // 定义基础数据
    const state = reactive({
        exportDisabled: false,
        listParm: {
            mainId: 0,
            channelId: '',//渠道商
            month: '',//月份
            enterpriseDominantId: '',//公司主体
            // pageNum: 1,
            // pageSize: 10,
            // total: 0,
        }
    })

    // 前端分页数据
    const listParm = reactive({
        pageNum: 1,
        pageSize: 10,
        total: 0,
        totalList: []
    })
    // 处理返回数据
    const { resData } = useReturnData()

    // 分页数据
    const billMainPageList = async () => {
        tableData.loading = true
        try {
            const res = await getBillMainPageSummaryListApi(state.listParm)
            const data = resData(res)
            // 前端只渲染10条数据
            pageSliceList(data)
            listParm.totalList = data
            listParm.total = data.length
        } catch (error) {
            console.error(error);
        } finally {
            tableData.loading = false
        }
    }

    //页容量改变时触发
    const sizeChange = (size: number) => {
        // state.listParm.pageSize = size;
        // billMainPageList()
        // 前端分页
        listParm.pageSize = size
        pageSliceList(listParm.totalList)
    }
    //页数改变时触发
    const currentChange = (page: number) => {
        // state.listParm.pageNum = page
        // billMainPageList()
        // 前端分页
        listParm.pageNum = page
        pageSliceList(listParm.totalList)
    }
    // 前端分页数据
    const pageSliceList = (list: Recordable) => {
        tableData.dataSource = []
        const data = cloneDeep(list)
        tableData.dataSource = data.slice(
            (listParm.pageNum - 1) *
            listParm.pageSize,
            listParm.pageNum * listParm.pageSize
        )
    }

    // 导出
    const hanleExport = async () => {
        state.exportDisabled = true
        try {
            const res = await getBillMainDownloadSummaryApi(state.listParm)
            if (res) {
                // 获取接口表格响应的头
                const header = res.headers['content-disposition'].split("''")
                // 转中文
                const fileName = decodeURIComponent(header[1])
                // 通过blod导出模板
                downloadByData(res.data, fileName)
                state.exportDisabled = false
            }
        } catch (error) {
            console.log(error, ":error");
            state.exportDisabled = false
        }
    }

    // 显示操作
    const show = (row: Recordable) => {
        dialog.title = `查看账单汇总`
        if (row) {
            state.listParm.mainId = row.id
            state.listParm.month = getPresMonth()//上一月份
            // 初始化数据
            billMainPageList()
        }
        onShow()
    }

    // 取消弹窗
    const hanleOnClose = () => {
        onClose()
        // 初始化内容
    }

    // 获取的数据
    const confirm = () => {
        // 取消弹窗
        hanleOnClose()
    }

    return {
        show,
        confirm,
        hanleOnClose,
        sizeChange,
        currentChange,
        billMainPageList,
        hanleExport,
        listParm,
        state
    }
}