<template>
	<view class="filebox">
		<!-- 老师有 -->
		<!-- 防溺水系统 -->
		
		<view class="fnbox flex0 bg2" v-if="joinflag">
			<image :src="fziconb"></image>已加入防溺水计划
		</view>
		<view class="fnbox flex0 bg1" v-else @click="getJoinFn">
			<image :src="fzicona"></image>加入防溺水计划
		</view>
		<view class="stutop" v-if="roles!='parents'">
			<view class="search flexc mb12">
				<image :src="searchimg" class="img"></image>
				<input placeholder="输入学生姓名进行搜索" v-model="name" class="flex1 input" />
				<view class="btn" @click="getSearch">搜索</view>
			</view>
			<view class="stutoa flexcj mb10">
				<view class="stutoap">
					<picker :value="classidx" range-key="className" :range="classlist" @change="bindDateChange">
						<view class="flexc">
							<view class=" f15 co16 f500">{{className||'请选择班级'}}</view>
							<image :src="upimg"></image>
						</view>
					</picker>
				</view>
				<view class="stutoap">
					<picker :value="isNearsidx" range-key="text" :range="ynrange" @change="bindDateChangea">
						<view class="flexc">
							<view class=" f15 co16 f500">{{isNearsightednessname||'近视/远视'}}</view>
							<image :src="upimg"></image>
						</view>
					</picker>
				</view>
				<view class="stutoap">
					<picker :value="isMedicalidx" range-key="text" :range="ywrange" @change="bindDateChangeb">
						<view class="flexc">
							<view class=" f15 co16 f500">{{isMedicalHistoryname||'有无病史'}}</view>
							<image :src="upimg"></image>
						</view>
					</picker>
				</view>
			</view>
			<view class="stutoar flex0">
				当前班级学生共有<text>{{count.zs||0}}</text>名；其中男生<text>{{count.nan||0}}</text>名；女生<text>{{count.nv||0}}</text>名； 近视/远视<text>{{count.js||0}}</text>人；留守儿童<text>{{count.ls||0}}</text>人；家庭贫困<text>{{count.pk||0}}</text>人；
			</view>
		</view>
		<view class="flex1 workbox">
			<view class="workboxr">
				<scroll-view scroll-y class="scroll-view"  lower-threshold="40" @scrolltolower="reach_btn">
					<da-list :roles="roles" :datalist="list" :wtdt="wtdt" datype="1" @getDetail="getDetail" @getHeightFn="getHeightFn"></da-list>
					<view style="height: 100rpx;"></view>
				</scroll-view>
			</view>
		</view>
		<!-- 家长 -->
		<!-- v-if="checkPermi(['student:info:add'])" -->
		<view class="addbtn" @click="getAdd" >添加
			<block v-if="roles!='parents'">学生</block>
			<block v-else>孩子</block>
		档案</view>
		<pop-up :type='type' :childrens="tablist" :classlist="classlist" @getChose="getChose" @getClose='getClose' @getJoin="getJoin"></pop-up>
		
	</view>
</template>

