<template>
    <div class="dashboard" v-if="userId === 1">
        <div class="page-header">
            <h1>数据概览</h1>
            <span class="date">{{ formattedDate }}</span>
        </div>

        <div class="stats-wrapper">
            <div class="stats-grid">
                <div 
                    v-for="(item, index) in cardData" 
                    :key="index" 
                    class="stat-box"
                    :class="`stat-box--${index}`"
                >
                    <div class="stat-box__icon">
                        <el-icon :size="28">
                            <component :is="item.icon" />
                        </el-icon>
                    </div>
                    <div class="stat-box__info">
                        <span class="stat-box__value">
                            <AnimatedNumber :value="Number(item.value) || 0" :decimals="0" />
                        </span>
                        <span class="stat-box__label">{{ item.label }}</span>
                    </div>
                </div>
            </div>
        </div>

        <div class="content-area">
            <div class="info-panel">
                <div class="panel-header">
                    <el-icon><User /></el-icon>
                    <span>当前用户</span>
                </div>
                <div class="panel-content">
                    <div class="user-info">
                        <div class="user-avatar">
                            <el-icon :size="32"><UserFilled /></el-icon>
                        </div>
                        <div class="user-details">
                            <span class="user-name">{{ userStore.userInfo?.username || '-' }}</span>
                            <!-- <el-tag size="small" :type="+userStore.userInfo.role_id ===  1 ? 'danger' : 'primary'">
                                {{ userStore.userInfo.role }}
                            </el-tag> -->
                        </div>
                    </div>
                    <div class="info-row">
                        <span class="info-label">用户ID</span>
                        <span class="info-value">{{ userStore.userInfo?.id || '-' }}</span>
                    </div>
                    <div class="info-row">
                        <span class="info-label">最后登录</span>
                        <span class="info-value">{{ userStore.userInfo?.last_login || '-'  }}</span>
                    </div>
                </div>
            </div>

            <div class="info-panel">
                <div class="panel-header">
                    <el-icon><Operation /></el-icon>
                    <span>快捷操作</span>
                </div>
                <div class="panel-content">
                    <div class="action-list">
                        <router-link 
                            v-for="action in quickActions" 
                            :key="action.name"
                            :to="action.path"
                            class="action-item"
                        >
                            <el-icon :size="18">
                                <component :is="action.icon" />
                            </el-icon>
                            <span>{{ action.name }}</span>
                            <el-icon class="arrow"><Right /></el-icon>
                        </router-link>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <Agent v-else />
</template>

<script setup lang="ts">
import { onMounted, ref, computed } from 'vue'
import { UserFilled, User, TrendCharts, Money, Wallet, Coin, Timer, Operation, Right, List, Goods, Document, Setting } from '@element-plus/icons-vue'
import Agent from './components/view/index.vue'
import AnimatedNumber from '@/views/agent/user/AnimatedNumber.vue'
import { getUserIndex } from '@/api/modules/dashboard'
import { useUserStore } from '@/stores/modules/user'

defineOptions({
  name: 'Dashboard'
})

const userStore = useUserStore()
const userId = userStore.userInfo?.id

const loginTime = ref('--')

const formattedDate = computed(() => {
    const now = new Date()
    return `${now.getFullYear()}/${String(now.getMonth() + 1).padStart(2, '0')}/${String(now.getDate()).padStart(2, '0')}`
})

const cardData = ref([
    { label: '代理总数', value: 0, icon: UserFilled },
    { label: '用户总数', value: 0, icon: User },
    { label: '今日新增用户', value: 0, icon: TrendCharts },
    { label: '今日充值金额', value: '0', icon: Money },
    { label: '今日提现金额', value: 0, icon: Wallet },
    { label: '总充值金额', value: 0, icon: Coin },
    { label: '总提现金额', value: 0, icon: Timer }
])

const quickActions = ref<{ name: string; path: string; icon: any }[]>([])

const iconMap: Record<string, any> = {
    user: User,
    list: List,
    goods: Goods,
    document: Document,
    setting: Setting,
    money: Money,
    trendcharts: TrendCharts,
    wallet: Wallet,
    coin: Coin,
    timer: Timer
}

const initQuickActions = () => {
    const actions: { name: string; path: string; icon: any }[] = []
    const menus = userStore.menusInfo || []
    
    let count = 0
    for (const menu of menus) {
        if (count >= 4) break
        
        if (menu.son && menu.son.length > 0) {
            for (const child of menu.son) {
                if (count >= 4) break
                if (child.path === '/dashboard') continue
                actions.push({
                    name: child.name || menu.name,
                    path: child.path || '/',
                    icon: iconMap[child.icon || ''] || List
                })
                count++
            }
        } else {
            if (menu.path === '/dashboard') continue
            actions.push({
                name: menu.name,
                path: menu.path || '/',
                icon: iconMap[menu.icon || ''] || List
            })
            count++
        }
    }
    
    quickActions.value = actions.slice(0, 4)
}

const fetchDashboardData = async () => {
    try {
        const res = await getUserIndex()
        if (res) {
            const { agent_count, user_count, today_user_count, today_recharge_amount,
                    today_withdraw_amount, total_recharge_amount, total_withdraw_amount } = res
            
            cardData.value = [
                { label: '代理总数', value: agent_count, icon: UserFilled },
                { label: '用户总数', value: user_count, icon: User },
                { label: '今日新增用户', value: today_user_count, icon: TrendCharts },
                { label: '今日充值金额', value: today_recharge_amount, icon: Money },
                { label: '今日提现金额', value: today_withdraw_amount, icon: Wallet },
                { label: '总充值金额', value: total_recharge_amount, icon: Coin },
                { label: '总提现金额', value: total_withdraw_amount, icon: Timer }
            ]
        }
    } catch (error) {
        console.error('获取首页数据失败:', error)
    }
}

