<template>
	<view class="content">
		<!-- 列表 -->
		<view class="box">
			<view class="list" v-for="(item,ind) in infoList" :key="ind" @click="handleDetail(item)">
				<view class="time">{{item.time}}</view>
				<view class="kcal">
					<view>摄入卡路里<text>{{item.val}}</text>{{item.unit}}</view>
					<view class="status">
						<text class="quan0" :style="'background:'+item.color"></text>
						{{item.describe}}
					</view>
					<uni-icons type="forward" size="20" color="#666"></uni-icons>
				</view>
			</view>
			<view class="endtext" v-if="!lastPage || page >= lastPage">—— 到底了，看看别的吧 ——</view>
			<view v-if="!infoList.length" class="nolist">
				<icon class="iconfont icon-wancan"></icon>
				<text>还没有记录哦</text>
			</view>
		</view>

	</view>
</template>

<script>
	import {
		mapState
	} from "vuex";
	export default {
		data() {
			return {
				list: [],
				page: 1,
				lastPage: '',
				markDays: {
					warning: [],
					success: [],
					error: [],
				},
				infoList: []
			}
		},
		computed: {
			...mapState(["user"]),
			end() {
				return this.$tools.getTime()
			},
			endDate() {
				return this.$tools.getDate("start")
			},
		},
		onLoad() {
			let that = this
			that.page = 1
			that.list = []
			that.handleList()
		},
		onReachBottom() {
			let that = this
			if (!this.lastPage || this.page >= this.lastPage) {
				uni.showToast({
					title: '没有更多数据！',
					icon: 'none'
				})
				return
			}
			this.page++
			this.handleList(this.page)
		},
		methods: {
			handleList() {
				let that = this
				that.$model.getMyLogList({
					aud_id: that.user.aud_id,
					page: that.page,
				}).then(res => {
					if (res) {
						console.log("11111", res)
						that.list = res.content_list
						for (var i = 0; i < res.pkList.list.length; i++) {
							if (Date.parse(that.endDate) == Date.parse(res.pkList.list[i].time)) {
								that.infoList.push(res.pkList.list[i]);
							}
							if (res.pkList.list[i].describe=="超标") {
								that.markDays.error.push(res.pkList.list[i].time);
							}
							if (res.pkList.list[i].describe=="达标") {
								that.markDays.success.push(res.pkList.list[i].time);
							}
							if (res.pkList.list[i].describe=="不达标") {
								that.markDays.warning.push(res.pkList.list[i].time);
							}
						}
					}
				})
			},
			// 日期选择
			maskClick(e) {
				let that = this
				console.log('maskClick事件:', e);
				that.infoList = []
				for (var i = 0; i < that.list.length; i++) {
					if (Date.parse(e.date) == Date.parse(that.list[i].time)) { //includes 检测数组是否有某个值
						that.infoList.push(that.list[i]);
					}
				}
			},
			onMonthClickPre(data) {
				let that = this
				console.log("上月", data)
				that.list = []
				that.infoList = []
				that.markDays = {
					warning: [],
					success: [],
					error: [],
				}
				that.startM = data.substring(0, 10)
				that.endM = data.substring(11, 21)
				that.handleList()
			},
			onMonthClickNext(data) {
				console.log("下月", data)
			},
			handleDetail(item) {
				let that = this
				uni.setStorageSync("startDay", item.time)
				uni.switchTab({
					url: '/pages/count/count'
				});
			}

		}
	}
</script>

<style scoped="scoped" lang="scss">
	.content {
		padding: 0 15px;
	}

	.calendar {
		text-align: center;
		background: #fff;
		position: fixed;
		top: 0;
		left: 0;
		right: 0;
		z-index: 20;
		height: 40px;
		line-height: 40px;
		box-shadow: 0px 1px 5px 2px #dfe2e1fc;
	}

	.box {
		margin-top: 15px;
		width: 100%;
	}

	.list {
		color: #999;
		width: calc(100% - 30px);
		background: #fff;
		margin-bottom: 15px;
		border-radius: 15px;
		padding: 10px 15px;


		.time {
			width: 100%;
			height: 30px;
			line-height: 30px;
		}

		.kcal {
			display: flex;
			justify-content: space-between;
			align-items: center;

			text {
				font-size: 16px;
				font-weight: bold;
				margin: 0 3px;
				color: #000;
			}

			.status {
				width: 85px;
			}

			.quan0 {
				width: 12px;
				height: 12px;
				background: $uni-color-warning;
				display: inline-block;
				border-radius: 50%;
				margin-right: 5px;
			}
		}
	}
</style>