<template>
    <div class="labs">
        <el-tooltip :content="$t('message.labs')">
            <div class="el-dropdown-link" :title="$t('message.labs')" @click="openLabs">
                <img :src="imgIcon" alt="" class="header_right_icon left">
                <span class="header_right_title_span">{{ $t('message.labs') }}</span>
            </div>
        </el-tooltip>
        <el-dialog v-model="dialogVisible" title="" :show-close="false">
            <template #header="{ close }">
                <div class="my-header">
                    <span class="labs_title">{{ $t('message.labs') }}</span>
                    <i class="material-icons right" style="margin-top: 5px;cursor: pointer;margin-right:-15px"
                        @click="labsClose">close</i>
                </div>
            </template>
            <el-row v-if="activeContent == 0" class="labs_body">
                <el-col class="labs_class_content" :span="6">
                    <p class="all_class">{{ $t('message.all_categorization') }}</p>
                    <ul class="labs_class_left">
                        <li :class="calssActive == 1 ? 'active' : ''" @click="getClassData(1, $t('message.AI'))">{{
                            $t('message.AI')
                            }}</li>
                        <li :class="calssActive == 2 ? 'active' : ''" @click="getClassData(2, $t('message.IoT'))">{{
                            $t('message.IoT') }}</li>
                        <li :class="calssActive == 3 ? 'active' : ''"
                            @click="getClassData(3, $t('message.robot_control'))">{{
                                $t('message.robot_control') }}</li>
                        <li :class="calssActive == 4 ? 'active' : ''" @click="getClassData(4, '科学实验')">科学实验</li>
                        <li :class="calssActive == 5 ? 'active' : ''"
                            @click="getClassData(5, $t('message.scientific_experiment'))">
                            {{ $t('message.scientific_experiment') }}</li>
                        <li :class="calssActive == 6 ? 'active' : ''" @click="getClassData(6, 'AR/VR')">AR/VR</li>
                    </ul>
                </el-col>
                <el-col :span="18" style="padding:0 0.75rem;">
                    <h5>{{ titleNmae }}</h5>
                    <ul class="labs_class_right">
                        <li v-if="calssActive == 1">
                            <AI @getNextValueAI="getNextValueAI"></AI>
                        </li>
                        <li v-else-if="calssActive == 2">
                            <Iot></Iot>
                        </li>
                        <li v-else-if="calssActive == 3">
                            <RobotContro></RobotContro>
                        </li>
                        <li v-else-if="calssActive == 4">
                            <Science></Science>
                        </li>
                        <li v-else-if="calssActive == 5">
                            <interactiveGames></interactiveGames>
                        </li>
                        <li v-else>
                            <ARVR></ARVR>
                        </li>
                    </ul>
                </el-col>
            </el-row>
            <el-row :gutter="20" v-else-if="activeContent == 1" class="activeContent">
                <el-col :span="12" style="background-color: #fff;box-shadow: 1px 2px 3px #eeeeee;">
                    <img style="width: 100%;" :src="disposition.img" alt="">
                    <h3>{{ disposition.title }}</h3>
                    <p>{{ disposition.content }}</p>
                </el-col>
                <el-col :span="12">
                    <div class="labs_card_container">
                        <h4>{{ $t('message.system_configure') }}</h4>
                        <p>{{ $t('message.system') }}{{ disposition.system }}</p>
                        <p>{{ $t('message.memory') }}{{ disposition.memory }}</p>
                        <p>{{ $t('message.HardDisk') }}{{ disposition.HardDisk }}</p>
                        <p>{{ $t('message.GraphicsCard') }}{{ disposition.GraphicsCard }}</p>
                        <p>{{ $t('message.camera') }}{{ disposition.camera ? $t('message.need') : $t('message.not_need')
                            }}</p>
                        <p>{{ $t('message.microphone') }}{{ disposition.microphone ? $t('message.need') :
                            $t('message.not_need') }}
                        </p>
                    </div>
                    <el-button @click="goNext()">{{ $t('message.enter') }}</el-button>
                </el-col>
            </el-row>
        </el-dialog>
        <el-dialog v-model="iframVisible" :width="disposition.iframeSrc ? '95%' : ''" :show-close="false">
            <template #header="{ close }">
                <span v-if="disposition.iframeSrc">
                    <i class="material-icons right" style="margin-top: -15px;cursor: pointer;margin-right:0px"
                        @click="close">close</i>
                </span>
                <div class="my-header" v-else>
                    <span class="labs_title">{{ disposition.title }}</span>
                    <i class="material-icons right" style="margin-top: 5px;cursor: pointer;margin-right:-15px"
                        @click="labsClose">close</i>
                </div>
            </template>
            <iframe id="postMessage" :src="disposition.iframeSrc" ref="labsData" frameborder="0"
                style="width: 100%;height:700px;"></iframe>
            <div class="model-loading" v-if="loading"
                style="width: 100%;height:100%;background-color: rgba(255, 255, 255, 0.9);">
                <img src="../../assets/img/preloader.gif" alt="">
            </div>
        </el-dialog>
    </div>
