<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>成分统计</view>
					</view>
					<view class="progress">
						<div class="chart-wrap">
							<qiun-data-charts type="ring" :opts="opts" canvasId="everyMeal01" :chartData="chartData"
								:cHeight="280" :cWidth="280" :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>
									<view>{{item.value}}{{item.unit}}<text>|</text>{{item.proportion}}%</view>
								</view>
							</view>
						</view>
					</view>
				</view>
				<!-- 早午晚餐 -->
				<view class="foodtools">
					<view class="type">
						<view class="title">
							<view><text class="quan"></text>食物类型</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>{{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>暂无食物</view>
						</view>
					</view>
				</view>
				<!-- 添加食物 -->
				<view class="add" @click="handleAddFood()">
					<text>+</text>添加食物
				</view>
			</view>
		</view>
		<!-- 营养含量分析 -->
		<view v-if="drawerVisible" class="drawerVisible">
			<view class="bgVisible" @click="handleDrawerClose"></view>
			<view class="infoVisible">
				<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}}千卡</view>
							</view>
						</view>
						<view class="foodContent">
							<view class="title">热量和营养</view>
							<view class="progress">
								<div class="chart-wrap">
									<qiun-data-charts type="ring" :opts="opts2"
										:chartData="chartData2" :cHeight="280" :cWidth="280" canvasId="everyMeal02" :canvas2d="true"/>
								</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>营养素</text>
								<text>{{activeFoodDetail.weight}}含量</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>
			</view>
		</view>
	</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: {},
				drawerVisible: false,
				chartVisible: false, // 单独控制图表显示
				actionOptions: [{
					text: '删除',
					style: {
						backgroundColor: '#dd524d',
						borderRadius: '10rpx'
					}
				}],
				time: "",
				bgimage: "",
				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.index = options.index
			that.bgimage = that.foodItem[options.index].icon_bg
			that.handleInfo()
		},
		watch: {
			user() {
				this.handleInfo()
			},
			countFoodInfo() {
				this.handleInfo()
			}
		},
		methods: {
			handleInfo() {
				let that = this
				that.time = that.countFoodInfo.date
				that.info = 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.drawerVisible = true;
				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
					}]
				}));
				that.$nextTick(() => {
					// 延迟显示图表，确保容器已渲染
					setTimeout(() => {
						that.chartVisible = true
					}, 100)
				})
			},
			handleDrawerClose() {
				// 先隐藏图表，再隐藏抽屉
				this.chartVisible = false
				this.drawerVisible = false;
			},
			// 添加食物
			handleAddFood() {
				uni.navigateTo({
					url: "/Food/count/search?name=" + this.info.name + "&time=" + this.time
				})
			},
			// 删除食材
			delAcitionItem(item) {
				let that = this
				uni.showModal({
					content: `是否删除[${item.name}]？`,
					success: (res) => {
						if (res.confirm) {
							this.$model.delCEatAction({
								aud_id: that.user.aud_id,
								eat_log_id: item.id
							}).then(res => {
								// 删除最新一天食物
								that.$store.dispatch("getCountFoodInfo", {
									aud_id: that.user.aud_id,
									time: that.time
								})
							})
						}
					}
				})
			}
		}
	}
</script>

<style scoped lang="scss">
	.content {
		min-height: 100vh;
		background: #f7f7f7;
		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;
		border-radius: 10px;
		margin: 15px 0 0;
		height: auto;
		overflow: hidden;

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

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

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

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

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

					text {
						color: #ccc;
						margin: 0px 5px;
					}

					.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;
						margin-left: 10px;

						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;
	}

	.no-scroll {
		height: 80vh !important;
		overflow: hidden !important;
	}
</style>