<template>
	<view class="common">
		<view class="history">
			<view class="list" v-for="(item, index) in ranklist" :key="index" @click="clickItemMethod(item)">
				<uni-swipe-action>
					<uni-swipe-action-item :right-options="item.options" @click="swipeClick($event, index)">
						<view class="item">
							<view class="time">
								{{item.createtime}}
							</view>
							<view>{{item.weight}}<text>体重</text></view>
							<view>{{item.bmi}}<text>BMI</text></view>
							<view>{{item.fat_r}}<text>脂肪率</text></view>
							<uni-icons type="right"></uni-icons>
						</view>
					</uni-swipe-action-item>
				</uni-swipe-action>
			</view>
			<view class="endtext" v-if="!lastPage || page >= lastPage">—— 到底了，看看别的吧 ——</view>
		</view>
		<view class="nolist" v-if="!lastPage">
			<uni-icons class="iconfont icon-zanwu1"></uni-icons>
		</view>
	</view>
</template>

<script>
	import {
		mapState
	} from "vuex";
	export default {
		computed: {
			...mapState(['user', "appTheme"]),
		},
		data() {
			return {
				options: [{
					text: '取消',
					style: {
						backgroundColor: '#007aff'
					}
				}, {
					text: '确认',
					style: {
						backgroundColor: '#dd524d'
					}
				}],
				list: [
					"测量时间",
					"体重",
					"BMI",
					"操作",
				],
				ranklist: [],
				page: 1,
				lastPage: 1,
				type: null,
				id: null,
			}
		},
		mounted() {
			let that = this
			let list = this.$json.historylist
			let options = [{
				text: '删除',
				style: {
					backgroundColor: '#dd524d'
				}
			}]
			list.forEach(item => {
				item.options = options
			})
			this.ranklist = this.ranklist.concat(list)
		},
		onReachBottom() {
			let that = this
			console.log("onReachBottom", this.lastPage)
			if (!this.lastPage || this.page >= this.lastPage) {
				uni.showToast({
					title: '没有更多数据！',
					icon: 'none'
				})
				return
			}
			this.page++
			this.getList(this.page)
		},
		methods: {
			swipeClick(e, index) {
				let that = this
				let id = that.ranklist[index].id
				uni.showModal({
					title: '友情提示',
					content: '是否删除当前测量记录？',
					success: function(res) {
						if (res.confirm) {
							that.$model.gethistorydelete({
								id: id,
							}).then((res) => {
								if (res.code != 0) {
									that.$tools.msg(res.message)
									return
								}
								that.ranklist.splice(index, 1)
								that.$store.dispatch("getUserInfo", {
									familyid: that.user.familyid,
								})
								that.$store.dispatch("getResult", {
									birthday: that.user.birthday,
									familyid: that.user.familyid,
									height: that.user.height,
									sex: that.user.sex,
								})

								that.$tools.msg("删除成功")
							})
						} else if (res.cancel) {
							that.$tools.msg("您已取消操作！");
						}
					},
				})
			},
			clickItemMethod(item) {
				uni.navigateTo({
					url: "/pages/history/historyDetail?index=" + JSON.stringify(item)
				})
			},
			getList(page) {
				let that = this
				that.$model.getHistoryList({
					familyId: that.user.familyid,
					pageNo: page,
					pageSize: 10
				}).then((res) => {
					console.log("历史记录", res)
					if (res.code != 0) return
					res.data.rows.forEach(item => {
						item.slide_x = 0
					})
					this.ranklist = this.ranklist.concat(res.data.rows)
					this.lastPage = res.data.totalpage
				})
			},
		}
	}
</script>
<style scoped="scoped" lang="scss">
	.common {
		width: 100%;
		min-height: 100.5vh; // 重点
		overflow-y: scroll;
		background-color: #f7f7f7;
	}

	.history {
		width: calc(100% - 30px);
		height: auto;
		margin: 15px 15px 0;
		padding-bottom: 40px;

		.list {
			width: 100%;
			margin-bottom: 12px;

			.item {
				width: calc(100% - 20px);
				height: auto;
				background: #fff;
				padding: 6px 10px;
				display: flex;
				justify-content: space-between;
				border-radius: 10px;
				align-items: center;
				text-align: center;
				font-weight: 700;
				line-height: 50rpx;
				font-size: 18px !important;

				text {
					display: block;
					color: #666;
					text-align: center;
					font-weight: 500;
					font-size: 14px;
				}
			}

			.time {
				color: #666;
				width: 35%;
				overflow: hidden;
				text-overflow: ellipsis;
				white-space: nowrap;
				font-size: 14px;
			}
		}
	}
</style>