<template>
    <div class="consumption-modal-overlay" v-if="visible" @click="handleOverlayClick">
        <div class="consumption-modal-container" @click.stop>
            <!-- 头部 -->
            <div class="modal-header">
                <h2 class="modal-title">积分详情</h2>
                <button class="close-btn" @click="handleClose">
                    <span class="close-icon">x</span>
                </button>
            </div>

            <!-- Tab切换区域 -->
            <div class="tabs-container">
                <button 
                    class="tab-btn" 
                    :class="{ active: activeTab === 'consume' }" 
                    @click="switchTab('consume')"
                >
                    消费记录
                </button>
                <button 
                    class="tab-btn" 
                    :class="{ active: activeTab === 'recharge' }" 
                    @click="switchTab('recharge')"
                >
                    充值记录
                </button>
                <button 
                    class="tab-btn" 
                    :class="{ active: activeTab === 'refund' }" 
                    @click="switchTab('refund')"
                >
                    退回记录
                </button>
            </div>

            <!-- 内容区域 -->
            <div class="modal-content">
                <!-- 加载状态 -->
                <div class="loading-state" v-if="loading">
                    <div class="loading-spinner"></div>
                    <p>加载中...</p>
                </div>

                <!-- 错误状态 -->
                <div class="error-state" v-else-if="error">
                    <div class="error-icon">
                        <svg t="1761112765081" class="icon" viewBox="0 0 1024 1024" version="1.1"
                            xmlns="http://www.w3.org/2000/svg" p-id="3755" width="200" height="200">
                            <path
                                d="M827.392 195.584q65.536 65.536 97.792 147.456t32.256 167.936-32.256 167.936-97.792 147.456-147.456 98.304-167.936 32.768-168.448-32.768-147.968-98.304-98.304-147.456-32.768-167.936 32.768-167.936 98.304-147.456 147.968-97.792 168.448-32.256 167.936 32.256 147.456 97.792zM720.896 715.776q21.504-21.504 18.944-49.152t-24.064-49.152l-107.52-107.52 107.52-107.52q21.504-21.504 24.064-49.152t-18.944-49.152-51.712-21.504-51.712 21.504l-107.52 106.496-104.448-104.448q-21.504-20.48-49.152-23.04t-49.152 17.92q-21.504 21.504-21.504 52.224t21.504 52.224l104.448 104.448-104.448 104.448q-21.504 21.504-21.504 51.712t21.504 51.712 49.152 18.944 49.152-24.064l104.448-104.448 107.52 107.52q21.504 21.504 51.712 21.504t51.712-21.504z"
                                p-id="3756"></path>
                        </svg>
                    </div>
                    <p>{{ error }}</p>
                    <button class="retry-btn" @click="fetchLogs">重试</button>
                </div>

                <!-- 空状态 -->
                <div class="empty-state" v-else-if="logs.length === 0">
                    <div class="empty-icon">
                        <svg t="1761112687478" class="icon" viewBox="0 0 1024 1024" version="1.1"
                            xmlns="http://www.w3.org/2000/svg" p-id="2683" width="200" height="200">
                            <path
                                d="M708.2 194.6c-4.8 1.1-9.9-0.2-13.5-3.6-2.7-2.5-4.2-5.9-4.2-9.6 0-3.6 1.5-7.1 4.2-9.6l37.2-37.3c2.5-2.7 5.9-4.2 9.6-4.2 3.7 0 7.1 1.5 9.6 4.2 2.7 2.5 4.2 5.9 4.2 9.6 0 3.6-1.5 7.1-4.2 9.6L713.9 191c-2.3 2.3-3.4 3.4-5.7 3.4M791.6 281.6c-1.7 0.9-3.7 1.3-5.7 1.2l-47.3-5.6c-7.5-0.6-13-7.2-12.4-14.7 0.6-7.5 7.2-13 14.7-12.4l47.3 4.5c7.5 0.7 13 7.2 12.4 14.7 0 5.6-3.6 10.6-9 12.3M628.1 176.6c-3.4 1.2-7.2 0.8-10.4-0.9-3.2-1.7-5.5-4.7-6.5-8.1l-15.8-45.2c-2.4-6.8 1.1-14.4 7.9-17 3.4-1.2 7.2-0.9 10.4 0.8 3.2 1.7 5.6 4.6 6.6 8.1l15.8 45.2c1.4 3.3 1.4 7-0.1 10.2-1.5 3.3-4.4 5.7-7.9 6.9M451.2 915.6H279.8c-6.2 0-11.3-5.1-11.3-11.3 0-6.2 5.1-11.3 11.3-11.3h171.3c3.1-0.2 6 0.9 8.2 3.1 2.2 2.2 3.3 5.2 3.1 8.2 0 3-1.2 5.9-3.3 8-2.1 2.1-4.9 3.3-7.9 3.3z m-171.4-60.1c-6.2 0-11.3-5.1-11.3-11.3 0-6.2 5.1-11.3 11.3-11.3l381.1-1.1c6.2 0 11.3 5.1 11.3 11.3 0 6.2-5.1 11.3-11.3 11.3l-381.1 1.1z m0 0"
                                fill="#DCDCDC" p-id="2684"></path>
                            <path
                                d="M208.5 390c6.8-2.4 11.6-8.6 12.3-15.8-13-40.7-26-81.5-39.1-122.2-2.4-7.6-11.5-11.5-20.3-8.7-8.8 2.8-14.1 11.2-11.6 18.8 13 41.1 26 82.2 39.1 123.4 5.1 5.2 12.8 7 19.6 4.5z m103.9 320.5c6.8-2.4 11.6-8.6 12.3-15.8-13-40.7-26-81.5-39.1-122.2-2.4-7.6-11.5-11.5-20.3-8.7-8.8 2.8-14.1 11.2-11.6 18.8 13 41.1 26 82.2 39.1 123.4 5 5.3 12.7 7 19.6 4.5z m28 78.8c6.8-2.4 11.6-8.6 12.3-15.8-13-40.7-0.3-0.6-13.3-41.3-2.4-7.6-11.5-11.5-20.3-8.7-8.8 2.8-14.1 11.2-11.6 18.8 13 41.1 0.3 1.4 13.3 42.5 5.1 5.3 12.8 7 19.6 4.5z m-7.3 2.2c41.2-12.7 1.3-0.6 42.5-13.3 7.6-2.3 11.6-11.5 8.9-20.3-2.7-8.8-11.1-14.1-18.7-11.8-40.9 12.5-0.6 0.1-41.5 12.7-5.5 4.7-7.8 12.1-6 19.1 1.8 7 7.6 12.3 14.8 13.6z m-73.1-241c6.8-2.4 11.6-8.6 12.3-15.8-13-40.7-26-81.5-39.1-122.2-2.4-7.6-11.5-11.5-20.3-8.7-8.8 2.8-14.1 11.2-11.6 18.8 13 41.1 26 82.2 39.1 123.4 5.1 5.2 12.8 6.9 19.6 4.5z m479.5 99.6c40.7-14.3 81.4-28.5 122.1-42.8 7.6-2.6 11.1-11.9 8.1-20.6-3-8.7-11.6-13.7-19.1-11.1-40.4 14-80.8 28.1-121.2 42.1-5.3 4.9-7.3 12.4-5.2 19.3 2.1 7 8.1 12.1 15.3 13.1zM419.8 761.3c40.7-14.3 81.4-28.5 122.1-42.8 7.6-2.6 11.1-11.9 8.1-20.6-3-8.7-11.6-13.7-19.1-11.1-40.4 14-80.8 28.1-121.2 42.1-5.3 4.9-7.3 12.4-5.2 19.3 2.1 6.9 8.1 12.1 15.3 13.1zM579.3 707c40.7-14.3 81.4-28.5 122.1-42.8 7.6-2.6 11.1-11.9 8.1-20.6-3-8.7-11.6-13.7-19.1-11.1-40.4 14-80.8 28.1-121.2 42.1-5.3 4.9-7.3 12.4-5.2 19.3 2 7 8 12.1 15.3 13.1z m228.6-102.5M406.7 574.8l42.8-82.9 96.4 49.8c4 2.1 8.6 2.5 12.9 1.1l0.4-0.1c4.4-1.5 8-4.8 9.9-9l102.6-226.5c3.9-8.5 0.1-18.6-8.4-22.4-8.5-3.8-18.6-0.1-22.4 8.4l-95.3 210.2-95.6-49.3c-8.3-4.3-18.5-1-22.8 7.3l-50.6 97.9c-4.3 8.3-1 18.5 7.3 22.8 8.3 4.2 18.5 1 22.8-7.3z m0 0"
                                fill="#666666" p-id="2685"></path>
                        </svg>
                    </div>
                    <p>{{ getEmptyText() }}</p>
                </div>

                <!-- 记录列表 -->
                <div class="logs-list" v-else>
                    <div class="log-item" v-for="log in logs" :key="log.id">
                        <div class="log-main">
                            <div class="log-left">
                                <div class="task-info">
                                    <span class="task-no">任务号: {{ log.taskNo }}</span>
                                    <span class="change-type">{{ log.changeTypeName }}</span>
                                </div>
                                <div class="description">{{ log.description }}</div>
                                <div class="time-info">
                                    <span class="create-time" >{{ formatTime(log.createTime) }}</span>
                                </div>
                            </div>
                            <div class="log-right">
                                <div 
                                    class="amount" 
                                    :class="{
                                        'amount-decrease': activeTab === 'consume',
                                        'amount-increase': activeTab === 'recharge' || activeTab === 'refund'
                                    }"
                                >
                                    {{ getAmountDisplay(log.changeAmount) }}
                                </div>
                                <div class="balance-info">
                                    <div class="balance-before">余额: {{ log.balanceBefore }}</div>
                                    <div class="balance-after">剩余: {{ log.balanceAfter }}</div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <!-- 分页 - 固定在底部 -->
            <div class="pagination" v-if="logs.length > 0">
                <button class="page-btn" :disabled="currentPage === 1" @click="prevPage">
                    上一页
                </button>
                <span class="page-info">第 {{ currentPage }} 页 / 共 {{ totalPages }} 页</span>
                <button class="page-btn" :disabled="currentPage >= totalPages" @click="nextPage">
                    下一页
                </button>
            </div>
        </div>
    </div>
