<template>
    <div class="smart-control-app">
        <!-- 主要内容 -->
        <div class="main-content">
            <!-- 时间显示 -->
            <!-- <div class="time-display">
                {{ currentTime }}
            </div> -->
            <div class="logo">
                <img src="@/assets/images/绿盟.png" alt="">
            </div>

            <!-- 主标题 -->
            <div class="main-title">
                <div class="title-decoration left"></div>
                <h1>智能中控装置</h1>
                <div class="title-decoration right"></div>
            </div>

            <!-- 功能卡片 -->
            <div class="function-cards">
                <el-card v-for="(card, index) in functionCards" :key="index" class="function-card" @click="handleCardClick(card.key)">
                    <div class="card-content">
                        <div class="card-icon">
                            <component :is="card.icon" />
                        </div>
                        <div class="card-title">{{ card.title }}</div>
                    </div>
                </el-card>
            </div>

            <!-- 底部状态栏 -->
            <div class="status-bar">
                <div class="status-item">
                    设备号：{{ commit_uid }}
                </div>
                <div class="status-item">
                    版本号：{{ version }}
                </div>
                <div class="status-item">
                    系统状态：<span class="status-online">开启</span>
                </div>
            </div>
        </div>
    </div>
</template>

<script setup>
import { ref, onMounted, onUnmounted } from 'vue'
import {
    Camera,
    Operation,
    Setting,
    VideoPlay,
    Bell,
    Tools
} from '@element-plus/icons-vue'
import RPC from '@/utils/rpc'
const currentTime = ref('')
const router = useRouter()
const route = useRoute()
// console.log(route.query.comm_uid)
if (route.query.comm_uid) {
    sessionStorage.setItem('central_control_comm_uid', route.query.comm_uid)
}
const version = ref('1.0.0')
const commit_uid = ref('')
RPC.post('/m9z/touchdisplay/getDeviceID').then(res => {
    commit_uid.value = res.deviceId
    version.value = res.version
    sessionStorage.setItem('central_control_comm_uid', res.deviceId)
    sessionStorage.setItem('central_control_meterAddr', res.meterAddr)
})

const functionCards = [
    { key: 'monitor', title: '实时监控', icon: Camera, route: '/monitoring' },
    { key: 'control', title: '手动控制', icon: Operation, route: '/handle-control' },
    { key: 'setting', title: '参数设置', icon: Setting, route: '/params-config' },
    { key: 'auto', title: '自动模式', icon: VideoPlay, route: '/automatic-mode' },
    { key: 'alarm', title: '报警浏览', icon: Bell, route: '/alarm-information' },
    { key: 'system', title: '系统设置', icon: Tools, route: '/system-settings' },
]

const updateTime = () => {
    const now = new Date()
    const year = now.getFullYear()
    const month = String(now.getMonth() + 1).padStart(2, '0')
    const day = String(now.getDate()).padStart(2, '0')
    const hours = String(now.getHours()).padStart(2, '0')
    const minutes = String(now.getMinutes()).padStart(2, '0')
    const seconds = String(now.getSeconds()).padStart(2, '0')

    currentTime.value = `${year}-${month}-${day}  ${hours}:${minutes}:${seconds}`
}

const handleCardClick = (key) => {
    const card = functionCards.find(c => c.key === key)
    if (card) {
        console.log(`点击了${card.title}功能`)
        if (card.route) {
            router.push(card.route)
        } else {
            ElMessage.info(`${card.title}功能开发中...`)
        }
    }
}

let timeInterval = null

// 处理 session 恢复后的重新加载
const handleSessionRestored = () => {
    console.log('Session 已恢复，重新加载数据')
    // Menu 页面重新获取设备信息
    RPC.post('/m9z/touchdisplay/getDeviceID').then(res => {
        commit_uid.value = res.deviceId
        version.value = res.version
        sessionStorage.setItem('central_control_comm_uid', res.deviceId)
        sessionStorage.setItem('central_control_meterAddr', res.meterAddr)
    })
}

onMounted(() => {
    updateTime()
    timeInterval = setInterval(updateTime, 1000)
    
    // 监听 session 恢复事件
    window.addEventListener('m9z:session-restored', handleSessionRestored)
})

onUnmounted(() => {
    if (timeInterval) {
        clearInterval(timeInterval)
    }
    
    // 移除事件监听
    window.removeEventListener('m9z:session-restored', handleSessionRestored)
})
</script>

<style scoped lang="scss">
.smart-control-app {
    width: 100vw;
    height: 100vh;
    position: relative;
    overflow: hidden;
    // background: center / cover url('@/assets/images/首页背景@2x.png') no-repeat;
    background-color: #008000;
}