onMounted(() => {
    loginTime.value = new Date().toLocaleTimeString('zh-CN', { hour: '2-digit', minute: '2-digit' })
    initQuickActions()
    if (userId === 1) {
        fetchDashboardData()
    }
})
</script>

<style scoped lang="scss">
.dashboard {
    padding: 20px;
    max-width: 1600px;
    margin: 0 auto;
}

.page-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 24px;
    padding-bottom: 16px;
    border-bottom: 1px solid var(--el-border-color-light);

    h1 {
        font-size: 22px;
        font-weight: 600;
        color: var(--el-text-color-primary);
        margin: 0;
    }

    .date {
        font-size: 14px;
        color: var(--el-text-color-secondary);
    }
}

.stats-wrapper {
    margin-bottom: 24px;
}

.stats-grid {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 20px;
    
    @media (max-width: 1200px) {
        grid-template-columns: repeat(3, 1fr);
    }
    
    @media (max-width: 900px) {
        grid-template-columns: repeat(2, 1fr);
    }
    
    @media (max-width: 600px) {
        grid-template-columns: 1fr;
    }
}

.stat-box {
    display: flex;
    align-items: center;
    gap: 16px;
    padding: 20px 24px;
    background: #fff;
    border-radius: 12px;
    border: 1px solid var(--el-border-color-lighter);
    transition: all 0.25s ease;

    &:hover {
        border-color: var(--el-color-primary-light-5);
        box-shadow: 0 4px 16px rgba(0, 0, 0, 0.06);
        transform: translateY(-2px);
    }

    &__icon {
        width: 52px;
        height: 52px;
        display: flex;
        align-items: center;
        justify-content: center;
        border-radius: 12px;
        flex-shrink: 0;
    }

    &__info {
        display: flex;
        flex-direction: column;
        min-width: 0;
        overflow: hidden;
    }

    &__value {
        font-size: 24px;
        font-weight: 700;
        color: var(--el-text-color-primary);
        line-height: 1.2;
        max-width: 100%;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
    }

    &__label {
        font-size: 14px;
        color: var(--el-text-color-secondary);
        margin-top: 6px;
    }
}

.stat-box--0 .stat-box__icon { background: linear-gradient(135deg, #ecf5ff, #d9ecff); color: #409eff; }
.stat-box--1 .stat-box__icon { background: linear-gradient(135deg, #f0f9eb, #e1f3d8); color: #67c23a; }
.stat-box--2 .stat-box__icon { background: linear-gradient(135deg, #fdf6ec, #fbeccb); color: #e6a23c; }
.stat-box--3 .stat-box__icon { background: linear-gradient(135deg, #fef0f0, #fad7d7); color: #f56c6c; }
.stat-box--4 .stat-box__icon { background: linear-gradient(135deg, #f4f4f5, #e4e4e7); color: #909399; }
.stat-box--5 .stat-box__icon { background: linear-gradient(135deg, #fdf5f6, #f9d7e3); color: #c71585; }
.stat-box--6 .stat-box__icon { background: linear-gradient(135deg, #f0f9ff, #d6ecff); color: #0089e5; }

.content-area {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 20px;
    
    @media (max-width: 768px) {
        grid-template-columns: 1fr;
    }
}

.info-panel {
    background: #fff;
    border-radius: 12px;
    border: 1px solid var(--el-border-color-lighter);
    overflow: hidden;
}

.panel-header {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 16px 20px;
    background: var(--el-fill-color-light);
    border-bottom: 1px solid var(--el-border-color-lighter);
    font-size: 15px;
    font-weight: 600;
    color: var(--el-text-color-primary);

    .el-icon {
        color: var(--el-color-primary);
    }
}

.panel-content {
    padding: 20px;
}

.user-info {
    display: flex;
    align-items: center;
    gap: 16px;
    padding-bottom: 16px;
    margin-bottom: 16px;
    border-bottom: 1px dashed var(--el-border-color-lighter);

    .user-avatar {
        width: 52px;
        height: 52px;
        display: flex;
        align-items: center;
        justify-content: center;
        background: linear-gradient(135deg, var(--el-color-primary-light-9), var(--el-color-primary-light-7));
        border-radius: 50%;
        color: var(--el-color-primary);
    }

    .user-details {
        display: flex;
        flex-direction: column;
        gap: 6px;

        .user-name {
            font-size: 16px;
            font-weight: 600;
            color: var(--el-text-color-primary);
        }
    }
}

.info-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 12px 0;
    border-bottom: 1px dashed var(--el-border-color-lighter);

    &:last-child {
        border-bottom: none;
    }

    .info-label {
        font-size: 14px;
        color: var(--el-text-color-secondary);
    }

    .info-value {
        font-size: 14px;
        font-weight: 600;
        color: var(--el-text-color-primary);
    }
}

.action-list {
    display: flex;
    flex-direction: column;
    gap: 10px;
}

.action-item {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 12px 14px;
    background: var(--el-fill-color-light);
    border-radius: 10px;
    font-size: 14px;
    color: var(--el-text-color-regular);
    cursor: pointer;
    transition: all 0.2s ease;
    text-decoration: none;

    &:hover {
        background: var(--el-color-primary-light-9);
        color: var(--el-color-primary);
        
        .arrow {
            opacity: 1;
        }
    }

    .el-icon {
        color: var(--el-color-primary);
        flex-shrink: 0;
    }

    .arrow {
        margin-left: auto;
        font-size: 12px;
        opacity: 0.4;
        transition: opacity 0.2s ease;
    }
}
</style>
