<template>
    <div class="book-catalog-container">
        <div class="section-header">
            <h3 class="section-title">目录</h3>
        </div>

        <!-- 章节列表 -->
        <transition-group v-if="chapters.length > 0" name="chapter-transition" tag="div" class="chapter-grid">
            <div v-for="(chapter, index) in displayedChapters" :key="chapter.id" 
                class="chapter-item"
                :class="{ 'is-current': chapter.id === currentChapterId }"
                @click="goToChapter(chapter.id)">
                <div class="chapter-title">
                    {{ chapter.title }}
                    <!-- <span class="current-badge" v-if="chapter.id === currentChapterId">正在阅读</span> -->
                    <!-- <span class="new-badge" v-if="chapter.isNew">NEW</span> -->
                </div>
                <div class="chapter-pay" v-if="chapter.isPay === 'Y'">
                    <span v-if="chapter.pay" class="paid-badge">已付费</span>
                    <span v-else class="pay-badge">付费</span>
                </div>
            </div>
        </transition-group>

        <!-- 空状态 -->
        <div v-else class="empty-state">
            <div class="empty-icon">
                <svg width="64" height="64" viewBox="0 0 64 64" fill="none" xmlns="http://www.w3.org/2000/svg">
                    <rect x="8" y="8" width="48" height="48" rx="4" stroke="#E5E5E5" stroke-width="2" fill="none"/>
                    <rect x="16" y="16" width="32" height="2" rx="1" fill="#E5E5E5"/>
                    <rect x="16" y="22" width="20" height="2" rx="1" fill="#E5E5E5"/>
                    <rect x="16" y="28" width="24" height="2" rx="1" fill="#E5E5E5"/>
                    <rect x="16" y="34" width="16" height="2" rx="1" fill="#E5E5E5"/>
                    <rect x="16" y="40" width="28" height="2" rx="1" fill="#E5E5E5"/>
                    <rect x="16" y="46" width="18" height="2" rx="1" fill="#E5E5E5"/>
                </svg>
            </div>
            <p class="empty-title">暂无章节</p>
            <p class="empty-desc">该书籍暂时没有可阅读的章节</p>
        </div>

        <div v-if="chapters.length > 0" class="catalog-footer">
            <div v-if="showAll" class="collapse-btn" @click="toggleShowAll">
                收起 <i class="collapse-icon"></i>
            </div>
            <div v-else-if="!showAll && chapters.length > maxDisplayChapters" class="expand-btn" @click="toggleShowAll">
                展开所有章节 <i class="expand-icon"></i>
            </div>
        </div>
    </div>
</template>

<script>
import { defineComponent, ref, computed } from 'vue';
import { useRouter } from 'vue-router';

export default defineComponent({
    name: 'BookCatalog',
    emits: ['chapter-click'],
    props: {
        bookId: {
            type: String,
            default: ''
        },
        chapters: {
            type: Array,
            default: () => [
            ]
        },
        maxDisplayChapters: {
            type: Number,
            default: 12
        },
        currentChapterId: {
            type: String,
            default: ''
        }
    },
    setup(props, { emit }) {
        const router = useRouter();
        const showAll = ref(false);

        const displayedChapters = computed(() => {
            if (showAll.value) {
                return props.chapters;
            } else {
                return props.chapters.slice(0, props.maxDisplayChapters);
            }
        });

        const toggleShowAll = () => {
            showAll.value = !showAll.value;
        };

        const goToChapter = (chapterId) => {
            // 先触发事件给父组件
            emit('chapter-click', chapterId);
            
            // 如果没有父组件处理，则默认路由跳转
            if (props.bookId) {
                router.push(`/book/${props.bookId}/chapter/${chapterId}`);
            }
        };

        return {
            showAll,
            displayedChapters,
            toggleShowAll,
            goToChapter
        };
    }
});
</script>