.main-content {
    position: relative;
    z-index: 2;
    width: 100%;
    height: 100%;
    display: flex;
    flex-direction: column;
    padding: 40px;
    box-sizing: border-box;
    justify-content: center;

    .logo {
        position: absolute;
        top: 20px;
        left: 20px;

        img {
            height: 30px;
        }
    }
}

.time-display {
    color: rgba(255, 255, 255, 0.9);
    font-size: 24px;
    font-weight: 300;
    letter-spacing: 1px;
    position: absolute;
    top: 20px;
}

.main-title {
    display: flex;
    align-items: center;
    justify-content: center;
    margin-bottom: 80px;
    position: relative;
}

.main-title h1 {
    color: #ffffff;
    font-size: 64px;
    font-weight: bold;
    margin: 0 40px;
    text-shadow: 0 0 20px rgba(255, 255, 255, 0.5);
    letter-spacing: 4px;
}

.title-decoration {
    width: 80px;
    height: 6px;
    background: linear-gradient(90deg, transparent, #64f6be, #42f5a4, #54e98d, transparent);
    position: relative;
    animation: titleGlow 2s ease-in-out infinite alternate;
}

.title-decoration::before {
    content: '';
    position: absolute;
    top: -3px;
    left: 50%;
    transform: translateX(-50%);
    width: 12px;
    height: 12px;
    background: linear-gradient(90deg, transparent, #64f6be, #42f5a4, #54e98d, transparent);
    border-radius: 50%;
    box-shadow:
        0 0 15px #64b5f6,
        0 0 30px rgba(100, 181, 246, 0.5);
    animation: centerPulse 2s ease-in-out infinite alternate;
}

@keyframes titleGlow {
    0% {
        opacity: 0.8;
        transform: scaleX(1);
    }

    100% {
        opacity: 1;
        transform: scaleX(1.1);
    }
}

@keyframes centerPulse {
    0% {
        transform: translateX(-50%) scale(1);
        opacity: 0.8;
    }

    100% {
        transform: translateX(-50%) scale(1.2);
        opacity: 1;
    }
}

.function-cards {
    display: flex;
    justify-content: center;
    gap: 40px;
}

.function-card {
    width: 15%;
    height: 200px;
    box-sizing: border-box;
    background:
        linear-gradient(145deg, rgba(255, 255, 255, 0.15) 0%, rgba(255, 255, 255, 0.05) 100%),
        rgba(33, 150, 243, 0.1);
    border: 1px solid rgba(100, 181, 246, 0.3);
    border-radius: 16px;
    cursor: pointer;
    transition: all 0.4s cubic-bezier(0.4, 0, 0.2, 1);
    backdrop-filter: blur(15px);
    box-shadow:
        0 8px 32px rgba(0, 0, 0, 0.3),
        inset 0 1px 0 rgba(255, 255, 255, 0.2);
    position: relative;
    overflow: hidden;
}

/* .function-card::before {
    content: '';
    position: absolute;
    top: 0;
    left: -100%;
    width: 100%;
    height: 100%;
    background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.1), transparent);
    transition: left 0.6s ease;
}

.function-card:hover::before {
    left: 100%;
} */

/* .function-card:hover {
    background:
        linear-gradient(145deg, rgba(255, 255, 255, 0.2) 0%, rgba(255, 255, 255, 0.1) 100%),
        rgba(33, 150, 243, 0.15);
    border-color: rgba(100, 181, 246, 0.6);
    transform: translateY(-8px) scale(1.02);
    box-shadow:
        0 20px 40px rgba(33, 150, 243, 0.4),
        0 0 20px rgba(100, 181, 246, 0.3),
        inset 0 1px 0 rgba(255, 255, 255, 0.3);
} */

.card-content {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    height: 100%;
    padding: 20px;
    box-sizing: border-box;
}

.card-icon {
    width: 60px;
    height: 60px;
    font-size: 48px;
    color: rgba(255, 255, 255, 0.9);
    margin-bottom: 20px;
}

.card-title {
    color: rgba(255, 255, 255, 0.9);
    font-size: 18px;
    font-weight: 500;
    text-align: center;
    letter-spacing: 1px;
}

.status-bar {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-top: auto;
    padding: 10px 30px;
    box-sizing: border-box;
    color: rgba(255, 255, 255, 0.7);
    font-size: 16px;
    background-color: rgba(0, 0, 0, 0.3);
    position: fixed;
    bottom: 0;
    left: 0;
    width: 100vw;
}

.status-item {
    display: flex;
    align-items: center;
    gap: 10px;
}

.status-online {
    color: #4caf50;
    font-weight: bold;
}

/* Element Plus 卡片样式重写 */
:deep(.el-card) {
    background: transparent !important;
    border: none !important;
}

:deep(.el-card__body) {
    padding: 0 !important;
    height: 100%;
}
</style>
