<!-- tabbar消息详情 -->
<template>
	<view>
		<mescroll-body ref="mescrollRef" @init="mescrollInit" top="0" :down="downOption" @down="downCallback"
			:up="upOption" @up="upCallback">
			<block v-for="(item,index) in dataList" :key="index">
				<msg-list :item='item' :index="index" @clickMsgItem='clickMsgItem'></msg-list>
			</block>
		</mescroll-body>
	</view>
</template>

<script>
	import msgList from "@/components/message/msg-list.vue";
	import MescrollMixin from "@/uni_modules/mescroll-uni/components/mescroll-uni/mescroll-mixins.js";
	import mescrollBody from "@/uni_modules/mescroll-uni/components/mescroll-body/mescroll-body.vue";
	import { getMyCrmMsgList,doRead,setAllRead } from "@/api/message.js";
	
	export default {
		mixins: [MescrollMixin], // 注意此处还需使用MescrollMoreItemMixin (必须写在MescrollMixin后面)
		components: {
			mescrollBody,
			msgList
		},
		data() {
			return {
				downOption: {
					auto: true // 不自动加载 (mixin已处理第一个tab触发downCallback)
				},
				upOption: {
					auto: false, // 不自动加载
					page: {
						num: 0, // 当前页码,默认0,回调之前会加1,即callback(page)会从1开始
						size: 10 // 每页数据的数量
					},
					noMoreSize: 1, //如果列表已无数据,可设置列表的总数量要大于半页才显示无更多数据;避免列表数据过少(比如只有一条数据),显示无更多数据会不好看; 默认5
					empty: {
						tip: '~ 暂无数据 ~' // 提示
						// btnText: '去看看'
					}
				},
				itemData: {},
				dataList: [
							 // {"content":"明天上午8点电话预约","un_read_count":0,"scene_name":"客户消息","create_time":"2022-12-03 09:48"},
							 // {"content":"明天上午8点电话预约","un_read_count":0,"scene_name":"客户消息","create_time":"2022-12-03 09:48"},
							 // {"content":"明天上午8点电话预约","un_read_count":0,"scene_name":"客户消息","create_time":"2022-12-03 09:48"}
						  ],
				ids: [],
				index:-1,
			}
		},
		onLoad() {
			this.itemData = this.$Route.query.itemData;
			this.index=this.$Route.query.index;
			this.doReadAct();
		},
		methods: {
			clickMsgItem(index) {
				this.msgTypeFilter(index)
			},
			msgTypeFilter(index) {
				switch (this.itemData.msgType) {
					case 1: //客户消息
					    let data=this.dataList[index];
					    this.$Router.push({
					    	name: 'mycustomerDtl',
					    	params: {
					    		cId:data.objId,
					    		customerUserId:data.companyUserId,
					    		isMyCts:true
					    	}
					    });
						break;
					case 2: //日程消息
						// let obj_djhd = {
						// 	id: this.listData[index].business_id
						// }
						// this.$Router.push({
						// 	name: "activityDetail",
						// 	params: obj_djhd
						// })
						break;
					case 3: //回款消息
						break;
					case 4: //党费缴纳
						
						break;
					case 5: //组织动态
						break;
					case 6: //在线投票
						let obj_zxtp = {
							id: this.listData[index].business_id
						}
						try {
							uni.navigateTo({
								url: "/pages/vote/voteDetail?" + this.param(obj_zxtp)
							})
						} catch (er) {
							//TODO handle the exception
							this.$navTo.push("/pages/vote/voteDetail", obj_zxtp)
						}
						break;
					default: //未知消息
						break;
				}
			},
			param(data) {
				let url = "";
				for (var k in data) {
					let value = data[k] !== undefined ? data[k] : "";
					url += "&" + k + "=" + encodeURIComponent(value);
				}
				return url ? url.substring(1) : "";
			},
			/*上拉加载的回调: 其中page.num:当前页 从1开始, page.size:每页数据条数,默认10 */
			upCallback(page) {
				getMyCrmMsgList(this.itemData.msgType,page.num).then(res => {
						if(res.code==200){
							setTimeout(()=>{
								this.mescroll.endByPage(res.data.list.length, res.data.pages); 
								if(page.num == 1) this.dataList = []; //如果是第一页需手动制空列表
								this.dataList=this.dataList.concat(res.data.list); //追加新数据
							},100);
						}
					},
					rej => {}
				).catch(()=>{
					//联网失败, 结束加载
					this.mescroll.endErr();
				});
			},
			//批量已读
			batchRead(index) {
				let item=this.dataList[index];
				uni.showLoading({title:""});
				let that=this;
				doRead(item.msgType).then(res => {
							if(res.code==200){
							   uni.showToast({ title: "操作成功" });
							   item.isRead=1;
							   that.dataList.splice(index,1,item);//删除
							}else{
								uni.showToast({title: res.msg,icon:"none"});
							}
						},
						rej => {}
				).catch(()=>{
									
				});
			},
			getMyCrmMsgList(){
				getMyCrmMsgList(page.num).then(res => {
						if(res.code==200){
							setTimeout(()=>{
								this.mescroll.endByPage(res.data.list.length, res.data.pages); 
								if(page.num == 1) this.dataList = []; //如果是第一页需手动制空列表
								this.dataList=this.dataList.concat(res.data.list); //追加新数据
							},100);
							
						}
					},
					rej => {}
				).catch(()=>{
					//联网失败, 结束加载
					this.mescroll.endErr();
				});
			},
			doReadAct(){
				doRead(this.itemData.msgType).then(res => {
							if(res.code==200){
							    uni.$emit('refreshMessage', this.index);
							}else{
							    uni.showToast({title: res.msg,icon:"none"});
							}
						},
						rej => {}
				).catch(()=>{
									
				});
			},
		}
	}
</script>

<style lang="scss">
	page {
		background-color: #F5F6F7;
	}
</style>
