<template>
	<view class="xiaoxi-pages">
		<view class="xiaoxi-header">
			<u-navbar :fixed="false" bgColor='rgba(0,0,0,0)' placeholder :safeAreaInsetTop="true">
				<view slot="left" class="xiaoxi-tabs u-flex u-row-between" style="margin-left: -11rpx;">
					<view class="tabs-item u-flex-col u-col-center u-flex-1" :class="{'tabs-item1':current == 1}"
						@click="change(1)" style="position: relative;">
						<text>消息</text>
						<u-badge v-if="read" :absolute='true' :isDot="true" type="error" :offset="[0,10]"></u-badge>
						<text></text>
					</view>
					<view class="tabs-item u-flex-col u-col-center u-flex-1" :class="{'tabs-item1':current == 2}"
						@click="change(2)" style="position: relative;">
						<text>通知</text>
						<u-badge :absolute='true' :isDot="true" type="error" :offset="[0,10]"
							v-if="news_length > 0"></u-badge>
						<text></text>
					</view>
				</view>
			</u-navbar>
			<view v-if="current == 1" class="tabs-header u-flex">
				<view class="u-flex tabs-header-item" :class="{'tabs-header-active' : tabs1 == 0}"
					@click="changetabs1(0)">
					<text>全部</text>
				</view>
				<view class="u-flex tabs-header-item" :class="{'tabs-header-active' : tabs1 == 1}"
					style="position: relative;" @click="changetabs1(1)">
					<u-badge v-if="read" :absolute='true' :isDot="true" type="error" :offset="[0,-10]"></u-badge>
					<text>未读</text>
				</view>
			</view>
			<view v-if="current == 2" class="tabs-header u-flex">
				<view style="position: relative;" class="u-flex tabs-header-item"
					:class="{'tabs-header-active' : tabs2 == index}" v-for="(item,index) in tabs2list" :key="index"
					@click="changetabs2(index)" v-if="index != 1">
					<text>{{item}}</text>
					<u-badge v-if='index==0&&ordershow' :absolute='true' :isDot="true" type="error"
						:offset="[0,-10]"></u-badge>
					<u-badge v-if='index==1&&infoshow' :absolute='true' :isDot="true" type="error"
						:offset="[0,-10]"></u-badge>
				</view>

			</view>
		</view>
		<view class=" xiaoxi-list" v-if="current == 1">
			<view class="xiaoxi-item u-flex u-row-between" v-for="(item,index) in list" :key="index"
				@click="tochat(index)">
				<view class="item-head">
					<!-- item.unReadCount -->
					<u-badge :absolute='true' type="error" :value="item.unReadCount" :offset="[0,0]"></u-badge>
					<image :src="item.user.avatar" mode=""></image>
				</view>
				<view class="xiaoxi-right u-flex-1">
					<view class="right-top u-flex u-row-between">
						<text>{{item.user.username}}</text>
						<text v-if='item.user.company_name'
							class="u-line-1">{{item.user.company_name || '' }}·{{item.user.type || ''}}</text>
						<text>{{ renderTime(item.lastMessage.time)}}</text>
					</view>
					<view class="right-down u-line-1" v-if="item.lastMessage && item.lastMessage.type == 'txt'">
						{{ item.lastMessage.msg }}
					</view>
					<view class="right-down u-line-1" v-if="item.lastMessage && item.lastMessage.type == 'img'">
						[图片]
					</view>
					<view class="right-down u-line-1" v-if="item.lastMessage && item.lastMessage.type == 'audio'">
						[语音]
					</view>
				</view>
			</view>
		</view>
		<view class="tongzhi-list" v-if="current == 2">
			<view class="tongzhi-item" v-for="(item,index) in list" :key="index" @click="info2(item)">
				<view v-if="tabs2 == 0">
					<view class="tongzhi-header u-flex u-row-between">
						<view class="tongzhi-title u-flex">
							<text>{{item.title}}</text>
							<text v-if="item.is_view == 0"></text>
						</view>
						<view class="tongzhi-time">
							{{item.createtime}}
						</view>
					</view>
					<view class="tongzhi-tips">
						{{item.content}}
					</view>
					<view class="tongzhi-body">
						<view class="tongzhi-body u-flex u-row-between"
							style="padding: 0;background: rgba(0, green, blue, 0);">
							<view class="body-title u-flex">
								<text></text>
								<text>{{item.info.job.job_name}}</text>
							</view>
							<view class="money">{{item.info.job.salary_min}}-{{item.info.job.salary_max}}K
								<text v-if="item.info.job.salary_structure">·{{item.info.job.salary_structure}}</text>
							</view>
						</view>
						<view class="body-name u-flex">
							<text></text>
							<text class="name-text">{{item.info.company.name || ''}}</text>
							<text class="name-text"
								style="margin-left: 5rpx;">{{item.info.company.category_name || ''}}</text>
						</view>
						<view class="body-address u-flex">
							<text class="text1">{{item.info.job.work_province}}·{{item.info.job.work_city}}</text>
							<text class="text2"></text>
							<text class="text1">{{item.info.job.experience_name}}</text>
							<text class="text2"></text>
							<text class="text1">{{item.info.job.education_name}}</text>
						</view>
					</view>
				</view>
				<view v-if="tabs2 == 1">
					<view class="tongzhi-header u-flex u-row-between">
						<view class="tongzhi-title u-flex">
							<text>{{item.title}}</text>
							<text v-if="item.is_view == 0"></text>
						</view>
						<view class="tongzhi-time">
							{{item.createtime}}
						</view>
					</view>
					<view class="tongzhi-tips">
						{{item.content}}
					</view>
					<view class="xitong-box" v-if="item.info.job_name">
						<view class="xitong-title u-flex">{{item.info.job_name}}</view>
						<view class="xitong-address u-flex">
							<text class="text1">{{item.info.work_city}}</text>
							<text class="text2"></text>
							<text class="text1">{{item.info.experience_name}}</text>
							<text class="text2"></text>
							<text class="text1">{{item.info.education_name}}</text>
						</view>
					</view>
				</view>
				<view v-if="tabs2 == 2">
					<view class="zixun-box">
						<view class="zixun-title">
							{{item.title}} {{item.createtime}}
						</view>
						<image :src="item.info.image" class="zixun-img" mode="aspectFill"></image>
						<view class="zixun-tips">
							{{item.content}}
						</view>
					</view>
				</view>
				<view class="tongzhi-down u-flex u-row-between">
					<text class="text">查看详情</text>
					<u-icon name="arrow-right" color="#0C66C2"></u-icon>
				</view>
			</view>
		</view>
		<view class="" style="border: 40rpx;"></view>
		<view style="height: 70vh;display: flex;justify-content: center;align-items: center;" v-if="list.length == 0">
			<u-empty text="暂无数据" mode="list"></u-empty>
		</view>
	</view>