</template>
<script setup>
import { ref } from 'vue';
import imgIcon from '../../assets/img/cocoblockly-navbar_labs-icon.png'
import AI from '../labs/ai.vue'
import Iot from '../labs/iot.vue'
import RobotContro from '../labs/robotContro.vue';
import Science from '../labs/science.vue'
import interactiveGames from '../labs/interactiveGames.vue'
import ARVR from '../labs/ARVR.vue'
import deepLearningImg from '../../assets/img/AppCenter_ai_teachableMachine.jpg'
import emotionImg from '../../assets/img/AppCenter_ai_emotion.jpg'
import gestureImg from '../../assets/img/AppCenter_ai_gesture_recognition.jpg'
import objectImg from '../../assets/img/object_recognition_header.jpg'
import postureImg from '../../assets/img/posenet_recogition.png'
import discerningEyeImg from '../../assets/img/tm_with_pose.jpg'
import speechImg from '../../assets/img/AppCenter_ai_speech.jpg'
import useUserinfo from '@/stores/modules/userInfo';
import { ElMessage } from 'element-plus'
import { useI18n } from 'vue-i18n'
const { t } = useI18n() // 解构出t方法

const domainName = location.href.indexOf('hk') > 0 ? true : false
const dialogVisible = ref(false)
const calssActive = ref(1)
const titleNmae = ref(t('message.AI'))
const activeContent = ref(0)
const dispositionList = [
    {
        title: t('message.Deep_learning_Machines'),
        content: t('message.Deep_learning_Machines_content'),
        img: deepLearningImg,
        system: "Windows (>Windows7), macOS (>10.11)",
        resolution: "1440*768",
        memoryCapacity: "8GB-16GB",
        HardDisk: "256GB-512GB",
        GraphicsCard: "650M",
        camera: true,
        microphone: false,
        iframeSrc: domainName ? "https://ai-demos.cocorobo.hk/teachable-machine-2/public/" : "https://ai-demos.cocorobo.cn/teachable-machine-2/public/"
    },
    {
        title: t('message.emotion_recognition'),
        content: t('message.emotion_recognition_content'),
        img: emotionImg,
        system: "Windows (>Windows7), macOS (>10.11)",
        resolution: "1440*768",
        memoryCapacity: "8GB-16GB",
        HardDisk: "256GB-512GB",
        GraphicsCard: "650M",
        camera: true,
        microphone: false,
        iframeSrc: domainName ? "https://beta.ai-demos.cocorobo.hk/labs/emotion-cloud/index.html" : "https://beta.ai-demos.cocorobo.cn/labs/emotion-cloud/index.html"
    },
    {
        title: t('message.gesture_recognition'),
        content: t('message.gesture_recognition_content'),
        img: gestureImg,
        system: "Windows (>Windows7), macOS (>10.11)",
        resolution: "1440*768",
        memoryCapacity: "8GB-16GB",
        HardDisk: "256GB-512GB",
        GraphicsCard: "650M",
        camera: true,
        microphone: false,
        iframeSrc: domainName ? "https://beta.ai-demos.cocorobo.hk/labs/gesture-cloud/index.html" : "https://beta.ai-demos.cocorobo.cn/labs/gesture-cloud/index.html"
    },
    {
        title: t('message.object_recognition'),
        content: t('message.object_recognition_content'),
        img: objectImg,
        system: "Windows (>Windows7), macOS (>10.11)",
        resolution: "1440*768",
        memoryCapacity: "8GB-16GB",
        HardDisk: "256GB-512GB",
        GraphicsCard: "650M",
        camera: true,
        microphone: false,
        iframeSrc: domainName ? "https://ai-demos.cocorobo.hk/object-detection-2/" : "https://ai-demos.cocorobo.cn/object-detection-2/"
    },
    {
        title: t('message.pose_recognition'),
        content: t('message.pose_recognition_content'),
        img: postureImg,
        system: "Windows (>Windows7), macOS (>10.11)",
        resolution: "1440*768",
        memoryCapacity: "8GB-16GB",
        HardDisk: "256GB-512GB",
        GraphicsCard: "650M",
        camera: true,
        microphone: false,
        iframeSrc: domainName ? "https://ai-demos.cocorobo.hk/posenet-2/demos/dist/" : "https://ai-demos.cocorobo.cn/posenet-2/demos/dist/"
    },
    {
        title: t('message.face_recognition'),
        content: t('message.face_recognition_content'),
        img: discerningEyeImg,
        system: "Windows (>Windows7), macOS (>10.11)",
        resolution: "1440*768",
        memoryCapacity: "8GB-16GB",
        HardDisk: "256GB-512GB",
        GraphicsCard: "650M",
        camera: true,
        microphone: false,
        iframeSrc: domainName ? "https://ai-demos.cocorobo.hk/teachable-machine-pose-recognition-2/public/" : "https://ai-demos.cocorobo.cn/teachable-machine-pose-recognition-2/public/"
    },
    {
        title: t('message.speech_recognition'),
        content: t('message.speech_recognition_content'),
        img: speechImg,
        system: "Windows (>Windows7), macOS (>10.11)",
        resolution: "1440*768",
        memoryCapacity: "8GB-16GB",
        HardDisk: "256GB-512GB",
        GraphicsCard: "650M",
        camera: false,
        microphone: true,
        iframeSrc: domainName ? "https://beta.ai-demos.cocorobo.hk/labs/speech-cloud/index.html" : "https://beta.ai-demos.cocorobo.cn/labs/speech-cloud/index.html"
    }
]
const disposition = ref({})
const iframVisible = ref(false)
const labsData = ref(null)
const loading = ref(true)

