<template>
	<view class="content">
		<view class="content_box">
			<view class="box">
				<!-- 类型 -->
				<view class="top">
					<image :src="bgimage" mode="aspectFill">
						<view class="name">{{info.name}}</view>
						<view class="time">{{time}}</view>
					</image>
				</view>
				<!-- 成分统计 -->
				<view class="everyDay">
					<view class="title">
						<view><text class="quan"></text>{{$t('countComponentStatistics')}}</view>
					</view>
					<view class="progress">
						<div class="chart-wrap">
							<qiun-data-charts type="ring" :opts="opts" canvasId="foodCharts" :chartData="chartData" :canvas2d="true" />
						</div>
						<view class="info" v-if="info.nutrients_four">
							<view class="info-item" v-for="(item,index) in info.nutrients_four.slice(1)" :key="index">
								<view>
									<view class="color" :style="{'background-color':`${item.color}`}"></view>
									<view>{{item.name}}</view>
								</view>
								<view class="value">
									<text>{{item.value}}{{item.unit}}</text>
									<text>|</text>
									<text>{{item.proportion}}%</text>
								</view>
							</view>
						</view>
					</view>
				</view>
				<!-- 早午晚餐 -->
				<view class="foodtools">
					<view class="type">
						<view class="title">
							<view><text class="quan"></text>{{$t('countFoodTypes')}}</view>
						</view>
						<view class="list" v-if="info.list.length">
							<uni-swipe-action>
								<uni-swipe-action-item v-for="(ite,ind) in info.list" :key="ind"
									:right-options="actionOptions" @click="delAcitionItem(ite)">
									<view class="item" @click="showFoodDetail(ite)">
										<image :src="ite.pic_url" mode="aspectFill"></image>
										<view class="weight">
											<view class="overflow bold">{{ite.name}}</view>
											<view class="size12 c999">{{ite.weight}}<text>|</text>{{ite.val}}kcal</view>
										</view>
									</view>
								</uni-swipe-action-item>
							</uni-swipe-action>
						</view>
						<view v-else class="nolist">
							<image src="/static/none.png"></image>
							<view>{{$t('countNoFood')}}</view>
						</view>
					</view>
				</view>
				<!-- 添加食物 -->
				<view class="add" @click="handleAddFood()">
					<text>+</text>{{$t('countAddFood')}}
				</view>
			</view>
		</view>
		<!-- 营养含量分析 -->
		<uni-drawer ref="showRight" mode="right">
			<scroll-view style="height: 100%;" scroll-y="true">
				<view class="foodDetail">
					<view class="foodInfo">
						<image :src="activeFoodDetail.pic_url" mode="aspectFill"></image>
						<view class="info">
							<view class="name">{{activeFoodDetail.name}}</view>
							<view class="kcal">{{activeFoodDetail.val}}kcal</view>
						</view>
					</view>
					<view class="foodContent">
						<view class="title">{{$t('CaloriesAndnutrients')}}</view>
						<view class="progress">
							<div class="chart-wrap">
								<qiun-data-charts type="ring" :opts="opts2" :chartData="chartData2" :cHeight="280"
									:cWidth="280" />
							</div>
							<view class="info" v-if="activeFoodDetail.nutrients_four">
								<view class="info-item" v-for="(item,index) in activeFoodDetail.nutrients_four.slice(1)"
									:key="index">
									<view class="color" :style="{'background-color':`${item.color}`}">
									</view>
									<view>{{item.name}}：{{item.proportion}}%</view>
								</view>
							</view>
						</view>
						<view class="tips">
							<text>{{$t('Nutrients')}}</text>
							<text>{{activeFoodDetail.weight}}{{$t('Content')}}</text>
						</view>
						<view class="foodDetailList">
							<view class="foodDetailItem" v-for="(item,index) in activeFoodDetail.nutrients_list"
								:key="index">
								<view class="name">{{item.name_ch}}</view>
								<view class="value">{{item.value}}{{item.unit}}</view>
							</view>
						</view>
					</view>
				</view>
			</scroll-view>
		</uni-drawer>
	</view>
</template>