</template>

<script>
	import {
		conn
	} from '@/utils/WebIM';
	import {
		renderTime
	} from '@/utils/index'
	import {
		get_list,
		notification,
		getEmchatUsersData,
		cget_total_number
	} from "@/units/inquire.js"
	export default {
		data() {
			return {
				current: 1,
				tabs1: 0,
				tabs1list: ['全部', '未读'],
				tabs2: 0,
				tabs2list: ['应聘通知', '系统通知', '资讯通知'],
				page: 1,
				total: 0,
				list: [],
				read: false,
				renderTime,
				news_length: 0,
				ordershow: false,
				infoshow: false
			}
		},
		onLoad() {

		},
		onShow() {
			// if (this.read || this.news_length > 0) {
			// 	uni.showTabBarRedDot({
			// 		index: 3,
			// 	})
			// }
			this.read = false
			this.getlist()
			this.getlist1()
			this.getnum()
		},
		onReachBottom() {
			if (this.total != this.list.length) {
				this.page++
				this.getlist()
			}
		},
		methods: {
			getnum() {
				cget_total_number().then((res) => {
					this.news_length = res.data.number
				})
			},
			change(index) {
				this.current = index
				this.list = []
				this.tabs2 = 0
				this.tabs1 = 0
				this.getlist()
			},
			tochat(index) {
				uni.navigateTo({
					url: "/pagesC/chat?user_no=" + this.list[index].user.user_no
				})
			},
			info2(item) {
				notification({
					id: item.id
				}).then(res => {
					item.is_view = 1
				})
				if (this.tabs2 == 0) {
					uni.navigateTo({
						url: "/pagesD/order-info?id=" + item.info.id
					})
				}
				if (this.tabs2 == 1) {
					if (item.source_type == 'job') {
						uni.navigateTo({
							url: "/pagesB/zhiwei-info2?id=" + item.info.id
						})
					}
				}
				if (this.tabs2 == 2) {
					uni.navigateTo({
						url: "/pagesD/zixun-info?id=" + item.info.id
					})
				}
			},
			handlecurrent(index) {
				this.current = index
				this.page = 1
				this.total = 0
				this.list = []
				this.getlist()
			},
			changetabs1(index) {
				this.tabs1 = index
				this.page = 1
				this.total = 0
				this.list = []
				this.getlist()
			},
			changetabs2(index) {
				this.tabs2 = index
				this.page = 1
				this.total = 0
				this.list = []
				this.getlist()
			},
			getlist1() {
				get_list({
					page: this.page,
					type: 'info'
				}).then(res => {
					this.total = res.data.total
					res.data.data.forEach(item => {
						if (item.is_view == 0) {
							this.infoshow = true
						}
					})
				})
			},
			getlist() {
				let that = this
				if (this.current == 1) {
					const options = {
						pageSize: 50,
						cursor: '',
					}
					conn.getServerConversations(options).then((res) => {
						that.list = res.data.conversations
						console.log(that.list);
						let list = []
						for (var i = 0; i < that.list.length; i++) {
							if (that.list[i].unReadCount > 0) {
								if (that.tabs1 == 1) {
									list.push(that.list[i])
								}
								this.$set(this, 'read', true)
							}
						}
						if (this.read || this.news_length > 0) {
							uni.showTabBarRedDot({
								index: 3,
								success(res) {
									console.log(res);
								}
							})

						} else {
							uni.hideTabBarRedDot({
								index: 3,
								success(res) {
									console.log(res);
								}
							})
						}
						if (that.tabs1 == 1) {
							that.list = list
						}
						// if (that.tabs1 == 1) {
						// 	for (var i = 0; i < that.list.length; i++) {
						// 		if (that.list[i].unReadCount > 0) {
						// 			list.push(that.list[i])
						// 		}
						// 	}
						// 	that.list = list
						// }
						let ids = ''
						if (that.list.length > 0) {
							for (var i = 0; i < that.list.length; i++) {
								ids += that.list[i].conversationId + ','
							}
							that.getData(ids)
						}
					}).catch((error) => {
						console.log('失败', error);
					})
				}
				if (this.current == 2) {
					get_list({
						page: this.page,
						type: {
							0: 'order',
							1: 'system',
							2: 'info'
						} [this.tabs2]
					}).then(res => {
						this.total = res.data.total
						if (this.page == 1) {
							this.list = res.data.data
						} else {
							this.list = this.list.concat(res.data.data)
						}
						// this.list = this.list.concat(res.data.data)
						this.list.forEach(item => {
							if (this.tabs2 == 0 && item.is_view == 0) {
								this.ordershow = true
							}
							if (this.tabs2 == 2 && item.is_view == 0) {
								this.infoshow = true
							}
						})
					})
				}
			},
			getData(ids) {
				let that = this
				ids = ids.slice(0, -1)
				getEmchatUsersData({
					user_no: ids,
				}).then((res) => {
					if (res.code == 1) {
						for (var i = 0; i < that.list.length; i++) {
							that.$set(that.list[i], 'user', res.data[i])
						}
					} else {
						that.$u.toast(res.msg)
					}
				})
			},
		}
	}
