<template>
	<view class="work-item" @click.stop="handleClick">
		<view class="cover-wrapper">
			<image class="cover" :src="work.image && work.image.split(',')[0]" mode="aspectFill"></image>
			<view class="tag original" v-if="work.isOriginal == 'Y'">原创</view>
		</view>

		<view class="info">
			<text class="title">{{work.name}}</text>

			<view class="readers">
				<text class="readers-count">达成成就人：{{work.readers || 0}}</text>
			</view>

            <!-- 状态标签 -->
            <view class="status-wrapper">
                <view class="status-tag" :class="statusClass">
                    {{statusText}}
                </view>
                
                <!-- 发布状态标签 -->
                <view class="publish-status" 
				:class="bookStatusClass"
				>
                    <text>{{bookStatusText}}</text>
                </view>
                
                <!-- 设置状态标签 -->
                <view class="publish-status" 
				:class="toolStatusClass"
				>
                    <text>{{toolStatusText}}</text>
                </view>
            </view>
            
		</view>
		
		<!-- 添加右箭头图标 -->
		<!-- <text class="iconfont icon-arrow">&#xe65f;</text> -->
	</view>
</template>

<script>
	export default {
		props: {
			work: {
				type: Object,
				default: {},
			},
			isManaging: {
				type: Boolean,
				default: false
			},
		},
		computed: {
			statusClass() {
				const statusMap = {
					'draft': 'new',
					'0': 'ongoing',
					'1': 'completed'
				};
				return statusMap[this.work.status] || 'ongoing';
			},
			statusText() {
				// 优先使用 dictText，如果没有则使用默认映射
				if (this.work.status_dictText) {
					return this.work.status_dictText;
				}
				const textMap = {
					'0': '连载中',
					'1': '已完结'
				};
				return textMap[this.work.status] || '连载中';
			},
			toolStatusClass() {
				const toolStatusMap = {
					'0': 'completed',
					'1': 'error',
					// '2': 'error',
				};
				return toolStatusMap[this.work.toolStatus] || '';
			},
			toolStatusText() {
				// 优先使用 dictText，如果没有则使用默认映射
				if (this.work.toolStatus_dictText) {
					return this.work.toolStatus_dictText;
				}
				const textMap = {
					'0': '设置审核中',
					'1': '设置审核通过',
					'2': '设置审核不通过'
				};
				return textMap[this.work.toolStatus] || '';
			},
			bookStatusClass() {
				const bookStatusMap = {
					'0': 'ongoing',
					'1': 'completed',
					'2': 'error',
				};
				return bookStatusMap[this.work.bookStatus] || '';
			},
			bookStatusText() {
				// 优先使用 dictText，如果没有则使用默认映射
				if (this.work.bookStatus_dictText) {
					return this.work.bookStatus_dictText;
				}
				const textMap = {
					'0': '发布审核中',
					'1': '发布审核通过',
					'2': '发布审核不通过'
				};
				return textMap[this.work.bookStatus] || '';
			},
		},
		methods: {
			handleClick() {
				console.log('handleClick called, isManaging:', this.isManaging);
				// 在编辑模式下，只触发选中事件
				if (this.isManaging) {
					this.$emit('click');
					return;
				}
				
				// 非编辑模式下，跳转到章节列表页面
				uni.navigateTo({
					url: '/pages_order/author/chapterList?id=' + this.work.id
				});
			},
			handleDelete() {
				this.$emit('delete');
			}
		}
	}
</script>

<style lang="scss" scoped>
	.work-item {
		width: 100%;
		display: flex;
		padding: 24rpx 0;
		border-bottom: 1rpx solid #f0f0f0;
		position: relative;
		align-items: center;
		
		.cover-wrapper {
			position: relative;
			width: 170rpx;
			height: 230rpx;
			margin-right: 20rpx;
			border-radius: 6rpx;
			overflow: hidden;
			flex-shrink: 0;
			
			.cover {
				width: 100%;
				height: 100%;
			}
			
			.tag {
				position: absolute;
				top: 0;
				left: 0;
				font-size: 20rpx;
				color: #fff;
				padding: 2rpx 8rpx;
				border-radius: 6rpx;
				
				&.original {
					background-color: #ffa502;
				}
			}
		}
		
		.info {
			flex: 1;
            display: flex;
            flex-direction: column;
			
			.title {
				font-size: 28rpx;
				color: #333;
				font-weight: bold;
				margin-bottom: 10rpx;
				line-height: 1.3;
				overflow: hidden;
				text-overflow: ellipsis;
				display: -webkit-box;
				-webkit-line-clamp: 1;
				-webkit-box-orient: vertical;
			}
			
			.readers {
				margin-top: 20rpx;
				
				.readers-count {
					font-size: 24rpx;
					color: #999;
				}
			}

            .status-wrapper {
                flex-shrink: 0;
                display: flex;
                align-items: center;
                margin-top: 20rpx;
				gap: 10rpx;

                .status-tag {
                    font-size: 22rpx;
                    padding: 4rpx 16rpx;
                    border-radius: 20rpx;
                    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 {
					&.error{
						color: #666;
						background-color: #f5f5f5;
					}
                    &.completed {
                        background-color: #67c23a;
                        color: #fff;
                    }
					font-size: 22rpx;
					color: #666;
					background-color: #f5f5f5;
					padding: 4rpx 16rpx;
					border-radius: 20rpx;
					white-space: nowrap;
                }
            }
		}
		
		.icon-arrow {
			color: #999;
			font-size: 16px;
			margin-left: 10rpx;
		}
	}
</style>
