<template>
	<view>
		<view class="search">
			<view class="search-title">
				输入机器编码
			</view>
			<view class="search-input">
				<input type="text" placeholder='输入设备编码后6位' v-model="searchClientId" />
			</view>
			<view class="search-button">
				<button type="primary" class="" @click="search()">
					<p class="">搜索</P>
				</button>
			</view>
		</view>
		<view class="td-right">
			<view class="uni-list">
				<view class="uni-list-cell">
					<view class="uni-list-cell-left">
						点击选择机器
					</view>
					<view class="uni-list-cell-db" style="overflow: hidden;text-overflow: ellipsis;">
						<picker @change="changeEquipment" :value="index" :range="equipmentNameList">
							<view class="uni-input">{{equipmentNameList[index]}}</view>
						</picker>
					</view>
				</view>
				<view class="uni-list-cell">
					<view class="uni-list-cell-left">
						点击选择商品
					</view>
					<view class="uni-list-cell-db" style="overflow: hidden;text-overflow: ellipsis;">
						<picker @change="changeProduct" :value="index2" :range="productList">
							<view class="uni-input">{{productList[index2]}}</view>
						</picker>
					</view>
				</view>
			</view>
		</view>
		<view class="p">
			<p style="text-align:right" @click=sugarList()>今日做糖列表>>></p>
		</view>
		<view class="tr">
			<button type="primary" formType="submit" @click="dosugar()" class="button">
				<p class="p1">提交</p>
			</button>
		</view>
	</view>
</template>

