<template>
	<view class="">
		<!-- <view class="line">
		</view> -->
		<view class="search">
			<view class="search-title">
				{{$t('dosugar.equipmentClientID')}}
			</view>
			<view class="search-input">
				<input type="text" :placeholder="$t('dosugar.placeholder')" v-model="searchClientId" />
			</view>
			<view class="search-button">
				<button type="primary" class="" @click="search()">
					<p class="">{{$t('dosugar.search')}}</P>
				</button>
			</view>
		</view>
		<view class="td-right">
			<view class="uni-list">
				<view class="uni-list-cell">
					<view class="uni-list-cell-left">
						{{$t('dosugar.chooseEquipment')}}
					</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>
		</view>
		<view v-show="show">
			<view class="body">
				<p class="p">X零点补偿：</p>
				<input class="input" type="text" v-model="D436" />
				<button type="primary" class="button" @click="update('D436',D436)">
					<p class="p2">{{$t('parameters.update')}}</P>
				</button>
			</view>
			<view class="line2"></view>
			<view class="body">
				<p class="p">Y零点补偿：</p>
				<input class="input" type="text" v-model="D438" />
				<button type="primary" class="button" @click="update('D438',D438)">
					<p class="p2">{{$t('parameters.update')}}</P>
				</button>
			</view>
			<view class="line2"></view>
			<view class="body">
				<p class="p">Z零点补偿：</p>
				<input class="input" type="text" v-model="D440" />
				<button type="primary" class="button" @click="update('D440',D440)">
					<p class="p2">{{$t('parameters.update')}}</P>
				</button>
			</view>
			<view class="line2"></view>
			<view class="body">
				<p class="p">取糖位置X设置：</p>
				<input class="input" type="text" v-model="D218" />
				<button type="primary" class="button" @click="update('D218',D218)">
					<p class="p2">{{$t('parameters.update')}}</P>
				</button>
			</view>
			<view class="line2"></view>
			<view class="body">
				<p class="p">取糖位置Y设置：</p>
				<input class="input" type="text" v-model="D268" />
				<button type="primary" class="button" @click="update('D268',D268)">
					<p class="p2">{{$t('parameters.update')}}</P>
				</button>
			</view>
			<view class="line2"></view>
			<view class="body">
				<p class="p">取糖位置Z设置：</p>
				<input class="input" type="text" v-model="D318" />
				<button type="primary" class="button" @click="update('D318',D318)">
					<p class="p2">{{$t('parameters.update')}}</P>
				</button>
			</view>
			<!-- //301特有 -->
			<view class="" v-show="equimentType=='MG301'">
				<view class="line2"></view>
				<view class="body">
					<p class="p">取棍位置X设置：</p>
					<input class="input" type="text" v-model="D226" />
					<button type="primary" class="button" @click="update('D226',D226)">
						<p class="p2">{{$t('parameters.update')}}</P>
					</button>
				</view>
				<view class="line2"></view>
				<view class="body">
					<p class="p">取棍位置Y设置：</p>
					<input class="input" type="text" v-model="D276" />
					<button type="primary" class="button" @click="update('D276',D276)">
						<p class="p2">{{$t('parameters.update')}}</P>
					</button>
				</view>
				<view class="line2"></view>
				<view class="body">
					<p class="p">取棍位置Z设置：</p>
					<input class="input" type="text" v-model="D326" />
					<button type="primary" class="button" @click="update('D326',D326)">
						<p class="p2">{{$t('parameters.update')}}</P>
					</button>
				</view>
			</view>
			<!-- //320特有 -->
			<view class="" v-show="equimentType=='MG320'">
				<view class="line2"></view>
				<view class="body">
					<p class="p">取棍位置X设置：</p>
					<input class="input" type="text" v-model="D228" />
					<button type="primary" class="button" @click="update('D228',D228)">
						<p class="p2">{{$t('parameters.update')}}</P>
					</button>
				</view>
				<view class="line2"></view>
				<view class="body">
					<p class="p">取棍位置Y设置：</p>
					<input class="input" type="text" v-model="D278" />
					<button type="primary" class="button" @click="update('D278',D278)">
						<p class="p2">{{$t('parameters.update')}}</P>
					</button>
				</view>
				<view class="line2"></view>
				<view class="body">
					<p class="p">取棍位置Z设置：</p>
					<input class="input" type="text" v-model="D328" />
					<button type="primary" class="button" @click="update('D328',D328)">
						<p class="p2">{{$t('parameters.update')}}</P>
					</button>
				</view>
				<view class="line2"></view>
				<view class="body">
					<p class="p">启动缺物料监控</p>
					<button type="primary" class="button" @click="update('M502','0')">
						<p class="p2">启动</P>
					</button>
					<button type="primary" class="button" @click="update('M502','1')">
						<p class="p2">关闭</P>
					</button>
				</view>
				<view class="line2"></view>
				<view class="body">
					<p class="p">外循环按钮</p>
					<button type="primary" class="button" @click="update('M506','0')">
						<p class="p2">启动</P>
					</button>
					<button type="primary" class="button" @click="update('M506','1')">
						<p class="p2">关闭</P>
					</button>
				</view>
			</view>
		</view>
		<view class="line2"></view>
	</view>
