<template>
	<view class="content">
		<view class="filter-box">
			<u-search placeholder="请输入关键字" v-model="filterText" @search="setFilterText" @custom="setFilterText"></u-search>
		</view>
		<scroll-view scroll-y class="scroll-box" @scrolltolower="handleLoadMore" :refresher-enabled="true"
		 :refresher-triggered="triggered" :refresher-threshold="100" refresher-background="white" @refresherrefresh="onRefresh"
		 @refresherrestore="onRestore">
			<u-checkbox-group :active-color="mainColor" shape="circle" size="56" icon-size="36">
				<u-card :head-border-bottom="false" :foot-border-top="false" padding="0px" margin="0" borderRadius="0" v-for="(item, index) in tableList"
				 :key="index" class="card-box" @click="changeStatus(item)">
					<view class="card-content" slot="body">
						<view class="card-info-img">
							<u-avatar :src="site.url" size="100"></u-avatar>
						</view>
						<view class="card-info-text">{{item.name}}</view>
						<u-checkbox v-model="item.checked" @click.native.stop=""></u-checkbox>
					</view>
				</u-card>
			</u-checkbox-group>
			<u-divider v-if="isOver" bg-color="transparent">没有更多了</u-divider>
		</scroll-view>
		<view class="handle-fix-box">
			<u-button type="warning" shape="circle" :ripple="true" :custom-style="customStyle" @click="selectStudent()">确定</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 {
				filterText: '',
				triggered: false,
				isOver: false,
				pageIndex: 1,
				tableList: [],
			}
		},
		onLoad() {
			this.getTableList()
		},
		onReady() {},
		methods: {
			//  设置过滤字段
			setFilterText(value) {
				this.onRefresh()
			},
			//  下拉刷新
			onRefresh() {
				this.triggered = true
				this.isOver = false
				this.pageIndex = 1
				this.tableList = []
				this.getTableList()
			},
			//  重置下拉刷新状态
			onRestore() {
				this.triggered = 'restore'
				this.triggered = false
			},
			//  懒加载
			handleLoadMore() {
				if (!this.isOver) {
					this.pageIndex++
					this.getTableList()
				}
			},
			//  获取列表数据
			getTableList() {
				NET.request(API.getOtherSignStudentList, {
					name: this.filterText,
					page: this.pageIndex,
					size: 10,
				}, 'POST').then(res => {
					this.triggered = false
					res.data.row.forEach(site => site.checked = false)
					this.tableList = this.tableList.concat(res.data.row)
					this.isOver = res.data.row.length != 10
				}).catch(error => {
					this.triggered = false
					this.$refs.uTips.show({
						title: error.message,
						type: 'warning',
					})
				})
			},
			//  变更状态
			changeStatus(item) {
				item.checked = !item.checked
			},
			//  选择学员
			selectStudent() {
				uni.setStorage({
					key: 'signUserList',
					data: this.tableList.filter(site => site.checked).map(site => {
						return {
							id: site.id,
							name: site.name,
							url: site.url,
							hasUrl: site.hasUrl,
							checked: false,
						}
					})
				})
				uni.navigateBack()
			}
		},
	}
</script>

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

	.content {
		width: 100%;
		float: left;

		.filter-box {
			height: 48px;
			padding: 10px 15px;
			background-color: #FFFFFF;
		}

		.scroll-box {
			width: 100%;
			height: calc(100vh - 108px);

			.card-box {
				border-bottom: 1px solid #cccccc;

				.card-content {
					padding: 5px 15px;
					display: flex;
					align-items: center;

					.card-info-img {
						width: 50px;
						height: 50px;
						margin-right: 5px;
						display: flex;
						align-items: center;
						justify-content: center;
					}

					.card-info-text {
						line-height: 20px;
						font-size: 14px;
						flex: 1;
					}
				}
			}
		}
	}
</style>
