<template>
	<view class="hzIndex">
		<hzHeader title="消息" btnName='一键已读' @optionTab="buttonTap"></hzHeader>
		<view class="container hzContent">
			<!-- <view >
					<uni-search-bar placeholder="查询搜索" @confirm="search" cancelButton='none' bgColor="white"></uni-search-bar>
				</view> -->
			<view class="card-box" v-if="noticeList.length > 0" v-for="(item,index) in noticeList" :key="index">
				<view class="left">
					<image class="leftIcon" v-if="item.title == '访客审批'" src="../../static/xxicon/fksp.png" mode="">
					</image>
					<image class="leftIcon" v-if="item.title == '理发预约'" src="../../static/xxicon/lifa.png" mode="">
					</image>
					<image class="leftIcon" v-if="item.title == '外卖预定'" src="../../static/xxicon/waimai.png" mode="">
					</image>
					<image class="leftIcon" v-if="item.title == '洗车预约'" src="../../static/xxicon/xiche.png" mode="">
					</image>
					<!-- <image class="leftIcon" v-else src="../../static/xxicon/waimai.png" mode="">
					</image> -->
				</view>
				<view class="right-content">
					<view class="title-box" @click="goView(item)">
						<view class="title">
							{{item.title}}
						</view>
						<view class="noread">
							<text v-if="item.read_status==0" style="color: #FB8107;">未读</text>
							<text v-if="item.read_status==1" style="color: #999999;">已读</text>
						</view>
					</view>
					<view @click="goView(item)" class="tip">
						<text>
							{{item.text}}
						</text>
					</view>
					<view class="time">
						{{formatDate(item.create_time,'dateTime')}}
					</view>
				</view>
				<!-- <view style="height: 20upx;"></view> -->
			</view>
			<content-none v-else :padTop="35"></content-none>
			<!-- <uni-popup ref="popup" type="center" style="">
					<view style="background: white;width: 80%;height: 520upx;width: 600upx;position: relative;border-radius: 10upx;padding: 14upx;">
						<view style="color: #333;font-size: 30upx;font-weight: bold;padding: 10upx 0px;">
							<text>意见建议：</text>
						</view>
						<view class="footer-btn footer-btn-two" style="position: absolute;margin-bottom: 20upx;">
							<view class="btn btn-col" style="width:40%;margin-left:7%;margin-right: 6%;border-radius: 10upx;" @click="closeCirculation()">取消</view>
							<view class="btn btn-col1" style="width:40%;border-radius: 10upx" @click="submitProblem()">提交</view>
						</view>
					</view>
				</uni-popup> -->
			<load-more :loadingType="loadingType" :contentText="contentText"></load-more>

		</view>

	</view>
</template>

