<template>
	<view class="content">
		<view class="box1 box" v-if="!token" @click="handleLogin">
			<view class="info2">
				未登录，点击登录
			</view>
		</view>
		<!-- 个人资料 -->
		<view v-if="token" class="content-box">
			<view class="box" v-if="info.aud_id!=''">
				<view class="info">
					<!-- /pageTwo/me/userEdit -->
					<view class="name" @click="navTo('/pageTwo/me/userEdit')">
						<image :src="info.head_pic"></image>
						<text class="bold">{{info.nickname}}</text>
						<text>{{info.gender=='1'?'男':'女'}}</text>
					</view>
					<view class="edit" @click="navTo('/pageTwo/me/userEdit')">
						<image src="/static/26.png"></image>
					</view>
				</view>
				<view class="age">
					<view class="age-item">
						<view><text>{{info.age}}</text>岁</view>
						<view>年龄</view>
					</view>
					<view class="age-item age-item2">
						<view><text>{{info.weight}}</text>kg</view>
						<view>体重</view>
					</view>
					<view class="age-item">
						<view><text>{{info.height}}</text>cm</view>
						<view>身高</view>
					</view>
				</view>
			</view>
			<view class="box1 box" v-else @click="navTo('/pageTwo/me/userEdit')">
				<view class="info2">
					完善资料后，记录更准确哦！
				</view>
			</view>
		</view>
		<!-- 计食器 -->
		<view class="jishiqi">
			<view class="top">
				<view class="date">{{foodInfo.date}}</view>
				<view class="detail" @click="navTo('/pageTwo/count/everyDay?page=home')">
					<image src="/static/fenxi.png"></image>
					营养分析
				</view>
			</view>
			<view class="left">
				<view class="chart-wrap">
					<qiun-data-charts v-if="handTrue" type="arcbar" :chartData="chartData" :cHeight="280"
						:cWidth="280" />
					<view class="center">
						摄入
						<text>{{foodInfo.nutrients_four[0].today_intake}}</text>
						<view class="unit">Kcal</view>
					</view>
				</view>
				<view class="mubiao">
					目标：<text>{{foodInfo.nutrients_four[0].suggestion}}</text>kcal
				</view>
			</view>
			<view class="right" v-if="foodInfo.list.length">
				<view class="item" v-for="(ite,ind) in foodInfo.nutrients_four.slice(1)">
					<view class="left-icon">
						<image :src="ite.icon"></image>
						<view class="val" :style="{color:ite.color}">{{ite.proportion_fp||0}}%</view>
					</view>
					<view class="right-info">
						<view class="right-info-top">
							<text class="name">{{ite.name}}</text>
							<text class="">
								{{ite.today_intake||0}}/{{ite.suggestion||0}}g
							</text>
						</view>
						<view class="right-info-bottom">
							<view class="val" :style="{ width: ite.proportion + '%',background:ite.color}">
							</view>
						</view>
					</view>
				</view>
			</view>
		</view>
		<!-- 早午餐 -->
		<view class="tools" v-if="foodInfo.list.length">
			<view class="tools-item" v-for="(ite,ind) in foodInfo.list">
				<view class="title">
					<image :src="ite.icon"></image>
					<text>{{ite.name}}</text>
				</view>
				<view class="kcal">
					<text>{{ite.val}}</text>kcal
				</view>
				<view class="tools-btn" @click="navTo('/pageTwo/count/search?name='+ite.name+'&time='+foodInfo.date)">
					添加
				</view>
				<view class="add" @click="navTo('/pageTwo/count/everyMeal?page=home&index='+ind)">
					<image src="/static/xiangqing.png"></image>
				</view>
			</view>
		</view>

		<!-- 搜索 -->
		<view class="serachBox">
			<view class="title">
				<view class="quan mr-5"></view>教你做
			</view>
			<view class="searchInput">
				<div class="search-wrap" @click="navTo('/pages/search/search')">
					<text>输入食材快速搜索菜谱</text>
					<image src="/static/28.png"></image>
				</div>
			</view>
		</view>

		<!-- 轮播 -->
		<view class="f_banner footbox" v-if="configInfo.banner_data.length">
			<swiper class="swiper" circular="true">
				<swiper-item v-for="(ite,index) in configInfo.banner_data" @click="$tools.NewsPtype(ite)">
					<image :src="ite.pic" mode="aspectFill"></image>
				</swiper-item>
			</swiper>
		</view>
	</view>
</template>

