<template>
	<view class="content">
		<u-card :title="'总数(' + studentList.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 studentList" :key="index" :custom-style="gridCustomStyle">
					<u-avatar :src="item.url" mode="circle" size="128" :show-level="item.state == 1 || item.checked" level-icon="checkbox-mark"
					 :level-bg-color="mainColor" v-if="item.hasHead == 1" @click="setSignCheck(item)"></u-avatar>
					<u-upload :custom-btn="true" :max-count="1" @on-success="uploadSuccess" @on-error="uploadError" v-else>
						<view slot="addBtn" class="slot-btn">
							<u-icon name="camera-fill" size="60" color="#999999"></u-icon>
						</view>
					</u-upload>
					<view class="grid-text">{{item.name}}</view>
				</u-grid-item>
				<u-grid-item :custom-style="gridCustomStyle" @click="goToSelectStudent">
					<view class="slot-btn">
						<u-icon name="plus" size="60" color="#999999"></u-icon>
					</view>
				</u-grid-item>
			</u-grid>
		</u-card>
		<view class="handle-fix-box">
			<u-button type="warning" shape="circle" :ripple="true" :custom-style="customStyle" :disabled="status != 0" @click="goToSignForm()">{{status == 1 ? '签到确认' : '今日已签到'}}</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 {
				classId: '',
				status: '',
				studentList: [{
					"hasHead": 1,
					"name": "即预约",
					"state": 0,
					"checked": false,
					"url": ""
				}, {
					"hasHead": 1,
					"name": "即预约",
					"state": 0,
					"checked": false,
					"url": ""
				}, {
					"hasHead": 1,
					"name": "即预约",
					"state": 1,
					"checked": false,
					"url": ""
				}, {
					"hasHead": 0,
					"name": "即预约",
					"state": 0,
					"checked": false,
					"url": ""
				}, {
					"hasHead": 1,
					"name": "即预约",
					"state": 1,
					"checked": false,
					"url": ""
				}, ],
				cardStyle: {
					fontWeight: 'bold'
				},
				gridCustomStyle: {
					padding: '0 2px'
				}
			}
		},
		onLoad(options) {
			this.classId = options.id
			this.status = options.status
		},
		onShow() {
			this.initialize()
		},
		methods: {
			//  获取初始化数据
			initialize() {
				NET.request(API.getSignStudentList, {
					id: this.classId
				}, 'POST').then(res => {
					res.data.forEach(site => site.checked = false)
					this.studentList = res.data.concat(uni.getStorageSync('extraLessonsUserList'))
				}).catch(error => {
					this.$refs.uTips.show({
						title: '获取本课程学员列表失败',
						type: 'warning',
					})
				})
			},
			//  文件上传成功回调
			uploadSuccess(res, index, lists, name) {
				this.$refs.uTips.show({
					title: '文件上传成功',
					type: 'success',
				})
				return true
			},
			//  文件上传失败回调
			uploadError(res, index, lists, name) {
				this.$refs.uTips.show({
					title: '文件上传失败',
					type: 'warning',
				})
			},
			//  设置是否签到
			setSignCheck(item) {
				if (this.status == 0) {
					item.checked = item.checked ? false : true
				}
			},
			//  跳转选择学生页面
			goToSelectStudent() {
				uni.navigateTo({
					url: '/pagesClass/signStudentList'
				});
			},
			//  跳转签到表单
			goToSignForm() {
				uni.navigateTo({
					url: '/pagesMember/signForm?id=' + this.classId
				});
			},
		},
	}
</script>

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

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

		/deep/.u-avatar__level {
			left: 50%;
			transform: translateX(-50%);
			bottom: -6px !important;
		}

		.grid-text {
			line-height: 18px;
		}

		.slot-btn {
			width: 128rpx;
			height: 128rpx;
			border-radius: 50%;
			border: 1px solid #999999;
			display: flex;
			justify-content: center;
			align-items: center;
		}
	}
</style>
