<template>
	<view class="content">
		<!--  -->
		<view class="kcal">
			<view class="set">
				<input type="digit" v-model="weight" :placeholder="$t('verifyRecord')" :focus="focus"
					@blur="handleBlur">
				<uni-icons v-if="weight!=''" type="close" size="24" class="uni-iocns" color="#999"
					@click="handleclear"></uni-icons>
				<view class="num">
					{{kcal.suggestion_kcal_unit}}
				</view>
			</view>
			<view class="desc">
				{{kcal.suggestion_kcal_range_val}}
			</view>
		</view>
		<!--  -->
		<view class="kcal">
			<view class="text">
				{{$t('countNutritionalProportion')}}
			</view>
			<view class="slider">
				<llt-slider-range :model-value="rangeValue" @change="handleChange" />
			</view>
			<view class="list">
				<view class="item" v-for="(ite,ind) in nutrition.list">
					<icon class="iconfont" :class="ite.icon"></icon>
					<text>{{ite.name}}</text>
					<text>{{ite.proportion}}%</text>
					<view class="val">{{ite.val}}{{ite.unit}}</view>
				</view>
			</view>
		</view>
		<view class="num">
			<view class="item" v-for="(ite,ind) in nutrition.describe">
				<text>{{ite}}</text>
			</view>
		</view>
		<view class="subbtn" @click="handleEditKcal">{{$t('btnSubmit')}}</view>
	</view>
</template>

<script>
	import {
		mapState
	} from "vuex";
	import lltSliderRange from '@/uni_modules/llt-slider-range/components/llt-slider-range/llt-slider-range.vue';
	export default {
		data() {
			return {
				weight: "",
				kcal: {},
				nutrition: {},
				focus: false,
				carbohydrate_v: 0,
				protein_v: 0,
				fat_v: 0,
				carbohydrate_p: 0,
				protein_p: 0,
				fat_p: 0,
				rangeValue: [0, 0]
			}
		},
		computed: {
			...mapState(["user"]),
			userInfo() {
				return this.user
			}
		},
		onLoad() {
			this.handleList()
			uni.setNavigationBarTitle({
				title: this.$t('titleSet')
			})
		},
		components: {
			lltSliderRange
		},
		watch: {},
		methods: {
			handleList() {
				let that = this
				that.$model.getCountSetKcal({
					aud_id: that.userInfo.aud_id
				}).then(res => {
					if (res.code == 0) {
						that.kcal = res.data.kcal
						that.nutrition = res.data.nutrition
						that.weight = res.data.kcal.suggestion_kcal_val
						that.rangeValue[0] = Number(that.nutrition.list[0].proportion)
						that.rangeValue[1] = Number(that.nutrition.list[0].proportion) + Number(that.nutrition
							.list[1].proportion)
						console.log("that.rangeValue", that.rangeValue)
						that.handleProportion()
					}
				})
			},
			handleEditUser() {
				uni.navigateTo({
					url: "/pageTwo/me/userEdit?familayData=" + JSON.stringify(this.userInfo)
				})
			},
			handleChange(val) {
				let that = this
				that.rangeValue = val
				that.weight = that.weight ? that.weight : Number(that.kcal.suggestion_kcal_val)
				that.handleProportion()
			},
			handleProportion() {
				let that = this
				that.nutrition.list.forEach(ite => {
					if (ite.key_v == "carbohydrate") {
						ite.proportion = that.rangeValue[0]
						ite.val = Number(that.weight * ite.proportion / 100 / 4).toFixed(2)
						that.carbohydrate_v = ite.val
						that.carbohydrate_p = ite.proportion
					}
					if (ite.key_v == "protein") {
						ite.proportion = that.rangeValue[1] - that.rangeValue[0]
						ite.val = Number(that.weight * ite.proportion / 100 / 4).toFixed(2)
						that.protein_v = ite.val
						that.protein_p = ite.proportion

					}
					if (ite.key_v == "fat") {
						ite.proportion = 100 - that.rangeValue[1]
						ite.val = Number(that.weight * ite.proportion / 100 / 9).toFixed(2)
						that.fat_v = ite.val
						that.fat_p = ite.proportion
					}
				})
			},
			handleBlur() {
				let that = this
				that.weight = that.weight ? that.weight : Number(that.kcal.suggestion_kcal_val)
				that.handleProportion()
			},
			handleEditKcal() {
				let that = this
				if (that.weight == '' || Number(that.weight) <= 0) {
					that.$tools.msg(that.$t('verifyCalorie'))
					return
				}
				that.$model.getCountSetUserKcal({
					aud_id: that.user.aud_id,
					set_kcal: that.weight,
					carbohydrate_v: that.carbohydrate_v,
					protein_v: that.protein_v,
					fat_v: that.fat_v,
					carbohydrate_p: that.carbohydrate_p,
					protein_p: that.protein_p,
					fat_p: that.fat_p,
				}).then(res => {
					if (res.code == 0) {
						that.$tools.msg(that.$t('msgSetSuccess'))
						that.$store.dispatch("getUserInfo")
						setTimeout(function() {
							uni.switchTab({
								url: '/pages/count/count'
							})
						}, 1000)
					}
				})
			},
			handleclear() {
				this.focus = true
				this.weight = ""
			}
		}
	}
</script>

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

	.kcal {
		width: calc(100% - 40rpx);
		background: #fff;
		margin: 20rpx 0;
		padding: 20rpx;
		border-radius: 10px;

		.set {
			width: calc(100% - 40rpx);
			background: #f7f7f7;
			border-radius: 10px;
			display: flex;
			align-items: center;
			justify-content: space-between;
			padding: 0 10px;
			position: relative;
			height: 45px;

			input {
				width: 100%;
				font-size: 16px;
			}

			.num {
				position: absolute;
				right: 10px;
			}

			.uni-iocns {
				position: absolute;
				right: 50px;
				z-index: 99;
			}
		}

		.desc {
			font-size: 24rpx;
			color: #999;
			line-height: 40rpx;
			margin-top: 10px;
		}
	}

	.slider {
		background: #f7f7f7;
		margin-top: 15px;
		border-radius: 10px;
		padding: -10px 0;
	}

	.list {
		display: flex;
		justify-content: space-between;
		margin-top: 15px;


		.item {
			display: flex;
			flex-wrap: wrap;
			width: 30%;
			background-color: #f7f7f7;
			justify-content: center;
			border-radius: 10px;

			icon {
				font-size: 40px;
				color: $uni-color-warning;
				border-radius: 30%;
				padding: 5px;
			}

			text {
				font-size: 14px;
				font-weight: 500;
				color: #666;
				display: inline-block;
				width: 100%;
				text-align: center;
				line-height: 20px;
				border-bottom: none;
			}

			.val {
				width: 100%;
				background: #f7f7f7;
				border-radius: 10px;
				margin: auto;
				text-align: center;
				font-weight: bold;
				padding: 2px 0;
			}
		}
	}

	.num {
		display: flex;
		font-size: 24rpx;
		flex-direction: column;

		.item {
			width: 100%;
		}
	}

	.subbtn {
		color: #fff;
		width: 100%;
		text-align: center;
		border-radius: 20rpx;
		height: 35px;
		line-height: 35px;
		margin: 15px 0;
		background-color: #f0ae43;
	}
</style>