<template>
    <div class="book-card" @click="handleClick">
        <div class="book-cover">
            <img :src="book.image" :alt="book.name">
            <!-- 原创标签 -->
            <div v-if="book.isOriginal == 'Y'" class="tag original">原创</div>
        </div>
        <div class="book-info">
            <h3 class="book-title">{{ book.name }}</h3>
            <p class="book-author">作者：{{ book.author }}</p>
            <p class="book-intro" v-if="!book.lastReadChapter && book.details">{{ book.details }}</p>
            <p class="last-read" v-if="book.lastReadChapter">上次阅读：{{ book.lastReadChapter }}</p>
            <div class="book-status">
                <span class="status-tag">{{ book.status || '连载中' }}</span>
                <span class="reading-count">{{ book.service || '大家都在读' }}</span>
            </div>
        </div>
        <div v-if="deleteMode" class="delete-icon" @click.stop="handleDelete">
            删除
        </div>
    </div>
</template>

<script>
import { useRouter } from 'vue-router';

export default {
    name: 'BookshelfCard',
    props: {
        book: {
            type: Object,
            required: true,
            default: () => ({
                id: '',
                name: '',
                author: '',
                details: '',
                image: '',
                status: '',
                service: '',
                lastReadChapter: ''
            })
        },
        deleteMode: {
            type: Boolean,
            default: false
        }
    },
    emits: ['click', 'delete'],
    setup(props, { emit }) {
        const router = useRouter();

        // 点击书籍卡片的处理函数
        const handleClick = () => {
            if (!props.deleteMode) {
                emit('click', props.book);
            }
        };

        // 点击删除图标的处理函数
        const handleDelete = () => {
            emit('delete', props.book.id);
        };

        return {
            handleClick,
            handleDelete
        };
    }
};
</script>

<style lang="scss" scoped>
@use '@/assets/styles/variables.scss' as vars;

.book-card {
    display: flex;
    border-radius: 4px;
    overflow: hidden;
    transition: all 0.3s;
    cursor: pointer;
    background-color: #fff;
    margin-bottom: 15px;


    .book-cover {
        width: 100px;
        min-width: 100px;
        height: 140px;
        overflow: hidden;
        position: relative;

        img {
            width: 100%;
            height: 100%;
            object-fit: cover;
        }

        .tag {
            position: absolute;
            top: 0;
            left: 0;
            font-size: 12px;
            color: #fff;
            padding: 2px 8px;
            border-radius: 0 0 4px 0;
            z-index: 1;
            
            &.original {
                background-color: #ffa502;
            }
        }

        .book-status {
            position: absolute;
            top: 10px;
            right: 10px;
            padding: 2px 8px;
            background-color: rgba(0, 0, 0, 0.6);
            color: #fff;
            font-size: 12px;
            border-radius: 10px;
        }

    }

    .book-info {
        flex: 1;
        padding: 12px;
        display: flex;
        flex-direction: column;
        justify-content: space-between;

        .book-title {
            margin: 0 0 6px;
            font-size: 16px;
            font-weight: bold;
            color: #333;
            overflow: hidden;
            text-overflow: ellipsis;
            white-space: nowrap;
        }

        .book-author {
            font-size: 14px;
            color: #666;
            margin: 0 0 8px;
        }

        .book-intro {
            font-size: 12px;
            color: #999;
            line-height: 1.5;
            margin: 0 0 auto;
            overflow: hidden;
            text-overflow: ellipsis;
            display: -webkit-box;
            -webkit-line-clamp: 2;
            -webkit-box-orient: vertical;
        }

        .last-read {
            font-size: 12px;
            color: #999;
            margin: 0 0 8px;
        }

        .book-status {
            margin-top: 8px;
            display: flex;
            align-items: center;
            gap: 10px;

            .status-tag {
                background-color: #67C23A33;
                color: vars.$primary-color;
                padding: 2px 8px;
                border-radius: 10px;
                font-size: 12px;
                color: #67C23A;
            }

            .reading-count {
                font-size: 12px;
                color: #999;
            }
        }
    }


    .delete-icon {
        margin: auto;
        margin-right: 20px;
        display: flex;
        justify-content: center;
        align-items: center;
        z-index: 2;
        background-color: rgb(192, 46, 46);
        color: #fff;
        height: 30px;
        width: 80px;
        border-radius: 2px;
        font-size: 14px;
    }
}
</style>