<template>
	<view class="container" v-if="detail">
		<view class="user-info-header">
			<u-avatar :src="detail.user && detail.user.headimgurl || 'https://via.placeholder.com/80'" size="80"></u-avatar>
			<view class="user-details">
				<text class="user-name">{{ detail.user && detail.user.name || detail.user && detail.user.nickname || '匿名用户' }}</text>
				<text class="post-time">{{ detail.created_at }}</text>
			</view>
			<view class="stats">
				<text class="type-badge" :class="detail.type === 1 ? 'supply' : 'demand'">{{ detail.type === 1 ? '供应' : '需求' }}</text>
				<text class="views">{{ detail.contact_count }}人私信 {{ detail.view_count }}浏览</text>
			</view>
		</view>

		<view class="content-card">
			<text class="title">{{ detail.title }}</text>
			<text class="description">{{ detail.content }}</text>
			<view class="tags" v-if="detail.tag">
				<text v-for="tag in detail.tag.split(',')" :key="tag" class="tag">{{ tag }}</text>
			</view>
		</view>

		<view class="footer">
			<view class="footer-action" @click="toggleCollect">
				<u-icon :name="isCollected ? 'star-fill' : 'star'" size="40" :color="isCollected ? '#f29100' : '#606266'"></u-icon>
				<text :style="{ color: isCollected ? '#f29100' : '#606266' }">收藏</text>
			</view>
			<u-button type="primary" shape="circle" class="message-btn" @click="goToChat">私信</u-button>
		</view>
	</view>
</template>

<script>
	import uAvatar from '@/uview-ui/components/u-avatar/u-avatar.vue';
	import uIcon from '@/uview-ui/components/u-icon/u-icon.vue';
	import uButton from '@/uview-ui/components/u-button/u-button.vue';
	
	export default {
		components: {
			uAvatar,
			uIcon,
			uButton
		},
		data() {
			return {
				detail: null,
				isCollected: false
			}
		},
		onLoad(options) {
			console.log("页面ID:", options.id);
			this.fetchDetailData(options.id);
		},
		methods: {
			fetchDetailData(id) {
				this.$u.api.supplyDemandDetail({ id: id }).then(res => {
					console.log('详情数据:', res);
					if (res && res.id) {
						this.detail = res;
					} else {
						this.$u.toast('获取详情失败');
					}
				}).catch(err => {
					console.error('获取详情失败:', err);
					this.$u.toast('网络错误，请重试');
				});
			},
			toggleCollect() {
				this.isCollected = !this.isCollected;
				this.$u.toast(this.isCollected ? '收藏成功' : '取消收藏');
			},
			goToChat() {
				if (this.detail && this.detail.user && this.detail.user.id) {
					const userName = this.detail.user.name || this.detail.user.nickname || '匿名用户';
					uni.navigateTo({
						url: `/packages/chat/chatWindow?userId=${this.detail.user.id}&userName=${userName}`
					});
				} else {
					this.$u.toast('用户信息不完整');
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.container {
		background: linear-gradient(to bottom, #e9f2fa, #f5f7fa);
		min-height: 100vh;
	}

	.user-info-header {
		display: flex;
		align-items: center;
		padding: 30rpx;
		background: transparent;
	}

	.user-details {
		display: flex;
		flex-direction: column;
		margin-left: 20rpx;
		flex-grow: 1;
	}

	.user-name {
		font-size: 32rpx;
		font-weight: bold;
	}

	.post-time {
		font-size: 24rpx;
		color: #909399;
		margin-top: 5rpx;
	}
	
	.stats {
		display: flex;
		flex-direction: column;
		align-items: flex-end;
	}

	.type-badge {
		font-size: 24rpx;
		padding: 8rpx 15rpx;
		border-radius: 10rpx;
	}

	.type-badge.supply {
		background-color: #fff0e6;
		color: #f29100;
	}

	.type-badge.demand {
		background-color: #e6f0ff;
		color: #007aff;
	}

	.views {
		font-size: 24rpx;
		color: #909399;
		margin-top: 10rpx;
	}

	.content-card {
		background-color: #fff;
		margin: 30rpx 30rpx 0;
		padding: 30rpx;
		border-radius: 20rpx;
	}

	.title {
		font-size: 36rpx;
		font-weight: bold;
		display: block;
		margin-bottom: 20rpx;
	}

	.description {
		font-size: 28rpx;
		color: #606266;
		line-height: 1.8;
		display: block;
		margin-bottom: 30rpx;
	}
	
	.content-image {
		width: 100%;
		border-radius: 12rpx;
		margin-bottom: 30rpx;
	}

	.tags {
		display: flex;
		flex-wrap: wrap;
	}

	.tag {
		background-color: #f5f5f5;
		color: #606266;
		font-size: 24rpx;
		padding: 10rpx 20rpx;
		border-radius: 30rpx;
		margin-right: 15rpx;
		margin-bottom: 10rpx;
	}

	.footer {
		position: fixed;
		bottom: 0;
		left: 0;
		width: 100%;
		display: flex;
		justify-content: space-between;
		align-items: center;
		padding: 20rpx 70rpx;
		background-color: #fff;
		box-shadow: 0 -2rpx 10rpx rgba(0, 0, 0, 0.05);
		box-sizing: border-box;
	}

	.footer-action {
		display: flex;
		flex-direction: column;
		align-items: center;
		color: #606266;
		font-size: 24rpx;
	}
	
	.message-btn {
		width: 250rpx;
	}
</style>