<template>
	<view>
		<u-toast ref="toast"></u-toast>
		<page-title :showBack="true">消息</page-title>
		<view class="information_top">
			<view class="information-top-le" @click="selectFn">选择</view>
			<view class="information-top-re" @click="delList(true)">全部清空</view>
		</view>
		<view v-if="list.length==0&&emptyControl">
		      <u-empty icon="search" text="未找到您要搜索的内容"></u-empty>
		</view>
		<checkbox-group>
			<view class="information_center" v-for="(item, index) in list" :key="index">
				<view class="center-top">
					<checkbox v-show="flag" @click="changeChecked(item)" :checked="item.checked"/>
					<u--image shape="circle" src="../../static/images/meSh.png" width="50px" height="50px"></u--image>
					<view class="center">
						<view class="center-tit">
							{{ item.title }}
							<text class="center-time">{{ msgTimeChange(item.msgTime) }}</text>
						</view>
						
						<view class="center-con" @click="returnRouter(item)">{{ item.content }}</view>
					</view>
				</view>
			</view>
		</checkbox-group>
		<view class="information_bottom" v-show="flag">
			<view class="qux" @click="flag = false">取消</view>
			<view class="del" @click="delList(false)">删除</view>
		</view>
		<view v-if="total > queryData.pageSize"><uni-load-more :status="loadStatus"></uni-load-more></view>
	</view>
</template>

<script>
import apiMessage from '@/api/system/message.js';
export default {
	data() {
		return {
			list: [],
			queryData: {
				pageIndex: 1,
				pageSize: 10
			},
			total: 0,
			flag: false, //控制勾选按钮的显示
			loadStatus: 'more', //more,loading,noMore, 应该划分出三个
			emptyControl:false,//控制未搜索到内容的显示
		};
	},
	onPullDownRefresh() {
		this.onReachBottom();
	},
	onReachBottom() {
		if(this.list.length<this.total){
			this.loadStatus='loading';
			this.queryData.pageIndex++;
			this.queryMessage(false);
			this.loadStatus='more';
		}else{
			this.loadStatus='noMore';
		}
	},
	methods: {
		returnRouter(item) {
			if (item.refType == 2) {
				uni.navigateTo({
					url: '/pages/resource/articeDetail?id=' + item.refId
				});
			    } else if (item.refType == 4) {
					uni.navigateTo({
						url: '/pages/resource/qaDetail?id=' + item.refId
					});
			} else {
			    if (item.conType == '10') {
					uni.navigateTo({
						url: '/pages/resource/microDetail?id='  + item.refId 
					});
			    } else if (item.conType == '20') {
					uni.navigateTo({
						url: '/pages/resource/courseDetail?id=' +item.refId
					});
			    }
			}
		},
		//控制勾选等操作是否显示
		selectFn() {
			if(this.list.length!=0&&!this.flag){
				this.flag = true;
			}
		},
		//改变checked的值
		changeChecked(val){
			val.checked=!val.checked
		},
		//改变时间格式
		msgTimeChange(msgTime) {
			return msgTime.split(' ')[0];
		},
		queryMessage(flag) {
			if (flag) {
				this.list = [];
				this.queryData.pageIndex = 1;
			}
			uni.showLoading({ title: '加载中...' });
			apiMessage.list(this.queryData).then(res => {
				if (res.status == 200) {
					this.emptyControl=true
					this.total = res.result.count;
					if(res.result.list.length!=0){
						res.result.list.forEach(item => {
							item.checked = false;
							this.list.push(item);
						});
						//设置已读
						let ids=[];
						for(let i=0;i<res.result.list.length;i++){
							if(!res.result.list[i].isRead){
								ids.push(res.result.list[i].id)
							}
						}
						apiMessage.updateIsRead(ids).then(res=>{
						          if(res.status==200){
						            // this.$store.dispatch('refrashMsg');
						          }
						        })
					}
				} else {
					this.$refs.toast.show({ message: '获取数据失败', type: 'error' })
				}
				uni.hideLoading();
			});
		},
		delList(flag) {
			if (this.list.length == 0) {
				return this.$refs.toast.show({ message: '数据为空，无法删除', type: 'error' });
			}
			let ids=[];
			if (flag) {
				ids = this.list.map(v => v.id);
			} else {
				for (let i = 0; i < this.list.length; i++) {
					if (this.list[i].checked) {
						ids.push(this.list[i].id);
					}
				}
			}
			if(ids.length==0){
				return this.$refs.toast.show({ message: '请先进行勾选', type: 'error' });
			}
			apiMessage.del(ids).then(res => {
				if (res.status == 200) {
					this.$refs.toast.show({ message: '删除成功', type: 'success' });
					this.queryMessage(true);
				} else {
					this.$refs.toast.show({ message: '删除失败', type: 'error' });
				}
			});
		}
	},
	onLoad: function(options) {
		this.queryMessage(true);
	}
};
</script>

<style lang="scss" scoped>
.information_top {
	// height: 90upx;
	display: flex;
	justify-content: space-between;

	.information-top-le {
		width: 116upx;
		height: 50upx;
		background: #dfebf4;
		border-radius: 40upx;
		margin-top: 20upx;
		margin-left: 37upx;
		font-size: 24upx;
		color: #999999;
		text-align: center;
		line-height: 50upx;
	}

	.information-top-re {
		width: 156upx;
		height: 48upx;
		background: #e8e8e8;
		font-size: 24upx;
		color: #999999;
		margin-top: 20upx;
		margin-right: 30upx;
		text-align: center;
		line-height: 48upx;
	}
}

.information_center {
	padding: 20upx;
	background: #ffffff;
	margin-top: 18upx;
	display: flex;
	justify-content: space-between;
	.center-top {
		display: flex;
		width: 100%;
		uni-checkbox {
			display: flex;
			justify-content: center;
			align-items: center;
			/deep/ .uni-checkbox-input {
				width: 24upx;
				height: 24upx;
			}
		}
		.center {
			width: 100%;
			margin-left: 20upx;
			display: flex;
			flex-direction: column;
			justify-content: space-around;
			.center-con {
				width: 271px;
				font-size: 24upx;
				color: #999999;
				overflow: hidden;
				text-overflow: ellipsis;
				white-space: nowrap;
			}
			.center-tit {
				font-size: 28upx;
				color: #333333;
				display: flex;
				justify-content: space-between;
				align-items: center;
				.center-time {
					font-size: 24upx;
					color: #cccccc;
				}
			}
		}
	}
}

.information_bottom {
	width: 100%;
	height: 120upx;
	background: #ffffff;
	position: fixed;
	bottom: 0px;
	display: flex;

	.qux {
		width: 316upx;
		height: 76upx;
		border: 1px solid #dcdcdc;
		margin-left: 42upx;
		margin-top: 25upx;
		border-radius: 40upx;
		font-size: 28upx;
		color: #999999;
		text-align: center;
		line-height: 76upx;
	}

	.del {
		width: 316upx;
		height: 76upx;
		background: #6194fe;
		margin-left: 42upx;
		margin-top: 25upx;
		border-radius: 40upx;
		font-size: 28upx;
		color: #ffffff;
		text-align: center;
		line-height: 76upx;
	}
}
</style>