<script>
	import {checkPermi,checkRole} from "@/utils/permission"; // 权限判断函数
	import {getDeptList,getselfList} from "@/api/mine/mine.js"
	import daList from "@/mine/components/list/list.vue"
	import popUp from "@/mine/components/popup/popup.vue"
	import {getstuListFn,getstuCountFn} from "@/api/mine/files.js"
	import {getDictionaryFn} from '@/api/mine/register.js'
	export default {
		data() {
			return {
				enter: require("@/mine/static/score/enter.png"),
				searchimg: require("@/mine/static/score/search.png"),
				upimg: require("@/mine/static/score/up.png"),
				fzicona: require("@/mine/static/score/fzicona.png"),
				fziconb: require("@/mine/static/score/fziconb.png"),
				fziconc: require("@/mine/static/score/fziconc.png"),
				fzicond: require("@/mine/static/score/fzicond.png"),
				classlist: [],
				ynrange:[{"value": 1,"text": "是"	},{"value": 2,"text": "否"}],
				ywrange:[{"value": 1,"text": "有"	},{"value": 2,"text": "无"}],
				list: [],
				pageSize: 10,
				pageNum: 1,
				reachflag: true,
				joinflag:false,
				wtdt: '',
				total: 0,
				className: '',
				name: '',
				roles:this.$store.state.user.choseroles,
				tablist:[],//家长孩子列表,
				type:0,
				isNearsightedness:'',
				isNearsightednessname:'',
				isMedicalHistory:'',
				isMedicalHistoryname:'',
				classidx:0,
				isNearsidx:0,
				isMedicalidx:0,
				count:{},
				schoolId:''
			}
		},
		components: {
			daList,popUp
		},
		onUnload() {
			uni.$off('refreshdalist')
		},
		onLoad: function() {
			uni.$on('refreshdalist', (e) => {
				this.reachflag = true;
				this.pageNum = 1;
				this.list = [];
				this.getDataFn();
				this.getstuCountFn()
			})
			// 判断角色
			var roles=this.$store.state.user.choseroles;
			this.roles=roles;
			if(roles=='parents'){
				var newArr=this.$store.state.user.student;
				if(newArr&&newArr.length){
					// var newArr=res.user.parentsStudent;
					this.tablist = newArr
				}
				this.getDataFn();
			}else if(roles=='teacher'){
				var id=this.$store.state.user.schoolId;
				this.schoolId=id;
				this.getselfList(id)
			}else {
				// 去拿班级部门
				this.schoolId=this.$store.state.user.deptId;
				this.getselfList(0)
			}
			uni.removeStorageSync('children')
		},
		methods: {
			checkPermi,checkRole,
			init(){
				// 
				getDictionaryFn('sys_subject').then(res=>{
					if(res.code==200){
						this.subject = res.data.map(v => {
							return {
								dictLabel: v.dictLabel,
								dictValue: v.dictValue
							}
						})
					}
				})
			},
			getAdd() {
				this.$tab.navigateTo('/mine/pages/files/addstudent?type=add')
			},
			getClose(){
				this.type=0;
			},
			getJoinFn(){
				this.type=8;
			},
			getJoin(){
				this.type=0;	
			},
			getChose(e){
				var obj={
					studentId:e.id,
					name:e.studentName,
					school:e.schoolName,
					idCard:e.studentNumber,
					classId:e.classId
				}
				this.type=0;
				uni.setStorageSync('children',JSON.stringify(obj))
				this.$tab.navigateTo('/mine/pages/files/addstudent?type=add')
			},
			reach_btn() {
				if (this.reachflag) {
					this.pageNum++
					this.getDataFn()
				}
			},
			getselfList(id){
				var params={
					parentId:id,
				}
				params['params[role]']=this.$store.state.user.choseroles
				getselfList(params).then(res=>{
					if(res.code==200){
						var newArr=res.data;
						if(newArr&&newArr.length){
							this.classId=newArr[0].deptId
							this.className=newArr[0].deptName
							this.classlist = newArr.map(v => {
								return {
									className: v.deptName,
									classId: v.deptId,
									schoolId: this.schoolId
								}
							})
							this.getDataFn()
							this.getstuCountFn()
						}
					}else{
						this.$toast(res.msg)
					}
				})
			},
			getstuCountFn(){
				var params = {
					classId: this.classId,
				}
				getstuCountFn(params).then(res=>{
					if (res.code == 200) {
						this.count=res.data
					} else {
						that.$toast(res.msg)
					} 
				})
			},
			getDataFn(a) {
				var params = {
					pageSize: this.pageSize,
					pageNum: this.pageNum,
				}
				if (this.name) {
					params.name = this.name
				}
				if (this.classId) {
					params.classId = this.classId
				}
				if(this.isNearsightedness){
					params.isNearsightedness = this.isNearsightedness
				}
				if(this.isMedicalHistory){
					params.isMedicalHistory = this.isMedicalHistory
				}
				params['params[role]']=this.$store.state.user.choseroles
				getstuListFn(params).then(res => {
					if (res.code == 200) {
						this.total = res.total;
						var data=res.rows;
						data.forEach((ite)=>{
							if(ite.identificationPhoto&&ite.identificationPhoto.length>0){
								var narr=ite.identificationPhoto.split(',')
								ite.identificationPhoto=narr[0]
							}
						})
						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 = '到底了~';
							}
						}
						if (this.pageNum == 1) {
							this.list = data;
						} else {
							this.list = this.list.concat(data)
						}
					} else {
						this.$toast(res.msg)
					}
				})
			},
			bindDateChange(e) {
				var idx = e.detail.value;
				this.classId = this.classlist[idx].classId;
				this.className = this.classlist[idx].className;
				this.pageNum = 1;
				this.list = [];
				this.reachflag = true;
				this.getDataFn()
				this.getstuCountFn()
			},
			bindDateChangea(e){
				var idx = e.detail.value;
				this.isNearsightednessname = this.ynrange[idx].text;
				this.isNearsightedness = this.ynrange[idx].value;
				this.pageNum = 1;
				this.list = [];
				this.reachflag = true;
				this.getDataFn()
			},
			bindDateChangeb(e){
				var idx = e.detail.value;
				this.isMedicalHistoryname = this.ynrange[idx].text;
				this.isMedicalHistory = this.ynrange[idx].value;
				this.pageNum = 1;
				this.list = [];
				this.reachflag = true;
				this.getDataFn()
			},
			
			getSearch() {
				this.pageNum = 1;
				this.list = [];
				this.reachflag = true;
				this.getDataFn()
			},
			getDetail(id) {
				this.$tab.navigateTo('/mine/pages/files/detail?id=' + id)
			},
			getHeightFn(data){
				var obj=JSON.parse(JSON.stringify(data))
				if(obj.type=='height'){
					this.$tab.navigateTo('/mine/pages/files/height?type=daup&name='+obj.name+'&id='+data.studentId)
				}else if(obj.type=='mind'){
					this.$tab.navigateTo('/mine/pages/files/mind?type=daup&name='+obj.name+'&id='+data.studentId)
				}
			}
		},

	}
