import { ref, reactive, computed, onMounted } from 'vue';
import { BillRateAddParams, BillRateParam } from "@/api/billrate/BillRateModle"
import { BaseTableProps } from '@/components/Table/src/types';
import { useMessage } from '@/hooks/useMessage';
import { addBillRateApi, deleteBillRateApi, editBillRateApi, getBillRateListApi } from '@/api/billrate/billrate';
import { formattedData } from '@/utils/formattedData';
import { EditType } from '@/type/BaseEnum';
import { formatDateTime } from '@/utils/dateFormate';
import { useReturnData } from '@/utils/useReturnData';
import { ListIitem } from '@/api/base/BaseDataModel';
import { useQueryList } from '@/hooks/useQueryList';

const { createMessage, createConfirm } = useMessage()
// TODO 处理：汇率版本基础业务
export default function useBillRateTable(store) {
    const addEditBillRateRef = ref<{ show: (type: string, row?: Recordable) => void }>()
    // 定义列表查询的参数
    const listParm = reactive<BillRateParam>({
        currencyCode: '',
        titleName: '',
        pageNum: 1,
        pageSize: 10,
        total: 10,
    })

    // 币种列表
    const currencyList = computed((): ListIitem[] => store.getCurrencyList)

    // 报表汇率-table列表数据
    const billRateTableList: BaseTableProps = reactive({
        border: true,
        columns: [
            {
                prop: "titleName",
                label: "名称",
            },
            {
                prop: "currencyCode",
                label: "币种",
            },
            {
                prop: "currencyRate",
                label: "汇率",
            },
            {
                prop: "updateUserName",
                label: "操作人员",
            },
            {
                prop: "createTime",
                label: "创建时间",
                showOverflowTooltip: true,
            },
            {
                prop: "action",
                label: "操作",
                width: 200,
                slot: true,
                slotName: 'action'
            },
        ],
        dataSource: [],//数据
    });

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

    // 初始化列表数据
    const billRateList = async () => {
        const res = await getBillRateListApi(listParams)
        // console.log(res, "初始化数据");
        const { data, total } = formattedData(res)
        // billrateList.list = data
        billRateTableList.dataSource = data
        listParm.total = total
    }
    // 重置
    const resetParams = () => {
        listParm.currencyCode = '';
        listParm.titleName = '';
    }
    // 初始化列表、分页数据
    const { sizeChange, currentChange, searchBtn, resetBtn, listParams } = useQueryList<BillRateParam>(billRateList, resetParams, listParm)

    // 编辑/删除操作
    const deleteBillRate = async (rateVersionId) => {
        let res = await deleteBillRateApi({ rateVersionId })
        if (res && res.code == 200) {
            //信息提示
            createMessage.success('删除成功！')
            //刷新表格
            billRateList();

        }
    }
    const hanleActionColumns = (actionType: string, data: Recordable) => {
        switch (actionType) {
            case EditType.EDIT:
                addEditBillRateRef.value?.show(EditType.EDIT, data)
                break;
            case EditType.DELETE:
                createConfirm({
                    type: "warning",
                    message: "系统提示",
                    cancelButtonText: "取消",
                    confirmButtonText: "确定",
                    beforeClose: (action, instance, done) => {
                        // console.log(action,"==> cancel , confirm ", instance, ";action, instance");
                        if (action === "confirm") {
                            deleteBillRate(data.rateVersionId)
                        }
                        done()
                    },
                })
                break;
            default:
                break;
        }
    }

    //新增
    const addBtn = () => {
        addEditBillRateRef.value?.show(EditType.ADD)
    }

    // 确定提交账单汇率
    const saveBillRate = async (row: Recordable, onclose) => {
        const startDate = formatDateTime(row.form.date[0])
        const endDate = formatDateTime(row.form.date[1])
        const titleName = row.form.titleName
        const formData: BillRateAddParams[] = []
        // 添加汇率表单数据
        for (const item of row.list) {
            formData.push({
                id: item.id || undefined,
                startDate,
                endDate,
                titleName,
                currencyCode: item.currencyCode,
                currencyRate: item.currencyRate,
                type: row.type,
                rateVersionId: item.rateVersionId
            })
        }
        switch (row.type) {
            case EditType.ADD:
                // 新增
                const resAdd = await addBillRateApi(formData)
                const codeAdd = resData(resAdd)
                if (codeAdd !== 500) {
                    onclose()
                    //刷新表格
                    billRateList();
                }
                break;
            default:
                // 编辑
                const resEdit = await editBillRateApi(formData)
                const codeDdit = resData(resEdit)
                if (codeDdit !== 500) {
                    onclose()
                    //刷新表格
                    billRateList();
                }
                break;
        }
    }

    onMounted(() => {
        store.addCurrencyList()
    })

    return {
        searchBtn,
        resetBtn,
        sizeChange,
        currentChange,
        addBtn,
        hanleActionColumns,
        saveBillRate,
        listParm,
        billRateTableList,
        addEditBillRateRef,
        currencyList
    }
}