<template>
	<view class="content">
		<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">
			<view class="filter-date-box">
				<u-icon name="arrow-left" color="#999999" size="48" @click="previousMonth()"></u-icon>
				<text class="filter-date">{{year}}年{{month}}月</text>
				<u-icon name="arrow-right" color="#999999" size="48" @click="nextMonth()"></u-icon>
			</view>
			<view class="rank-box">当月销售额：￥{{saleCount}}</view>
			<u-card :show-head="false" :show-foot="false" :border="false" padding="0px" margin="0px" borderRadius="0" v-for="(item, index) in tableList"
			 :key="index" class="card-box">
				<view class="card-content" slot="body">
					<view class="info-sort">
						<u-image :src="index == 0 ? iconPath1 : (index == 1 ? iconPath2 : iconPath3)" mode="aspectFit" width="24px"
						 height="24px" v-if="index < 3"></u-image>
						<text v-else>{{index + 1}}</text>
					</view>
					<view class="info-img">
						<u-avatar :src="item.url" size="80"></u-avatar>
					</view>
					<view class="info-text">{{item.name}}</view>
					<view class="info-money">￥{{item.amount}}</view>
				</view>
			</u-card>
			<u-divider v-if="isOver" bg-color="transparent" style="padding-top: 10px;">没有更多了</u-divider>
		</scroll-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'
			])
		},
		data() {
			return {
				iconPath1: API.getServerImg + 'jinpai.png',
				iconPath2: API.getServerImg + 'yinpai.png',
				iconPath3: API.getServerImg + 'tongpai.png',
				year: '',
				month: '',
				saleCount: '',
				triggered: false,
				isOver: false,
				pageIndex: 1,
				tableList: [],
			}
		},
		onLoad() {
			this.year = new Date().getFullYear()
			this.month = new Date().getMonth() + 1
			this.getTableList()
		},
		onReady() {},
		methods: {
			//  上一个月
			previousMonth() {
				if (this.month == 1) {
					this.year--
					this.month = 12
				} else {
					this.month--
				}
				this.onRefresh()
			},
			//  下一个月
			nextMonth() {
				if (this.month == 12) {
					this.year++
					this.month = 1
				} else {
					this.month++
				}
				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.getSaleSortList, {
					date: this.year + '-' + this.month,
					page: this.pageIndex,
					size: 10,
				}, 'POST').then(res => {
					this.triggered = 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',
					})
				})
				NET.request(API.getSaleSortInfo, {
					date: this.year + '-' + this.month
				}, 'POST').then(res => {
					this.saleCount = res.data.amount
				}).catch(error => {
					this.$refs.uTips.show({
						title: error.message,
						type: 'warning',
					})
				})
			}
		},
	}
</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-date-box {
			height: 48px;
			display: flex;
			justify-content: space-between;
			align-items: center;
			padding: 10px 15px 0 15px;
			background-color: #ffffff;

			.filter-date {
				text-align: center;
				font-size: 16px;
				font-weight: bold;
				line-height: 28px;
				flex: 1;
			}
		}

		.rank-box {
			padding: 0 15px;
			color: $mainColor;
			text-align: center;
			line-height: 28px;
			background-color: #ffffff;
		}

		.scroll-box {
			width: 100%;
			height: 100vh;

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

					.info-sort {
						width: 34px;
						display: flex;
						align-items: center;
						justify-content: center;
					}

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

					.info-text {
						line-height: 20px;
						font-size: 12px;
						color: $mainColor;
						flex: 1;
					}

					.info-money {
						line-height: 20px;
						font-size: 12px;
						color: $mainColor;
					}
				}
			}
		}
	}
</style>
