<template>
    <div class="example">
        <div class="el-dropdown-link" title="样例" @click="exampleDialog = true">
            <img :src="imgIcon" alt="" class="header_right_icon left">
            <span class="header_right_title_span">样例</span>
        </div>
        <el-dialog v-model="exampleDialog" title="样例" class="example_dialog">
            <div class="example_dialog_body">
                <div class="example_item">
                    <div :class="selectFirst == index ? 'example_item_div_active example_item_div' : 'example_item_div'"
                        v-for="(item, index) in exampleList" :key="index" @click="clickFirst(index)">
                        {{ item.name }}
                    </div>
                </div>
                <div class="example_item">
                    <div :class="selectTwo == index ? 'example_item_div_active example_item_div' : 'example_item_div'"
                        v-for="(item, index) in exampleList[selectFirst].childrenList" :key="index"
                        @click="clickTwo(index)">
                        {{ item.name }}
                    </div>
                </div>
                <div class="example_item">
                    <div class="example_item_div"
                        v-for="(item, index) in exampleList[selectFirst].childrenList[selectTwo].childrenList"
                        :key="index" @click="openExapmle(item)">
                        {{ item.name }}
                    </div>
                </div>
            </div>
        </el-dialog>
    </div>
</template>
<script setup>
import { ref } from 'vue';
import imgIcon from '../../assets/img/cocoblockly-navbar_labs-icon.png'
import axios from 'axios';
import store from '../../stores/blockly'
import { ElMessage } from 'element-plus';
const importFileName = store.useImportFileNameStore()
const emit = defineEmits(["setFileName"])
const LoadBlocklyStatus = store.useLoadBlocklyStatus()
const loadXml = store.useyXmlStore()
const exampleDialog = ref(false)
const exampleList = [
    {
        id: 1,
        name: '基础样例',
        childrenList: [
            {
                id: 1,
                name: '1-1',
                childrenList: [
                    {
                        name: '1-1-1',
                        url: '基础/未命名.xml'
                    },
                    {
                        name: '1-1-2',
                        url: '基础/未命名.xml'
                    }
                ]
            },
            {
                id: 2,
                name: '1-2',
                url: 'https://www.baidu.com',
                childrenList: [
                    {
                        name: '1-2-3',
                        url: '基础/未命名.xml'
                    },
                    {
                        name: '1-2-4',
                        url: '基础/未命名.xml'
                    }
                ]
            }

        ]
    },
    {
        id: 2,
        name: '媒体处理',
        childrenList: [
            {
                name: '2-1',
                childrenList: [
                    {
                        name: '2-1-1',
                        url: '基础/未命名.xml'
                    },
                    {
                        name: '2-1-2',
                        url: '基础/未命名1.xml'
                    }
                ]
            }

        ]
    },
]
const selectFirst = ref(0)
const selectTwo = ref(0)
const clickFirst = (index) => {
    selectFirst.value = index
    selectTwo.value = 0
}
const clickTwo = (index) => {
    selectTwo.value = index
}
const openExapmle = (item) => {
    axios.get(`/example/${item.url}`).then(res => {
        let xml = res.data;
        console.log(res.data,isXmlFile(xml))
        if (isXmlFile(xml)) {
            importFileName.$patch({ fileName: item.name })
            emit("setFileName", item.name)
            loadXml.$patch({ xmlStr: xml })
            localStorage.setItem('workspaceXml', xml)
            LoadBlocklyStatus.$patch({ status: true })
            setTimeout(() => {
                LoadBlocklyStatus.$patch({ status: false })
            }, 100)
            exampleDialog.value = false
        }else{
            ElMessage({
                message: '打开失败',
                type: 'error'
            });
        }
    })
}
// 读取文件内容
const isXmlFile = (content) => {
    const parser = new DOMParser();
    const parsedDoc = parser.parseFromString(content, "application/xml");
    return parsedDoc.getElementsByTagName('parsererror').length === 0;
};

</script>
<style scoped lang="scss">
.example_dialog {

    .example_dialog_body {
        margin-top: 20px;
        display: flex;
        justify-content: space-between;
        height: 550px;
        text-align: center;
        border: 1px solid #cdd9ff;
        border-left: none;
        border-right: none;

        .example_item {
            width: 32%;
            box-sizing: border-box;
            overflow-y: auto;
            overflow-x: hidden;
            padding: 0;
            border-left: 1px solid #cdd9ff;
            border-right: 1px solid #cdd9ff;
        }

        .example_item_div {
            padding: 10px;
            cursor: pointer;
            background-color: #e7edff;
            color: #2c4fcd;

            &:hover {
                background-color: #6689fc9e;
            }
        }

        .example_item_div_active {
            background-color: #6689fc;
            color: #fff;
        }
    }


}
</style>