<template>
	<view class="content">
		<u-card :title="'总数(' + studentNum + ')人'" 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" @click.native.stop="setSignCheck(item)">
					<view class="avatar-box">
						<u-avatar :src="item.url" mode="circle" size="160" v-if="item.hasHead == 1"></u-avatar>
						<u-upload :custom-btn="true" :max-count="1" :index="index" :multiple="false" :show-progress="false"
						 :preview-full-image="false" :action="uploadUrl" :header="uploadHeader" @on-success="uploadSuccess" @on-error="uploadError"
						 @on-remove="uploadRemove" @on-preview-error="uploadPreview" v-else>
							<view slot="addBtn" class="slot-btn">
								<u-icon name="camera-fill" size="60" color="#999999"></u-icon>
							</view>
						</u-upload>
						<u-icon name="checkmark-circle-fill" v-if="item.state == 1 || item.checked" class="check-icon"></u-icon>
						<view class="grid-text">{{item.name}}</view>
					</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" @click="submitForm()">
				{{status == 0 ? '签到确认' : '今日已签到'}}
				 <!-- :disabled="status != 0" -->
			</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: '',
				venueId:'',
				status: '',
				uploadUrl: API.uploadFile,
				uploadHeader: {
					Authorization: uni.getStorageSync('token')
				},
				studentList: [],
				cardStyle: {
					fontWeight: 'bold'
				},
				gridCustomStyle: {
					padding: '0 2px'
				},
				studentNum: 0
			}
		},
		onLoad(options) {
			this.classId = options.id
			this.status = options.status
			this.venueId = options.venueId
		},
		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
					const checkedList = uni.getStorageSync('signUserList').length ? uni.getStorageSync('signUserList') : []
					checkedList.forEach(
						checkedItem => {
							if(this.studentList.filter(item=>checkedItem.id==item.id).length==0){
								this.studentList.push(checkedItem)
							}
						}
					)
					this.studentNum = this.studentList.length
				}).catch(error => {
					this.$refs.uTips.show({
						title: error.message,
						type: 'warning',
					})
				})
			},
			//  文件上传成功回调
			uploadSuccess(res, index, lists, name) {
				this.studentList[name].url = res.data.url
				this.studentList[name].fileId = res.data.id
				this.$refs.uTips.show({
					title: '文件上传成功',
					type: 'success',
				})
				return true
			},
			//  文件上传失败回调
			uploadError(res, index, lists, name) {
				this.$refs.uTips.show({
					title: '文件上传失败',
					type: 'warning',
				})
			},
			//  移除文件回调
			uploadRemove(index, lists, name) {
				this.studentList[name].url = ''
				this.studentList[name].fileId = ''
			},
			//  预览文件回调
			uploadPreview(url, lists, name) {
				if (this.status == 0) {
					this.studentList[name].checked = this.studentList[name].checked ? false : true
				}
			},
			//  设置是否签到
			setSignCheck(item) {
				if (this.status == 0) {
					item.checked = item.checked ? false : true
				}
			},
			//  跳转选择学生页面
			goToSelectStudent() {
				uni.navigateTo({
					url: '/pagesClass/signStudentList?venueId='+this.venueId
				});
			},
			//  跳转签到表单
			goToSignForm() {
				uni.navigateTo({
					url: '/pagesMember/signForm?id=' + this.classId
				});
			},
			//  提交表单
			submitForm() {
				NET.request(API.submitSignForm, {
					classId: this.classId,
					signStudentIdList: this.studentList.filter(site => site.state == 0 && site.checked).map(site => {
					if (site.fileId) {
						return {
							id: site.id,
							fileId: site.fileId,
						}
					} else {
						return {
							id: site.id,
						}
					}
				})
				}, 'POST').then(res => {
					this.$refs.uTips.show({
						title: '签到成功',
						type: 'success',
					})
					setTimeout(() => {
						uni.navigateBack()
					}, 1000)
				}).catch(error => {
					this.$refs.uTips.show({
						title: error.message,
						type: 'warning',
					})
				})
			},
		},
	}
</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-list-item {
			width: 160rpx !important;
			height: 160rpx !important;
			border-radius: 50% !important;
			margin: 0 !important;
		}

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

		/deep/.u-upload {
			height: 160rpx !important;
		}

		.check-icon {
			color: $mainColor;
			font-size: 20px;
			color: #ff6e3e;
			font-size: 26px;
			position: absolute;
			top: 120rpx;
			left: 50%;
			transform: translateX(-50%);
			background: #ffffff;
			border-radius: 50%;
		}

		.avatar-box {
			height: 200rpx;
			position: relative;
		}

		.grid-text {
			line-height: 40rpx;
			text-align: center;
			margin-top: 10rpx;
		}

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