<template>
    <div class="money-log">
        <!-- 页面标题和剩余余额 -->
        <div class="header">
            <h1 class="title">
                <el-icon class="title-icon"><Wallet /></el-icon>
                钱包流水
            </h1>
            <div class="remaining-balance">
                当前余额：
                <span class="count">{{ balance }}</span> 
                豆豆
            </div>
        </div>

        <!-- 流水类型选择 -->
        <div class="filter-section">
            <el-radio-group v-model="logType" @change="handleTypeChange">
                <el-radio-button label="all">全部</el-radio-button>
                <el-radio-button label="income">收入</el-radio-button>
                <el-radio-button label="expense">支出</el-radio-button>
            </el-radio-group>
        </div>

        <!-- 流水记录表格 -->
        <el-table
            :data="moneyLogs"
            style="width: 100%"
            v-loading="loading"
            border
        >
            <el-table-column prop="createTime" label="时间" width="180">
                <template #default="scope">
                    {{ formatDate(scope.row.createTime) }}
                </template>
            </el-table-column>
            <el-table-column prop="title" label="交易内容" min-width="200" />
            <el-table-column prop="type" label="类型" width="100">
                <template #default="scope">
                    <el-tag :type="getTypeTagStyle(scope.row.type)">
                        {{ getTypeText(scope.row.type) }}
                    </el-tag>
                </template>
            </el-table-column>
            <el-table-column prop="money" label="豆豆" width="120">
                <template #default="scope">
                    <span :class="getAmountClass(scope.row.type)">
                        {{ getAmountPrefix(scope.row.type) }}{{ scope.row.money }}
                    </span>
                </template>
            </el-table-column>
            <!-- <el-table-column prop="state" label="状态" width="100">
                <template #default="scope">
                    <el-tag :type="getStatusType(scope.row.state)">
                        {{ getStatusText(scope.row.state) }}
                    </el-tag>
                </template>
            </el-table-column> -->
        </el-table>

        <!-- 分页 -->
        <div class="pagination-container">
            <el-pagination
                v-model:current-page="currentPage"
                v-model:page-size="pageSize"
                :page-sizes="[10, 20, 50, 100]"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total"
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
            />
        </div>
    </div>
</template>

<script>
import { ref, reactive, onMounted, computed } from 'vue';
import { ElMessage } from 'element-plus';
import { Wallet } from '@element-plus/icons-vue';
import { moneyApi } from '@/api/user.js';
import { formatDate as formatDateUtil } from '@/utils/date';

