<template>
    <div class="maincontainer">
        <div class="container_item">
            <!-- 文件名显示 -->
            <div class="filename_display" v-if="!isLastPage">
                <img src="../assets/文件图标-面.png">
                {{ currentItem?.name || '暂无文件' }}
            </div>
            <div class="filename_display" v-else>
                地图
            </div>
            <!-- 轮播内容区域 -->
            <div class="carousel-container">
                <!-- 左侧导航按钮 -->
                <button class="nav-button left" @click="prevItem">
                    <svg t="1754038374827" class="icon" viewBox="0 0 1024 1024" version="1.1"
                        xmlns="http://www.w3.org/2000/svg" p-id="5034" width="200" height="200">
                        <path
                            d="M481.233 904c8.189 0 16.379-3.124 22.628-9.372 12.496-12.497 12.496-32.759 0-45.256L166.488 512l337.373-337.373c12.496-12.497 12.496-32.758 0-45.255-12.498-12.497-32.758-12.497-45.256 0l-360 360c-12.496 12.497-12.496 32.758 0 45.255l360 360c6.249 6.249 14.439 9.373 22.628 9.373z"
                            p-id="5035"></path>
                    </svg>
                </button>
                <div class="contemer_content">
                    <div class="jzsb" v-if="!isLastPage && currentItem.url == 'jzsb'">
                        <img src="../assets/加载失败.svg"></img>
                        <span>图片加载失败</span>
                    </div>
                    <img v-else-if="!isLastPage" :src="currentItem.url" />
                    <div v-else class="last-page-box">
                        <MapContainer ref="mapRef" :dataListMap="dataListMap"></MapContainer>
                    </div>
                </div>

                <!-- 右侧导航按钮 -->
                <button class="nav-button right" @click="nextItem">
                    <svg style="transform: rotateZ(180deg);left: 10px;" t="1754038374827" class="icon"
                        viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5034" width="200"
                        height="200">
                        <path
                            d="M481.233 904c8.189 0 16.379-3.124 22.628-9.372 12.496-12.497 12.496-32.759 0-45.256L166.488 512l337.373-337.373c12.496-12.497 12.496-32.758 0-45.255-12.498-12.497-32.758-12.497-45.256 0l-360 360c-12.496 12.497-12.496 32.758 0 45.255l360 360c6.249 6.249 14.439 9.373 22.628 9.373z"
                            p-id="5035"></path>
                    </svg>
                </button>
            </div>
        </div>
    </div>
</template>

<script setup>
import { ref, computed } from 'vue'
import MapContainer from "./MapContainer.vue";

const imgeList = ref([])
const currentIndex = ref(0)
const dataListMap = ref([])
const mapRef = ref(null)
const isLastPage = computed(() => currentIndex.value === imgeList.value.length)

const currentItem = computed(() => {
    if (isLastPage.value) {
        return {}
    }
    return imgeList.value[currentIndex.value] || {}
})


const onloaddata = async (jsondata, dataList) => {
    if (jsondata && jsondata.length > 0 && dataList && dataList.length > 0) {
        dataListMap.value = dataList
        imgeList.value = jsondata.flat()
        currentIndex.value = 0
    }
}

// 新增：清空方法，确保图片与地图点都能被清理
const clear = () => {
    imgeList.value = []
    dataListMap.value = []
    currentIndex.value = 0
    mapRef.value?.clearMap()
}


// 上一项
const prevItem = () => {
    if (imgeList.value.length === 0) return
    currentIndex.value =
        (currentIndex.value - 1 + (imgeList.value.length + 1)) %
        (imgeList.value.length + 1)
}

// 下一项
const nextItem = () => {
    if (imgeList.value.length === 0) return
    currentIndex.value = (currentIndex.value + 1) % (imgeList.value.length + 1)
}

// 跳转到指定项
const goToItem = (index) => {
    if (index >= 0 && index < imgeList.value.length) {
        currentIndex.value = index
    }
}

defineExpose({
    onloaddata,
    clear
})
</script>

<style scoped lang="less">
.maincontainer {
    width: 97.5%;
    height: 100%;
    background: #FDFDFD;
    position: relative;
    border-radius: 4px;
    float: right;

    .container_item {
        width: 100%;
        height: 100%;
        display: flex;
        flex-direction: column;

        .filename_display {
            width: 100%;
            height: 70px;
            font-size: 20px;
            color: #434959;
            font-weight: 600;
            display: flex;
            justify-content: center;
            align-items: center;

            &>img {
                width: 16px;
                height: 20px;
                margin-right: 6px;
            }
        }

        .carousel-container {
            display: flex;
            align-items: center;
            justify-content: space-between;
            height: calc(100% - 100px);
            position: relative;



            .contemer_content {
                flex: 1;
                padding: 0 20px;
                font-size: 18px;
                color: #6B7181;
                width: 100%;
                height: 30vh;
                display: flex;
                // padding-top: 20px;

                &>img {
                    width: 100%;
                    height: 100%;
                    object-fit: contain;
                }

                &>.last-page-box {
                    width: 100%;
                    height: 100%;
                }

                & > .jzsb {
                    width: 100%;
                    height: 100%;
                    display: flex;
                    flex-direction: column;
                    align-items: center;
                    justify-content: center;
                    &>img {
                        width: 70%;
                        height: 70%;
                    }
                    &>span {
                        font-size: 14px;
                        color: #6B7181;
                    }
                }

            }

            .nav-button {
                width: 60px;
                height: 90px;
                background: rgba(243, 245, 250, 1);
                font-size: 24px;
                cursor: pointer;
                display: flex;
                align-items: center;
                border-radius: 4px;
                justify-content: center;
                color: #6B7181;
                box-shadow: none;
                cursor: pointer;
                position: relative;

                &>svg {
                    position: absolute;
                    width: 30px;
                    height: 30px;
                    left: 20px;

                    &>path {
                        fill: rgba(107, 113, 129, 1);
                    }
                }

                &:hover:not(:disabled) {
                    background: rgba(66, 113, 238, 0.10);
                    border: 1.4px solid #4271EE;

                    &>svg {
                        position: absolute;
                        width: 30px;
                        height: 30px;
                        left: 20px;

                        &>path {
                            fill: rgba(66, 113, 238, 1);
                        }
                    }
                }

                &:disabled {
                    opacity: 0.3;
                    cursor: not-allowed;
                }

                &.left {
                    margin-left: 16px;
                }

                &.right {
                    margin-right: 16px;
                }
            }
        }

        .carousel-indicators {
            display: flex;
            justify-content: center;
            height: 40px;
            align-items: center;

            span {
                width: 10px;
                height: 10px;
                border-radius: 50%;
                background: #ccc;
                margin: 0 5px;
                cursor: pointer;

                &.active {
                    background: #434959;
                }
            }
        }
    }
}
</style>