<template>
	<view class="message-page" :style="{paddingTop:`${statusBarHeight}rpx`}">
		<view class="message-header flex-row-center-between">
			<image
				src="https://mrrplus.oss-cn-beijing.aliyuncs.com/photo/system/8f59f3a0-cb23-4625-9c32-a9842e794489.png"
				class="message-header-left"></image>
			<view class="message-header-right">
				<image class="icon-broom" @tap="messageReadAll"
					src="https://mrrplus.oss-cn-beijing.aliyuncs.com/photo/system/0b137ffb-6165-465c-9497-b5c9375bac74.png"
					mode="aspectFit"></image>
				<view class="icon-dots" @click="goMessageSetting">
					<view class="dot"></view>
					<view class="dot"></view>
					<view class="dot"></view>
				</view>
			</view>
		</view>
		<!-- <view class="message-state flex-row-center-between">
			<view class="message-state-card">
				<image class="state-card-img"
					src="https://mrrplus.oss-cn-beijing.aliyuncs.com/photo/system/ad0ef855-416f-4178-a7a8-c6b114c1e10d.png">
				</image>
				<text class="state-card-text">订单服务</text>
				<view class="state-card-num" v-show="nums[0]">{{ nums[0] }}</view>
			</view>
			<view class="message-state-card" @click="goToCouponNotice">
				<image class="state-card-img"
					src="https://mrrplus.oss-cn-beijing.aliyuncs.com/photo/system/b1e1c9ab-7ca3-4f70-8e00-4bb1982a6e2d.png">
				</image>
				<text class="state-card-text">优惠通知</text>
				<view class="state-card-num" v-show="nums[0]">{{ nums[1] }}</view>
			</view>
		</view> -->


		<CommonList ref="groupRef" apiUrl="/sj/push/messageList"
			:listScrollHeight="`calc(100vh - ${statusBarHeight + 250}rpx)`">
			<template #listData="{ list }">
				<!-- 订单超时卡片 -->
				<view class="message-list">
					<view class="message-item" v-for="(item,index) in list" :key="index" @click="goOrder(item)">
						<view class="item-content">
							<view class="item-header">
								<view class="item-title-group">
									<image class="item-icon"
										src="https://mrrplus.oss-cn-beijing.aliyuncs.com/photo/system/e233f727-e358-4eb2-8e0f-e55f95728dc2.png"
										mode="aspectFit"></image>

									<text class="item-title">{{ item.title }}</text>
								</view>
								<view class="flex-row-center"><text class="item-time">{{ item.send_time }}</text>
									<view v-if="item.is_read==2" class="item-dot"></view>
								</view>

							</view>
							<view class="item-body">
								<view class="item-info">
									<text class="item-desc">{{ item.desc }}</text>
								</view>
								<image class="item-product-img" :src="item.ext.data.order_photo" mode="aspectFill">
								</image>
							</view>
						</view>
					</view>
				</view>
			</template>

			<!-- 空数据状态 -->
			<template #empty>
				<noData></noData>
			</template>
		</CommonList>

		<TabBar :selected="1"></TabBar>
	</view>
</template>

<script>
	import request from "@/utils/request";
	import TabBar from "@/components/tabbar/tabbar.vue";
	import CommonList from "@/components/common/CommonList.vue";
	export default {
		components: {
			TabBar,
			CommonList
		},
		data() {
			return {
				nums: [0, 0], //[订单通知数量,优惠通知数量]
				statusBarHeight: 0,
				queryData: {},
			};
		},
		async onShow() {
			if (uni.getStorageSync('accessToken')) {
				this.$nextTick(() => {
					this.search();
				});
				// let OrderNum = await this.messageUnreadNum(1)
				// this.$set(this.nums,0,OrderNum)
				// let discountNum = await this.messageUnreadNum(2)
				// this.$set(this.nums,1,discountNum)
			}

		},
		async onLoad() {
			uni.hideTabBar();
			this.statusBarHeight = this.getRpxStatusBarHeight();
		},
		methods: {
			goMessageSetting() {
				if (uni.getStorageSync('accessToken')) {
					uni.navigateTo({
						url: `/pages/message/settings`
					})
				}else{
					uni.navigateTo({
						url: "/pages/blogPopup/blogPopup",
					});
				}

			},
			async messageUnreadNum(id) {
				try {
					let typeRes = await request.post('/sj/push/messageUnreadNum', {
						message_type_id: id
					});
					console.log('消息数量', typeRes);
					if (typeRes.code == 200) {
						return typeRes.data.count <= 99 ? typeRes.data.count : "99+"
					} else {
						return 0
					}

				} catch (err) {
					console.error('获取数量失败', err);
				}
			},
			messageReadAll() {
				request.post('/sj/push/messageReadAll').then(async res => {
					if (res.code == 200) {
						this.search()
						// let OrderNum = await this.messageUnreadNum(1)
						// this.$set(this.nums,0,OrderNum)
						// let discountNum = await this.messageUnreadNum(2)
						// this.$set(this.nums,1,discountNum)
						this.messageUpudateNum()
						uni.showToast({
							title: '清除成功',
							icon: 'none'
						})
					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
					}
				});
			},
			goToCouponNotice() {
				uni.navigateTo({
					url: '/pages/message/coupon_notice'
				});
			},
			search() {
				this.$refs.groupRef.manualRefresh(this.queryData);
			},
			formatParams(params) {
				let paramStr = '';
				for (let key in params) {
					if (params.hasOwnProperty(key)) {
						// 对参数值进行编码，避免特殊字符导致的问题
						paramStr += `${key}=${encodeURIComponent(params[key])}&`;
					}
				}
				// 去除最后一个&符号
				return paramStr.slice(0, -1);
			},
			goOrder(order) {
				console.log('order', order)
				uni.navigateTo({
					// 拼接路径和参数（uniapp中参数需要通过url拼接传递）
					url: `${order.ext.path}?${this.formatParams(order.ext.data)}&push_id=${order.id}`,
					// 跳转成功的回调
					success: (res) => {
						console.log('页面跳转成功', res);
					},
					// 跳转失败的回调
					fail: (err) => {
						console.error('页面跳转失败', err);
					}
				});
			}
		}
	}
