<template>
	<view class="content">
		<u-card :title="classInfo.name" :sub-title="classInfo.useLessonsType == 1 ? '体验班' : ''" :show-foot="false" title-size="32" margin="0px 0px 10px 0px" :head-style="cardStyle">
			<view slot="body">
				<view class="class-info-text">
					<u-icon name="clock"></u-icon>
					{{classInfo.classStartDate}}&nbsp;~&nbsp;{{classInfo.classEndDate}}
				</view>
				<view class="class-info-text" v-for="(item, index) in classInfo.classExtrasList" :key="index">
					<u-icon name="calendar" style="visibility: hidden;"></u-icon>
					<text>{{item.week}}&nbsp;{{item.startTime}}-{{item.endTime}}</text>
				</view>
				<view class="class-info-text">
					<u-icon name="map"></u-icon>
					{{classInfo.address}}
				</view>
				<!--<view style="display: flex;justify-content: flex-end;" v-if="classInfo.state != 0">-->
				<view style="display: flex;justify-content: flex-end;">
						<u-button type="warning" :custom-style="{background: mainColor}" size="mini" shape="circle" :ripple="true" @click="handleUpdateClassClick">修改</u-button>
						<u-button v-if="!classInfo.studentSignList.length" type="warning" :custom-style="{background: mainColor,marginLeft: '5px'}" size="mini" shape="circle" :ripple="true" @click="handleDeleteClick">删除</u-button>
				</view>
			</view>
		</u-card>
		<u-card :title="'学员信息(' + getStudentsNumber(1) + ')'" title-size="32" margin="0px 0px 10px 0px" :head-style="cardStyle">
			<u-grid :col="3" slot="body" :border="false">
				<u-grid-item v-for="(item, index) in classInfo.studentSignList" :key="index" :custom-style="gridCustomStyle" @click="goToStudentInfo(item)">
					<view class="class-student-col" :class="item.state ? 'student-active' : ''">
						{{item.name}}
						<u-icon name="bookmark" :color="mainColor" size="48"></u-icon>
					</view>
				</u-grid-item>
			</u-grid>
		</u-card>
		<u-card margin="0px 0px 10px 0px" :head-style="cardStyle" :show-head="false"  @click="handleScanClick">
			<view slot="body" style="display: flex;justify-content: space-between;">
				<view style="font-size: 32rpx;font-weight: bold;color:#000000;">扫码</view>
				<u-icon name="scan" size="32"></u-icon>
			</view>
		</u-card>
		<u-card :title="'事假(' + getStudentsNumber(2) + ')'" title-size="32" margin="0px 0px 10px 0px" :head-style="cardStyle">
			<u-grid :col="3" slot="body" :border="false" v-if="classInfo.studentLeaveRecordList.length">
				<u-grid-item v-for="(item, index) in classInfo.studentLeaveRecordList" :key="index" :custom-style="gridCustomStyle">
						<view class="class-student-col" @click="handleleaveClick(item)">
						{{item.name}}
						<u-icon :name="item.agreeType == 1 ?  'bookmark-fill' : 'bookmark'" :color="mainColor" size="48"></u-icon>
					</view>
				</u-grid-item>
			</u-grid>
		</u-card>
		<u-card :title="'病假结束确认(' + getStudentsNumber(3) + ')'" title-size="32" margin="0px 0px 10px 0px" :head-style="cardStyle">
			<u-grid :col="3" slot="body" :border="false" v-if="classInfo.studentLeaveList.length">
				<u-grid-item v-for="(item, index) in classInfo.studentLeaveList" :key="index" :custom-style="gridCustomStyle">
						<view class="class-student-col" @click="handleOtherClick(item)">
						{{item.name}}
						<u-icon :name="item.agreeType == 1 ?  'bookmark-fill' : 'bookmark'" :color="mainColor" size="48"></u-icon>
					</view>
				</u-grid-item>
			</u-grid>
		</u-card>
		<u-card :title="'续费卡(' + getStudentsNumber(4) + ')'" title-size="32" margin="0px 0px 10px 0px" :head-style="cardStyle">
			<u-grid :col="3" slot="body" :border="false"  v-if="classInfo.studentRenewList.length">
				<u-grid-item v-for="(item, index) in classInfo.studentRenewList" :key="index" :custom-style="gridCustomStyle">
					<view class="class-student-col" :class="item.state ? 'student-active' : ''" @click="handleVtCardClick(item)">
						{{item.name}}
						<u-icon name="bookmark" :color="mainColor" size="48"></u-icon>
					</view>
				</u-grid-item>
			</u-grid>
		</u-card>
		<u-card title="班级近况" :sub-title="imgEdit ? '完成' : '管理'" :sub-title-color="imgEdit ? '#19be6b' : '#909399'" :show-foot="false"
		 title-size="32" margin="0px" :head-style="cardStyle" @sub-click="imgEdit = !imgEdit">
			<view class="class-show-box" slot="body">
				<view v-for="(item, index) in classShowList" :key="index" class="class-show-card">
					<u-image :src="item.url" mode="aspectFill" height="30vw" border-radius="10px" v-if="item.type == 0"></u-image>
					<view class="video-col" v-if="item.type == 1">
						<video :src="item.url" object-fit="cover" controls :id="'video' + index"></video>
					</view>
					<view class="class-show-name">{{item.name}}</view>
					<u-icon name="close-circle-fill" color="#fa3534" size="48" v-if="imgEdit" class="delete-icon" @click="deleteClassShow(item)"></u-icon>
				</view>
				<view class="class-show-card" style="width: calc(100% - 16px);text-align: center;" v-if="imgEdit">
					<u-icon name="plus-circle-fill" size="100" :color="mainColor" @click="uploadTypeShow = true"></u-icon>
				</view>
			</view>
		</u-card>
		<view class="handle-fix-box">
			<u-button type="warning" shape="circle" :ripple="true" :custom-style="customStyle" @click="goToSignForm">立即签到</u-button>
		</view>
		<u-action-sheet :list="uploadTypeList" v-model="uploadTypeShow" @click="selectUploadType"></u-action-sheet>
		<!-- 事假 -->
		<u-popup v-model="leaveShow" mode="center" border-radius="30" width="600rpx">
			<view class="common-title">事假</view>
			<view class="menber-box">
				<u-form :model="leaveForm" ref="leaveFormRef" label-width="140">
					<u-form-item label="请假时间" prop="time">
						<u-input v-model="leaveForm.time" disabled  type="text"  />
					</u-form-item>
					<u-form-item label="请假理由" prop="leaveReason">
						<u-input v-model="leaveForm.leaveReason" disabled type="text" />
					</u-form-item>
				</u-form>
				<view style="height:20px;"></view>
				<u-button type="warning" shape="circle" :ripple="true" :custom-style="customStyle" @click="handleAgreeClick">同意</u-button>
			</view>
		</u-popup>
		<!-- 病假确认 -->
		<u-modal v-model="leaveOtherShow" content="提示" show-cancel-button @confirm="handleEndClick">
			<view style="text-align: center;margin: 10px 0;">
				是否确认{{ studentName }}的病假结束?
			</view>
		</u-modal>
		<!-- 续费卡 -->
		<u-popup v-model="vtCardShow" mode="center" border-radius="30" width="600rpx">
			<view class="common-title">续费卡</view>
			<view class="menber-box">
				<u-radio-group v-model="vtCardValue" @change="handleRadioChange">
					<u-radio  class="menber-col" v-for="(item,index) in vtCardList" :key="index" :name="item.cardName">
						<view class="menber-label">卡名:{{ item.cardName }}</view>
						<!-- <view class="menber-label">售价: ¥ 1111</view> -->
						<!-- <view class="menber-label">使用时间: 11111</view> -->
						<!-- <u-checkbox v-model="true" active-color="#ff6e3e">选择</u-checkbox> -->
						<!-- <view class="menber-icon iconfont iconzu4931"></view> -->
					</u-radio>
				</u-radio-group>
				<view style="height:20px;"></view>
				<u-button type="warning" shape="circle" :ripple="true" :custom-style="customStyle" @click="handleAffirmClick">确认</u-button>
			</view>
		</u-popup>
		<u-top-tips ref="uTips"></u-top-tips>
	</view>
