<template>
	<view class="content">
		<!-- 搜索 -->
		<search @handleSearch="handleSearch"></search>

		<view class="box menu">
			<!-- 左侧菜单栏 -->
			<view class="left">
				<view class="name" v-for="(ite,ind) in menu" :key="ind" :class="[index==ind?'active':'']"
					@click="handleToggle(ind)">
					{{ite.name}}
				</view>
			</view>

			<!-- 右侧商品 -->
			<view class="right">
				<view class="right_list" v-for="(ite,ind) in menulist" :key="ind">
					<view class="title">
						<view>{{ite.name}}</view>
					</view>
					<view class="list">
						<view class="item" v-for="(it,id) in ite.list" :key="id" @click="handleDetail(it)">
							<text>{{it.name}}</text>
						</view>
					</view>
				</view>
			</view>
		</view>
		<!-- 底部购物车 -->
		<view class="groupbtn">
			<view class="che" @click="isShop =! isShop">
				<text v-if="ActiveList.length">{{ActiveList.length}}</text>
				<!-- <image src="../../static/pan.png"></image> -->
				<icon class="t-icon t-icon-canpan"></icon>
			</view>
			<view class="subbtn" @click="handlesubbtn"> 确定</view>
		</view>
		<!-- 购物车列表 -->
		<view class="wrapper activeList" v-if="isShop">
			<view class="bg" @click='isShop=false'>
				<view class="box" @click.stop>
					<view class="title">共{{ActiveList.length}}条记录</view>
					<view class="list" v-if="ActiveList.length">
						<view class="item" v-for="(ite,ind) in ActiveList" :key="ind">
							<view class="">
								<text class="name">{{ite.name}}</text>
								<text>（{{ite.weight}}{{ite.unit}}）</text>
							</view>
							<icon class="iconfont icon-ashbin" @click="handledelactive(ite)"></icon>
						</view>
					</view>
					<view v-else class="nolist list">
						<icon class="iconfont icon-wancan"></icon>
						<text>还没有记录哦</text>
					</view>
				</view>
			</view>
		</view>
		<!-- 称重区 -->
		<view class="wrapper" v-if="IsWeight">
			<view class="bg" @click='IsWeight=false'>
				<view class="box" @click.stop>
					<view class="title">
						<view class="weight" @click="handleWeight()">
							<icon class="iconfont icon-lianjie"></icon>
							食物秤称重
						</view>
						<view class="cancel">
							<icon class="iconfont icon-error" @click="Cancelword()"></icon>
						</view>

					</view>
					<view class="name">{{activeType.name}}</view>
					<view class="val">
						<text>{{activeType.weight?activeType.weight:'100'}}</text>g
					</view>
					<view class="mybrankmask">
						<view class="MymaskAll">
							<view class="MymaskList">
								<view class="maskListItem" @click="NumberCk(1)">1</view>
								<view class="maskListItem" @click="NumberCk(2)">2</view>
								<view class="maskListItem" @click="NumberCk(3)">3</view>
							</view>
							<view class="MymaskList">
								<view class="maskListItem" @click="NumberCk(4)">4</view>
								<view class="maskListItem" @click="NumberCk(5)">5</view>
								<view class="maskListItem" @click="NumberCk(6)">6</view>
							</view>
							<view class="MymaskList">
								<view class="maskListItem" @click="NumberCk(7)">7</view>
								<view class="maskListItem" @click="NumberCk(8)">8</view>
								<view class="maskListItem" @click="NumberCk(9)">9</view>
							</view>
							<view class="MymaskList">
								<view class="maskListItem" @click="NumberCk('.')">.</view>
								<view class="maskListItem" @click="NumberCk(0)">0</view>
								<view class="maskListItem" @click="Cancelword()">
									<image src="../../static/Clear.png"></image>
								</view>
							</view>
						</view>
						<view class="MymaskList MymaskList2">
							<view class="maskListItem text" @click="Tuige()">
								<image src="../../static/close.png" class="close"></image>
							</view>
							<view class="maskListItem text" @click="Clear()">清空</view>
							<view class="maskListItem width48" @click="Next()">保存</view>
						</view>
					</view>
				</view>
			</view>
		</view>
		<!--蓝牙连接区  -->
		<view class="wrapper" v-if="isBle">
			<view class="bg" @click='isBle=false'>
				<view class="box" @click.stop>
					<view class="title">
						<view class="name">{{activeType.name}}</view>
						<icon class="iconfont icon-error" @click='isBle=false'></icon>
					</view>
					<blue-tooth @handleBle="handleBle" :weightType="'2'"></blue-tooth>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		mapState
	} from "vuex";
	let myTime
	const plugin = requirePlugin("sdkPlugin").AiLink;
	import search from "../../components/search.vue"
	import blueTooth from "../../components/bluetooth.vue"
	export default {
		components: {
			search,
			blueTooth
		},
		data() {
			return {
				typename: "",
				typeind: 0,
				index: 0,
				menu: [],
				menulist: [],
				myvalue: [],
				ActiveList: [],
				activeType: {},
				IsWeight: false,
				isShop: false,
				isBle: false,
				kcal: "",
				weight: "",
				stopblue: true,
				isConnection: 0, //是否连接成功
			};
		},
		computed: {
			...mapState(["user", 'isConnected', "isBluetoothTyle"]),
		},
		onLoad(options) {
			let that = this
			that.typename = options.name
			that.typeind = options.ind
			that.menu = that.$json.food
			that.menulist = that.$json.foodlist
		},
		methods: {
			
			// 搜索
			handleSearch(ite){
				console.log("计食器搜索",ite)
			},
			// 左侧切换
			handleToggle(index) {
				let that = this
				that.index = index
			},
			// 商品选择
			handleDetail(ite) {
				var that = this;
				that.myvalue = []
				that.activeType = ite
				that.activeType.weight = 100
				that.IsWeight = true
				that.isShop = false
				console.log("选中的商品", ite)
			},
			// 购物车提交
			handlesubbtn() {
				let that = this
				let pages = getCurrentPages()
				let prevPage = pages[pages.length - 2]
				let data = {
					index: "",
					title: "",
					list: []
				}
				data.index = that.typeind
				data.title = that.typename
				data.list = that.ActiveList
				prevPage.$vm.getAddFood(data)
				uni.navigateBack({
					delta: 1
				})
			},
			//删除购物车食材
			handledelactive(ite) {
				let that = this
				that.ActiveList.splice(that.ActiveList.indexOf(ite), 1);
			},
			// 键盘点击
			NumberCk(val) {
				let that = this
				that.myvalue = that.myvalue[0] == '.' ? [0, '.'].concat(val) : that.myvalue.concat(val)
				if (that.myvalue.toString().indexOf('.') != -1) {
					let ind = that.myvalue.indexOf(".")
					that.myvalue = that.myvalue.slice(0, ind + 2)
				}
				if (that.myvalue.toString().indexOf('.') == -1 && that.myvalue.length > 3) {
					that.myvalue = ["9", "9", "9"]
				}
				that.activeType.weight = that.myvalue.join("")
			},
			// 键盘退格
			Tuige() {
				let that = this
				var txt = that.myvalue
				if (txt.length >= 1) {
					if (txt[txt.length - 2] != '.') {
						that.myvalue = txt.slice(0, txt.length - 1)
					} else {
						that.myvalue = txt.slice(0, txt.length - 2)
					}
				}
				that.activeType.weight = that.myvalue.join("")
				// console.log("退格1", txt, that.myvalue.join(""))
			},
			// 键盘取消
			Cancelword() {
				this.IsWeight = false
				this.myvalue = []
				this.activeType = {}
				// console.log("取消")
			},
			// 键盘清空
			Clear() {
				this.myvalue = []
				this.activeType.weight = null
				// console.log("清空")
			},
			// 键盘保存
			Next() {
				let that = this
				let ite = that.activeType
				that.IsWeight = false
				if (that.ActiveList.indexOf(ite) == -1) {
					that.ActiveList.push(ite);
				} else {
					let index = that.ActiveList.indexOf(ite)
					that.ActiveList[index].weight = that.activeType.weight;
					that.ActiveList[index].unit = that.activeType.unit;
				}
				console.log("保存", that.ActiveList)
			},
			// 称重
			handleWeight() {
				let that = this
				that.isBle = true
				that.IsWeight = false
			},
			//称重返回
			handleBle(weight, unit, kcal) {
				let that = this
				that.isBle = false
				that.activeType.weight = weight
				that.activeType.unit = unit
				that.Next()
				console.log("返回", that.activeType, weight, unit, kcal)
			}
		}
	}
