<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="https://static.hshuishang.com//user_wallet2.png" mode="widthFix" />
                    </view>
                </view>
            </view>
        </view>

        <!-- 余额变动记录 -->
        <view class="records-container">
            <view class="records-title" v-if="topVal == '待收岁银'">岁银变动记录</view>
            <view v-if="records.length == 0">
                <view class="records-list">暂无变动记录</view>
            </view>
            <view class="records-list" v-else>
                <view class="record-item" v-for="(record, index) in records" :key="index">
                    <view class="record-info">
                        <view class="record-name">{{ record.change_reason == 1 ? '下单' : '退单' }} - {{
                            record.related_order }}</view>
                        <view class="record-time">{{ record.change_time }}</view>
                    </view>
                    <view class="record-amount">
                        <view class="record-balance" v-if="topVal == '应收' || topVal == '已收'">{{ record.change_reason ==
                            1 ? '收款' :
                            '退款' }}</view>
                        <view class="record-balance" v-else-if="topVal == '待收岁银'">待收</view>
                        <view class="record-balance" v-else-if="topVal == '手续费'">手续费</view>
                        <view>
                            <text :class="['amount-sign', record.change_reason == 1 ? 'positive' : 'negative']">{{
                                record.change_reason == 1 ? '+' : '-' }}</text>
                            <text :class="['amount-value', record.change_reason == 1 ? 'positive' : 'negative']">¥{{
                                record.amount_change }}</text>
                        </view>
                    </view>
                </view>
            </view>
        </view>
    </view>
</template>
<script>
import { menuButtonInfo, NavgateTo, picUrl, request } from '../../../utils/index';
import { apiArr as walletApi } from '../../../api/wallet';

export default {
    data() {
        return {
            top: "",
            localHeight: "",
            topVal: "",
            balance: "",
            records: [],
            community_id: "",
        }
    },
    onLoad(options) {
        const meun = menuButtonInfo();
        this.top = meun.top;
        this.localHeight = meun.height;
        this.getTypeName(options.type);
        this.balance = options.amount;
        this.community_id = options.community_id;
        this.getWalletInfo();
    },
    methods: {
        back() {
            uni.navigateBack({
                delta: 1
            });
        },
        // 获取钱包类型名称
        getTypeName(type) {
            const numType = parseInt(type);
            switch (numType) {
                case 1:
                    this.topVal = '应收';
                    break;
                case 2:
                    this.topVal = '已收';
                    break;
                case 3:
                    this.topVal = '待收岁银';
                    break;
                case 4:
                    this.topVal = '手续费';
                    break;
            }
        },
        // 获取钱包信息
        getWalletInfo() {
            const params = {
                community_id: this.community_id,
            }
            request(walletApi.walletChangeList, 'POST', params, { silent: false }).then(res => {
                let filteredRows = res.rows;
                if (this.topVal == '应收') {
                    filteredRows = res.rows.filter(item => item.change_category == 2);
                } else if (this.topVal == '已收') {
                    filteredRows = res.rows.filter(item => item.change_category == 1);
                } else if (this.topVal == '待收岁银') {
                    filteredRows = res.rows.filter(item => item.change_category == 4);
                } else {
                    filteredRows = res.rows.filter(item => item.change_category == 3);
                }
                this.records = filteredRows;
            })
        }
    }
}
</script>
<style>
@import url("./index.css");
</style>