<template>
	<view>
		
		<u-navbar 
		title="设置" 
		title-color="#fff" 
		:custom-back="customBack" 
		:border-bottom="false" 
		back-icon-color="#CCE8FF" 
		:background="{background: 'linear-gradient(145deg, #41AFF9 0%, #2D8CFB 100%)' }"></u-navbar>
		
		<view class="cell-group" style="margin-top: 30rpx;">
			<u-cell-group>				
				<u-cell-item title="修改密码" @click="openPage('pages/modifypwd/modifypwd')">
					<u-icon slot="icon" custom-prefix="custom-icon" size="35" name="wodecheliang"></u-icon>
				</u-cell-item>
				<u-cell-item title="打印走纸" @click="onPrintFeed()">
					<u-icon slot="icon" custom-prefix="custom-icon" size="35" name="wodecheliang"></u-icon>
				</u-cell-item>
				<u-cell-item title="地锁控制" @click="lockControl()">
					<u-icon slot="icon" custom-prefix="custom-icon" size="35" name="wodecheliang"></u-icon>
				</u-cell-item>
				<u-cell-item title="语音播报开关" @click="speakControl()">
					<u-icon slot="icon" custom-prefix="custom-icon" size="35" name="wodecheliang"></u-icon>
				</u-cell-item>
				<u-cell-item title="版本检测" @click="onCheckVersion()">
					<u-icon slot="icon" custom-prefix="custom-icon" size="35" name="wodecheliang"></u-icon>
				</u-cell-item>
				<u-cell-item title="退出登录" @click="onLoginOut()">
					<u-icon slot="icon" custom-prefix="custom-icon" size="35" name="wodecheliang"></u-icon>
				</u-cell-item>
			</u-cell-group>
		</view>
		<u-popup v-model="speakShow" mode="center" length="auto" >
			<view class="speack">
				<view class="speack-pop">
					<u-form>
					<u-form-item label="设备入场开关" label-width="350"><u-switch v-model="switchVal1" @change="change1"></u-switch></u-form-item>
					<u-form-item label="设备出场开关" label-width="350"><u-switch  v-model="switchVal2" @change="change2"></u-switch></u-form-item>
					<u-form-item label="收费员入场开关" label-width="350"><u-switch  v-model="switchVal3" @change="change3"></u-switch></u-form-item>
					<u-form-item label="收费员出场开关" label-width="350"><u-switch  v-model="switchVal4" @change="change4"></u-switch></u-form-item>
					</u-form>
				</view>
				<view class="speack-btn">
					<u-button @click="submitSpeak" type="primary" size="medium">执行</u-button>	
				</view>
			</view>
		</u-popup>
		<u-popup v-model="show" mode="center" length="auto" >
			<view>
			<u-form :model="lockForm" ref="uForm">
					<u-form-item label="车位" label-width="150"><u-input v-model="lockListForm.spaceName" type="select" @click="lockListForm.show = true"/></u-form-item>
					<u-form-item label="命令" label-width="150"><u-input v-model="lockForm.cmdLabel" type="select" @click="lockForm.cmdFlag = true"/></u-form-item>
			</u-form>
			</view>
			<u-button @click="submit" type="primary">执行</u-button>
		</u-popup>
		<u-select v-model="lockForm.cmdFlag" :list="list" @confirm="getCmdValue"></u-select>
		<u-select v-model="lockListForm.show" :list="lockList" @confirm="onLockSelect"></u-select>
		<u-toast ref="uToast" />
		
		<view class="version">
			<view  > 版本号: {{wgtInfo.version + '-' + wgtInfo.versionCode}}</view>
			<view > APPID: {{wgtInfo.appid}}</view>
		</view>
	</view>
</template>

