<template>
    <view class="withdraw-detail">
        <!-- 列表区域 -->
        <page-scroll @updateList="handleUpdateList" ref="pageRef" slotEmpty url="/token/user/withdrawList">
            <view class="detail-list">
                <view class="detail-item" v-for="(item, index) in detailList" :key="index" @click="goToDetail(item)">
                    <view class="item-left">
                        <text class="title">{{ item.title }}</text>
                    <text class="time">{{ item.createTime }}</text>
                </view>
                <view class="item-right">
                    <text class="amount">-{{ item.changeMoney }}</text>
                    <text class="status">{{ item.desc }}</text>
                </view>
                </view>
            </view>
        </page-scroll>
    </view>
</template>

<script>
import PageScroll from '@/components/pageScroll/index.vue'
export default {
    components: {
        PageScroll
    },
    data() {
        return {
            detailList: []
        }
    },
    // #ifdef MP-ALIPAY
    onPullDownRefresh() {
        this.$refs.pageRef?.loadData(true)
    },
    // #endif

    onLoad(){
        this.$refs.pageRef?.loadData(true)
    },
    methods: {
        handleUpdateList(data) {
            this.detailList = data
        },
        goToDetail(item) {
            if (item.desc == '待确认收款') {
                uni.$u.http.post('/token/user/withdrawConfirm', {
                    orderNo: item.orderNo
                }).then(res => {
                    if (res.code === 200) {
                        this.handleConfirmReceipt(res.data)
                    }
                }).catch(err => {
                    uni.showToast({
                        title: err.message || '确认失败',
                        icon: 'none'
                    });
                });
            }
        },
        //执行微信确认收款操作
        handleConfirmReceipt(data) {
            if (wx.canIUse('requestMerchantTransfer')) {
                wx.requestMerchantTransfer({
                    mchId: data.mchId,
                    appId: data.appId,
                    package: data.packageStr,
                    success: (res) => {
                        // res.err_msg将在页面展示成功后返回应用时返回ok，并不代表付款成功
                        uni.showToast({
                            title: '确认收款成功',
                            icon: 'none'
                        })
                        // 刷新列表
                        this.$refs.pageRef.loadData(true);
                    },
                    fail: (res) => {
                        console.log('fail:', res);
                    },
                });
            } else {
                wx.showModal({
                    content: '你的微信版本过低，请更新至最新版本。',
                    showCancel: false,
                });
            }
        },
    }
}
</script>

<style lang="scss" scoped>
.withdraw-detail {
    min-height: 100vh;
    background: #F5F5F5;
    padding: 20rpx;

    .detail-list {
        max-height: 100vh;
        overflow-y: auto;
        
        .detail-item {
            background: #FFFFFF;
            border-radius: 12rpx;
            padding: 30rpx;
            margin-bottom: 20rpx;
            display: flex;
            justify-content: space-between;
            align-items: center;

            .item-left {
                .title {
                    font-size: 28rpx;
                    color: #333;
                    display: block;
                    margin-bottom: 10rpx;
                }

                .time {
                    font-size: 24rpx;
                    color: #999;
                }
            }

            .item-right {
                text-align: right;

                .amount {
                    font-size: 32rpx;
                    color: #38C148;
                    font-weight: 500;
                    display: block;
                    margin-bottom: 10rpx;
                }

                .status {
                    font-size: 24rpx;
                    color: #999;
                }
            }
        }
    }

    .load-more {
        text-align: center;
        color: #999;
        font-size: 24rpx;
        padding: 30rpx 0;

        &::before,
        &::after {
            content: '';
            display: inline-block;
            width: 100rpx;
            height: 1px;
            background: #EEEEEE;
            vertical-align: middle;
            margin: 0 20rpx;
        }
    }
}
</style>