<template>
  <view class="mind">
	  <!-- tab栏 切孩子 -->
	  <view class="top">
	  		<view class="top_t" @click="getTab(ite.val)" :class="[tabval==ite.val?'act':'',daflag?'top_tda':'']"  v-for="(ite,idx) in studentlist" :key="idx">{{ite.tit}}<image :src="tapline" class="line"></image></view>
	  </view>
	  <view class="pt12">
		  <xl-list :datype='datype' :datalist="list" :wtdt="wtdt" @getDel="getDel"></xl-list>
	  </view>
	  <view style="height: 100rpx;" v-if="checkPermi(['live:old:add'])&&!daflag"></view>
	  <view class="mbtn" v-if="checkPermi(['live:old:add'])&&!daflag" @click="getAdd">更新身高体重状态</view>
  </view>
</template>

<script>
	import xlList from "@/mine/components/list/list.vue"
	import {getliveListFn,getliveDelFn} from "@/api/mine/files.js"
	import {checkPermi,checkRole} from "@/utils/permission"; // 权限判断函数
  export default {
	  components:{
		  xlList
	  },
	data(){
		return{
			sucimg:require("@/mine/static/score/ncheck.png"),
			tapline:require("@/static/images/hline.png"),
			list:[],
			datype:6,
			pageSize: 10,
			pageNum: 1,
			reachflag: true,
			wtdt: '',
			tabval:'',
			actidx:-1,
			daflag:false,
			studentlist:[]
		}
	},
	onUnload() {
		uni.$off('refreshlivelist')
	},
	onReachBottom() {
		if (this.reachflag) {
			this.pageNum++
			this.getDataFn()
		}
	},
	onLoad: function(e) {
		uni.$on('refreshlivelist', (e) => {
			this.reachflag = true;
			this.pageNum = 1;
			this.list = [];
			this.getDataFn();
		})
		if(e.type&&e.type=='da'){
			this.daflag=true;
			this.tabval=e.id
			this.studentlist=[{
				tit:e.name,
				val: e.id,
			}]
		}else{
			var roles=this.$store.state.user.choseroles;
			var newArr=this.$store.state.user.student;
			// console.log(newArr,1)
			this.tabval=newArr[0].id
			this.studentlist = newArr.map(v => {
				return {
					tit:v.studentName,
					val: v.id,
				}
			})
		}
		this.getDataFn()
	},
	methods:{
		checkPermi,checkRole,
		getLook(e) {
			if (this.actidx == e) {
				this.actidx = -1
			} else {
				this.actidx = e;
			}
		},
		toTree2(arr){
			var list=this.list;
			let newA = JSON.parse(JSON.stringify(arr));
			if(list&&list.length){
				list.forEach(ite=>{
					for(var i =0;i<newA.length;i++){
						if(ite.ayear === newA[i].ayear&&ite.amonth === newA[i].amonth){
							ite.children.push(newA[i])
						}else{
							var obj={
								ayear:newA[i].ayear,
								amonth:newA[i].amonth,
								check:true,
								children:[]
							}
							obj.children.push(newA[i])
							list.push(obj)
						}
					}
				})
				return list
			}else{
				var newArrs = [];
				var larr=[]
				const map = new Map();
				newArrs = newA.filter(v => !map.has(v.ayear)&&!map.has(v.amonth) && map.set(v.ayear, 1)&& map.set(v.amonth, 1));
				newArrs.forEach(ite=>{
					var obj={
						ayear:ite.ayear,
						amonth:ite.amonth,
						check:true
					}
					larr.push(obj)
				})
				larr.forEach(ite=>{
					ite.children = newA.filter(e => {
					    return ite.ayear === e.ayear&&ite.amonth === e.amonth
					})
				})
				return larr
			}
		},
		getAdd(){
			this.$tab.navigateTo(`/mine/pages/files/height`)
		},
		getTab(idx){
			this.tabval=idx;
			this.list=[];
			this.reachflag=true;
			this.pageNum=1;
			this.getDataFn();
		},
		getDel(id){
			var that=this;
			getliveDelFn(id).then(res=>{
				if (res.code == 200) {
					that.$toast('删除成功')
						setTimeout(function(){
							that.reachflag = true;
							that.pageNum = 1;
							that.list = [];
							that.getDataFn();
						},1500)
				} else {
					that.$toast(res.msg)
				} 
			})
		},
		getDataFn(a) {
			var params = {
				pageSize: this.pageSize,
				pageNum: this.pageNum,
				studentId:this.tabval
			}
			getliveListFn(params).then(res => {
				if (res.code == 200) {
					this.total = res.total;
					if (res.rows.length < this.pageSize) {
						this.reachflag = false
						this.wtdt = '到底了~';
					} else {
						var num = parseInt(res.rows.length) + parseInt(this.pageSize) * parseInt(this.pageNum -1)
						if (num < res.total) {
							this.reachflag = true
							this.wtdt = '上拉加载更多'
						} else {
							this.reachflag = false
							this.wtdt = '到底了~';
						}
					}
					// 数据处理
					var newArr=res.rows;
					if(newArr&&newArr.length){
						var timeArr=[];
						Object.keys(newArr).some((key) => {
							var time=newArr[key].createTime.split(' ');
							var a=time[0].split('-')
							newArr[key].ayear=a[0];
							newArr[key].amonth=a[1];
						})
						this.list=this.toTree2(newArr)
					}else{
						this.list=[]
					}
				} else {
					this.$toast(res.msg)
				}
			})
		},
	},
	
  }
</script>

<style lang="scss" scoped>
.mind{padding: 100rpx 36rpx 0rpx;min-height: 100vh;background-color: #ffffff;box-sizing: border-box;
	.mbtn{position: fixed;left: 0;right: 0;bottom: 0;height: 100rpx;display: flex;align-items: center;justify-content: center;z-index: 2;background: #1f57e6;font-size: 34rpx;font-weight: 500;
color: #FFFFFF;}
	.top{height: 96rpx;border-bottom: 2rpx solid #E5E5E5;position: fixed;top: 0;left: 0;right: 0;overflow-x: scroll;display: flex;align-items: center;z-index: 1;background-color: #ffffff;
		.top_t{position: relative;font-size: 30rpx;font-weight: 500;color: #AAAAAA;height: 94rpx;line-height: 94rpx;padding:0 20rpx;flex:1 0 auto;text-align: center;
			&.act{color: #161616;font-weight: bold;
				.line{display: block;}
				// &::after{content: '';width: 42rpx;height: 10rpx;background: #1f57e6;border-radius: 6rpx;position: absolute;left: 50%;margin-left: -21rpx;bottom: 12rpx;}
			}
			&.top_tda{flex: 0 0 auto !important;padding: 0 40rpx;}
			.line{width: 40rpx;height: 12rpx;position: absolute;left: 50%;margin-left: -20rpx;bottom: 16rpx;display: none;}
		}
	}
}

</style>