</script>

<style lang="scss" scoped>
	.right {
		.item {
			width: 30%;
			border: 1px solid #f7f7f7;
			display: flex;
			align-items: center;
			justify-content: space-around;
			height: 28px;
			border-radius: 8px;
			margin-bottom: 10px;

			text {
				margin-bottom: 0 !important;
			}
		}

		.icon-xuanzhong {
			color: $mainColor;
		}

		.active0 {
			border: 1px solid $mainColor;
		}
	}

	.weightPages {
		display: flex;
		flex-wrap: wrap;
		flex-direction: column;
		position: absolute;
		left: 0;
		right: 0;
		bottom: 20px;
		top: 60px;
		justify-content: space-around;

		.weight {
			background: #fff;
			color: #666;
			font-size: 16px;
			flex-wrap: wrap;
			text-align: center;

			view {
				width: 60%;
				height: 50px;
				display: flex;
				margin-left: 25%;
				align-items: flex-end;
				margin-bottom: 15px;

				text {
					width: 80px;
					display: inline-block;
					border-bottom: 1px solid #dfdfdf;
					margin: 0 10px;
					font-size: 18px;
					font-weight: bold;
					color: #f0ae43;
				}
			}
		}

		.tips {
			font-size: 12px;
			text-align: center;
		}

		.btn {
			color: #fff;
			width: 80%;
			margin-left: 10%;
		}

		.table {
			width: 100%;
			font-size: 16px;
			font-weight: bold;
			text-align: center;
			margin: 15px 0;
		}

		.image {
			width: 160px;
			height: 160px;
			margin: auto;

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

		.tips {
			margin-bottom: 15px;
			margin-left: 15px;
			display: flex;
			color: #999;
		}
	}

	.groupbtn {
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: 0 10px;
		position: absolute;
		bottom: 0;
		left: 0;
		right: 0;
		height: 55px;
		z-index: 15;
		background-color: #fff;
		overflow: hidden;
		box-shadow: 0px 1px 5px 2px #dfe2e1fc;

		.subbtn {
			color: #fff;
			width: 40%;
			text-align: center;
			border-radius: 10px;
			height: 31px;
			line-height: 31px;
			background-color: $mainColor;
		}

		.che {
			width: 50px;
			height: 40px;
			position: relative;

			text {
				position: absolute;
				height: 15px;
				background: red;
				width: 15px;
				border-radius: 50%;
				display: inline-block;
				color: #fff;
				line-height: 15px;
				text-align: center;
				font-size: 12px;
				right: 0;
				top: 5px;
			}

			image,
			.t-icon {
				width: 50px;
				height: 50px;
				margin-top: -3px;
				border-radius: 50%;
			}
		}
	}

	.menu {
		.left {
			bottom: 55px;
			height: calc(100vh - 120px);
		}

		.right {
			bottom: 55px;
			height: calc(100vh - 120px);

		}

	}

	.activeList {
		z-index: 12;
		bottom: 50px;

		.title {
			font-weight: bold;
			margin: 5px 0;
		}

		.list {
			padding-bottom: 55px;

			.name {
				margin-right: 5px;
			}


		}
	}

	.icon-error {
		font-size: 45px;
		margin-top: -30px;
		background: #fff;
		border-radius: 50%;
		width: 45px;
		height: 45px;
	}
</style>