</script>

<style lang="scss">
	.xiaoxi-pages {
		.tongzhi-list {
			padding: 1rpx 0;

			.tongzhi-item {
				margin: 20rpx auto;
				width: 702rpx;
				background: #FFFFFF;
				border-radius: 20rpx;
				padding: 0 24rpx;

				.xitong-box {
					width: 100%;
					background: rgba(243, 243, 243, 0.5);
					border-radius: 12rpx;
					padding: 24rpx;

					.xitong-address {
						.text1 {
							font-size: 26rpx;
							font-family: PingFangSC-Regular, PingFang SC;
							font-weight: 400;
							color: #666666;
						}

						.text2 {
							height: 20rpx;
							border: 2rpx solid #E5E5E5;
							margin: 0 16rpx;
						}
					}

					.xitong-title {
						font-size: 32rpx;
						font-family: PingFangSC-Regular, PingFang SC;
						font-weight: 400;
						color: #222222;
						margin-bottom: 20rpx;
					}
				}

				.zixun-box {
					.zixun-tips {
						font-size: 30rpx;
						font-family: PingFangSC-Regular, PingFang SC;
						font-weight: 400;
						color: #333333;
						padding: 24rpx 0;
					}

					.zixun-img {
						width: 100%;
						height: 284rpx;
					}

					.zixun-title {
						padding: 24rpx 0 20rpx 0;
						font-size: 24rpx;
						font-family: PingFangSC-Regular, PingFang SC;
						font-weight: 400;
						color: #888888;
					}
				}

				.tongzhi-down {
					height: 86rpx;
					border-top: 2rpx solid #F0F0F0;

					.text {
						font-size: 26rpx;
						font-family: PingFangSC-Regular, PingFang SC;
						font-weight: 400;
						color: #0C66C2;
					}
				}

				.tongzhi-body {
					background: rgba(243, 243, 243, 0.5);
					border-radius: 12rpx;
					padding: 20rpx;
					margin-bottom: 24rpx;

					.body-address {
						padding: 0 32rpx;

						.text2 {
							height: 20rpx;
							border: 2rpx solid #E5E5E5;
							margin: 0 14rpx;
						}

						.text1 {
							font-size: 26rpx;
							font-family: PingFangSC-Regular, PingFang SC;
							font-weight: 400;
							color: #666666;
						}
					}

					.body-name {
						margin-bottom: 20rpx;

						text:first-child {
							width: 12rpx;
							height: 12rpx;
							background: #0C66C2;
							border-radius: 100rpx;
							margin-right: 20rpx;
						}

						text :nth-child(2) {
							width: 220rpx;
							overflow: hidden;
							white-space: nowrap;
							text-overflow: ellipsis;
							font-size: 24rpx;
							font-family: PingFangSC, PingFang SC;
							font-weight: 400;
							color: #555555;

						}

						text:last-child {
							font-size: 24rpx;
							font-family: PingFangSC, PingFang SC;
							font-weight: 400;
							color: #555555;
						}
					}

					.body-title {
						// margin-bottom: 28rpx;

						text:first-child {
							width: 12rpx;
							height: 12rpx;
							background: #131415;
							margin-right: 20rpx;
							border-radius: 100rpx;
						}

						text:last-child {
							font-size: 32rpx;
							font-family: PingFangSC-Regular, PingFang SC;
							font-weight: 400;
							color: #222222;
						}
					}
				}

				.tongzhi-tips {
					font-size: 28rpx;
					font-family: PingFangSC-Regular, PingFang SC;
					font-weight: 400;
					color: #666666;
					margin-bottom: 24rpx;
				}

				.tongzhi-header {
					height: 96rpx;

					.tongzhi-time {
						font-size: 24rpx;
						font-family: SFPro-Regular, SFPro;
						font-weight: 400;
						color: #666666;
					}

					.tongzhi-title {
						text:first-child {
							font-size: 32rpx;
							font-family: PingFangSC-Medium, PingFang SC;
							font-weight: 500;
							color: #222222;
							margin-right: 16rpx;
						}

						text:nth-child(2) {
							width: 12rpx;
							height: 12rpx;
							background: #F2413A;
							border-radius: 100rpx;
						}
					}
				}
			}
		}

		.money {
			font-size: 32rpx;
			font-family: JDZhengHT-Regular, JDZhengHT;
			font-weight: 400;
			color: #0C66C2;
		}

		.xiaoxi-list {
			width: 702rpx;
			background: #FFFFFF;
			border-radius: 20rpx;
			margin: 20rpx auto;

			.xiaoxi-item {
				padding: 24rpx 20rpx;

				.xiaoxi-right {
					min-width: 1rpx;

					.right-down {
						font-size: 26rpx;
						font-family: PingFangSC-Regular, PingFang SC;
						font-weight: 400;
						color: #444444;
					}

					.right-top {
						margin-bottom: 18rpx;

						text:first-child {
							font-size: 30rpx;
						}

						text:nth-child(2) {
							font-size: 22rpx;
							font-family: PingFangSC-Regular, PingFang SC;
							font-weight: 400;
							color: #888888;
							margin: 0 8rpx;
						}

						text:last-child {
							font-size: 24rpx;
							font-family: SFPro-Regular, SFPro;
							font-weight: 400;
							color: #999999;
						}
					}
				}

				.item-head {
					width: 96rpx;
					height: 96rpx;
					border-radius: 100rpx;
					margin-right: 20rpx;
					position: relative;

					image {
						width: 96rpx;
						height: 96rpx;
						border-radius: 100rpx;
					}
				}
			}
		}

		.xiaoxi-header {
			position: sticky;
			top: 0;
			left: 0;
			width: 750rpx;
			z-index: 99;
			background: linear-gradient(180deg, #EDF6FF 0%, #F8FAFD 100%);

			.tabs-header {
				height: 88rpx;
				padding: 0 8rpx;

				.tabs-header-item {
					margin: 0 24rpx;

					text {
						font-size: 26rpx;
						font-family: PingFangSC-Regular, PingFang SC;
						font-weight: 400;
						color: #777777;
					}
				}

				.tabs-header-active {
					text {
						color: #0C66C2;
					}
				}
			}

			.xiaoxi-tabs {
				width: 250rpx;

				.tabs-item {
					text:first-child {
						font-size: 32rpx;
						font-family: PingFangSC-Medium, PingFang SC;
						font-weight: 500;
						color: #777777;
						position: relative;
						z-index: 1;
					}

					text:last-child {
						width: 76rpx;
						height: 12rpx;
						border-radius: 100rpx;
						margin-top: -14rpx;
					}
				}

				.tabs-item1 {
					text:first-child {
						font-size: 36rpx;
						font-family: PingFangSC-Medium, PingFang SC;
						font-weight: 500;
						color: #222222;
					}

					text:last-child {
						background: linear-gradient(270deg, #208EFF 0%, rgba(28, 159, 227, 0) 100%);
					}
				}
			}
		}
	}

	page {
		background-color: #F3F3F3;
	}
</style>