<style lang="scss" scoped>
.book-catalog-container {
    background-color: #fff;
    border-radius: 6px;
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.03);
    padding: 15px;
    margin-bottom: 3px;

    .section-header {
        display: flex;
        justify-content: space-between;
        align-items: center;
        margin-bottom: 16px;
        padding-bottom: 12px;
        border-bottom: 1px solid #eee;

        .section-title {
            font-size: 18px;
            font-weight: bold;
            color: #333;
            margin: 0;
            position: relative;
            padding-left: 12px;

            &::before {
                content: '';
                position: absolute;
                left: 0;
                top: 4px;
                height: 18px;
                width: 4px;
                background-color: #0A2463;
                border-radius: 2px;
            }
        }
    }

    .empty-state {
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        padding: 60px 20px;
        text-align: center;

        .empty-icon {
            margin-bottom: 16px;
            opacity: 0.6;
        }

        .empty-title {
            font-size: 16px;
            font-weight: 500;
            color: #666;
            margin: 0 0 8px 0;
        }

        .empty-desc {
            font-size: 14px;
            color: #999;
            margin: 0;
            line-height: 1.5;
        }
    }

    .chapter-grid {
        display: grid;
        grid-template-columns: repeat(4, 1fr);
        gap: 10px;
        margin-bottom: 20px;

        @media (max-width: 768px) {
            grid-template-columns: 2fr;
        }

        .chapter-item {
            padding: 8px 12px;
            border-radius: 4px;
            cursor: pointer;
            transition: all 0.2s;
            display: flex;
            align-items: center;
            justify-content: space-between;

            &:hover {
                background-color: #f8f9ff;
            }

            &.is-current {
                background-color: #e6f7ff;
                border: 1px solid #91d5ff;
                
                &:hover {
                    background-color: #e6f7ff;
                }
                
                .chapter-title {
                    color: #0A2463;
                    font-weight: bold;
                }
            }

            .chapter-title {
                font-size: 14px;
                color: #333;
                white-space: nowrap;
                overflow: hidden;
                text-overflow: ellipsis;
                display: flex;
                align-items: center;
                flex: 1;

                .current-badge {
                    background-color: #52c41a;
                    color: #fff;
                    font-size: 12px;
                    padding: 1px 6px;
                    border-radius: 4px;
                    margin-left: 8px;
                    flex-shrink: 0;
                }

                .new-badge {
                    background-color: #ff5252;
                    color: #fff;
                    font-size: 12px;
                    padding: 1px 6px;
                    border-radius: 4px;
                    margin-left: 8px;
                    flex-shrink: 0;
                }
            }

            .chapter-pay {
                flex-shrink: 0;
                padding-left: 10px;
                .pay-badge {
                    background-color: #FF9E2D;
                    color: #fff;
                    font-size: 12px;
                    padding: 2px 10px;
                    border-radius: 12px;
                }
                .paid-badge {
                    background-color: #52c41a;
                    color: #fff;
                    font-size: 12px;
                    padding: 2px 10px;
                    border-radius: 12px;
                }
            }
        }
    }

    .catalog-footer {
        display: flex;
        justify-content: center;
        padding-top: 10px;
        border-top: 1px solid #f0f0f0;
        margin-top: 10px;
        
        .expand-btn, .collapse-btn {
            display: flex;
            align-items: center;
            justify-content: center;
            color: #0A2463;
            font-size: 14px;
            cursor: pointer;
            padding: 8px 0;
            transition: all 0.3s;
            
            &:hover {
                opacity: 0.8;
            }
        }
        
        .expand-btn {
            .expand-icon {
                display: inline-block;
                width: 18px;
                height: 18px;
                margin-left: 8px;
                background: url('data:image/svg+xml;charset=utf-8,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="%230A2463"><path d="M7.41 8.59L12 13.17l4.59-4.58L18 10l-6 6-6-6 1.41-1.41z"/></svg>') no-repeat center;
                background-size: contain;
                transition: transform 0.3s;
            }
        }
        
        .collapse-btn {
            .collapse-icon {
                display: inline-block;
                width: 18px;
                height: 18px;
                margin-left: 8px;
                background: url('data:image/svg+xml;charset=utf-8,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="%230A2463"><path d="M7.41 15.41L12 10.83l4.59 4.58L18 14l-6-6-6 6z"/></svg>') no-repeat center;
                background-size: contain;
                transition: transform 0.3s;
            }
        }
    }
}

// 章节过渡动画
.chapter-transition-enter-active,
.chapter-transition-leave-active {
  transition: all 0.3s ease;
}

.chapter-transition-enter-from {
  opacity: 0;
  transform: translateY(20px);
}

.chapter-transition-leave-to {
  opacity: 0;
  transform: translateY(-20px);
}

.chapter-transition-move {
  transition: transform 0.3s ease;
}
</style>