<script>
	import {
		mapState
	} from "vuex";
	let next = 0
	import qiunDataCharts from '@/uni_modules/qiun-data-charts/components/qiun-data-charts/qiun-data-charts.vue';
	export default {
		data() {
			return {
				opts: {
					color: [],
					title: {
						name: "",
					}
				},
				opts2: {
					color: [],
					title: {
						name: "",
					}
				},
				chartData: {},
				chartData2: {},
				activeFoodDetail: {},
				actionOptions: [{
					text: this.$t('btnDelete'),
					style: {
						backgroundColor: '#dd524d',
						borderRadius: '10rpx'
					}
				}],
				time: "",
				bgimage: "",
				page: "",
				index: "",
				info: {
					date: "",
					list: [],
					nutrients_four: [],
				}
			}
		},
		components: {
			qiunDataCharts
		},
		computed: {
			...mapState(["user", "countFoodInfo", "configInfo"]),
			foodItem() {
				return this.configInfo.meal_list
			},
		},
		onLoad(options) {
			let that = this
			that.page = options.page
			that.index = options.index
			that.bgimage = that.foodItem[options.index].icon_bg
			uni.setNavigationBarTitle({
				title: this.$t('titleEveryMeal')
			})
			that.handleInfo()
		},
		watch: {
			user() {
				this.handleInfo()
			},
			countFoodInfo() {
				this.handleInfo()
			}
		},
		methods: {
			handleInfo() {
				let that = this
				that.time = that.page == "home" ? that.user.food_count.date : that.countFoodInfo.date
				that.info = that.page == "home" ? that.user.food_count.list[that.index] : that.countFoodInfo.list[that
					.index]
				let chart_data = []
				that.opts.color = []
				for (let i = 1; i < that.info.nutrients_four.length; ++i) {
					this.opts.color.push(that.info.nutrients_four[i].color)
					chart_data.push({
						name: that.info.nutrients_four[i].name,
						value: Number(that.info.nutrients_four[i].proportion),
					})
				}
				this.opts.title.name = that.info.val
				this.chartData = JSON.parse(JSON.stringify({
					series: [{
						data: chart_data
					}]
				}));
			},
			// 详情
			showFoodDetail(item) {
				console.log("item", item)
				let that = this
				let chart_data = []
				this.activeFoodDetail = item
				this.$refs.showRight.open();
				this.opts2.color = []
				for (let i = 1; i < item.nutrients_four.length; ++i) {
					this.opts2.color.push(item.nutrients_four[i].color)
					chart_data.push({
						name: item.nutrients_four[i].name,
						value: Number(item.nutrients_four[i].proportion),
					})
				}
				this.opts2.title.name = that.activeFoodDetail.val
				this.chartData2 = JSON.parse(JSON.stringify({
					series: [{
						data: chart_data
					}]
				}));
			},
			// 添加食物
			handleAddFood() {
				uni.navigateTo({
					url: "/pageTwo/count/search?index=" + this.index + "&time=" + this.time
				})
				// uni.navigateTo({
				// 	url: "/pageTwo/count/search?name=" + this.info.name + "&time=" + this.time
				// })
			},
			// 删除食材
			delAcitionItem(item) {
				let that = this
				uni.showModal({
					content: that.$t("WhetherTodelete") + item.name,
					success: (res) => {
						if (res.confirm) {
							this.$model.delCEatAction({
								aud_id: that.user.aud_id,
								eat_log_id: item.id
							}).then(res => {

								// 删除最新一天食物
								if (that.time == that.user.food_count.date) {
									that.$store.dispatch("getUserInfo")
								} else {
									that.$store.dispatch("getCountFoodInfo", {
										aud_id: that.user.aud_id,
										time: that.time
									})
								}
							})
						}
					}
				})
			}
		}
	}
</script>

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

	.content_box {
		width: 100%;
	}

	.quan {
		margin-right: 70rpx;
	}

	.top {
		width: 100%;
		height: 280rpx;
		display: flex;
		position: relative;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		background: #dfdfdf;
		border-radius: 10px;
		margin-top: 15px;

		.name {
			width: 100%;
			text-align: center;
			font-size: 25px;
			font-weight: bold;
		}

		view {
			z-index: 11;
		}

		image {
			position: absolute;
			width: 100%;
			height: 100%;
			z-index: 9;
			border-radius: 10px;
		}
	}

	.everyDay {
		background: #fff;
		padding: 10px 0 10px;
		border-radius: 10px;
		margin-top: 15px;
		height: auto;
		overflow: hidden;

		.progress {
			display: flex;
			align-items: center;

			.chart-wrap {
				position: relative;
				width: 280rpx;
				height: 280rpx;
				margin-left: -5px;
			}

			.info {
				display: flex;
				flex-direction: column;
				justify-content: space-between;
				font-size: 24rpx;
				width: calc(100% - 300rpx);
				height: 200rpx;

				.info-item {
					display: flex;
					align-items: center;
					justify-content: space-between;
					margin-top: 30rpx;

					view {
						display: flex;
						align-items: center;
					}

					.value {
						:nth-child(1) {
							width: 50px;
							text-align: right;
						}

						:nth-child(2) {
							color: #ccc;
							margin: 0px 2px;
						}

						:nth-child(3) {
							width: 50px;
							text-align: left;
						}
					}



					.color {
						width: 10rpx;
						height: 10rpx;
						margin-right: 10rpx;
						border-radius: 3rpx;
					}
				}
			}
		}
	}

	.foodtools {
		margin-top: 15px;

		.type {
			background: #fff;
			margin-bottom: 10px;
			border-radius: 10px;
			padding: 10px 10px 0;

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

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

				.text {
					width: 50%;
					display: flex;
					align-items: center;

					view {
						font-size: 32rpx;
						font-weight: bold;
						margin: 0 10px;
					}
				}

				.detail {
					color: #999;
					display: flex;
					font-size: 24rpx;
					align-items: center;
				}
			}

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

				.item {
					display: flex;
					padding: 10px 0;
					border-bottom: 1px solid #f7f7f7;

					image {
						width: 90rpx;
						height: 90rpx;
						border-radius: 50%;
						border: 1px solid #f7f7f7;
					}

					.weight {
						display: flex;
						flex-direction: column;
						justify-content: space-between;
						width: calc(100% - 110rpx);
						margin-left: 20rpx;

						text {
							margin: 0 10px;
							color: #dfdfdf;
						}
					}

				}
			}
		}
	}

	.nolist {
		image {
			width: 120rpx;
			height: 120rpx;
		}

		view {
			margin-bottom: 15px;
			text-align: center;
			width: 100%;
			margin-top: 10px;
			color: #999;
		}
	}

	.add {
		width: 100%;
		text-align: center;
		font-weight: bold;
		margin: 30rpx 0;
	}
</style>