<template>
    <div class="work-item">
        <div class="work-image">
            <img :src="work.image && work.image.split(',')[0] || defaultCover" :alt="work.name || '未命名作品'" />
            <!-- 原创标签 -->
            <div v-if="work.isOriginal == 'Y'" class="tag original">原创</div>
        </div>
        <div class="work-info">
            <h3 class="work-title">{{ work.name || '未命名作品' }}</h3>
            
            <!-- 读者信息 -->
            <div class="readers">
                <span class="readers-count">达成成就人：{{ work.readers || 0 }}</span>
            </div>
            
            <!-- 状态标签区域 -->
            <div class="status-wrapper">
                <!-- 连载状态 -->
                <div class="status-tag" :class="statusClass">
                    {{ statusText }}
                </div>
                
                <!-- 发布状态标签 -->
                <div v-if="bookStatusText" class="publish-status" :class="bookStatusClass">
                    {{ bookStatusText }}
                </div>
                
                <!-- 设置状态标签 -->
                <div v-if="toolStatusText" class="publish-status" :class="toolStatusClass">
                    {{ toolStatusText }}
                </div>
            </div>
            
            <div class="work-actions">
                <el-button class="action-btn" type="text" @click="handleSetup">作品设置</el-button>
                <el-button class="action-btn" type="text" @click="handleEdit">去写作</el-button>
                <el-button class="action-btn delete-btn" type="text" @click="handleDelete">删除</el-button>
            </div>
        </div>
    </div>
</template>

<script>
import { defineComponent, computed } from 'vue';

export default defineComponent({
    name: 'WorkItem',
    props: {
        work: {
            type: Object,
            required: true
        },
    },
    emits: ['setup', 'edit', 'delete'],
    setup(props, { emit }) {
        const defaultCover = computed(() => {
            // 使用网络图片作为默认封面，避免静态资源路径问题
            return 'https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png';
        });

        // 连载状态样式类
        const statusClass = computed(() => {
            const statusMap = {
                '0': 'ongoing',
                '1': 'completed'
            };
            return statusMap[props.work.status] || 'ongoing';
        });

        // 连载状态文本
        const statusText = computed(() => {
            // 优先使用 dictText，如果没有则使用默认映射
            if (props.work.status_dictText) {
                return props.work.status_dictText;
            }
            const textMap = {
                '0': '连载中',
                '1': '已完结'
            };
            return textMap[props.work.status] || '连载中';
        });

        // 设置状态样式类
        const toolStatusClass = computed(() => {
            const toolStatusMap = {
                '0': 'error',
                '1': 'completed',
                // '2': 'error',
            };
            return toolStatusMap[props.work.toolStatus] || '';
        });

        // 设置状态文本
        const toolStatusText = computed(() => {
            // 优先使用 dictText，如果没有则使用默认映射
            if (props.work.toolStatus_dictText) {
                return props.work.toolStatus_dictText;
            }
            const textMap = {
                '0': '设置审核中',
                '1': '设置审核通过',
                '2': '设置审核不通过'
            };
            return textMap[props.work.toolStatus] || '';
        });

        // 发布状态样式类
        const bookStatusClass = computed(() => {
            const bookStatusMap = {
                '0': 'completed',
                '1': 'error',
            };
            return bookStatusMap[props.work.bookStatus] || '';
        });

        // 发布状态文本
        const bookStatusText = computed(() => {
            // 优先使用 dictText，如果没有则使用默认映射
            if (props.work.bookStatus_dictText) {
                return props.work.bookStatus_dictText;
            }
            const textMap = {
                '0': '发布审核中',
                '1': '发布审核通过',
                '2': '发布审核不通过'
            };
            return textMap[props.work.bookStatus] || '';
        });

        const handleSetup = () => {
            emit('setup', props.work.id);
        };

        const handleEdit = () => {
            emit('edit', props.work.id);
        };

        const handleDelete = () => {
            emit('delete', props.work.id);
        };

        return {
            defaultCover,
            statusClass,
            statusText,
            toolStatusClass,
            toolStatusText,
            bookStatusClass,
            bookStatusText,
            handleSetup,
            handleEdit,
            handleDelete
        };
    }
});
</script>

<style lang="scss" scoped>
.work-item {
    display: flex;
    padding: 20px 0;
    border-bottom: 1px solid #eee;
    
    &:last-child {
        border-bottom: none;
    }

    .work-image {
        position: relative;
        width: 100px;
        height: 133px;
        margin-right: 20px;
        overflow: hidden;
        border-radius: 4px;
        background-color: #f5f5f5;
        
        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;
            
            &.original {
                background-color: #ffa502;
            }
            
            &.new {
                background-color: #0A2463;
            }
        }
    }

    .work-info {
        flex: 1;
        display: flex;
        flex-direction: column;
        
        .work-title {
            font-size: 16px;
            font-weight: bold;
            margin: 0 0 10px;
            color: #333;
            line-height: 1.3;
            overflow: hidden;
            text-overflow: ellipsis;
            display: -webkit-box;
            -webkit-line-clamp: 1;
            -webkit-box-orient: vertical;
        }
        
        .readers {
            margin-bottom: 15px;
            
            .readers-count {
                font-size: 14px;
                color: #999;
            }
        }

        .status-wrapper {
            display: flex;
            align-items: center;
            margin-bottom: 15px;
            gap: 10px;

            .status-tag {
                font-size: 12px;
                padding: 4px 12px;
                border-radius: 12px;
                text-align: center;
                width: fit-content;
                
                &.new {
                    background-color: #e8f3ff;
                    color: #5cadff;
                }
                
                &.ongoing {
                    background-color: #fff7e6;
                    color: #ff9900;
                }
                
                &.completed {
                    background-color: #f0f9eb;
                    color: #67c23a;
                }
            }
            
            .publish-status {
                font-size: 12px;
                color: #666;
                background-color: #f5f5f5;
                padding: 4px 12px;
                border-radius: 12px;
                white-space: nowrap;
                
                &.error {
                    color: #666;
                    background-color: #f5f5f5;
                }
                
                &.completed {
                    background-color: #67c23a;
                    color: #fff;
                }
                
                &.ongoing {
                    background-color: #409EFF;
                    color: #fff;
                }
            }
        }
        
        .work-actions {
            display: flex;
            gap: 15px;
            margin-top: auto;
            
            .action-btn {
                padding: 0;
                font-size: 14px;
                font-weight: normal;
                color: #0A2463;
                
                &:hover {
                    opacity: 0.8;
                }
                
                &.delete-btn {
                    color: #F56C6C;
                    
                    &:hover {
                        color: #F78989;
                        opacity: 1;
                    }
                }
            }
        }
    }
}
</style> 