import { ListIitem } from "@/api/base/BaseDataModel";
import { getBillMainDetailExportApi, getBillMainDetailPageListApi } from "@/api/billmain/billMain";
import { BaseTableProps } from "@/components/Table/src/types";
import { EditType } from "@/type/BaseEnum";
import { DialogModel } from "@/type/BastType";
import { formatDate } from "@/utils/dateFormate";
import { downloadByData } from "@/utils/download";
import { formattedData } from "@/utils/formattedData";
import { ref, reactive, unref } from "vue";

// TODO 处理：查看明细
export default function useViewBillDetailRate(dialog: DialogModel, onClose: Function, onShow: Function, tableData: BaseTableProps, store) {
    // 定义基础数据
    const state = reactive({
        type: EditType.ADD,
        exportDisabled: false,
        listParm: {
            mainId: 0,
            channelName: '',
            waybillNoList: [],
            ids: [],
            billDate: '',
            pageNum: 1,
            pageSize: 10,
            total: 0,
        }
    })
    // 币种列表
    const currencyList = ref<ListIitem[]>()
    const addBillMainOrderRef = ref<{ show: (props: string[]) => void }>()
    const waybillNoListRef = ref()

    // 触发订单号输入框
    const hanleFocus = () => {
        addBillMainOrderRef.value?.show(state.listParm.waybillNoList)
        waybillNoListRef.value.blur()
    }

    // 订单号/运单号
    const hanleSaveOrder = (data: string[]) => {
        state.listParm.waybillNoList = data as never
    }

    // 分页数据
    const detailPageList = async () => {
        if (state.listParm.billDate) state.listParm.billDate = formatDate(state.listParm.billDate)
        const res = await getBillMainDetailPageListApi(state.listParm)
        const { data, total } = formattedData(res)
        tableData.dataSource = data
        state.listParm.total = total
    }

    //页容量改变时触发
    const sizeChange = (size: number) => {
        state.listParm.pageSize = size;
        detailPageList()
    }
    //页数改变时触发
    const currentChange = (page: number) => {
        state.listParm.pageNum = page
        detailPageList()
    }

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

    // 显示操作
    const show = (row: Recordable) => {
        dialog.title = `${row.billName}账单明细`
        currencyList.value = store.getCurrencyList
        console.log(row, ":data");
        if (row) {
            state.listParm.mainId = row.id
            detailPageList()
        }
        onShow()
    }

    // 取消弹窗
    const hanleOnClose = () => {
        onClose()
    }

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

    return {
        show,
        confirm,
        hanleOnClose,
        sizeChange,
        currentChange,
        detailPageList,
        hanleExport,
        hanleFocus,
        hanleSaveOrder,
        addBillMainOrderRef,
        waybillNoListRef,
        currencyList,
        state
    }
}