const getClassData = (val, name) => {
    calssActive.value = val
    titleNmae.value = name
}

const getNextValueAI = (val) => {
    activeContent.value = 1
    disposition.value = dispositionList.find(item => item.title == val)
}

const goNext = () => {
    iframVisible.value = true
    loading.value = true
    setTimeout(() => {
        iframeMessage()
    }, 100)
}
const iframeMessage = () => {
    const data = {
        data: useUserinfo().userInfo,
        id: "loginVerify",
        url: localStorage.getItem('lang')=="zhHans"?"zh-hans":localStorage.getItem('lang')=="zhHant"?"zh-hant":"en"
    }
    if (labsData.value) {
        loading.value = false
        labsData.value.contentWindow.postMessage(JSON.parse(JSON.stringify(data)), "*")
    }
}

const labsClose = () => {
    console.log(activeContent.value, disposition.value.iframeSrc)
    if (disposition.value.iframeSrc == "" && iframVisible.value) {
        iframVisible.value = false
        activeContent.value = 1
        disposition.value = {}
    }
    else if (activeContent.value == 1) {
        activeContent.value = 0
        disposition.value = {}
    }
    else {
        dialogVisible.value = false
        disposition.value = {}
    }
}

const openLabs = () => {
    console.log(useUserinfo().userInfo)
    if (JSON.stringify(useUserinfo().userInfo) != "{}") {
        dialogVisible.value = true
        activeContent.value = 0
        disposition.value = {}
    } else {
        // dialogVisible.value = false
        ElMessage({
            message: t('message.please_login'),
            type: 'warning'
        })
    }
}

</script>
<style lang="scss">
.labs {
    color: #000;
    cursor: default;

    .el-dropdown-link {
        cursor: pointer;
    }

    .my-header {
        padding: 0 20px 20px;
    }

    .labs_title {
        color: #000;
        font-size: 2.28rem;
    }

    .labs_body {
        border-top: 1px solid #ddd;
        margin: 0px 20px;
        padding-bottom: 20px;

        h5 {
            font-size: 1.64rem;
            line-height: 110%;
            margin: 1.0933333333rem 0 0.656rem 0;
        }

        .labs_class_content {
            .all_class {
                font-weight: bold;
                margin: 1rem 0;
            }


            ul.labs_class_left {
                border: 1px solid #dddddd;
                border-bottom: none;
                border-radius: 2px;

                li {
                    background-color: #fff;
                    line-height: 1.5rem;
                    padding: 10px 20px;
                    margin: 0;
                    border-bottom: 1px solid #e0e0e0;
                    font-weight: bold;
                    cursor: pointer;
                }

                .active {
                    background-color: #2196F3;
                    color: white;
                    border-radius: 2px;
                }
            }

            ul.labs_class_right {}
        }
    }

    .activeContent {
        border-top: 1px solid #ddd;
        background-color: rgb(250, 250, 250);
        // padding-top: 10px;
        padding: 10px;

        .el-col {
            position: relative;
            padding: 10px;

            img {
                width: 100%;
            }

            h3 {
                font-size: 1.64rem;
                line-height: 110%;
                padding: 10px 0 15px;
                color: #212121;
            }

            .labs_card_container {
                background-color: #fff;

                h4 {
                    background-color: rgb(33, 150, 243);
                    color: #fff;
                    padding: 5px 10px;
                    font-size: 15px;
                    border-top-left-radius: 5px;
                    border-top-right-radius: 5px;
                }

                p {
                    padding: 3px;
                }
            }

            .el-button {
                position: absolute;
                width: calc(100% - 20px);
                bottom: 0px;
                background-color: #2979FF;
                color: #fff;
            }
        }

    }
}
</style>