/**
 * @name: index.utli
 * @author: kahu4
 * @date: 2023-10-31 11:19
 * @description：index.utli
 * @update: 2023-10-31 11:19
 * */
import { ref, computed } from "vue";
import { onShow } from '@dcloudio/uni-app'
import {getCouponList} from "@/api/coupon";
import { tabList } from "@/pages/discountCoupon/index.data";

export function useDiscountCoupon(){
    const tabCurrent = ref(0) // tab选中
    const couponType = ref(1) // 1、可使用，2、已使用，3、已失效
    const couponListLoading = ref(false)
    const couponList = ref([])
    const showEmpty = computed(() => couponList.value.length <= 0)

    /**
     * tab发生改变
     * @param e
     */
   async function handleTabsChange(e){
        console.log(e)
        couponList.value = []
        tabCurrent.value = e.index
        couponType.value = e.value
        await doGetCouponList()
    }
    /**
     * 获取购物车列表
     * @returns {Promise<void>}
     */
    async function doGetCouponList() {
        try {
            couponListLoading.value = true
            const res = await getCouponList(couponType.value)
            couponList.value = res
        } finally {
            couponListLoading.value = false
        }
    }


    onShow(async () => {
        await doGetCouponList()
    })

    return {
        tabList,
        tabCurrent,
        handleTabsChange,
        showEmpty,
        couponListLoading,
        couponList,
        doGetCouponList
    }
}
