import { shallowRef } from 'vue'
import { useDataTable } from '@/hooks/datatable'
import { v4 } from 'uuid'
import { ClientType } from '@/constants/client'
import { ScoreConfigType } from '@/constants/credit'
import { useLoginStore } from '@/stores'
import { queryUserAccount } from '@/services/api/account'
import { queryUserScoreLog, queryTHJScoreConfig, thjRedPacketLottery } from '@/services/api/credit'

// 积分流水列表
export function useCreditStatementList(stype?: ScoreConfigType) {
    const { getUserId } = useLoginStore()
    const { dataList, total, pageIndex, pageSize, pageCount } = useDataTable<Model.UserScoreLogRsp>()
    const loading = shallowRef(false)

    const getCreditStatementList = async () => {
        loading.value = true
        await queryUserScoreLog({
            data: {
                stype,
                userid: getUserId(),
                page: pageIndex.value,
                pagesize: pageSize.value,
            },
            success: (res) => {
                total.value = res.total
                dataList.value = res.data
            },
            complete: () => {
                loading.value = false
            }
        })
        return dataList.value
    }

    return {
        loading,
        dataList,
        total,
        pageIndex,
        pageSize,
        pageCount,
        getCreditStatementList,
    }
}

// 积分抽奖
export function useCreditLottery() {
    const { getUserId } = useLoginStore()
    const loading = shallowRef(false)
    const creditList = shallowRef<Model.UserScoreLogRsp[]>([])
    const creditConfig = shallowRef<Partial<Model.THJScoreConfigRsp>>({})
    const userAccount = shallowRef<Partial<Model.UserAccount>>({})

    const redEnvelope = shallowRef<Partial<Proto.THJRedPacketLotteryRsp>>({
        GearLevel: 0,
        RedPacketAmount: 0,
    })

    const formRefresh = () => {
        queryUserAccount({
            data: {
                userID: getUserId()
            },
            success: (res) => {
                userAccount.value = res.data
            }
        })
        queryUserScoreLog({
            data: {
                stype: ScoreConfigType.Lottery,
                userid: getUserId(),
                page: 1,
                pagesize: 10,
            },
            success: (res) => {
                creditList.value = res.data
            },
        })
    }

    const formSubmit = () => {
        loading.value = true
        return thjRedPacketLottery({
            data: {
                UserID: getUserId(),
                ClientType: ClientType.Web, // 终端类型
                ClientSerialNo: v4() // 客户端流水号
            },
            success: (res) => {
                redEnvelope.value = res
            },
            complete: () => {
                loading.value = false
            }
        })
    }

    queryTHJScoreConfig({
        data: {
            stype: ScoreConfigType.Lottery
        },
        success: (res) => {
            if (res.data.length) {
                creditConfig.value = res.data[0]
            }
        },
    })

    return {
        loading,
        creditConfig,
        userAccount,
        creditList,
        redEnvelope,
        formSubmit,
        formRefresh,
    }
}