<template>
	<view>
		<view class="nav-bar"></view>
		<view class="select-area">
			<uni-data-picker 
				:localdata="pracRange" 
				popup-title="请选择练习类型" 
				@change="pracChange"
				:clear-icon="false"
				style="overflow: hidden;"
				ref="picker"
			>
			</uni-data-picker>
		</view>
		<u-loading-page :loading="showLoading" color="#000" loading-text="跳转练习中,请稍后..."></u-loading-page>
	</view>
</template>

<script>
import { publicPath } from '../../public';
	export default {
		data() {
			return {
				showLoading: false,
				pracVal: '',
				pracRange: [],
				param: ''
			}
		},
		methods: {
			showTree () {
				this.$refs.pengTree._show();
			},
			treeCancel (e) {
				console.log(e);
			},
			treeConfirm (e) {
				console.log(e);
			},
			pracChange (e) {
				let that = this
				that.showLoading = true
				let sec = e.detail.value
				console.log(sec[sec.length - 1].value);
				// 根据id获取练习类型
				uni.request({
					url: publicPath + '/backstage/grade/toSelfExam',
					method: 'POST',
					header: {
						'content-type':'application/json'
					},
					data: JSON.stringify({
						difficultId: '1',
						chooseType: '1',
						fabuIdCard: uni.getStorageSync('idCard'),
						fabuUserid: uni.getStorageSync('id'),
						typeId: '',
						examPerfession: sec[sec.length - 1].value
					}),
					success: (pracRes) => {
						console.log(pracRes);
						if (pracRes.data.res == 1) {
							that.showLoading = false
							let examId = pracRes.data.obj.examId
							console.log(that.param);
							if (that.param == 1) {
								uni.navigateTo({
									url: `/pages/backQues/backQues?examId=${examId}`
								})
							} else {
								uni.navigateTo({
									url: `/pages/pracPage/pracPage?examId=${examId}`
								})
							}
						} else {
							uni.showToast({
								icon: 'none',
								title: '获取练习失败，请稍后再试！'
							})
						}
					}
				})
			},
			veri (data) {
				let that = this
				if(!data || data.length <= 0){
					return null;
				}
				return data.map(x => {
				    const model = { 
						text: x.name,
						value: x.id,
					};
				    const children = that.veri(x.child);
										
				    if(children){ 
					    model.children = children;
				    }
										
					return model;
				});
			}
		},
		onShow() {
			let that = this
			this.pracRange = []
			// 获取三级联动内容
			uni.request({
				url: publicPath + '/backstage/user/appOrgTree',
				method: 'POST',
				header: {
					'content-type':'application/x-www-form-urlencoded; charset=UTF-8'
				},
				data: {},
				success: (res) => {
					console.log(res);
					let list = res.data.obj
					that.pracRange = that.veri(list)
					console.log(that.pracRange);
					that.$refs.picker.show()
				}
			})
		},
		onLoad(params) {
			console.log(params);
			this.param = params.param
		}
	}
</script>

<style lang="scss" scoped>
	.nav-bar{
		width: 100%;
		height: var(--status-bar-height);
		padding-top: var(--status-bar-height);
	}
	.select-area{
		width: 95%;
		margin: 30vh auto;
		display: flex;
		align-items: center;
	}
</style>