</template>

<script>
	import {
		mapGetters
	} from 'vuex'
	const NET = require('@/utils/request')
	const API = require('@/config/api')
	export default {
		computed: {
			...mapGetters([
				'mainColor',
				'customStyle',
			])
		},
		data() {
			return {
				classId: '',
				studentId: '',
				studentName: '',
				classInfo: {
					name: '',
					classStartDate: '',
					classStartHours: '',
					classEndDate: '',
					classEndHours: '',
					residue: '',
					address: '',
					classExtrasList: [],
					studentSignList: [],
					showList: [],
					studentLeaveRecordList: [],
					studentRenewList: []
				},
				classShowList: [],
				uploadTypeShow: false,
				uploadTypeList: [{
						text: '图片'
					},
					{
						text: '视频'
					}
				],
				imgEdit: false,
				cardStyle: {
					fontWeight: 'bold'
				},
				gridCustomStyle: {
					padding: '0 2px'
				},
				leaveShow: false,
				leaveForm: {},
				leaveOtherShow: false,
				vtCardShow: false,
				vtCardId: '',
				vtCardValue: '',
				vtCardList: [],
			}
		},
		onLoad(options) {
			this.classId = options.id
		},
		onShow() {
			this.initialize()
			this.getClassShow()
		},
		onPullDownRefresh() {
			this.initialize()
			this.getClassShow()
		},
		methods: {
			//  获取初始化数据
			initialize() {
				NET.request(API.getClassDetail, {
					id: this.classId
				}, 'POST').then(res => {
					this.classInfo = res.data
					uni.stopPullDownRefresh();
				}).catch(error => {
					this.$refs.uTips.show({
						title: error.message,
						type: 'warning',
					})
				})
			},
			// 扫码
			handleScanClick() {
				try {
					let that = this
					uni.scanCode({
						onlyFromCamera: true,
						success: function (res) {
							// const studentId = parseInt(res.result)
							const studentId = 8999
							const classId = parseInt(that.classId)
							NET.request(API.classCourseWriteOff, {
								studentId,classId
							}, 'POST').then(res => {
								if(res.status == 10000) {
									that.$refs.uTips.show({
										title: res.message,
										type: 'success',
									})
								} else {
									that.$refs.uTips.show({
										title: res.message,
										type: 'warning',
									})
								}
							})
						},
						fail: function (res) {
							that.$refs.uTips.show({
								title: "调用摄像头失败",
								type: 'warning',
								})
							},
						});
					} catch {
						this.$refs.uTips.show({
							title: "调用摄像头失败",
							type: 'warning',
						})
					}
			},
			//  班级近况
			getClassShow() {
				NET.request(API.getClassDetailShowList, {
					id: this.classId,
					page: 0,
					size: 1000
				}, 'POST').then(res => {
					this.classShowList = res.data.row
				}).catch(error => {
					this.$refs.uTips.show({
						title: error.message,
						type: 'warning',
					})
				})
			},
			// 删除班级(班级内无学员)
			handleDeleteClick() {
				NET.request(API.deleteById, {
					id: parseInt(this.classId)
				}, 'POST').then( res=> {
					if(res.status == 10000) {
						uni.navigateBack({})
					}
				})
			},
			//  获取学生数量
			getStudentsNumber(num) {
				// 1 学生数晾   2 事假数量   3 续费卡数量
				switch (num){
					case 1:
						return this.classInfo.studentSignList.length
					case 2: 
						return this.classInfo.studentLeaveRecordList.length
					case 3:
							return this.classInfo.studentLeaveList.length
					case 4:
						return this.classInfo.studentRenewList.length
				}
			},
			handleOtherClick(item) {
				this.studentId = item.id
				this.studentName = item.name
				this.leaveOtherShow = true
			},
			// 病假知晓
			handleEndClick() {
				NET.request(API.leaveLessonsEndOk,{
					classId:parseInt(this.classId), studentId:parseInt(this.studentId)
				},'POST').then(res=> {
					if(res.status == 10000) {
						this.$refs.uTips.show({
							title: res.message,
							type: 'success',
						})
						this.initialize()
						this.getClassShow()
					} else {
						this.$refs.uTips.show({
							title: res.message,
							type: 'warning',
						})
					}
				})
			},
			// 事假信息
			handleleaveClick(item) {
				if(item.agreeType == 1) return
				NET.request(API.leaveLessonsInfo,{
					id: parseInt(item.leaveRecordId)
				},'POST').then(res=> {
					if(res.status == 10000) {
						this.leaveForm = { ...res.data }
						this.leaveShow = true
					} else {
						this.$refs.uTips.show({
							title: res.message,
							type: 'warning',
						})
					}
				})
			},
			//  事假信息->同意
			handleAgreeClick() {
				NET.request(API.leaveLessonsOk, {
					leaveId: parseInt(this.leaveForm.leaveId)
				}, 'POST').then(res => {
					if(res.status == 10000) {
						this.leaveShow = false
						this.leaveForm = {}
						this.initialize()
					} else {
						this.leaveShow = false
						this.$refs.uTips.show({
							title: res.message,
							type: 'warning',
						})
					}
				})
			},
			//  续费卡列表
			handleVtCardClick(item) {
				this.studentId = parseInt(item.id)
				const data = {
					studentId: parseInt(this.studentId),
					classId: parseInt(this.classId)
				}
				NET.request(API.stuRenewalCardList, data, 'POST').then(res=> {
						if(res.status == 10000) {
							this.vtCardList = res.data
							this.vtCardShow = true
						} else {
							this.$refs.uTips.show({
								title: res.message,
								type: 'warning',
							})
							this.vtCardShow = false
						}
					})
			},
			//  续费卡切换
			handleRadioChange(cardName) {
				this.vtCardId = this.vtCardList.find( item => item.cardName = cardName).id
				this.vtCardValue = cardName
			},
			//  续费卡确认
			handleAffirmClick() {
				const data = {
					cardId: parseInt(this.vtCardId),
					studentId: parseInt(this.studentId),
					classId: parseInt(this.classId)
				}
				NET.request(API.openRenewalCard, data,'POST').then(res=> {
					if(res.status == 10000) {
						this.vtCardShow = false
						this.$refs.uTips.show({
							title: res.message,
							type: 'success',
						})
						this.initialize()
					} else {
						this.$refs.uTips.show({
							title: res.message,
							type: 'warning',
						})
						this.vtCardShow = false
					}
				})
			},
			//  选择上传文件类型
			selectUploadType(index) {
				if (index == 0) {
					uni.chooseImage({
						count: 1,
						success: (res) => {
							this.uploadFile(res.tempFilePaths[0])
						},
						fail: (error) => {

						}
					});
				} else if (index) {
					uni.chooseVideo({
						count: 1,
						success: (res) => {
							this.uploadFile(res.tempFilePath)
						},
						fail: (error) => {

						}
					});
				}
			},
			//  上传文件
			uploadFile(path) {
				let that = this
				uni.uploadFile({
					url: API.uploadFile,
					filePath: path,
					name: 'file',
					header: {
						Authorization: uni.getStorageSync('token')
					},
					success: (uploadFileRes) => {
						that.uploadSuccess(JSON.parse(uploadFileRes.data).data.id)
					}
				});
			},
			//  文件上传成功回调
			uploadSuccess(id) {
				NET.request(API.insertClassShow, {
					fileId: [id],
					id: this.classId,
				}, 'POST').then(res => {
					this.getClassShow()
					this.$refs.uTips.show({
						title: '班级近况发布成功',
						type: 'success',
					})
				}).catch(error => {
					this.$refs.uTips.show({
						title: error.message,
						type: 'warning',
					})
				})
			},
			//  删除班级近况
			deleteClassShow(site) {
				NET.request(API.deleteClassShow, {
					id: site.id,
				}, 'POST').then(res => {
					this.getClassShow()
					this.$refs.uTips.show({
						title: '删除成功',
						type: 'success',
					})
				}).catch(error => {
					this.$refs.uTips.show({
						title: error.message,
						type: 'warning',
					})
				})
			},
			//  跳转修改班级
			handleUpdateClassClick() {
				const form = {
					classId: this.classId,
					name: this.classInfo.name,
					startDate: this.classInfo.classStartDate,
					endDate: this.classInfo.classEndDate,
					timeReqList: this.classInfo.classExtrasList,
					maxStudentCount: this.classInfo.maxStudentCount
				}
				uni.navigateTo({
					url: '/pagesClass/updateClassForm?form=' + encodeURIComponent(JSON.stringify(form))
				});
			},
			//  跳转学生详情
			goToStudentInfo(item) {
				uni.navigateTo({
					url: '/pagesMain/studentInfo?type=1&id=' + item.id + '&classId=' + this.classId
				});
			},
			//  跳转签到表单
			goToSignForm() {
				uni.removeStorageSync('signUserList')
				uni.navigateTo({
					url: '/pagesClass/signForm?id=' + this.classId + '&status=' + this.classInfo.signStatus
				});
			},
			// 跳转续费卡详情
			jumpVtCardPage(id) {
				uni.navigateTo({
					url: `/pagesClass/vtCardInfo?id=${id}`
				})
			}
		},
	}
