<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%;

    .el-container {
        width: 100%;
        height: 100%;
        background-color: #000d2e;
        
        .el-header {
            border-bottom: 1px solid #00c2ff4d;
        }

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

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

                .date {
                    display: flex;
                    align-items: center;
                    justify-content: center;
                    gap: 20px;
                    color: #00c2ff;
                    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: #00c2ff;
                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: #003c8c80;
                        border: 1px solid rgba(0, 194, 255, 0.35);
                        color: #a0d8f0;
                        padding: 6px 20px;
                        border-radius: 3px;
                        font-size: 14px;
                        cursor: pointer;
                        transition: all 0.2s;
                        white-space: nowrap;
                    }

                    .active {
                        background: rgba(0, 120, 220, 0.7);
                        border-color: #00c2ff;
                        color: #fff;
                        box-shadow: 0 0 10px rgba(0, 194, 255, 0.5);
                    }
                }
            }

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

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

</style>