<!-- ArtsDetail.vue -->
<template>
    <div class="arts-detail">
        <div class="card-container">
            <div class="card_left">
                <div class="card_img">
                    <div class="carousel-wrapper">
                        <div class="carousel-inner">
                            <div class="carousel-track" :style="trackStyle">
                                <div v-for="(item, index) in originalMediaData" :key="index" class="carousel-item">
                                    <a-image  :src="item.url" />
                                    <!-- <img :src="item.url" :alt="item.title" /> -->
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="card_right">
                <div class="card_text_title"></div>
                <div class="card_text_content">
                    {{ currentDescription }}
                </div>
            </div>
        </div>
        <div style="width: 100%;display: flex;justify-content: center;">
            <div class="card_button">
                <LeftOutlined @click="prevSlide" :class="{ 'disabled': currentIndex === 0 }"
                    :style="{ cursor: currentIndex === 0 ? 'not-allowed' : 'pointer' }" />
                <div class="pagination-dots">
                    <span v-for="(item, index) in originalMediaData" :key="index" class="dot-item"
                        :class="{ 'active': currentIndex === index }" @click="jumpToSlide(index)" />
                </div>
                <RightOutlined @click="nextSlide" :class="{ 'disabled': currentIndex === originalMediaData.length - 1 }"
                    :style="{ cursor: currentIndex === originalMediaData.length - 1 ? 'not-allowed' : 'pointer' }" />
            </div>
        </div>
    </div>
</template>

<script lang="ts" setup>
import { ref, computed, onMounted, watch } from 'vue';
import { LeftOutlined, RightOutlined } from '@ant-design/icons-vue';

defineOptions({
    name: 'ArtsDetail'
});

const props = defineProps<{
    dataSource: Array<{
        title: string;
        description: string;
        url: string;
    }>;
    index:any
}>();

interface MediaItem {
    title: string;
    description: string;
    url: string;
}

const originalMediaData = ref<MediaItem[]>([]);
const currentIndex = ref(0);

// 初始化数据
const initOriginalData = () => {
    if (props.dataSource && props.dataSource.length > 0) {
        originalMediaData.value = props.dataSource.map(item => ({
            title: item.title,
            description: item.description,
            url: item.url
        }));
    }
};

// 根据 index prop 设置当前索引
const setCurrentIndexFromProps = () => {
    if (props.index !== undefined && props.index !== null && originalMediaData.value.length > 0) {
        const validIndex = Math.max(0, Math.min(props.index, originalMediaData.value.length - 1));
        currentIndex.value = validIndex;
    } else {
        currentIndex.value = 0;
    }
};

// 计算轨道样式 - 完全按照 React 版本的逻辑
const trackStyle = computed(() => {
    return {
        transform: `translateX(calc((68.8px - 48vw) * ${currentIndex.value}))`
    };
});

// 切换到下一张
const nextSlide = () => {
    if (currentIndex.value < originalMediaData.value.length - 1) {
        currentIndex.value++;
    }
};

// 切换到上一张
const prevSlide = () => {
    if (currentIndex.value > 0) {
        currentIndex.value--;
    }
};

// 跳转到指定页面
const jumpToSlide = (targetIndex: number) => {
    currentIndex.value = targetIndex;
};

// 计算当前显示的描述文字
const currentDescription = computed(() => {
    return originalMediaData.value[currentIndex.value]?.description || '';
});

// 页面加载时执行
onMounted(() => {
    console.log('ArtsDetail mounted - props.index:', props.index, 'props.dataSource.length:', props.dataSource?.length);
    initOriginalData();
    setCurrentIndexFromProps();
    console.log('After setCurrentIndexFromProps - currentIndex.value:', currentIndex.value);
});

// 只监听 dataSource 变化，不监听 index 变化
watch(
    () => props.dataSource,
    (newData) => {
        if (newData && newData.length > 0) {
            // 数据更新时重新初始化
            initOriginalData();
            // 重置索引为 0 或根据当前 props.index 设置
            setCurrentIndexFromProps();
        }
    },
    { deep: true }
);
</script>

<style lang="scss" scoped>
.arts-detail {
    width: 100%;
    box-sizing: border-box;

    .card-container {
        width: 100%;
        display: flex;
        justify-content: space-between;
        align-items: center;
        box-sizing: border-box;
    }

    .card_left {
        // flex: 6;
        width: 60%;
        height: 630px;
        padding: 12px;
        border: 1px solid rgba(0, 0, 0, 0.12);
        box-sizing: border-box;

        .card_img {
            width: 100%;
            height: 100%;
            padding: 12px;
            border: 1px solid rgba(0, 0, 0, 0.12);
            overflow: hidden;
            box-sizing: border-box;
        }

        .carousel-wrapper {
            width: 100%;
            height: 100%;
            position: relative;
            overflow: hidden;

            .carousel-inner {
                width: 100%;
                height: 100%;
                overflow: hidden;
            }

            .carousel-track {
                height: 100%;
                display: flex;
                justify-content: flex-start;
                align-items: center;
                flex-wrap: nowrap;
                transition: transform 0.3s ease-in-out;

                .carousel-item {
                    width: calc(48vw - 80.8px);
                    flex: 0 0 calc(48vw - 80.8px);
                    height: 100%;
                    margin-right: 12px;
                    display: flex;
                    justify-content: center;
                    align-items: center;

                    img {
                        max-height: 100%;
                        max-width: 100%;
                        border: none;
                        object-fit: contain;
                    }
                }
            }
        }
    }

    .card_right {
        // flex: 4;
        width: 40%;
        height: 630px;
        padding-left: 40px;
        box-sizing: border-box;

        .card_text_title {
            width: 6px;
            height: 24px;
            background-color: rgb(108, 164, 247);
            box-sizing: border-box;
        }

        .card_text_content {
            width: 100%;
            height: 96%;
            padding: 0px 16px 16px 0px;
            box-sizing: border-box;
            line-height: 1.5;
            overflow: auto;
            font-size: 20px;
            text-indent: 2em;
        }
    }

    .card_button {
        display: flex;
        // justify-content: space-between;
        align-items: center;
        padding: 16px 0;
        box-sizing: border-box;

        .pagination-dots {
            display: flex;
            align-items: center;

            .dot-item {
                display: inline-block;
                width: 12px;
                height: 12px;
                border-radius: 50%;
                background-color: #adaaaa;
                margin: 0 6px;
                cursor: pointer;
                transition: all 0.3s ease;

                &:hover {
                    background-color: #8a8a8a;
                    transform: scale(1.1);
                }

                &.active {
                    background-color: #5E9BFE;
                    transform: scale(1.2);
                }
            }
        }

        :deep(.anticon) {
            font-size: 24px;
            cursor: pointer;
            color: #6c8cf7;
            transition: all 0.3s;

            &:hover:not(.disabled) {
                color: #4a6fd4;
                transform: scale(1.1);
            }

            &.disabled {
                color: #d9d9d9;
                cursor: not-allowed !important;
            }
        }
    }
}
</style>