export default {
    name: 'MoneyLog',
    components: {
        Wallet
    },
    setup() {
        const balance = ref(0);
        const moneyLogs = ref([]);
        const loading = ref(false);
        const logType = ref('all');
        const currentPage = ref(1);
        const pageSize = ref(10);
        const total = ref(0);

        // 获取我的可用积分数
        const getMyMoneyNum = async () => {
            try {
                const res = await moneyApi.getMyMoneyNum();
                balance.value = res.result || 0;
            } catch (error) {
                console.error('获取余额失败:', error);
                ElMessage.error('获取余额失败');
            }
        };

        // 获取我的流水列表
        const getMyMoneyLogPage = async () => {
            loading.value = true;
            try {
                const params = {
                    pageNo: currentPage.value,
                    pageSize: pageSize.value,
                    type: getTypeFilter()
                };
                const res = await moneyApi.getMyMoneyLogPage(params);
                if (res.result) {
                    moneyLogs.value = res.result.records || [];
                    total.value = res.result.total || 0;
                }
            } catch (error) {
                console.error('获取流水列表失败:', error);
                ElMessage.error('获取流水列表失败');
            } finally {
                loading.value = false;
            }
        };

        // 根据选择的类型获取类型过滤值
        const getTypeFilter = () => {
            switch (logType.value) {
                case 'income':
                    return 0; // 收入
                case 'expense':
                    return 1; // 支出
                default:
                    return null; // 全部
            }
        };

        // 格式化日期
        const formatDate = (dateString) => {
            return formatDateUtil(dateString, 'YYYY-MM-DD HH:mm:ss');
        };

        // 获取金额样式类
        const getAmountClass = (type) => {
            return type === 0 ? 'income-amount' : 'expense-amount';
        };

        // 获取金额前缀
        const getAmountPrefix = (type) => {
            return type === 0 ? '+' : '-';
        };

        // 获取类型文本
        const getTypeText = (type) => {
            switch (type) {
                case 0:
                    return '收入';
                case 1:
                    return '支出';
                default:
                    return '未知';
            }
        };

        // 获取类型标签样式
        const getTypeTagStyle = (type) => {
            return type === 0 ? 'success' : 'danger';
        };

        // 获取状态类型
        const getStatusType = (state) => {
            switch (state) {
                case 0:
                    return 'info'; // 处理中
                case 1:
                    return 'success'; // 成功
                case 2:
                    return 'danger'; // 失败
                default:
                    return 'info';
            }
        };

        // 获取状态文本
        const getStatusText = (state) => {
            switch (state) {
                case 0:
                    return '处理中';
                case 1:
                    return '成功';
                case 2:
                    return '失败';
                default:
                    return '未知';
            }
        };

        // 处理类型变更
        const handleTypeChange = () => {
            currentPage.value = 1;
            getMyMoneyLogPage();
        };

        // 处理页码变更
        const handleCurrentChange = (val) => {
            currentPage.value = val;
            getMyMoneyLogPage();
        };

        // 处理每页条数变更
        const handleSizeChange = (val) => {
            pageSize.value = val;
            currentPage.value = 1;
            getMyMoneyLogPage();
        };

        // 初始化数据
        const initData = async () => {
            try {
                await Promise.all([
                    getMyMoneyNum(),
                    getMyMoneyLogPage()
                ]);
            } catch (error) {
                console.error('初始化数据失败:', error);
            }
        };

        onMounted(() => {
            initData();
        });

        return {
            balance,
            moneyLogs,
            loading,
            logType,
            currentPage,
            pageSize,
            total,
            formatDate,
            getAmountClass,
            getAmountPrefix,
            getTypeText,
            getTypeTagStyle,
            getStatusType,
            getStatusText,
            handleTypeChange,
            handleCurrentChange,
            handleSizeChange
        };
    }
};
</script>

<style lang="scss" scoped>
.money-log {
    padding: 24px;
    max-width: 1200px;
    margin: 0 auto;

    .header {
        display: flex;
        justify-content: space-between;
        align-items: center;
        margin-bottom: 32px;
        padding-bottom: 16px;
        border-bottom: 1px solid #ebeef5;

        .title {
            display: flex;
            align-items: center;
            font-size: 24px;
            font-weight: 600;
            color: #0A2463;
            margin: 0;

            .title-icon {
                margin-right: 8px;
                font-size: 28px;
            }
        }

        .remaining-balance {
            font-size: 14px;
            color: #666;

            .count {
                color: #EB8D00;
                font-weight: 600;
                font-size: 16px;
            }
        }
    }

    .filter-section {
        margin-bottom: 20px;
    }

    .income-amount {
        color: #67c23a;
        font-weight: 600;
    }

    .expense-amount {
        color: #f56c6c;
        font-weight: 600;
    }

    .pagination-container {
        margin-top: 20px;
        display: flex;
        justify-content: flex-end;
    }
}

// 响应式设计
@media (max-width: 768px) {
    .money-log {
        padding: 16px;

        .header {
            flex-direction: column;
            align-items: flex-start;
            gap: 12px;

            .title {
                font-size: 20px;
            }
        }

        .el-table {
            font-size: 12px;
        }

        .pagination-container {
            justify-content: center;
        }
    }
}
</style>