<script>
	import {
		mapState,
		mapActions,
		mapMutations
	} from 'vuex'
	export default {
		data() {
			return {
				globalUser: {},
				index: null,
				index2: null,
				equipmentName: null,
				equipmentNameList: [],
				products: [],
				productList: [],
				productName: null,
				equipmentId: null,
				searchClientId:null
			}
		},
		onShow() {
			this.globalUser = uni.getStorageSync("globalUser");
			// uni.setNavigationBarTitle({
			// 	title: this.$t('equipmentStatusList.title')
			// });
			// uni.setTabBarItem({
			// 	index: 3,
			// 	text: this.$t('tabs.tab4')
			// });
			var token = uni.getStorageSync("token");
			if (token.length > 1) {
				this.getEquipmentListData();
			} else {
				uni.reLaunch({
					url: '../Login/Login',
				});
			}

		},
		methods: {
			...mapActions('chart', ['getEquipmentListByUser']),
			getEquipmentListData() {
				this.getEquipmentListByUser(this.globalUser)
					.then(data => {
						this.merchantList = data;
						var listName = data[0].equipmentList;
						var equipmentNameList = [];
						for (var i = 0; i < listName.length; i++) {
							equipmentNameList.push("名称:" + listName[i].name + " 编号:" + listName[i].clientId.substring(
								listName[i].clientId.length - 6, listName[i].clientId.length));
						}
						this.equipmentNameList = equipmentNameList;
						var listId = data[0].id;
						if (listId != null && listId != '1') {
							uni.setStorageSync("listName", listName);
						}
						uni.stopPullDownRefresh();
					}, _ => void uni.stopPullDownRefresh());
			},
			search(){
				var clientId = this.searchClientId;
				var list = uni.getStorageSync("listName");
				var n = 0;
				for (var i = 0; i < list.length; i++) {
					var code = list[i].clientId.substring(list[i].clientId.length - 6, list[i].clientId.length);
					if(code==clientId){
						n++;
						this.index = i;
						var id = list[i].id;
						this.getProName(id);
						this.equipmentId = id;
						this.productName = null;
						this.index2 = null;
						break;
					}
				}
				if(n==0){
					uni.showModal({
						title: "提示",
						content: "找不到该机器",
						success: (res) => {
					
						}
					})
				}
			},
			sugarList() {
				uni.navigateTo({
					url: 'sugarList',
				});
			},
			//改变机器
			changeEquipment: function(e) {
				this.index = e.target.value;
				// console.log("index="+this.index)
				var list = uni.getStorageSync("listName");
				this.getProName(list[e.target.value].id);
				this.equipmentId = list[e.target.value].id;
				this.productName = null;
				this.index2 = null;
				this.searchClientId = null;
			},
			changeProduct: function(e) {
				this.index2 = e.target.value;
				// console.log("index="+this.index2)
				var list = this.products;
				this.productName = list[this.index2].productName;
			},
			//获取商品列表
			getProName(equipmentId) {
				// console.log("equipmentId="+equipmentId)
				var token = uni.getStorageSync("token");
				uni.request({
					url: this.serverurl + '/TProduct/selectProducts',
					data: {
						"equimentId": equipmentId
					},
					header: {
						'token': token
					},
					method: "GET",
					success: (res) => {
						var listName = res.data.data;
						this.products = listName;
						var productList = [];
						for (var i = 0; i < listName.length; i++) {
							productList.push(listName[i].productName);
						}
						this.productList = productList;
					},
				});
			},
			dosugar() {
				uni.showModal({
					title: "提示",
					content: "是否远程做糖？",
					success: (res) => {
						if (res.confirm) {
							var that = this;
							var token = uni.getStorageSync("token");
							var productName = this.productName;
							if (productName == null) {
								return;
							}
							var adminId = uni.getStorageSync("globalUser").id;
							var equipmentId = this.equipmentId;
							if (equipmentId == null) {
								return;
							}
							uni.request({
								url: this.serverurl + '/TSugarDo/doSugar',
								data: {
									"equipmentId": equipmentId,
									"adminId": adminId,
									"productName": productName
								},
								header: {
									'token': token
								},
								method: "GET",
								success: (res) => {
									var code = res.data.code;
									if (code == true) {
										uni.showToast({
											title: "发送成功",
											duration: 2000
										});
										var sugar = res.data.data;
										console.log(sugar.no);
										var no = sugar.no;
										setTimeout(function() {
											that.selectSugar(no);
										}, 7000);
										// this.selectSugar(sugar.no)
									} else {
										uni.showToast({
											title: res.data.message,
											duration: 2000
										});
									}
								},
							});
						} else if (res.cancel) {}

					}
				})
			},
			selectSugar(no) {
				var token = uni.getStorageSync("token");
				uni.request({
					url: this.serverurl + '/TSugarDo/selectSugarStatus',
					data: {
						"no": no
					},
					header: {
						'token': token
					},
					method: "GET",
					success: (res) => {
						var code = res.data.code;
						if (code == true) {
							uni.showModal({
								title: "提示",
								content: res.data.message,
								success: (res) => {

								}
							})

							// var sugar = res.data.data;
						} else {
							uni.showModal({
								title: "提示",
								content: res.data.message,
								confirmText: "重新查询",
								success: (res) => {
									if (res.confirm) {
										this.selectSugar(no);
									} else if (res.cancel) {}
								}
							})
						}
					},
				});
			}
		}
	}
</script>

<style>
	.search{
		width: 100%;
		padding-top: 10upx;
		display: flex;
		flex-direction: row;
		justify-content: flex-start;
	}
	.search-title{
		width: 28%;
		text-align: center;
		font-size: 26upx;
		font-family: "PingFang-SC-Bold";
	}
	.search-input{
		width: 45%;
		text-align: center;
		font-size: 26upx;
		font-family: "PingFang-SC-Bold";
		box-shadow: 0upx 0upx 20upx #D3D3D3;
		border-radius: 5upx;
	}
	.search-button{
		width: 20%;
		padding-left: 7upx;
		text-align: center;
	}
	.tr {
		padding-top: 15upx;
		display: flex;
		flex-direction: row;
		justify-content: flex-start;
		font-size: 48upx;
		font-family: "PingFang-SC-Bold";
		position: fixed;
		bottom: 100upx;
		width: 100%;
	}

	.button {
		margin: auto;
		width: 60%;
		height: 100upx;
	}
.p2 {
		font-size: 30upx;
		/* #ifndef H5 */
		padding-top: 3upx;
		/* #endif */
		position: absolute;
		/* 水平居中 */
		left: 50%;
		-webkit-transform: translateX(-50%);
		transform: translateX(-50%);
	}
	.p1 {
		font-size: 48upx;
	}

	.p {
		align: right;
		color: #007AFF;
		padding-top: 30upx;
	}
</style>
