<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="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="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>
</template>

<script>
	//#ifdef APP-PLUS
	var device = uni.requireNativePlugin("DeviceInfo")
	//#endif
	export default{
		data(){
			return{
				show: false,
				lockForm:{
					deviceNo:'',
					cmd:'',
					cmdLabel: '',
					cmdFlag: false
				},
				lockListForm:{
					show: false,
					deviceNo:'',
					spaceName:''
				},
				lockList:[],
				list:[{
						value: 'up',
						label: '升板'
					},
					{
						value: 'down',
						label: '降板'
					}]
			}
		},
		onLoad(){
			
		},
		onShow(){
			
		},
		methods:{
			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;
			},
			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) {
							  
						  }
					  
				  }  
				})  
				
			}
			
		}
	}
</script>

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