<template>
	<view>
		<tree :checkList="checkList" :props="prop" @sendValue="confirm" :isCheck="true" :trees="tree" />
		
		<view class="btn box_sizing">
			<button class="sureBtn" type="primary" @click="backConfirm">确认</button>
		</view>
	</view>
</template>

<script>
	import {getMaterialTree} from '@/common/api/commonApi.js';
	
	export default {
		
		data() {
			return {
				tree: [],
				checkList: [],
				prop: {
					label: 'name',
					children: 'children',
					multiple:false,
					nodes:true
				}
			}
		},
		onLoad(o) {
			// var that = this
			this.$util.persistLogin(this); 
		},
		created:function(){		
			let classCode=this.$util.getQuery("classCode");
			this.getMaterialTree(classCode);
		},
		updated:function(){
			
		},
		mounted:function(){
			
		},
		methods: {
			//检查默认选中,要是字段钟本身包含了checked字段就不需要这个
			checks() {
				var that = this;
				for (var i = 0, len = that.tree.length; i < len; i++) {
					for (var j = 0, lens = that.checkList.length; j < lens; j++) {
						if (that.checkList[j].id === that.tree[i].id) {
							that.$set(that.tree[i], 'checked', true)
							break
						} else {
							that.$set(that.tree[i], 'checked', false)
						}
					}
				}
			},
			//获取选中的
			confirm(val) {
				this.checkList = val
				console.log(this.checkList,588)
			},
			backConfirm() {
				var pages = getCurrentPages();
				var currPage = pages[pages.length - 1]; //当前页面
				//var prevPage = pages[pages.length - 2]; //上一个页面
				//h5写法
				//prevPage.query = this.checkList;
				console.info('prevPage.query ',this.checkList);
				uni.$emit("outtype",this.checkList);
				//return;
				//小程序写法
				//prevPage.$vm.query =this.checkList;
				uni.navigateBack();
			},
			getMaterialTree(classCode){
				let that=this;
				return new Promise((resolve,reject)=>{
					getMaterialTree(classCode).then(
					(res)=>{						
						console.info(JSON.stringify(res));
						//that.tree=res;
						res.forEach((item,index,array)=>{
							that.$set(that.tree,index,item);
						});
						console.info(that.tree);
						 resolve('suc');
						
					}).catch((err)=>{
						reject(err);
					});
				})
				
			},
			 
			 
		}
	}
</script>

<style lang="scss">
	.box_sizing {
		-webkit-box-sizing: border-box;
		-moz-box-sizing: border-box;
		box-sizing: border-box;
	}

	.btn {
		position: fixed;
		bottom: 0;
		padding: 10px;
		background-color: #fff;
		width: 100%;

		.sureBtn {
			background-color: #007AFF;
			color: #fff;
		}
	}
	
	.btn1 {
		position: fixed;
		bottom: 600px;
		padding: 10px;
		background-color: #fff;
		width: 100%;
	
	}
	.btn2 {
		position: fixed;
		bottom: 150px;
		padding: 10px;
		background-color: #fff;
		width: 100%;
	
	}
</style>
