<template>
    <view>
        <view class="header">
            <view class="searchBox" :style="{ height: localHeight + 'px', paddingTop: top + 'px' }">
                <view class="searchBox_left" @click="back">
                    <u-icon name="arrow-left" size="20px" color="#000"></u-icon>
                </view>
                <view class="searchBox_mid">{{ topVal }}</view>
            </view>
        </view>

        <!-- 余额显示部分 -->
        <view class="balance-container">
            <view class="balance-content">
                <view>
                    <view class="balance-title">{{ topVal }}余额（元）</view>
                    <view class="balance-amount">{{ balance }}</view>
                    <view class="balance-icon">
                        <image :src="photoVal" mode="widthFix" />
                    </view>
                </view>
            </view>
        </view>

        <!-- 余额变动记录 -->
        <view class="records-container">
            <view class="records-title">{{ topVal }}变动记录</view>
            <view class="records-list">
                <view v-if="records.length > 0">
                    <view class="record-item" v-for="(record, index) in records" :key="index">
                        <view class="record-info">
                            <view class="record-name" v-if="topVal == '积分'">
                                <view v-if="record.change_reason == '积分商城购物'">
                                    {{ record.change_reason }} - {{ record.commodity_order ?
                                        record.commodity_order.order_no : '' }}
                                </view>
                                <view v-else>
                                    {{ record.change_reason }} - {{ record.community_order_pay ?
                                        record.community_order_pay.order_pay_no :
                                        record.quick_payment_record.order_no }}
                                </view>
                            </view>
                            <view class="record-name" v-else>{{ record.change_reason }} - {{ record.related_order }}
                            </view>
                            <view class="record-time">{{ record.create_time }}</view>
                        </view>
                        <view class="record-amount">
                            <view>
                                <text
                                    :class="['amount-sign', record.change_reason == '下单' ? 'positive' : 'negative']">{{
                                        record.change_reason == '下单' ? '+' : '-' }}</text>
                                <text v-if="topVal == '积分'"
                                    :class="['amount-value', record.change_reason == '下单' ? 'positive' : 'negative']">¥{{
                                        record.change_value }}</text>
                                <text v-else
                                    :class="['amount-value', record.change_reason == '下单' ? 'positive' : 'negative']">¥{{
                                        record.amount_change }}</text>
                            </view>
                            <view class="record-balance">余:¥{{ record.balance_after }}</view>
                        </view>
                    </view>
                </view>
                <view v-else>
                    <view class="no-record">暂无变动记录</view>
                </view>
                <!-- 加载更多提示 -->
                <view v-if="records.length > 0" class="load-more">
                    <text v-if="loading">加载中...</text>
                    <text v-else-if="!hasMore">没有更多数据了</text>
                </view>
            </view>
        </view>
    </view>
</template>
<script>
import { apiArr } from '../../../api/v2User';
import { menuButtonInfo, NavgateTo, picUrl, request } from '../../../utils/index';
export default {
    data() {
        return {
            top: "",
            localHeight: "",
            topVal: "",
            photoVal: '',
            balance: "",
            records: [],
            itemObj: {},
            itemType: '',
            page_num: 1,
            page_size: 10,
            loading: false,
            hasMore: true
        }
    },
    onLoad(options) {
        const meun = menuButtonInfo();
        this.top = meun.top;
        this.localHeight = meun.height;
        this.topVal = options.type == 1 ? '积分' : (options.type == 2 ? '岁银' : '红包卡券');
        this.photoVal = options.type == 1 ? 'https://static.hshuishang.com//user_wallet1.png' : (options.type == 2 ? 'https://static.hshuishang.com//user_wallet2.png' : 'https://static.hshuishang.com//user_wallet3.png');
        this.itemObj = JSON.parse(options.item);
        this.itemType = options.type;
        this.balance = options.type == 1 ? this.itemObj.points : (options.type == 2 ? this.itemObj.property_housing_fund : 0);
        this.page_num = 1;
        this.records = [];
        this.hasMore = true;
        this.getWalletInfo();
    },

    // 下拉刷新
    onPullDownRefresh() {
        this.page_num = 1;
        this.records = [];
        this.hasMore = true;
        this.getWalletInfo().then(() => {
            uni.stopPullDownRefresh();
        });
    },

    // 触底加载更多
    onReachBottom() {
        if (this.loading || !this.hasMore) return;
        this.page_num++;
        this.getWalletInfo();
    },
    methods: {
        back() {
            uni.navigateBack({
                delta: 1
            });
        },
        // 获取钱包信息
        getWalletInfo() {
            if (this.loading) return Promise.resolve();

            this.loading = true;
            const params = {
                page_num: this.page_num,
                page_size: this.page_size,
            }

            const requestPromise = new Promise((resolve, reject) => {
                if (this.itemType == 1) {
                    request(apiArr.getPoints, 'POST', params, { silent: false }).then(res => {
                        this.handleResponse(res);
                        resolve();
                    }).catch(err => {
                        reject(err);
                    })
                } else if (this.itemType == 2) {
                    request(apiArr.getAccumulationFund, 'POST', params, { silent: false }).then(res => {
                        this.handleResponse(res);
                        resolve();
                    }).catch(err => {
                        reject(err);
                    })
                } else {
                    resolve();
                }
            });

            requestPromise.finally(() => {
                this.loading = false;
            });

            return requestPromise;
        },

        // 处理响应数据
        handleResponse(res) {
            if (res && res.rows) {
                if (this.page_num === 1) {
                    // 第一页数据直接替换
                    this.records = res.rows;
                } else {
                    // 后续页数据追加
                    this.records = [...this.records, ...res.rows];
                }

                // 判断是否还有更多数据
                this.hasMore = res.rows.length >= this.page_size;
            }
        }
    }
}
</script>
<style>
@import url("./index.css");
</style>