<script>
	//#ifdef APP-PLUS
	var device = uni.requireNativePlugin("DeviceInfo")
	//#endif
	import update from '@/utils/update.js';
	export default{
		data(){
			return{
				switchVal1:'',
				switchVal2:'',
				switchVal3:'',
				switchVal4:'',
				speakShow:false,
				show: false,
				wgtInfo:{},
				lockForm:{
					deviceNo:'',
					cmd:'',
					cmdLabel: '',
					cmdFlag: false
				},
				lockListForm:{
					show: false,
					deviceNo:'',
					spaceName:''
				},
				lockList:[],
				list:[{
						value: 'up',
						label: '升板'
					},
					{
						value: 'down',
						label: '降板'
					}]
			}
		},
		onLoad(){
			this.getVersion();
			
			
		},
		onShow(){
			
		},
		methods:{
			change1(status){
				// console.log(status)
				this.switchVal1=status;
			},
			change2(status){
				// console.log(status)
				this.switchVal2=status;
			},
			change3(status){
				// console.log(status)
				this.switchVal3=status;
			},
			change4(status){
				// console.log(status)
				this.switchVal4=status;
			},
			speakControl(){
				this.speakShow=true;
				uni.getStorage({
					key:'speakData',
					success: (res) => {
						this.switchVal1 = res.data.switchVal1;
						this.switchVal2 = res.data.switchVal2;
						this.switchVal3 = res.data.switchVal3;
						this.switchVal4 = res.data.switchVal4;
						// console.log('passWord',res.data)
					},
					fail: (err) => {
						// console.log('passWord err',err)
					}
				})
			},
			submitSpeak(){
				uni.setStorage({
					key:'speakData',
					data:{switchVal1:this.switchVal1, switchVal2:this.switchVal2, switchVal3:this.switchVal3, switchVal4:this.switchVal4 }							
				});
				this.speakShow=false;
			},
			lockControl(){
				let that = this;
				let roadInfo = this.$store.state.vuex_user?.roadList?.[0]||[];
				that.lockList = [];
				this.$u.api.lockList({roadNo:roadInfo.roadNo})
				.then(res => {
					if(res.code == 200){
						let list = res.data?.lockList;
						if(list == null || list.length <=0){
							plus.nativeUI.toast('没有可操作的车位锁' );
						}else {
							list.forEach(item => {
								let pushItem = {
									value: item.deviceNo,
									label: item.spaceName
								}
								that.lockList.push(pushItem);
							})
							this.show=true;
						}
					}
				})
				.catch(err =>{
					
				})
			},
			submit(){
				let reqParams = {
					deviceNo: this.lockListForm.deviceNo,
					cmd: this.lockForm.cmd
				}
				this.$u.api.lockControl(reqParams)
				.then( res =>{
					if(res.code == 200){
						this.$refs.uToast.show({
							title: res.msg,
							type: 'success',
						});
					}
				}).catch(err=>{
						this.$refs.uToast.show({
							title: err.msg,
							type: 'error',
						});
					})
			},
			customBack(){
				uni.navigateBack({
				    delta: 1
				})
			},
			openPage(path) {
				// console.log('path',path);
				this.$u.route({ 
					url: path
				});
			},
			onPrintFeed(){
				//#ifdef APP-PLUS
				device.printFeed(10);
				//#endif
				//#ifdef H5
				uni.showToast({
					title: '非APP环境',
					duration: 2000
				});
				//#endif
			},
			getCmdValue(val) {
				console.log(val)
				this.lockForm.cmd = val[0].value
				this.lockForm.cmdLabel = val[0].label
			},
			onLockSelect(val){
				this.lockListForm.deviceNo = val[0].value;
				this.lockListForm.spaceName = val[0].label;
			},
			onCheckVersion(){
				update(this); 
			},
			onLoginOut(){
				uni.showModal({
				  title: '温馨提示',  
				  content: '是否确认退出?',  
				  success: (res) => { 
					   if (res.confirm) {
							  this.$u.api.loginOut({})
							  .then(res=>{
								  uni.clearStorage();
								  this.$u.vuex('vuex_token', null);
								  this.$u.vuex('vuex_user', null);
								  this.$u.vuex('vuex_hasLogin', false);
								  this.userInfo = [];
								  this.theRoad = [];
								  uni.setStorageSync('pushreg_switch', '1');
								  plus.nativeUI.toast('退成成功');
								  uni.reLaunch({
								      url: '/pages/login/login'
								  });
							  }).catch(err=>{
								  uni.showToast({
								  	title: '退出失败',
								  	duration: 2000
								  });
							  });
						  } else if (res.cancel) {
							  
						  }
					  
				  }  
				})  
				
			},
			getVersion(){
				plus.runtime.getProperty(plus.runtime.appid, (info) => {
					this.wgtInfo = info;
				});
			}
			
		}
	}
</script>

<style lang="scss">
	@import  './setting.scss'
</style>
