<template>
	<view class="preference">
		<uni-nav-bar left-icon="back" :statusBar="true" fixed="true" title="课程内容选择"
			@clickLeft="clickLeft"></uni-nav-bar>
		<view class="header">
			已选学时：{{ chapterClassHours(chapterIdListAllNum)}} / {{options.minClassHour}}
		</view>
		<view class="dis_flex">
			<u-button type="success" :plain="true" size="medium" @click="autoGet" v-if="firstChoiceStatusBtn">一键选课</u-button>
			<u-button type="success" size="medium" @click="submit">保存</u-button>
		</view>
		<view class="fgx"></view>
		<view class="contents">
			<u-checkbox-group>
				<u-checkbox class="u_checkbox" v-for="(item, index) in courseMenu" :key="index" v-model="item.checked"
					shape="circle" :disabled="item.disabled" :name="item.menuId">{{item.menuName}}<text
						v-if="item.type == 2"
						style="color:red">（学时：{{ chapterClassHours(item.totalTime) }}）</text></u-checkbox>
			</u-checkbox-group>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				options: {},
				courseMenu: [],
				chapterIdListAllNum: 0,
				firstChoiceStatusBtn: false,
			};
		},
		onLoad(options) {
			options.minClassHour = parseFloat(options.minClassHour)
			this.options = options
			this.init()
		},
		watch: {
			courseMenu: {
				handler(newVal, oldVal) {
					let A = 0
					this.courseMenu.forEach(i => {
						if (i.checked == true) {
							A += i.totalTime || 0
						}
					})
					this.chapterIdListAllNum = A
				},
				// 深度监听 属性的变化
				deep: true
			}
		},
		methods: {
			autoGet() {
				this.courseMenu.forEach((i, k) => {
					this.$set(this.courseMenu[k], 'checked', i.firstChoiceStatus == 1)
				})
			},
			chapterClassHours(num) {
				return num ? (parseInt(num) / 60 / 45).toFixed(1) : 0;
			},
			init() {
				this.firstChoiceStatusBtn = false
				this.$api
					.reMenuList({
						courseId: this.options.courseId,
						gradeId: this.options.gradeId,
						orderGoodsId: this.options.orderGoodsId,
						getHours: 1
					})
					.then(res => {
						res.data.rows.forEach(i => {
							i.checked = false
							i.disabled = i.type == 2 ? false : true;
							if (i.firstChoiceStatus) {
								this.firstChoiceStatusBtn = true
							}
						});
						this.courseMenu = res.data.rows;
					}); //获取课程内容
			},
			clickLeft() {
				uni.navigateBack({
					delta: 1
				});
			},
			submit() {

				if (
					this.chapterClassHours(this.chapterIdListAllNum) < this.options.minClassHour
				) {
					uni.showModal({
						showCancel: false,
						content: "未选够" + this.options.minClassHour + "学时"
					})
					return;
				}
				if (
					this.chapterClassHours(this.chapterIdListAllNum) >
					this.options.minClassHour + 3
				) {
					uni.showModal({
						showCancel: false,
						content: `学时范围（${this.options.minClassHour}~${this.options
				            .minClassHour + 3} ）,已超出,请重新选择`
					})
					return;
				}
				let array = []
				this.courseMenu.forEach(i => {
					if (i.checked == true) {
						array.push(i.menuId)
					}
				})
				let ary = {
					chapterIdList: array,
					courseId: this.options.courseId,
					goodsId: this.options.goodsId,
					gradeId: this.options.gradeId,
					orderGoodsId: this.options.orderGoodsId
				};
				this.$api.orderfirstChoiceGoods(ary).then(res => {
					uni.redirectTo({
						url: `/pages3/polyv/detail?id=${
					    res.data.data.courseId
					  }&goodsId=${res.data.data.goodsId}&orderGoodsId=${
					    res.data.data.orderGoodsId
					  }&gradeId=${res.data.data.gradeId}&periodWaitTime=${
					    this.options.periodWaitTime
					  }&isQ=${this.options.isQ}`,
					});
				});
			},
		},
	};
</script>

<style scoped lang="scss">
	.header {
		padding: 16rpx 0rpx;
		background-color: rgb(88, 190, 238);
		color: #fff;
		text-align: center;
	}

	.dis_flex {
		display: flex;
		align-items: center;
		height: 100rpx;
		justify-content: space-around;
	}

	::v-deep .u-size-medium {
		width: 150rpx !important;
	}

	.fgx {
		height: 1rpx;
		background-color: #dedede;
		margin: 20rpx 0rpx 20rpx;
	}

	.contents {
		background-color: #fff;
	}

	::v-deep .u-checkbox-group {
		display: flex;
		flex-direction: column;
		padding: 0rpx 20rpx;
	}

	::v-deep .u_checkbox {
		padding: 10rpx 0rpx;
		border-bottom: 1rpx solid #dedede;
	}
</style>