<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" v-if="topVal != '保证金'">
                        <image src="http://localhost:8080/user_wallet2.png" mode="widthFix" />
                    </view>
                    <view v-else class="balance-text">
                        去充值
                    </view>
                </view>
            </view>
        </view>

        <!-- 变动记录 -->
        <view class="records-container">
            <view class="tabs" v-if="topVal == '保证金'">
                <view v-for="(item, index) in tabList" :key="index"
                    :class="['tabItem', selectedTab === item.id ? 'active2' : '']" @click="selectTab(index, item)">
                    {{ item.tabName }}
                </view>
            </view>
            <view class="records-list">
                <view class="record-item" v-for="(record, index) in records" :key="index">
                    <view class="record-info">
                        <view class="record-name">{{ record.name }}</view>
                        <view class="record-time">{{ record.time }}</view>
                    </view>
                    <view class="record-amount" v-if="topVal == '保证金'">
                        <view>
                            <text :class="['amount-sign', record.amount.startsWith('+') ? 'positive' : 'negative']">{{
                                record.amount.startsWith('+') ? '+' : '-' }}</text>
                            <text :class="['amount-value', record.amount.startsWith('+') ? 'positive' : 'negative']">¥{{
                                record.amount.replace(/[+-]/, '') }}</text>
                        </view>
                        <view class="record-balance">余:¥{{ record.remain }}</view>
                    </view>

                    <view class="record-amount" v-else-if="topVal == '应收'">
                        <view :class="['amount-sign', record.amount.startsWith('+') ? 'positive' : 'negative']">{{
                            record.amount.startsWith('+') ? '收款' : '退款' }}</view>
                        <view>
                            <text :class="['amount-sign', record.amount.startsWith('+') ? 'positive' : 'negative']">{{
                                record.amount.startsWith('+') ? '+' : '-' }}</text>
                            <text :class="['amount-value', record.amount.startsWith('+') ? 'positive' : 'negative']">¥{{
                                record.amount.replace(/[+-]/, '') }}</text>
                        </view>
                    </view>

                    <view class="record-amount" v-else>
                        <view class="record-balance">手续费</view>
                        <view>
                            <text class="amount-sign">{{ record.amount.startsWith('+') ? '+' : '-' }}</text>
                            <text class="amount-sign">¥{{ record.amount.replace(/[+-]/, '') }}</text>
                        </view>
                    </view>
                </view>
            </view>
        </view>
    </view>
</template>
<script>
import { menuButtonInfo, NavgateTo, picUrl, request } from '../../../utils/index';
export default {
    data() {
        return {
            top: "",
            localHeight: "",
            topVal: "",
            balance: "160.80",
            records: [
                {
                    name: "退单-1312312317972",
                    time: "2025-08-12 13:00:00",
                    amount: "-1808.5",
                    remain: "286.79"
                },
                {
                    name: "退单-1312312317972",
                    time: "2025-08-12 13:00:00",
                    amount: "-1808.5",
                    remain: "286.79"
                }
            ],
            tabList: [
                {
                    id: 0,
                    tabName: '全部'
                },
                {
                    id: 1,
                    tabName: '充值记录'
                },
                {
                    id: 2,
                    tabName: '消费记录'
                }
            ],
            selectedTab: 0
        }
    },
    onLoad(options) {
        const meun = menuButtonInfo();
        this.top = meun.top;
        this.localHeight = meun.height;
        this.topVal = options.type == 1 ? '保证金' : (options.type == 2 ? '应收' : '手续费');
        this.getWalletInfo();
    },
    methods: {
        back() {
            uni.navigateBack({
                delta: 1
            });
        },
        selectTab(index, item) {
            console.log("🚀 ~ selectTab ~ item:", item)
            this.selectedTab = item.id;
        },
        // 获取钱包信息
        getWalletInfo() {
            // 这里应该是真实的接口调用
            // 暂时使用模拟数据
            console.log('获取钱包信息');
            // 实际项目中应该调用接口获取余额和交易记录
            // request('/api/wallet/info', 'GET').then(res => {
            //     if (res.code === 0) {
            //         this.balance = res.data.balance;
            //         this.records = res.data.records;
            //     }
            // })
        }
    }
}
</script>
<style>
@import url("./index.css");
</style>