<script>
	import {
		mapState
	} from "vuex";
	import qiunDataCharts from '@/uni_modules/qiun-data-charts/components/qiun-data-charts.vue';
	export default {
		data() {
			return {
				token: "",
				handTrue: false,
				// foodInfo: {
				// 	date: "",
				// 	list: [],
				// 	details: {},
				// 	remaining_kcal: 0,
				// 	nutrients_four: [],
				// 	trace_elements_all_day: []
				// },
				unwatch: null,
				chartData: {
					series: [{
						data: 0,
						color: "#3CB383"
					}]
				},
			}
		},
		components: {
			qiunDataCharts
		},
		computed: {
			...mapState(["configInfo", "user", "countFoodInfo", "bleValue"]),
			info() {
				return this.user
			},
			kcalVal() {
				return this.configInfo.kcal_data
			},
			foodInfo() {
				let that = this
				this.handTrue = false
				this.$nextTick(() => {
					this.handTrue = true
					that.chartData.series[0].data = this.user.aud_id != "" ? Number(this.user.food_count
						.nutrients_four[0].proportion) / 100 : 0
				})
				return this.user.aud_id != "" ? this.user.food_count : this.configInfo.default_count_foot
			}
		},
		onLoad() {
			let that = this
			that.token = uni.getStorageSync('token')
			that.$store.dispatch("getHomeConfig")
			if (that.token) {
				that.$ble.openBluetoothAdapter()
			}
			that.$ble.onBLEConnectionStateChange()
			uni.onBluetoothAdapterStateChange(function(res) {
				that.$store.commit("changeBluetooth", res.available);
			})
		},
		onPullDownRefresh() {
			let that = this
			that.index = 0
			that.footlist = []
			that.chartData.series[0].data = 0
			that.$store.dispatch("getHomeConfig")
			setTimeout(function() {
				uni.stopPullDownRefresh();
			}, 500);
		},
		onShow() {
			let that = this
			that.handTrue = false
			this.$nextTick(() => {
				that.handTrue = true
				that.chartData.series[0].data = that.user.aud_id != "" ? Number(that.user.food_count
					.nutrients_four[0].proportion) / 100 : 0
			})
			this.startWatching()
		},
		onHide() {
			this.stopWatching()
		},
		methods: {
			handleInfo() {
				let that = this
				this.$nextTick(() => {
					that.chartData.series[0].data = that.user.aud_id == "" ? 0 : Number(that.user.food_count
						.nutrients_four[0]
						.proportion) / 100
				})
			},
			startWatching() {
				this.unwatch = this.$watch('bleValue', (newVal, oldVal) => {
					// 这里可以执行你的业务逻辑
					if (newVal.serviceId != '' && newVal.oldCountWeight != newVal.countWeight) {
						uni.switchTab({
							url: "/pages/count/count"
						})
						newVal.oldCountWeight = newVal.countWeight
					}
				}, {
					immediate: false, // 是否立即执行
					deep: true
				})
			},
			stopWatching() {
				if (this.unwatch) {
					this.unwatch() // 调用取消监听函数
					this.unwatch = null
				}
			},
			// 登录
			handleLogin() {
				uni.reLaunch({
					url: "/pageTwo/login/login"
				})
			},
			handleAddFood(name) {
				let that = this
				if (!that.token) {
					that.$tools.msg("登录后查看更多")
					return
				}
				unwatch();
				uni.navigateTo({
					url: "/pageTwo/count/search?name=" + name + "&time=" + this.foodInfo.date
				})
			},
			// 功能页面跳转
			navTo(url) {
				if (!this.token) {
					this.$tools.msg("登录后查看更多")
					return
				}
				uni.navigateTo({
					url
				})
				uni.switchTab({
					url
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	.headerbgimg {
		position: absolute;
		width: 400rpx;
		height: 360rpx;
		top: 0;
		right: -15%;
	}

	.content-box {
		width: 100%;
		position: relative;
		overflow: hidden;

	}

	.chart-wrap {
		position: relative;
		width: 250rpx;
		height: 250rpx;
		margin-top: -30rpx;
		margin-left: -5px;
		display: flex;
		flex-wrap: wrap;

		.center {
			border: none;
			width: 280rpx;
			height: 210rpx;
			position: absolute;
			top: 64rpx;
		}
	}

	.box {
		width: calc(100% - 100rpx);
		background: $maincolor;
		display: flex;
		padding: 30rpx 20rpx;
		align-items: center;
		overflow: hidden;
		margin: 15px 15px 0;
		border-radius: 10px;
		flex-wrap: wrap;

		.info {
			color: #fff;
			width: 100%;
			display: flex;
			align-items: center;

			.name {
				width: 80%;
				display: flex;
				font-size: 32rpx;
				float: left;
				align-items: center;

				text {
					margin-right: 30rpx;
				}

				image {
					width: 35px;
					height: 35px;
					margin-right: 15px;
				}
			}

			.edit {
				width: 20%;
				text-align: right;
				float: left;

				image {
					width: 22px;
					height: 22px;
				}
			}
		}

		.age {
			display: flex;
			width: 100%;
			justify-content: space-between;
			color: #fff;
			margin-top: 15px;

			.age-item {
				width: 33.3%;
				text-align: center;

				text {
					font-size: 20px;
					margin-right: 5px;
					display: inline-block;
				}
			}

			.age-item2 {
				position: relative;
			}

			.age-item2::after {
				content: "";
				position: absolute;
				left: 0;
				top: 10px;
				bottom: 10px;
				width: 1px;
				background-color: #9CDCBF;
			}

			.age-item2::before {
				content: "";
				position: absolute;
				right: 0;
				top: 10px;
				bottom: 10px;
				width: 1px;
				background-color: #9CDCBF;
			}

		}

	}

	.jishiqi {
		width: calc(100% - 50px);
		background: #fff;
		padding: 10px 10px 15px;
		border-radius: 10px;
		margin: 15px;
		height: auto;
		overflow: hidden;
		box-shadow: 0px 1px 5px 2px #dfe2e1fc;

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

		.date {
			font-size: 14px;
			font-weight: bold;
		}

		.detail {
			color: #3CB383;
			width: auto;
			padding: 3px 10px;
			background: #fff;
			border-radius: 8px;
			border: 1px solid #3CB383;
			display: flex;
			align-items: center;

			image {
				width: 44rpx;
				height: 44rpx;
				margin-right: 5px;
			}
		}

		.left {
			margin-top: -10px;
		}

		.right {
			margin-top: 0;
		}
	}

	.box1 {
		.header {
			width: 100rpx;
			height: 100rpx;
			background: #fff;
			text-align: center;
			line-height: 100rpx;

			image {
				width: 80rpx;
				height: 80rpx;
				color: #fff;
				margin-top: 10rpx;
			}
		}

		.info2 {
			color: #fff;
			font-size: 36rpx;
			font-weight: bold;
		}
	}

	.tools {
		margin: 0 30rpx 0;
		display: flex;
		flex-wrap: wrap;
		width: calc(100% - 60rpx);
		justify-content: space-between;

		.tools-item {
			width: 42%;
			background: #fff;
			position: relative;
			padding: 10rpx 20rpx;
			height: 240rpx;
			overflow: hidden;
			border-radius: 20rpx;
			margin-bottom: 30rpx;
			display: flex;
			flex-direction: column;
			justify-content: space-around;
			box-shadow: 0px 1px 5px 2px #dfe2e1fc;

			.title {
				display: flex;
				align-items: center;
				font-size: 28rpx;

				image {
					width: 50rpx;
					height: 50rpx;
					margin-right: 5px;
				}
			}

			.kcal {
				text-align: center;

				text {
					font-size: 56rpx;
					font-weight: bold;
					margin-right: 20rpx;
				}
			}

			.tools-btn {
				border: 1px solid #9CDCBF;
				text-align: center;
				border-radius: 20rpx;
				width: 60%;
				margin-left: 20%;
				color: $maincolor;
				padding: 3px 0;
			}

			.add {
				width: 48rpx;
				height: 48rpx;
				position: absolute;
				top: 20rpx;
				right: 20rpx;
				z-index: 999;

				image {
					width: 100%;
					height: 100%;
				}
			}

			.bgimg {
				width: 180rpx;
				height: 180rpx;
				position: absolute;
				top: 20rpx;
				right: -50rpx;
				opacity: 0.6;
			}
		}
	}

	.serachBox {
		width: calc(100% - 60rpx);

		.title {
			display: flex;
			align-items: center;
			font-size: 32rpx;
			font-weight: bold;
		}

		.searchInput {
			width: calc(100% - 60rpx);
			background-color: #fff;
			padding: 30rpx;
			margin-top: 20rpx;
			border-radius: 20rpx;
		}

		.search-wrap {
			height: 80rpx;
			display: flex;
			align-items: center;
			justify-content: space-between;
			border: 1px solid $maincolor;
			border-radius: 30rpx;
			padding: 0 20rpx;
			background: #fff;

			image {
				width: 50rpx;
				height: 50rpx;
			}
		}
	}

	.f_banner {
		width: 100% !important;
		height: 450rpx;
		margin: 30rpx auto;

		/deep/swiper {
			height: 450rpx;
		}

		image {
			width: 100%;
			height: 100%;
			background-size: 100%;
			position: relative;
		}
	}
</style>