<template>
	<view class="xiaoxi">
		<view class="xiaoxi-navbar">
			<u-navbar :is-back="false" title="消息">
				<view slot="right">
					<view class="search" @click="clearInfo()">
						<image src="@/static/icon-clear.png"></image>
					</view>
				</view>
			</u-navbar>
		</view>
		<view class="xiaoxi-tab">
			<view class="tab">
				<u-tabs :list="list" bg-color="transparent" :is-scroll="false" active-color="#222222" inactive-color="#666666" font-size="30" :active-item-style="activeStyle" bar-height="10" bar-width="60" v-model="current" @change="changeTabs"></u-tabs>
			</view>
		</view>
		<view class="xiaoxi-select" v-if="current == 1">
			<view class="select">
				<view class="item" @click="selectItem(0)" :class="tabIndex == 0?'item-on':''">好友</view>
				<view class="item" @click="selectItem(1)" :class="tabIndex == 1?'item-on':''">关注</view>
				<view class="item" @click="selectItem(2)" :class="tabIndex == 2?'item-on':''">粉丝</view>
			</view>
		</view>
		<view class="xiaoxi-list">
			<view class="item" v-if="current == 0" @click="openPage('/pages/xiaoxi/dashan')">
				<view class="pic"><image src="@/static/dashan.png"><view class="num" v-if="count">{{count<=99?count:"+99"}}</view></image></view>
				<view class="info">
					<view class="name">搭讪消息</view>
					<view class="desc">{{count?'您有'+count+'条搭讪消息':'暂无搭讪消息'}}</view>
				</view>
			</view>
			<view class="item" v-if="current == 0" @click="openPage('/pages/xiaoxi/xitong')">
				<view class="pic"><image src="@/static/xitong.png"></image></view>
				<view class="info">
					<view class="name">系统消息</view>
					<view class="desc">{{xiaoxiinfo.noticeTitle?xiaoxiinfo.noticeTitle:'暂无消息'}}</view>
				</view>
			</view>
			<view class="item" @click="openPage('/pages/xiaoxi/fangke')">
				<view class="pic"><image src="@/static/kan.png"></image></view>
				<view class="info">
					<view class="name">谁看过我</view>
					<view class="desc">对你感兴趣的人一直默默关注你</view>
				</view>
			</view>
			<u-swipe-action v-for="(a,i) in frindList" :index="i" :key="a.userid" @click="click" :options="options">
				<view class="item" @click.stop="toChat(a)">
					<view class="pic"><image :src="a.faceImage"></image><view class="num" v-if="a.noReadCount">{{a.noReadCount<=99?a.noReadCount:"+99"}}</view></view>
					<view class="info">
						<view class="name">{{a.nickname}}<text>{{a.createTime}}</text></view>
						<view class="desc" v-if="a.type==3" v-html="a.message"></view>
						<view class="desc" v-if="a.type==1">你有一条新的语音！</view>
						<view class="desc" v-if="a.type==2">你有一条新的视频！</view>
						<view class="desc" v-if="a.type==4">你有一张新的图片！</view>
						<view class="desc" v-if="a.type==5">你有一个新的礼物！</view>
						<view class="desc" v-if="a.type==6" v-html="a.message"></view>
					</view>
				</view>
			</u-swipe-action>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				tabIndex: 0,
				current: 0,
				list:[{'name': '消息'},{'name': '好友'}],
				activeStyle:{'fontSize': '35rpx'},
				frindList: [],
				type: 0,
				count: 0,
				xiaoxiinfo: {},
				options: [
					{
						text: '删除',
						style: {
							backgroundColor: '#dd524d'
						}
					}
				]
			}
		},
		onShow() {
			this.getList();
			this.dashaCount();
			this.getXiaoxiList();
			this.watchSocket();
		},
		methods: {
			watchSocket() {
				const that = this;
				uni.$on("toChatXiaoxiObj",function (res) {
					const obj = res.data?JSON.parse(res.data):{}
					if(obj.topic == 'message'){
						if(obj.type <= 7){
							if(uni.getStorageSync("userInfo").id == obj.sellerId){
								if(obj.userMsgVoice == 1){
									uni.$emit("playAudio")
								}
								if(obj.userMsgShock == 1){
									uni.vibrateLong({
										complete: function (res) {
											console.log('success',res);
										}
									});
								}
								that.getList()
							}
						}
					}
				});
			},
			//去聊天
			async toChat(a) {
				const { code, data } = await this.$api.findZegoToken({
					userId: uni.getStorageSync("userInfo").id,
					sellerId: a.userid
				})
				if(code == 200){
					uni.setStorageSync("chatToken",data.token);
					uni.navigateTo({
						url: "/pages/users/chat/index?id="+a.userid+"&roomId="+data.roomId
					})
				}
			},
			async click(i){
				const { code, data, msg } = await this.$api.cleanOneMsg({
					msgId: this.frindList[i].msgId,otherUserStatus: this.frindList[i].otherUserStatus, userId: uni.getStorageSync("userInfo").id
				})
				if(code == 200){
					uni.showToast({
						title: "删除成功"
					})
					this.getList();
				}else{
					uni.showToast({
						title: msg,
						icon: "none",
						position: "bottom"
					})
				}
			},
			async getXiaoxiList() {
				const {code,data} = await this.$api.systemNoticeList({
					userId: uni.getStorageSync("userInfo").id,
					limit: 1,
					page: 1
				})
				if(code == 200){
					this.xiaoxiinfo = data.toal == 0?{}:data.list[0]
				}
			},
			async dashaCount() {
				const { code, data } = await this.$api.dashaCount({
					userId: uni.getStorageSync("userInfo").id
				})
				if(code == 200){
					this.count = data
				}
			},
			async getList() {
				const { code, data } = await this.$api.findFrindList({
					userId: uni.getStorageSync("userInfo").id,
					type: this.type
				})
				if(code == 200){
					this.frindList = data
				}
			},
			openPage(url) {
				uni.navigateTo({
					url
				})
			},
			//清空
			clearInfo() {
				const that = this;
				uni.showModal({
					title: "提示",
					content: "确定要清空所有未读消息吗？",
					confirmColor: "#000",
					async success(res){
						if(res.confirm){
							const { code, data } = await that.$api.cleanAllMsg({userId: uni.getStorageSync("userInfo").id});
							if(code == 200){
								uni.showToast({
									title: "清除成功"
								})
								that.getList();
								that.dashaCount();
							}
						}
					}
				})
			},
			selectItem(index) {
				this.tabIndex = index;
				if(index == 0){
					this.type = 5
					this.getList();
				}else if(index == 1){
					this.type = 1
					this.getList();
				}else if(index == 2){
					this.type = 3
					this.getList();
				}
			},
			changeTabs(index){
				this.current = index
				if(index==0){
					this.type = 0
					this.getList();
				}else{
					this.type = 5
					this.getList();
				}
			},
		}
	}