</script>

<style lang="scss" scoped>
.filebox{height: 100vh;background-color: #FFFFFF;display: flex;flex-direction: column;overflow: hidden;}
.stutop{
	padding: 28rpx 38rpx 0rpx;background-color: #FFFFFF;flex: 0 0 auto;
}
.fnbox{
	width: 100%;height: 76rpx;display: flex;align-items: center;justify-content: center;font-size: 28rpx;font-weight: bold;
	&.bg1{background: #FFF4EF;color: #FE4C0E;}
	&.bg2{background:  #EEF3FE;color: #4775EA;}
	image{width: 20rpx;height: 26rpx;margin-right: 12rpx;}
}
.stutoa{font-size: 28rpx;font-weight: bold;color: #666666;
	text{color: #4775EA;padding: 0 8rpx;}
	image{width: 22rpx;height: 12rpx;margin-left: 24rpx;}
	.stutoap{flex: 1;padding: 16rpx 0;margin: 0 6rpx;}
}
.stutoar{font-size: 28rpx;font-weight: 500;color: #666666;
	text{color: #4775EA;padding: 0 8rpx;font-weight: bold;}
}
.search{height: 80rpx;background: #F2F5FB;border-radius: 40rpx;padding-left: 40rpx;
	.img{width: 30rpx;height: 32rpx;flex:0 0 auto;margin-right: 30rpx;}
	.btn{width: 120rpx;height: 80rpx;background: #1f57e6;border-radius: 40rpx;font-size: 30rpx;font-weight: 500;
color: #FFFFFF;text-align: center;line-height: 80rpx;flex:0 0 auto}
	.input{font-size: 30rpx;color: #161616;line-height: 80rpx;}
}

.workbox{overflow: auto;
	.workboxr{height: 100%;display: flex;}
	.scroll-view{width: 100%;height: 100%;box-sizing: border-box;}
}

.addbtn{width:100%;
height: 100rpx;
background: #1f57e6;position: fixed;left: 0;right: 0;bottom: 0;z-index: 1;font-size: 34rpx;font-weight: 500;
color: #FFFFFF;display: flex;align-items: center;justify-content: center;}
</style>