</template>

<script setup>
import { ref, watch, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import { getConsumptionLogs } from '../api/package'

const props = defineProps({
    visible: {
        type: Boolean,
        default: false
    }
})

const emit = defineEmits(['close'])

const loading = ref(false)
const error = ref('')
const logs = ref([])
const currentPage = ref(1)
const pageSize = ref(10)
const totalPages = ref(0)
const activeTab = ref('consume') // 默认显示消费记录

// 获取积分记录
const fetchLogs = async () => {
    try {
        loading.value = true
        error.value = ''

        const response = await getConsumptionLogs({
            page: currentPage.value,
            size: pageSize.value,
            changeType: activeTab.value
        })

        if (response.code === 200) {
            logs.value = response.data.records || []
            totalPages.value = response.data.totalPages || 0
        } else {
            error.value = response.message || '获取记录失败'
            ElMessage.error(error.value)
        }
    } catch (err) {
        console.error('获取记录失败:', err)
        error.value = '网络错误，请稍后重试'
        ElMessage.error(error.value)
    } finally {
        loading.value = false
    }
}

// 切换Tab
const switchTab = (tab) => {
    if (activeTab.value === tab) return
    activeTab.value = tab
    currentPage.value = 1 // 切换时重置页码
    fetchLogs()
}

// 获取积分显示（根据类型显示+/-）
const getAmountDisplay = (amount) => {
    if (activeTab.value === 'recharge' || activeTab.value === 'refund') {
        return `+${amount} 积分`
    } else {
        return `-${amount} 积分`
    }
}

// 获取空状态提示文字
const getEmptyText = () => {
    const textMap = {
        consume: '暂无消费记录',
        recharge: '暂无充值记录',
        refund: '暂无退回记录'
    }
    return textMap[activeTab.value] || '暂无记录'
}

// 格式化时间
const formatTime = (timeStr) => {
    if (!timeStr) return '-'
    const date = new Date(timeStr)
    const year = date.getFullYear()
    const month = String(date.getMonth() + 1).padStart(2, '0')
    const day = String(date.getDate()).padStart(2, '0')
    const hours = String(date.getHours()).padStart(2, '0')
    const minutes = String(date.getMinutes()).padStart(2, '0')
    const seconds = String(date.getSeconds()).padStart(2, '0')
    return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`
}

// 上一页
const prevPage = () => {
    if (currentPage.value > 1) {
        currentPage.value--
        fetchLogs()
    }
}

// 下一页
const nextPage = () => {
    if (currentPage.value < totalPages.value) {
        currentPage.value++
        fetchLogs()
    }
}

// 关闭弹窗
const handleClose = () => {
    emit('close')
}

// 点击遮罩关闭
const handleOverlayClick = () => {
    handleClose()
}

// 监听弹窗显示状态
watch(() => props.visible, (newVisible) => {
    if (newVisible) {
        currentPage.value = 1
        fetchLogs()
    } else {
        // 重置状态
        logs.value = []
        error.value = ''
        loading.value = false
        totalPages.value = 0
    }
})

// 组件挂载时如果弹窗可见则获取数据
onMounted(() => {
    if (props.visible) {
        fetchLogs()
    }
})
</script>

<style scoped>
.consumption-modal-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.6);
    backdrop-filter: blur(10px);
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 2000;
}

.consumption-modal-container {
    width: 800px;
    height: 800px;
    box-sizing: border-box;
    border: 1px solid rgba(255, 255, 255, 0.08);
    border-radius: 12px;
    backdrop-filter: blur(74px);
    background: rgba(198, 208, 219, 0.2);
    box-shadow: 0 20px 40px rgba(0, 0, 0, 0.3);
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

.modal-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 24px 32px;
    border-bottom: 1px solid rgba(255, 255, 255, 0.1);
}

/* Tab切换区域 */
.tabs-container {
    display: flex;
    gap: 0;
    padding: 0 32px;
    border-bottom: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(255, 255, 255, 0.02);
}

.tab-btn {
    flex: 1;
    padding: 16px 24px;
    background: transparent;
    border: none;
    color: rgba(255, 255, 255, 0.6);
    font-size: 14px;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.3s ease;
    position: relative;
}

.tab-btn:hover {
    color: rgba(255, 255, 255, 0.8);
    background: rgba(255, 255, 255, 0.05);
}

.tab-btn.active {
    color: rgba(59, 130, 246, 1);
    background: rgba(59, 130, 246, 0.1);
}

.tab-btn.active::after {
    content: '';
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    height: 2px;
    background: linear-gradient(90deg, #3B82F6 0%, #2563EB 100%);
}

.modal-title {
    color: rgba(255, 255, 255, 1);
    font-family: PingFang SC;
    font-size: 24px;
    font-weight: 400;
    line-height: 30px;
    letter-spacing: 0%;
    margin: 0;
}

.close-btn {
    width: 32px;
    height: 32px;
    border: none;
    background: none;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    color: rgba(255, 255, 255, 0.6);
    border-radius: 8px;
    transition: all 0.2s ease;
}

.close-btn:hover {
    background: rgba(255, 255, 255, 0.1);
    color: rgba(255, 255, 255, 1);
}

.modal-content {
    flex: 1;
    padding: 24px 32px;
    overflow-y: auto;
    /* 美化滚动条 */
    scrollbar-width: thin;
    scrollbar-color: rgba(255, 255, 255, 0.3) rgba(255, 255, 255, 0.1);
}

/* Webkit浏览器滚动条样式 */
.modal-content::-webkit-scrollbar {
    width: 8px;
}

.modal-content::-webkit-scrollbar-track {
    background: rgba(255, 255, 255, 0.1);
    border-radius: 4px;
}

.modal-content::-webkit-scrollbar-thumb {
    background: rgba(255, 255, 255, 0.3);
    border-radius: 4px;
    transition: background 0.2s ease;
}

.modal-content::-webkit-scrollbar-thumb:hover {
    background: rgba(255, 255, 255, 0.5);
}

/* 加载状态 */
.loading-state,
.error-state,
.empty-state {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 60px 20px;
    color: rgba(255, 255, 255, 0.8);
}

.loading-spinner {
    width: 40px;
    height: 40px;
    border: 3px solid rgba(255, 255, 255, 0.1);
    border-top: 3px solid rgba(255, 255, 255, 0.8);
    border-radius: 50%;
    animation: spin 1s linear infinite;
    margin-bottom: 16px;
}

@keyframes spin {
    0% {
        transform: rotate(0deg);
    }

    100% {
        transform: rotate(360deg);
    }
}

.error-icon,
.empty-icon {
    font-size: 48px;
    margin-bottom: 16px;
}

.retry-btn {
    margin-top: 16px;
    padding: 8px 24px;
    background: rgba(255, 255, 255, 0.1);
    color: rgba(255, 255, 255, 0.8);
    border: 1px solid rgba(255, 255, 255, 0.2);
    border-radius: 6px;
    cursor: pointer;
    font-size: 14px;
    transition: all 0.2s ease;
}

.retry-btn:hover {
    background: rgba(255, 255, 255, 0.2);
    border-color: rgba(255, 255, 255, 0.4);
}

/* 记录列表 */
.logs-list {
    display: flex;
    flex-direction: column;
    gap: 16px;
}

.log-item {
    background: rgba(255, 255, 255, 0.05);
    border: 1px solid rgba(255, 255, 255, 0.1);
    border-radius: 12px;
    padding: 20px;
    transition: all 0.2s ease;
}

.log-item:hover {
    background: rgba(255, 255, 255, 0.08);
    border-color: rgba(255, 255, 255, 0.2);
}

.log-main {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
}

.log-left {
    flex: 1;
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.task-info {
    display: flex;
    gap: 16px;
    font-size: 13px;
    color: rgba(255, 255, 255, 0.6);
}

.task-no {
    font-family: monospace;
}

.change-type {
    background: rgba(239, 68, 68, 0.2);
    color: rgba(239, 68, 68, 1);
    padding: 2px 8px;
    border-radius: 8px;
    font-size: 12px;
    font-weight: 500;
}

.description {
    font-size: 16px;
    font-weight: 500;
    color: rgba(255, 255, 255, 1);
    margin: 4px 0;
    text-align: left;
}

.time-info {
    font-size: 13px;
    color: rgba(255, 255, 255, 0.6);
    text-align: left;
}

.log-right {
    display: flex;
    flex-direction: column;
    align-items: flex-end;
    gap: 8px;
}

.amount {
    font-size: 20px;
    font-weight: 600;
}

.amount-decrease {
    color: rgba(239, 68, 68, 1);
}

.amount-increase {
    color: rgba(16, 185, 129, 1);
}

.balance-info {
    display: flex;
    flex-direction: column;
    align-items: flex-end;
    gap: 4px;
    font-size: 12px;
    color: rgba(255, 255, 255, 0.6);
}

.balance-before {
    color: rgba(255, 255, 255, 0.5);
}

.balance-after {
    color: rgba(16, 185, 129, 1);
    font-weight: 500;
}

/* 分页 */
.pagination {
    display: flex;
    justify-content: center;
    align-items: center;
    gap: 16px;
    padding: 20px 32px;
    border-top: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(198, 208, 219, 0.1);
    backdrop-filter: blur(10px);
}

.page-btn {
    padding: 8px 16px;
    background: rgba(255, 255, 255, 0.1);
    color: rgba(255, 255, 255, 0.8);
    border: 1px solid rgba(255, 255, 255, 0.2);
    border-radius: 6px;
    cursor: pointer;
    font-size: 14px;
    transition: all 0.2s ease;
}

.page-btn:hover:not(:disabled) {
    background: rgba(255, 255, 255, 0.2);
    border-color: rgba(255, 255, 255, 0.4);
}

.page-btn:disabled {
    opacity: 0.4;
    cursor: not-allowed;
}

.page-info {
    color: rgba(255, 255, 255, 0.8);
    font-size: 14px;
}

/* 移动端适配 */
@media (max-width: 768px) {
    .consumption-modal-container {
        width: 95vw;
        height: 90vh;
    }

    .modal-header,
    .modal-content {
        padding: 20px;
    }

    .log-main {
        flex-direction: column;
        gap: 16px;
    }

    .log-right {
        align-items: flex-start;
    }

    .task-info {
        flex-direction: column;
        gap: 4px;
    }
}
</style>
