<template>
    <ion-page>
        <ion-header>
            <ion-toolbar>
                <ion-buttons slot="start">
                    <ion-back-button text="" default-href="#" @click="$router.back()"></ion-back-button>
                </ion-buttons>
                <ion-title>{{ $t('mine.wallet') }}</ion-title>
            </ion-toolbar>
        </ion-header>
        <ion-content class="watching">
            <img src="@/assets/png-zhuiju-bg.png" class="top-bg !absolute w-full h-[200px] top-0 left-0 !z-0" alt="" />
            <div class="watching-content">
                <div class="py-[14px] px-[18px]">
                    <van-empty v-if="empty" :image="emptyImg" description="暂无记录" />
                    <van-list
                        class="list"
                        v-model:loading="loading"
                        :finished="finished"
                        :finished-text="!empty ? $t('loadingFinished') : ''"
                        @load="getData"
                        :immediate-check="false"
                        ref="listRef"
                    >
                        <div
                            v-for="(item, index) in list"
                            :key="index"
                            class="wallet bg-[#20223C] flex h-[68px] items-center px-[14px] rounded-lg"
                        >
                            <img class="w-[36px] h-[36px]" :src="getIcon(item.type)" alt="" />
                            <div class="flex-1 ml-[12px]">
                                <div class="text-sm text-white">{{ getTitle(item) }}</div>
                                <div class="text-xs text-[#797A8A]">{{ formateDay(item.createdAt) }}</div>
                            </div>
                            <div class="text-[18px] text-white AlimamaShuHeiTi">{{ getAmount(item.amount) }}</div>
                        </div>
                    </van-list>
                </div>
            </div>
        </ion-content>
    </ion-page>
</template>
<script setup>
import { useRouter } from 'vue-router'
import { ref } from 'vue'
import { http } from '@/plugins/http'
import { format } from 'date-fns'
import tixianImg from '@/assets/icon-tixian.png'
import qiandaoImg from '@/assets/icon-qiandao.png'
import yaoqingImg from '@/assets/icon-yaoqing.png'
import useList from '../plugins/list'
import emptyImg from '../assets/png-kong.png'

const router = useRouter()
function back() {
    router.back()
}

const isFixed = ref(false)
function changeSticky(fixed) {
    isFixed.value = fixed
}

const { empty, size, loading, finished, list, getData } = useList('/api/balanceRecords', () => {}, 'get')
getData(true)

function formateDay(day) {
    console.log(new Date(day))
    return format(new Date(day), 'MM-dd hh:mm', new Date())
}

function getAmount(amount) {
    let _amount = Number(amount)
    return (_amount > 0 ? '+' : '') + _amount
}

function getIcon(type) {
    let img = tixianImg
    switch (type) {
        case 'reward':
            img = qiandaoImg
            break
        case 'recharge':
            img = yaoqingImg
            break
        case 'purchase':
            img = tixianImg
            break
    }
    return img
}

function getTitle(info) {
    let title = ''
    if (info.series) {
        title = '购买《' + info.series.title + '》'

        if (info.episode) {
            title += '第' + info.episode.episodeNum + '集'
        }
    } else if (info.episode) {
        title = '购买第' + info.episode.episodeNum + '集'
    }
    if (info.description) {
        title = info.description
    }
    return title
}
</script>

<style lang="less" scoped>
.watching {
    .watching-content {
        position: relative;
        z-index: 1;
    }
}

.van-nav-bar {
    --van-nav-bar-background: transparent;
    --van-border-width: 0;
    --van-nav-bar-title-text-color: #fff;
    --van-nav-bar-icon-color: #fff;

    &.isFixed {
        --van-nav-bar-background: #11131e;
    }
}

.wallet + .wallet {
    margin-top: 14px;
}
</style>
