import { reactive, computed, onBeforeMount } from "vue";
import { ListIitem } from "@/api/base/BaseDataModel";
import { useQueryList } from "@/hooks/useQueryList";
import { useCurrencyStore } from "@/store/currency";
import { CentralBankRatePageParam, CentralBankRateResult } from "@/api/home/homeModle";
import { api_PageCentralBankRateList } from "@/api/home/home";
import { getCurrentDateAndPretDate } from "@/utils/dateTimeStr";

/**
 * 查询央行汇率
 */
export default function useQueryCustomerrateList() {
    //定义表格数据
    const customerRateList = reactive({
        list: [] as CentralBankRateResult[],
        loading: false,//加载
    });
    //定义列表查询的参数
    const listParm = reactive<CentralBankRatePageParam>({
        currencyCode: undefined,
        releaseTime: undefined,
        pageNum: 1,
        pageSize: 10,
        total: 0
    })
    // 基础数据
    const store = useCurrencyStore();
    // 币种列表
    const currencyList = computed((): ListIitem[] => store.getCurrencyList)
    const { formattedDate } = getCurrentDateAndPretDate()

    //获取央行汇率信息列表
    const initCustomerRateList = async () => {
        try {
            customerRateList.loading = true
            let res = await api_PageCentralBankRateList(listParams)
            if (res && res.code == 200) {
                customerRateList.list = res.data.records
                listParm.total = res.data.total;
            }
        } finally {
            customerRateList.loading = false
        }
    }

    // 重置
    const resetParams = () => {
        listParm.releaseTime = formattedDate;
        listParm.currencyCode = undefined;
    }
    // 初始化列表、分页数据
    const { sizeChange, currentChange, searchBtn, resetBtn, listParams } = useQueryList<CentralBankRatePageParam>(initCustomerRateList, resetParams, listParm)

    onBeforeMount(() => {
        listParm.releaseTime = formattedDate;
        store.addCurrencyList()
    })

    return {
        customerRateList,
        listParm,
        currencyList,
        sizeChange,
        currentChange,
        searchBtn,
        resetBtn
    }
};