</template>
<script>
	import {
		mapState,
		mapActions,
		mapMutations
	} from 'vuex'
	export default {
		data() {
			return {
				D436: 0,
				D438: 0,
				D440: 0,
				D218: 0,
				D268: 0,
				D318: 0,
				D226: 0,
				D276: 0,
				D326: 0,
				D228: 0,
				D278: 0,
				D328: 0,
				M502: 0,
				M506: 0,
				index: null,
				equipmentName: null,
				equimentType: null,
				equipmentId:null,
				equipmentNameList: [],
				globalUser: null,
				show: true,
				searchClientId:null
			}
		},
		onShow() {
			var token = uni.getStorageSync("token");
			this.globalUser = uni.getStorageSync("globalUser");
			if (token.length > 1) {
				this.getEquipmentListData();
				// this.getParam();
			} else {
				uni.reLaunch({
					url: '../Login/Login',
				});
			}

		},
		methods: {
			...mapActions('chart', ['getParameters', 'updateParameters', '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");
				console.log(clientId);
				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.D436 = null;
						this.D438 = null;
						this.D440 = null;
						this.D218 = null;
						this.D268 = null;
						this.D318 = null;
						this.D226 = null;
						this.D276 = null;
						this.D326 = null;
						this.D228 = null;
						this.D278 = null;
						this.D328 = null;
						this.M502 = null;
						this.M506 = null;
						this.index = i;
						var id = list[i].id;
						this.equipmentId =  id;
						this.equimentType = list[i].equimentType;
						this.clientId = list[i].clientId;
						this.show = true;
						if (this.equimentType == 'MG280') {
							this.show = false;
						}
						this.getParam();
						break;
					}
				}
				if(n==0){
					uni.showModal({
						title: "提示",
						content: "找不到该机器",
						success: (res) => {
					
						}
					})
				}
			},
			//改变机器
			changeEquipment: function(e) {
				this.D436 = null;
				this.D438 = null;
				this.D440 = null;
				this.D218 = null;
				this.D268 = null;
				this.D318 = null;
				this.D226 = null;
				this.D276 = null;
				this.D326 = null;
				this.D228 = null;
				this.D278 = null;
				this.D328 = null;
				this.M502 = null;
				this.M506 = null;
				this.searchClientId = null;
				this.index = e.target.value;
				var list = uni.getStorageSync("listName");
				this.equipmentId = list[e.target.value].id;
				this.equimentType = list[e.target.value].equimentType;
				this.clientId = list[e.target.value].clientId;
				this.show = true;
				if (this.equimentType == 'MG280') {
					this.show = false;
				}
				this.getParam();
			},
			getParam() {
				const param = {};
				param['id'] = this.equipmentId;
				// 0：通用参数；  1：进阶参数
				param['status'] = '1';
				param['clientId'] = this.clientId;
				this.getParameters(param).then(res => {
					var code = res.code;
					if(code==false){
						uni.showToast({
							title: res.message,
							duration: 2000
						});
						return;
					}
					var info = res.data;
					if (info != null || info != '') {
						for (var i = 0; i < info.length; i++) {
							if (info[i].name == 'D436') {
								this.D436 = info[i].val;
							}
							if (info[i].name == 'D438') {
								this.D438 = info[i].val;
							}
							if (info[i].name == 'D440') {
								this.D440 = info[i].val;
							}
							if (info[i].name == 'D218') {
								this.D218 = info[i].val;
							}
							if (info[i].name == 'D268') {
								this.D268 = info[i].val;
							}
							if (info[i].name == 'D318') {
								this.D318 = info[i].val;
							}
							if (info[i].name == 'D226') {
								this.D226 = info[i].val;
							}
							if (info[i].name == 'D276') {
								this.D276 = info[i].val;
							}
							if (info[i].name == 'D326') {
								this.D326 = info[i].val;
							}
							if (info[i].name == 'D228') {
								this.D228 = info[i].val;
							}
							if (info[i].name == 'D278') {
								this.D278 = info[i].val;
							}
							if (info[i].name == 'D328') {
								this.D328 = info[i].val;
							}
						}
					}

				}, _ => void uni.stopPullDownRefresh());
			},
			update(name, val) {
				const param = {};
				param['id'] = this.equipmentId;
				param['name'] = name;
				param['val'] = val;
				this.updateParameters(param).then(res => {
					if (res.data == 'SUCCESS') {
						uni.showToast({
							title: '发送成功',
							duration: 1000
						});
					}
				}, _ => void uni.stopPullDownRefresh());
			},
		},
	}
</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;
	}
	.line {
		height: 50upx;
	}

	.line2 {
		height: 10upx;
	}

	.body {
		background-color: #FFFFFF;
		width: 700upx;
		padding: 10upx 10upx 10upx 10upx;
		display: flex;
		flex-direction: row;
		justify-content: flex-start;
		font-size: 32upx;
	}

	.p {
		width: 350upx;
		text-align: right;
	}

	.input {
		width: 200upx;
		box-shadow: 0upx 0upx 20upx #D3D3D3;
		border-radius: 5upx;
		text-align: center;
	}

	.button {
		width: 120upx;
		height: 60upx;
		text-align: left;
		border-radius: 10upx;

	}

	.p2 {
		font-size: 30upx;
		/* #ifndef H5 */
		padding-top: 3upx;
		/* #endif */
		position: absolute;
		/* 水平居中 */
		left: 50%;
		-webkit-transform: translateX(-50%);
		transform: translateX(-50%);
	}
</style>
