<template>
	<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" size="mini" class="" @click="search()">
					<p class="">{{$t('alarmClock.add')}}</P>
				</button>
			</view>
		</view>
		<!-- //多选下拉框 -->
		<view class="select-item">
			<ld-select :multiple="true" :list="equipmentNameList" label-key="label" value-key="value"
				:cancel-text="$t('common.cancel')" :confirm-text="$t('common.ok')"
				:placeholder="$t('alarmClock.placeholder')" clearable v-model="equipmentIds" @change="selectChange2"></ld-select>
		</view>
		<view class="search">
			<view class="search-title">
				{{$t('updatePassword.adminPwd')}}：
			</view>
			<view class="search-input2">
				<input type="text"  v-model="adminPwd" />
			</view>
		</view>
		<view class="search">
			<view class="search-title">
				{{$t('updatePassword.guestPwd')}}：
			</view>
			<view class="search-input2">
				<input type="text"  v-model="guestPwd" />
			</view>
		</view>
		<view class="line"></view>
		<!-- //提交按钮 -->
		<view class="tr">
			<button  type="primary" formType="submit" @click="update()" class="button">
				<p class="p1">{{$t('alarmClock.update')}}</p>
			</button>
		</view>
	</view>
</template>

<script>
	import {
		mapState,
		mapActions,
		mapMutations
	} from 'vuex'
	import MxDatePicker from "../../components/mx-datepicker/mx-datepicker.vue";
	// import ldSelect from "../../components/ld-select/ld-select.vue";
	import ldSelect from "../../components/ld-select/ld-select.vue"
	import likeButton from "../../components/like-button/like-button.vue";
	import {
		dateUtils
	} from '@/common/util.js';
	export default {
		components: {
			MxDatePicker,
			ldSelect,
			likeButton
		},
		data() {
			return {
				value: '',
				searchClientId: null,
				equipmentName: null,
				equipmentNameList: [],
				globalUser: {},
				equipmentIds: [],
				adminPwd:null,
				guestPwd:null
			}
		},
		onShow() {
			this.globalUser = uni.getStorageSync("globalUser");
			this.globalUser.todayDate = dateUtils.getCurrentDate();
			uni.setNavigationBarTitle({
				title: this.$t('remoteList.password')
			});
			var token = uni.getStorageSync("token");
			if (token.length > 1) {
				this.getEquipmentListData();
			} else {
				uni.reLaunch({
					url: '../../../pages/Login/Login',
				});
			}

		},
		onLoad() {

		},
		methods: {
			...mapActions('chart', ['getEquipmentListByUser', 'addAlarmClock', 'addAlarmClockItem', 'updateAlarmClock']),
			getEquipmentListData() {
				this.getEquipmentListByUser(this.globalUser)
					.then(data => {
						// this.merchantList = data;
						// console.log("1223");
						var listName = data[0].equipmentList;
						var equipmentNameList = this.equipmentNameList;
						var co = {};
						co["value"] = 'all';
						co["label"] = this.$t('common.allDevice');
						equipmentNameList.push(co);
						for (var i = 0; i < listName.length; i++) {
							var cov = {};
							cov["value"] = listName[i].id;
							cov["label"] = this.$t('common.name') + listName[i].name + ' , '+ this.$t('common.ID') + listName[i].clientId.substring(
								listName[i].clientId.length - 6, listName[i].clientId.length);
							equipmentNameList.push(cov);
						}
						this.equipmentNameList = equipmentNameList;
						var listId = data[0].id;
						if (listId != null && listId != '1') {
							uni.setStorageSync("listName", listName);
						}

						uni.stopPullDownRefresh();
					}, _ => void uni.stopPullDownRefresh());
			},

			onSelected(e) { //选择
				this.showPicker = false;
				if (e) {
					this[this.type] = e.value;
					//选择的值
					// console.log('value => ' + e.value);
					//原始的Date对象
					// console.log('date => ' + e.date);
				}
			},
			// bindPickerChange: function(e) {
			// 	this.indexType = e.target.value
			// },
			addEids(equipmentIds) {
				// console.log('equipmentIds' + equipmentIds);
				var eids = equipmentIds.split(",");
				var idss = this.equipmentIds;
				var is = this.equipmentNameList
				for (var k = 0; k < eids.length; k++) {
					var id = eids[k];
					if (id != "" && id != '') {
						this.equipmentIds.push(parseInt(id));
					}
				}
			},
			search() {
				// console.log("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++;
						var id = list[i].id;
						this.equipmentIds.push(id);
						break;
					}
				}
				if (n == 0) {
					uni.showModal({
						title: this.$t('common.tip'),
						content: this.$t('common.noDevice'),
						success: (res) => {

						}
					})
				}
				if (n > 0) {
					uni.showModal({
						title: this.$t('common.tip'),
						content: this.$t('common.addDevice'),
						success: (res) => {

						}
					})
				}
			},
			selectChange2(val) {
				//有改动文件 import likeButton from "../../components/like-button/like-button.vue";
				if (val == "all") {
					this.equipmentIds = [];
					var list = uni.getStorageSync("listName");
					for (var i = 0; i < list.length; i++) {
						this.equipmentIds.push(list[i].id);
					}
				} else {
					this.equipmentIds = val
				}
				// console.log('this.equipmentIds => ' + this.equipmentIds);
			},
			switchChange: function(e) {
				this.status = e.target.value;
			},
			update() {
				var that = this;
				uni.showModal({
					title: that.$t('equipmentStatusList.tip'),
					content:that.$t('updatePassword.sure') ,
					success: (re) => {
						if (re.confirm) {
							var serverurl = that.serverurl;
							var token = uni.getStorageSync("token");
							uni.request({
								url: serverurl + '/TEquipment/updatePassword?equipmentIds='+that.equipmentIds+'&guestPwd='+that.guestPwd+'&adminPwd='+that.adminPwd,
								// data: {
								// 	"equipmentIds": that.equipmentIds,
								// 	"guestPwd": that.guestPwd,
								// 	"adminPwd": that.adminPwd,
								// },
								method: "POST",
								header: {
									'token': token
								},
								success: (res) => {
									if(res.data.code){
										uni.showToast({
											title: res.data.message,
											duration: 1000
										});
									}
								}
							});
						}
					}
				})
			},
		}
	}