</script>

<style scoped lang="scss">
.xiaoxi{
	background-color: #fff;
	min-height: 100vh;
	overflow: hidden;
	&-navbar{
		image{
			width: 45rpx;
			height: 45rpx;
			margin-right: 30rpx;
		}
	}
	&-tab{
		display: flex;
		margin-top: 10rpx;
		overflow: hidden;
		.tab{
			width: 250rpx;
		}
	}
	&-select{
		padding: 20rpx 25rpx;
		overflow: hidden;
		.select{
			display: flex;
			justify-content: center;
			border-radius: 20rpx;
			border: 1px solid #CCCCCC;
			overflow: hidden;
			.item{
				flex: 1;
				text-align: center;
				line-height: 80rpx;
				border-left: 1px solid #CCCCCC;
				&:first-child{
					border-left-color: #fff;
				}
				&-on{
					&:first-child{
						border-left-color: #000000;
					}
					background-color: #000000;
					color: #FFFFFF;
				}
			}
		}
	}
	&-list{
		padding: 0 25rpx 25rpx;
		overflow: hidden;
		.item{
			display: flex;
			align-items: center;
			.pic{
				width: 90rpx;
				height: 90rpx;
				margin-right: 20rpx;
				position: relative;
				.num{
					width: 30rpx;
					height: 30rpx;
					line-height: 30rpx;
					background: #FC2C2C;
					border-radius: 50%;
					font-weight: 500;
					font-size: 18rpx;
					color: #FFFFFF;
					text-align: center;
					position: absolute;
					right: 0;
					top: 0;
					z-index: 2;
				}
				image{
					width: 100%;
					height: 100%;
					border-radius: 50%;
				}
			}
			.info{
				padding: 30rpx 0;
				width: 550rpx;
				border-bottom: 1px solid #EAEAEA;
				.name{
					font-weight: 400;
					font-size: 30rpx;
					color: #222222;
					text{
						float: right;
						font-weight: 400;
						font-size: 24rpx;
						color: #AAAAAA;
					}
				}
				.desc{
					font-weight: 400;
					font-size: 24rpx;
					color: #AAAAAA;
					margin-top: 15rpx;
						image{
							width: 40rpx;
							height: 40rpx;
						}
				}
			}
		}
	}
}
</style>