</script>

<style>
	page {
		width: 100%;
		height: 100%;
		background-color: #f7f7f7;
		position: relative;
	}
</style>
<style lang="scss" scoped>
	@import "@/static/css/themes.scss";

	.content {
		width: 100%;
		float: left;
		padding-bottom: 60px;

		.class-info-text {
			color: #999999;
			line-height: 18px;

			u-icon {
				margin-right: 4px;
			}
		}

		.class-student-col {
			height: 28px;
			display: flex;
			padding: 0 10px;
			line-height: 28px;
			color: $mainColor;
			white-space: nowrap;
			border: 1px solid $mainColor;
			border-radius: 5px;

			/deep/u-icon {
				margin-left: 5px;
			}
		}

		.student-active {
			background-color: $mainColor;
			color: #FFFFFF;
		}

		.class-student-box {
			display: flex;
			justify-content: space-around;
		}

		.class-show-box {
			width: 100%;
			float: left;

			.class-show-card {
				width: calc(50% - 16px);
				margin: 0 8px 16px 8px;
				float: left;
				position: relative;

				.class-show-name {
					width: 100%;
					text-align: center;
					line-height: 20px;
					font-size: 14px;
					margin-top: 5px;
				}

				.delete-icon {
					position: absolute;
					right: -5px;
					top: -5px;
				}

				.video-col {
					width: 100%;
					height: 30vw;
					border-radius: 10px;

					video {
						width: 100%;
						height: 30vw;
					}
				}
			}
		}
		
		.menber-box {
			width: 100%;
			// float: left;
			padding: 10px 15px;
			margin-bottom: 10px;
			
			.menber-col {
				width: 100%;
				padding: 15px;
				margin-bottom: 10px;
				display: inline-block;
				background-color: #FFFFFF;
				border-radius: 15px;
				box-shadow: 0 0 5px 0 rgba(0, 0, 0, 0.2);
				position: relative;
				overflow: hidden;
				box-sizing: border-box;
				
				.menber-label {
					width: 100%;
					margin-bottom: 5px;
					float: left;
					font-size: 14px;
					// line-height: 20px;
				}
		
				.menber-num {
					width: 100%;
					float: left;
					font-size: 26px;
					line-height: 28px;
					color: $mainColor;
				}
		
				.menber-icon {
					font-size: 100px;
					color: $mainColor;
					position: absolute;
					right: -5px;
					bottom: -30px;
					opacity: 0.5;
				}
			}
		}
		.common-title {
			width:100%;
			text-align: center;
			font-size: 20px;
			margin: 10px 0;
		}
	}
</style>
