<template>
	<view>
		<view class="my_ak">
			<view class="my_ak_text_01">
				<text>每月1日自动更新</text>
			</view>
			<view class="my_ak_text_02">
				<text>{{pwd}}</text>
			</view>
			<view class="my_ak_text_03">
				<text>有效期至{{endTime}}</text>
			</view>
			<view class="cu-bar bg-white">
				<view class="action">
					<text class="text-xl text-bold">说明</text>
				</view>
			</view>
			<view class="my_ak_text_explain">
				<text>1.在门禁机上点击"密码开门"，再输入密码直接开门</text>
			</view>
			<view class="my_ak_text_explain">
				<text>2.该密码能开启的门禁机有：{{loactions}}</text>
			</view>
			<view class="my_ak_text_explain">
				<text>3.如果有新门禁添加时，则重新申请钥匙才能开新的门禁</text>
			</view>
			<view class="my_ak_text_explain">
				<text>4.如果分享密码给来访者，建议分享访客密码</text>
			</view>
		</view>
	</view>
</template>

<script>
	// pages/myApplicationKey/myApplicationKey.js
	import context from '../../lib/java110/Java110Context.js';
	const constant = context.constant;

	export default {
		data() {
			return {
				applicationKeys: [],
				pwd: '请先申请钥匙',
				loactions: '没有门禁',
				endTime: '0000-00-00 00:00:00',
				idCard: "",
				communityId: ""
			};
		},

		components: {},
		props: {},

		/**
		 * 生命周期函数--监听页面加载
		 */
		onLoad: function(options) {
			context.onLoad(options);
		},

		/**
		 * 生命周期函数--监听页面初次渲染完成
		 */
		onReady: function() {},

		/**
		 * 生命周期函数--监听页面显示
		 */
		onShow: function() {
			let _that = this;

			context.getOwner(function(_owner) {
				let _idCard = _owner.idCard;
				let _communityId = _owner.communityId;

				_that.idCard = _idCard;
				_that.communityId = _communityId;

				_that.loadApplicationKey();
			});
		},

		/**
		 * 生命周期函数--监听页面隐藏
		 */
		onHide: function() {},

		/**
		 * 生命周期函数--监听页面卸载
		 */
		onUnload: function() {},

		/**
		 * 页面相关事件处理函数--监听用户下拉动作
		 */
		onPullDownRefresh: function() {},

		/**
		 * 页面上拉触底事件的处理函数
		 */
		onReachBottom: function() {},

		/**
		 * 用户点击右上角分享
		 */
		onShareAppMessage: function() {},
		methods: {
			loadApplicationKey: function() {
				let _that = this;

				let _objData = {
					page: 1,
					row: 10,
					idCard: this.idCard,
					communityId: this.communityId,
					typeFlag: '1100102'
				};
				context.request({
					url: constant.url.listApplicationKeys,
					header: context.getHeaders(),
					method: "GET",
					data: _objData,
					//动态数据
					success: function(res) {
						console.log(res);

						if (res.statusCode == 200) {
							//成功情况下跳转
							let _applicationKeys = res.data.applicationKeys;

							if (_applicationKeys.length == 0) {
								wx.showToast({
									title: "未查询到钥匙",
									icon: 'none',
									duration: 2000
								});
								return;
							}

							_that.applicationKeys = _applicationKeys;

							_that.showPwd();
						}
					},
					fail: function(e) {
						wx.showToast({
							title: "服务器异常了",
							icon: 'none',
							duration: 2000
						});
					}
				});
			},
			showPwd: function() {
				let _applicationKeys = this.applicationKeys;
				let _pwd = '';
				let _loactions = '';
				let _endTime = '';

				for (let _akIndex = 0; _akIndex < _applicationKeys.length; _akIndex++) {
					if (_applicationKeys[_akIndex].state != '10001') {
						continue;
					}

					_pwd = _applicationKeys[_akIndex].pwd;
					_loactions += _applicationKeys[_akIndex].locationObjName + ",";
					_endTime = _applicationKeys[_akIndex].endTime;
				}

				if (_pwd == '') {
					return;
				}

				this.pwd = _pwd;
				this.loactions = _loactions;
				this.endTime = _endTime;
			}
		}
	};
</script>
<style>
	@import "./myApplicationKey.css";
</style>
