<template>
	<view class="container">
		<image class="cbg" :src="base.imgHost('course-bg.png')"></image>
		<view v-if="!hasImg"></view>
		<view v-else>
			<view class="detail">
				<!-- <view class="detail-title">{{info.name}}</view>
				<view class="detail-time" v-if="info.start_date">
					<u-icon name="clock"></u-icon>
					<text>开课日期：{{info.start_date}} 至 {{info.end_date}}</text>
				</view> -->
				<block v-if="imgList.length>0">
					<view v-for="item in imgList">
						<image mode="widthFix" :src="item.url"></image>
					</view>
				</block>
				<block v-else>
					<view class="detail-noinfo">
						<image mode="widthFix" :src="base.imgHost('mycourse-status.png')"></image>
						<text>课程详情敬请期待...</text>
					</view>
				</block>
			</view>
			<view class="detail-btn" v-if="info.sign_status===10">
				<view @click="toApply">
					<image class="course-btn" :src="base.imgHost('course-btn.png')"></image>
					<text>我要报名</text>
				</view>
			</view>
		</view>


		<tabbar :currentPage="1"></tabbar>
		<view class="modal">
			<u-popup v-model="showRegister" mode="bottom">
				<view>
					<view class="modal-tip">提示</view>
					<view class="modal-content">
						<view>如您已是我方校友，请先绑定账号</view>
						<view @click="goBind" class="modal-bind">
							去绑定
						</view>
						<view>如您还不是我方校友，请先注册</view>
						<view @click="toRegister" class="modal-register">
							去注册
						</view>
					</view>
				</view>

			</u-popup>
		</view>
	</view>
</template>

<script>
	import tabbar from '@/components/tabbar/tabbar.vue';
	import {
		ROOTPATH as baseUrl
	} from "@/common/config.js"
	export default {
		components: {
			tabbar,
		},
		data() {
			return {
				hasMobile: false,
				course_id: '',
				info: {},
				imgList: [],
				showRegister: false,
				hasImg: false,
				sign_info: {}
			}
		},

		onShareAppMessage(res) {
			return {
				path: "/packages/course/detail?id=" + this.course_id, //这是为了传参 onload(data){let id=data.id;}
				title: this.info.name,
				imageUrl: '/static/share.jpg'
			}
		},
		onShareTimeline() {
			return {
				path: "/packages/course/detail?id=" + this.course_id, //这是为了传参 onload(data){let id=data.id;}
				title: this.info.name,
				imageUrl: '/static/share.jpg'
			}
		},
		onLoad(options) {
			this.course_id = options.id
			let token = uni.getStorageSync('stbc1_lifeData') ? uni.getStorageSync('stbc1_lifeData').vuex_token : ''
			if (this.base.isNull(token)) {
				this.getToken()
			} else {
				let user = uni.getStorageSync("stbc1_lifeData") ? uni.getStorageSync("stbc1_lifeData").vuex_user : {}
				if (!this.base.isNull(user.mobile)) {
					this.hasMobile = true
				} else {
					this.hasMobile = false
				}
				this.getCourseDetail(this.course_id)
				this.getCourseSign()
			}
		},
		onHide() {
			this.showRegister = false
		},
		methods: {
			async getToken() {
				let that = this
				await uni.login({
					provider: 'weixin',
					success: (res) => {
						this.$u.api.login({
							code: res.code
						}).then(res1 => {
							uni.setStorageSync("stbc1_lifeData", {
								'vuex_token': res1.token
							})
							uni.request({
								url: baseUrl + '/api/mobile/user/get-user-info',
								header: {
									Authorization: `Bearer ${res1.token}`
								},
								method: "GET",
								success(res2) {
									uni.setStorageSync("stbc1_lifeData", {
										'vuex_token': res1.token,
										"vuex_user": res2.data.user
									})
									if (!that.base.isNull(res2.data.user.mobile)) {
										that.hasMobile = true
									} else {
										that.hasMobile = false
									}
									that.getCourseDetail(that.course_id)
									that.getCourseSign(that.course_id)
								},
								fail(err) {
									console.log("err", err)
								}
							})
						}).catch(err => {
							console.log('login-error:', JSON.stringify(err))
						})
					},
					fail: (res) => {
						console.log("errtoken", JSON.stringify(res))
					}
				});
			},
			async getCourseDetail(id) {
				const res = await this.$u.api.courseDetail({
					course_id: id
				})
				this.info = this.base.requestToForm(this.info, res)
				if (res.publicize && res.publicize.length > 0) {
					this.imgList = res.publicize
				}
				this.hasImg = true
				let typeName = res.type_detail ? res.type_detail.name + " | " : ''
				uni.setNavigationBarTitle({
					title: typeName + res.name,
					success: function() {
						console.log('标题设置成功');
					},
					fail: function(err) {
						console.error('标题设置失败', err);
					}
				});
			},
			async getCourseSign() {

			},
			// 是否有手机号，有则是学员， 没有就是游客。
			// 游客可以选择绑定  或  注册报名
			// 报名
			async toApply() {
				let that = this
				// 我已报名该课程
				if (this.info.my_user) {
					const res = await this.$u.api.courseGetSign({
						course_id: this.course_id
					})
					this.sign_info = res.detail
					// 默认状态页
					let url = '/packages/mycourse/courseStatus?id=' + this.course_id
					// 审核通过
					if (this.sign_info.status === 1) {
						// 不需要付费 有群聊二维码进详情，没有进状态
						if (this.info.is_fee === 0) {
							if (!this.base.isNull(this.info.qun_image_id)) {
								url = '/packages/course/freeDetail?id=' + this.course_id
							}
						} else {
							// 付费 进详情
							url = '/packages/mycourse/detail?id=' + this.course_id
						}
					} else {
						// 待审核 不通过 备选 放弃 4取消 6黑名单
						url = '/packages/mycourse/courseStatus?id=' + this.course_id
					}

					this.base.toast("您已报名该课程", 2000, function() {
						setTimeout(function() {
							uni.navigateTo({
								url: url
							})
						}, 500)
					})
					return
				}
				// 收费的不限制报名人数 total没值也不限制
				// 免费的限制报名人数
				if (!this.info.is_fee && (this.info.total && this.info.total > 0)) {
					// 报名人数已满
					if (this.info.course_signs_count >= this.info.total) {
						this.base.toast("当前报名人数已满")
						return
					}
				}
				// 游客就登录 或 注册
				if (!this.hasMobile) {
					this.showRegister = true
					return
				} else {
					uni.navigateTo({
						url: '/packages/apply/index?id=' + this.course_id
					})
				}
			},
			// 去绑定登录
			goBind() {
				uni.navigateTo({
					url: '/packages/register/login?id=' + this.course_id
				})
			},
			// 去注册
			toRegister() {
				uni.navigateTo({
					url: '/packages/register/index?id=' + this.course_id
				})
			},

		}

	}