</script>

<style>
	.test {
		text-align: center;
		padding: 10px 0;
	}

	.time {
		font-size: 78upx;
	}

	.btime {
		margin: 20upx;
		font-size: 28upx;
	}

	/* 	button {
		margin: 20upx;
		font-size: 28upx;
	} */

	/* #ifdef MP-ALIPAY */
	.uni-badge {
		margin-left: 20rpx;
	}

	/* #endif */
	.uni-badge {
		font-size: 40upx;
	}

	.example-body {
		flex-direction: row;
		justify-content: flex-start;
		padding-bottom: 15upx;
		font-size: 40upx;
	}

	.uni-badge-left-margin {
		font-size: 40upx;
		margin-left: 50upx;
	}

	.uni-list-cell {
		padding-top: 20upx;
		padding-bottom: 10upx;
		font-size: 35upx;
	}

	.line {
		height: 2upx;
		background: #8C959F;
	}

	.search {
		width: 100%;
		padding-top: 15upx;
		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: 48%;
		height: 25px;
		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: 10px;
		padding-bottom: 5px;
		text-align: center;
		height: 60upx;
		text-align: left;
	}
	.search-input2 {
		width: 65%;
		text-align: center;
		font-size: 26upx;
		font-family: "PingFang-SC-Bold";
		box-shadow: 0upx 0upx 20upx #D3D3D3;
		border-radius: 5upx;
	}

	.select-item {
		/* padding-top: 20upx; */
	}

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

	.p1 {
		font-size: 48upx;
	}
</style>
