<template>
	<view>
		<image class="bg" src="../../static/img/basicTraining-bg.png" mode="widthFix"></image>
		<u-tabs ref="btabs" :list="tabs" :is-scroll="false" :current="tabIndex" @change="tabChange"></u-tabs>
		<onlineTraining v-show="tabIndex=='0'" :key="1" :onlineData="onlineData" :rate="rate"></onlineTraining>
		<offlineTraining v-show="tabIndex=='1'" :key="2" :offlineId="offlineId"></offlineTraining>
	</view>
</template>

<script>
	import onlineTraining from './components/online.vue';
	import offlineTraining from './components/offline.vue';
	export default{
		components:{
			onlineTraining,
			offlineTraining
		},
		data(){
			return{
				tabs: [{name: '培训课程'},{name:'线下培训'}],
				tabIndex: 0 ,// tab下标
				onlineData:[],
				rate:{},
				offlineId:'',
				analysisParams:{
					platform:'1',//平台:1-H5 2-APP 3-小程序 4-PC端
					pages:location.href,//页面路径
					btnName:'',//按钮名称
					btnEvent:'1',//按钮事件: 1-点击 2-长按 3-滑动
					ipAddress:'',//IP地址
					typeName:'适应性培训',//类型名称 例：学校关注度 、适应性考试等
					typeCode:'',//类型编码 例：类型名称首字母缩写(XXGZD)
					categoryName:'',//类别名称 例：XX学校，SS考试
				}
			}
		},
		onLoad(page){
			if(page?.type=='offline'){
				this.tabIndex = 1;
				if(page.id){
					this.offlineId = page.id;
					console.log('this.offlineId',this.offlineId)
				}
			};
			
		
		},
		onShow(){
			this.getAdaptTrainOnline();
			this.getAdaptTrainOnlineRate();			
		},
		mounted() {
			this.postAnalysis();
			
		},
		methods:{
			getip(){
				let that = this;
				uni.request({
				    url:'/apis/cityjson?ie=utf-8',
					method:'GET',
					success: (res) => {
						// console.log('this',this);
				        const reg = /\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/;
						let ip = reg.exec(res.data);
						that.analysisParams.ipAddress = ip[0];
						console.log('analysisParams',this.analysisParams);
					}
				});
			},
			async postAnalysis(){
				await this.getip();
				this.$u.api.postAnalysis(this.analysisParams).then(res=>{
					console.log('res',res);
				});
			},
			// 切换菜单
			tabChange(index) {
				console.log('tabChange index',index)
				this.tabIndex = index;
				
			},
			getAdaptTrainOnline(){
				this.$u.api.getAdaptTrainOnline().then(res=>{
					if(res.code == 200){
						this.onlineData = res.data;
					}else{
						uni.showToast({
							icon:'none',
							title:res.msg
						})
					}
					
					console.log('getAdaptTrainOnline',JSON.parse(JSON.stringify(res)));
				})
			},
			getAdaptTrainOnlineRate(){
				this.$u.api.getAdaptTrainOnlineRate().then(res=>{
					this.rate = res.data;
					console.log('getAdaptTrainOnlineRate',JSON.parse(JSON.stringify(res)));
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	page{background-color: #f9f9f9;padding-bottom: 48rpx;}
	.bg{width: 100%;}
	
	// @import  './BasicTraining.scss'
</style>
