<template>
  <div class="common-layout">
    <el-container>
        <el-header>
            <div class="header-container">
            <div class="back" @click="back">&lt;&nbsp;返回</div>
            <div class="left">
                <div class="date">
                    <div>{{ currentTime }}</div>
                    <div class="week">{{ weekText }}</div>
                </div>
                <div class="binary-strip">{{ binaryText }}</div>
            </div>
            <div class="application-title">两江鱼复智能网联新能源汽车产业基地上位机</div>
            <div class="right">
                <div class="binary-strip">{{ binaryText }}</div>
                <div class="btn-container">
                    <button class="nav-btn"  :class="{ active: activePage === 'chassis' }" @click="handleBtn('chassis')">底盘控制</button>
                    <button class="nav-btn"  :class="{ active: activePage === 'mechanicalArm' }" @click="handleBtn('mechanicalArm')">机械臂控制</button>
                    <button class="nav-btn"  :class="{ active: activePage === 'camera' }" @click="handleBtn('camera')">相机</button>
                </div>
            </div>
        </div>
      </el-header>
      <el-main>
        <Chassis v-if="activePage === 'chassis'" />
        <MechanicalArm v-if="activePage === 'mechanicalArm'"  />
        <Camera v-show="activePage === 'camera'" />
      </el-main>
    </el-container>
  </div>
</template>
<script setup>
import Chassis from './chassis/Chassis.vue';
import MechanicalArm from './arm/MechanicalArm.vue';
import Camera from './camera/index.vue'
import { useRouter } from 'vue-router';

import autofit from 'autofit.js'
autofit.init()

const router = useRouter()

const currentTime = ref('')
const weekText = ref('')

const binaryText = ref('13513034532438213')

const weekNames = ['星期日','星期一','星期二','星期三','星期四','星期五','星期六'];
const updateClock = () => {
    const now = new Date();
    const y = now.getFullYear();
    const mo = String(now.getMonth()+1).padStart(2,'0');
    const d = String(now.getDate()).padStart(2,'0');
    const h = String(now.getHours()).padStart(2,'0');
    const mi = String(now.getMinutes()).padStart(2,'0');
    const s = String(now.getSeconds()).padStart(2,'0');
    currentTime.value = `${y}-${mo}-${d} ${h}:${mi}:${s}`
    weekText.value = weekNames[now.getDay()]
}

const genBinary = (len) => {
    let str = '';
    for (let i = 0; i < len; i++) str += Math.round(Math.random()).toString();
    return str;
}

const animateBinary = () => {
    binaryText.value = genBinary(30)
}

const activePage = ref('chassis')
const handleBtn = (page) => {
    activePage.value = page
}

const back = () => {
    router.back()
}

let clockTimer, binaryTimer;
onMounted(() => {
    updateClock();
    clockTimer = setInterval(updateClock, 1000);
    animateBinary()
    binaryTimer = setInterval(animateBinary, 400);
})

onUnmounted(() => {
    clearInterval(clockTimer)
    clearInterval(binaryTimer)
})
</script>

<style lang="scss" scoped>
.common-layout {
    width: 100%;
    height: 100%;
    background: $color-background-base;
    color: $color-text-primary;

    .el-container {
        width: 100%;
        height: 100%;
        background: radial-gradient(circle at 50% -20%, $color-accent-radial-glow, transparent 38%), $color-background-base;
        
        .el-header {
            background: $color-background-header-translucent;
            border-bottom: 1px solid $color-accent-border-header;
            box-shadow: 0 6px 24px $color-shadow-header;
        }

        .header-container {
            height: 100%;
            display: flex;
            gap: 20px;
            margin: 0 20px;
            
            .back {
                display: flex;
                align-items: center;
                justify-content: center;
                color: $color-accent-secondary;
                cursor: pointer;
            }

            .left { 
                flex: 1;
                display: flex;
                justify-content: space-evenly;

                .date {
                    display: flex;
                    align-items: center;
                    justify-content: center;
                    gap: 20px;
                    color: $color-text-accent-soft;
                    font-size: 16px;
                    font-weight: 600;
                    letter-spacing: 2px;

                    .week {
                        font-size: 18px;
                    }
                }
            }

            .application-title { 
                flex: 1;
                display: flex;
                align-items: center;
                justify-content: center;
                font-size: 26px;
                font-weight: 700;
                text-align: center;
                color: $color-text-accent-bright;
                text-shadow: 0 0 18px $color-accent-border-strong;
                letter-spacing: 4px;
                white-space: nowrap;
            }

            .right {
                flex: 1;
                display: flex;
                justify-content: space-evenly;

                .btn-container {
                    display: flex;
                    align-items: center;
                    justify-content: space-between;
                    gap: 12px ;

                    .nav-btn {
                        background: $color-accent-subtle;
                        border: 1px solid $color-accent-border-muted;
                        color: $color-text-navigation;
                        padding: 6px 20px;
                        border-radius: 3px;
                        font-size: 14px;
                        cursor: pointer;
                        transition: all 0.2s;
                        white-space: nowrap;

                        &:hover {
                            border-color: $color-accent-border-hover;
                            color: $color-text-accent-bright;
                            background: $color-accent-medium;
                        }
                    }

                    .active {
                        background: $color-accent-selected;
                        border-color: $color-accent-border-active;
                        color: $color-text-navigation-active;
                        box-shadow: 0 0 14px $color-accent-glow, inset 0 0 12px $color-accent-inset-glow;
                    }
                }
            }

            .binary-strip {
                display: flex;
                align-items: center;
                justify-content: center;
                font-family: 'Orbitron', monospace;
                font-size: 14px;
                color: $color-accent-text-muted;
                letter-spacing: 2px;
                white-space: nowrap;
                overflow: hidden;
            }
        }

        .el-main {
            padding: 0;
        }
    }
}

</style>
