<template>
	<view class="content">
		<u-card :title="classInfo.name" :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;{{classInfo.classStartHours}}&nbsp;~&nbsp;{{classInfo.classEndDate}}&nbsp;{{classInfo.classEndHours}}
				</view>
				<view class="class-info-text" v-for="(item, index) in classInfo.classExtrasList">
					<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>
		</u-card>
		<u-card :title="'学员信息(' + classInfo.studentSignList.length + ')'" 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 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"></u-image>
					<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)" v-if="imgEdit">
					<u-upload :action="uploadUrl" :header="uploadHeader" :show-upload-list="false" :custom-btn="true" @on-success="uploadSuccess"
					 @on-error="uploadError">
						<view slot="addBtn" style="width: calc(100vw - 32px);text-align: center;">
							<u-icon name="plus-circle-fill" size="100" :color="mainColor"></u-icon>
						</view>
					</u-upload>
				</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-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 {
				uploadUrl: API.uploadFile,
				uploadHeader: {
					Authorization: uni.getStorageSync('token')
				},
				classId: '',
				classInfo: {
					name: '',
					classStartDate: '',
					classStartHours: '',
					classEndDate: '',
					classEndHours: '',
					residue: '',
					address: '',
					classExtrasList: [],
					studentSignList: [],
					showList: [],
				},
				classShowList: [],
				imgEdit: false,
				cardStyle: {
					fontWeight: 'bold'
				},
				gridCustomStyle: {
					padding: '0 2px'
				}
			}
		},
		onLoad(options) {
			this.classId = options.id
			this.initialize()
			this.getClassShow()
		},
		onReady() {},
		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',
					})
				})
			},
			//  文件上传成功回调
			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',
					})
				})
			},
			//  文件上传成功回调
			uploadSuccess(res, index, lists, name) {
				NET.request(API.insertClassShow, {
					fileId: [res.data.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',
					})
				})
				return true
			},
			//  文件上传失败回调
			uploadError(res, index, lists, name) {
				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',
					})
				})
			},
			//  跳转学生详情
			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
				});
			},
		},
	}
</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;
				}
			}
		}
	}
</style>