</script>

<style lang="scss" scoped>
	.container {
		width: 100%;
		height: 100vh;

		.cbg {
			position: absolute;
			top: 0;
			left: 0;
			width: 100%;
			height: 100vh;
		}

		.detail {
			width: 100%;
			height: 100vh;
			overflow: scroll;
			// padding: 40rpx 80rpx;
			position: relative;
			padding-bottom: 115rpx;

			image {
				width: 100%;
			}

			&-title {
				font-size: 32rpx;
				color: #000096;
				text-align: center;
				padding: 40rpx 0rpx;
			}

			&-time {
				display: flex;
				align-items: center;
				padding-bottom: 40rpx;
				color: rgba(0, 0, 0, 0.4);
				justify-content: center;

				&>text {
					margin-left: 10rpx;
				}
			}

			&-noinfo {
				width: calc(100% -160rpx);
				position: absolute;
				top: calc(50% - 340rpx);
				left: 50%;
				transform: translate(-50%, 0%);

				&>image {
					width: 100%;
				}

				&>text {
					font-size: 42rpx;
					color: #000;
				}
			}
		}

		.detail-btn {
			width: 100%;
			height: 199rpx;
			position: fixed;
			left: 0;
			bottom: 160rpx;
			text-align: center;

			&>view {

				>image {
					width: 580rpx;
					height: 199rpx;
					margin: 0 auto;
					display: block;
				}

				>text {
					position: absolute;
					top: 50%;
					left: 50%;
					transform: translate(-50%, -50%);
					color: #fff;
					font-size: 32rpx;
				}
			}
		}

		.modal {
			::v-deep .u-drawer-bottom {
				border-radius: 40rpx;
			}

			&-tip {
				text-align: center;
				padding: 30rpx;
				font-size: 32rpx;
			}

			&-content {
				height: 450rpx;
				padding: 0 30rpx;
				font-size: 32rpx;
				text-align: center;

				&>view {
					margin: 30rpx auto;
				}
			}

			&-bind {
				width: 45%;
				text-align: center;
				margin: 0 auto;
				color: #fff;
				border-radius: 30rpx;
				padding: 20rpx;
				background: linear-gradient(to right, #e4cdb4, #c69c6d);
			}

			&-register {
				width: 45%;
				text-align: center;
				margin: 0 auto;
				color: #fff;
				border-radius: 30rpx;
				padding: 20rpx;
				background: linear-gradient(to right, #5e5fbc, #0d0398);
			}
		}
	}
</style>