</script>

<style lang="less">
	.message-page {
		background-position: 0 0;
		background-size: 100%;
		background-repeat: no-repeat;
		background-image: url("https://mrrplus.oss-cn-beijing.aliyuncs.com/photo/system/22cf01ee-4a27-4a65-a032-9ceb6265ac2c.png");

		.message-header {
			padding: 25rpx 30rpx 0rpx 25rpx;
			display: flex;
			align-items: center;
			justify-content: space-between;

			.message-header-left {
				width: 191rpx;
				height: 69rpx;
			}

			.message-header-right {
				display: flex;
				align-items: center;
				gap: 30rpx;

				.icon-broom {
					width: 38rpx;
					height: 37rpx;
				}

				.icon-dots {
					display: flex;
					align-items: center;
					gap: 6rpx;

					.dot {
						width: 6rpx;
						height: 6rpx;
						border-radius: 50%;
						background-color: #333;
					}
				}
			}
		}

		.message-state {
			box-sizing: border-box;
			padding: 30rpx 110rpx;
			margin-top: 33rpx;
			width: 100%;
			height: 212rpx;
			background-size: 100%;
			background-repeat: no-repeat;
			background-image: url("https://mrrplus.oss-cn-beijing.aliyuncs.com/photo/system/110df663-bb15-4b63-a46e-571b65c30955.png");

			.message-state-card {
				width: 148rpx;
				display: flex;
				flex-wrap: wrap;
				justify-content: center;
				position: relative;

				.state-card-num {
					position: absolute;
					left: 112rpx;
					top: -13rpx;
					padding: 3rpx 12rpx;
					border-radius: 19rpx;
					background: #ED5B36;
					font-weight: 500;
					font-size: 24rpx;
					color: #FFFFFF;
					line-height: 33rpx;
					text-align: left;
				}

				.state-card-img {
					width: 148rpx;
					height: 94rpx;
				}

				.state-card-text {
					margin-top: 18rpx;
					font-weight: 400;
					font-size: 28rpx;
					color: #1F0508;
					line-height: 40rpx;
					text-align: left;
				}
			}
		}

		/* 订单卡片列表 */
		.message-list {
			padding: 0rpx;
			margin-top: 20rpx;
		}

		.message-item {
			background-color: #ffffff;
			border-radius: 20rpx;
			padding: 20rpx;
			margin-bottom: 20rpx;
			box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.02);
		}

		/* 头部：标题和时间两端对齐 */
		.item-header {
			display: flex;
			justify-content: space-between;
			align-items: center;
			margin-bottom: 40rpx;
		}

		.item-title-group {
			display: flex;
			align-items: center;
		}

		.item-icon {
			width: 57rpx;
			height: 49rpx;
			margin-right: 10rpx;
			flex-shrink: 0;
		}

		.item-title {
			font-family: PingFangSC, PingFang SC;
			font-weight: 500;
			font-size: 28rpx;
			color: #333333;
			line-height: 40rpx;
			text-align: left;
		}

		.item-dot {
			width: 16rpx;
			height: 16rpx;
			border-radius: 50%;
			background-color: #E8101E;
			margin-left: 8rpx;
		}

		.item-time {
			font-family: PingFangSC, PingFang SC;
			font-weight: 400;
			font-size: 24rpx;
			color: #A5A5A5;
			line-height: 33rpx;
			text-align: left;
			flex-shrink: 0;
			margin-left: 20rpx;
		}

		/* 主体：左右结构 */
		.item-body {
			display: flex;
			flex-direction: row;
			align-items: center;
			gap: 59rpx;
		}

		.item-info {
			flex: 1;
			display: flex;
			flex-direction: column;
			gap: 5rpx;
			font-weight: 400;
			font-size: 26rpx;
			color: #666666;
			line-height: 37rpx;
		}

		.item-order {
			word-break: break-all;
			letter-spacing: 0;
		}

		.item-desc {
			word-break: break-all;
		}

		.item-product-img {
			width: 100rpx;
			height: 100rpx;
			border-radius: 10rpx;
			flex-shrink: 0;
		}
	}

	.common-list {
		background: #f5f5f5 !important;

	}
</style>