<script>
	import uniSearchBar from '@/components/uni-search-bar/uni-search-bar.vue'
	import loadMore from '@/components/loadMore.vue';
	import contentNone from '@/components/contentNone.vue';
	import {
		getMyNoticeList,
		updateNoticeRead,
		getUnReadNum,
		clearNoticeData
	} from '@/common/api.js';
	import {
		callbackRequest,
		setStorage,
		getStorage,
		alertTip,
		formatDate
	} from '@/common/util.js';
	import UniPopup from '@/components/uni-popup/uni-popup2.vue';
	import {
		mapMutations,
		mapState
	} from 'vuex';
	export default {
		data() {
			return {
				formatDate: formatDate,
				page: 1,
				pageSize: 10,
				loadingType: 0, //0-loading前；1-loading中；2-没有更多了
				contentText: {
					contentdown: "上拉加载更多",
					contentrefresh: "正在加载...",
					contentnomore: "没有更多数据了"
				},
				keyword: '',
				userInfo: getStorage("userInfo"),
				iscId: getStorage("iscId"),
				noticeList: [],
			}
		},
		components: {
			uniSearchBar,
			loadMore,
			contentNone,
			UniPopup
		},
		onLoad(option) {
			// console.log(this.authUrl);
			this.getUnReadNum();
		},
		onShow() {
			this.page = 1;
			this.loadingType = 0;
			this.initData();
			this.getUnReadNum();
		},
		computed: {
			...mapState(['authUrl'])
		},
		methods: {
			getUnReadNum() {
				let data = {
					method: getUnReadNum,
					data: {
						// userId: this.userInfo.id,
						userId: this.iscId,
					}
				};
				callbackRequest(data).then(res => {
					if (res.data.returnCode == 1) {
						if (res.data.returnData > 0) {
							uni.setTabBarBadge({
								index: 1,
								text: res.data.returnData + ''
							})
						} else if (res.data.returnData == 0) {
							uni.hideTabBarRedDot({
								index: 1,
							})
						}
					} else {}
				});
			},
			goView(item) {
				var url = item.url;
				var id = item.id;
				let params = {
					"method": updateNoticeRead,
					data: {
						id: item.id
					}
				}
				callbackRequest(params)
					.then(res => {
						res = res.data;
						if (res.returnCode == 1) {
							if (this.authUrl.indexOf(url.split("?")[0]) == -1) {
								alertTip("暂无访问权限");
							} else {
								uni.navigateTo({
									url
								});
							}
						}
					})
			},
			search(v) {
				this.page = 1;
				this.loadingType = 0;
				this.keyword = v.value;
				this.noticeList = [];
				this.initData();
			},
			buttonTap() {
				uni.showModal({
					title: '确认',
					content: '所有消息都将设置为已读，请确认是否继续？',
					showCancel: true,
					cancelText: '取消',
					confirmText: '确定',
					success: res => {
						if (res.confirm) {
							let params = {
								"method": clearNoticeData,
								data: {
									userId: this.userInfo.id,
								}
							}
							callbackRequest(params)
								.then(res => {
									if (res.data.returnCode == 1) {
										this.page = 1;
										this.loadingType = 0;
										this.initData();
										uni.hideTabBarRedDot({
											index: 1,
										})
									} else {}
								})
						}
					}
				});
			},
			initData() {
				if(this.userInfo.id==this.userInfo.userId&&this.userInfo.userId==this.userInfo.wxOpenId){
					let params = {
						"method": getMyNoticeList,
						data: {
							pageNum: this.page,
							pageSize: this.pageSize,
							userId: this.userInfo.id
						}
					}
					callbackRequest(params).then(res => {
						res = res.data;
						if (res.returnCode == 1) {
							if (this.page == 1) {
								this.noticeList = res.returnData;
							} else {
								if (res.returnData.length > 0) {
									this.noticeList = this.noticeList.concat(res.returnData);
									this.loadingType = 0;
								} else {
									this.loadingType = 2;
								}
							}
							// console.log(this.noticeList)
						} else {
							alertTip(res.returnMsg);
						}
					})
				}

					
			},
			drawtext(text) {
				var reg = /<br\/>g/;
				var rowText = text.replace(/<br\s*\/?>/gi, "\n");
				return rowText;
			}
		},
		onReachBottom() {
			if (this.loadingType !== 0 || this.noticeList.length == 0) {
				return false;
			}
			this.loadingType = 1;
			this.page = this.page + 1;
			this.initData();
		}
	}
</script>

<style lang="scss" scoped>
	@import '@/static/css/common.scss';

	.container {
		// background: #f8f8f8;
		min-height: 100vh;
		padding: 24upx;
	}

	.box {
		display: flex;
		display: -webkit-flex;
	}

	/*水平排列*/
	.box-row {
		flex-direction: row;
	}

	/*上下排列*/
	.box-column {
		flex-direction: column;
	}


	.box-left {
		justify-content: flex-start;
	}

	/*居右*/
	.box-right {
		justify-content: flex-end;
	}

	/*居zhong */
	.box-center {
		justify-content: center;
	}

	/*两端对齐*/
	.box-between {
		justify-content: space-between;
	}


	.box-align-center {
		align-items: center;
	}

	.box-wrap {
		flex-wrap: wrap;
		margin-left: 28upx;
	}

	.sztx-btn {
		font-size: 24upx;
		padding: 6upx;
		margin: 0upx 20upx 20upx 0upx;
		border-radius: 10upx;
		background-color: #00c277;
		color: #FFFFFF;
	}

	// 新样式
	.card-box {
		background: #FFFFFF;
		border-radius: 10*2upx;
		margin-top: 24upx;
		padding: 24upx;
		display: flex;
		flex-direction: row;
		width: 100%;

		.left {
			width: 92upx;
			height: 92upx;
			flex-shrink: 0;

			.leftIcon {
				width: 92upx;
				height: 92upx;
			}
		}

		.right-content {
			margin-left: 24upx;
			width: 100%;

			.title-box {
				width: 100%;
				display: flex;
				flex-direction: row;
				align-items: center;
				justify-content: space-between;

				.title {
					font-weight: 800;
					font-size: 18*2upx;
					color: #333333;
				}

				.noread {
					font-size: 12*2upx;
					color: #FB8107;
				}
			}

			.tip {
				font-size: 28upx;
				color: #555555;
				margin-top: 12upx;
			}

			.time {
				font-size: 24upx;
				color: #999999;
				margin-top: 12upx;
			}
		}
	}
</style>