<template>
	<view class="content">
		<view class="user_name">
			<view class="text_user">无线名称</view>
			<u-input class="user_pass" v-model="userInfo.name" border="bottom" placeholder="请输入您的无线名称"></u-input>
			<view class="text_user">无线密码</view>
			<view class="password">
				<u-input class="user_pass" :type="type" border="bottom" v-model="userInfo.password"
					placeholder="请输入您的无线密码">
					<template slot="suffix">
						<u-icon name="eye" @click="showpassword"></u-icon>
					</template>
				</u-input>
			</view>
		</view>
		<view class="but_login" @click="writeData">下载</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				userInfo: {
					name: '',
					password: '',
				},
				type: 'password',
			}
		},
		onLoad() {
			// 这里是获取wifi信息的
			uni.startWifi({
				success: (result) => {
					console.log(result)
					uni.getConnectedWifi({
						success: (res) => {
							console.log(res)
							this.userInfo.name = res.wifi.SSID
						},
						fail: (err) => {
							console.log('获取Wi-Fi信息失败', err);
						}
					});
				},
				fail: (err) => {
					console.log('start - Wi-Fi信息失败', err);
				}
			})


			// uni.getNetworkType({
			//   success: function (res) {
			//     console.log(res);
			//     if (res.networkType === 'wifi') {
			//       uni.getConnectedWifi({
			//         success: function (wifi) {
			//           console.log(wifi.SSID); // 注意：SSID可能返回undefined或空字符串
			//         },
			//         fail: function (error) {
			//           console.error('获取WiFi信息失败:', error);
			//         }
			//       });
			//     }
			//   }
			// });
		},
		onShow() {

		},
		methods: {
			// 下载文件
			oneClickDownload(url) {
				// 获取缓存目录路径
				let cachePath = plus.io.convertLocalFileSystemURL('_doc/') + "PaperTakingMachineConfig.txt"; // 获取应用私有目录下的缓存路径
				console.log(cachePath);
				// 假设文件路径为 '/path/to/your/PaperTakingMachineConfig.txt'
				// const filePath = 'file:///storage/emulated/0/Android/data/com.yozo.vivo.office/cache/yozoCloud';
				// /storage/emulated/0/Android/data/io.dcloud.HBuilder/apps/HBuilder/doc/
				// const fileSystemManager = uni.getFileSystemManager();
				// 删除文件
				uni.getSavedFileList({
				  success: function (res) {
				    console.log(res);
						console.error('Failee');
						uni.removeSavedFile({
						  filePath: res.fileList[0].filePath, // 文件路径
						  success: function (res) {
							console.log('文件删除成功', res);
						  },
						  fail: function (err) {
							console.error('文件删除失败', err);
						  }
						});
					
				  }
				});
				const desiredFileName = 'PaperTakingMachineConfig.txt'; // 你想要的文件名
				// 下载文件资源到本地
				uni.downloadFile({
					url: url, // 后端返回的线上文件路径
					success: function(res) {
						if (res.statusCode === 200) {
							// 文件到本地
							uni.saveFile({
								tempFilePath: res.tempFilePath, //临时路径
								success: function(data) {
									var savedFilePath = data.savedFilePath
									const tempFilePath = res.tempFilePath;
									// 使用plus.io的文件管理API修改文件名
									plus.io.resolveLocalFileSystemURL(savedFilePath, (entry) => {
										entry.getParent((parentEntry) => {
											// 将文件移动到同一目录但使用新文件名
											entry.moveTo(parentEntry, '/' +
												desiredFileName, (
												newEntry) => {
													// 文件名修改成功，newEntry.fullPath是新的文件路径
													console.log(
														'File renamed to:',
														newEntry.fullPath);
													// 你可以在这里打开文件或进行其他处理
													uni.openDocument({
														filePath: newEntry
															.fullPath,
														fileType: 'txt', // 指定文件类型（如果需要）
														success: () => {
															console
																.log(
																	'File opened successfully'
																	);
														},
														fail: (
														err) => {
															console
																.error(
																	'Failed to open file:',
																	err
																	);
														}
													});
												}, (err) => {
													console.error(
														'Failed to move file:',
														err);
												});
										});
									});










									// 在app端执行
									// #ifdef APP-PLUS
									let osname = plus.os.name
									// 如果是安卓的话弹出提示
									if (osname == 'Android') {
										uni.showToast({
											icon: 'none',
											mask: true,
											title: '保存成功',
											duration: 1000
										})
									}
									// #endif
									uni.removeSavedFile({
										filePath: res.tempFilePath,
										complete: () => {
											// 删除操作完成
										},
									});
									//ios手机直接打开文件，手动存储文件到手机，Android手机从根目录创建文件夹，保存文件并改名
									// setTimeout(() => {
									// 	//打开文档查看
									// 	uni.openDocument({
									// 		filePath: data.savedFilePath,
									// 		success: function(ress) {
									// 			console.log("成功打开文件")
									// 		},
									// 		fail() {
									// 			console.log("打开文件失败")
									// 		}
									// 	})
									// }, 1000)
								}
							})
							console.log('下载成功');
						}
					},
					fail: function(res) {
						console.log(res.errMsg);
					},
					complete: function(res) {
						console.log(res.statusCode);
					},
					progress: function(res) {
						console.log('下载进度' + res.progress);
					}
				});
			},
			writeData() {
				let _this = this;
				const params = {
					url: '/uploadbluetooth',
					data: {
						id: _this.userInfo.name,
						pwd: _this.userInfo.password,
					},
				};
				this.$request(params).then(res => {
					this.oneClickDownload(res.data.url)
				})
			},
			login() {
				let _this = this;
				if (_this.userInfo.name.length == 0) {
					this.$u.toast('请输入您的手机号')
					return
				}
				if (_this.userInfo.password.length == 0) {
					this.$u.toast('请输入您的密码')
					return
				}
				return
				const params = {
					url: '/login',
					data: {
						username: _this.userInfo.name,
						password: _this.userInfo.password,
					},
				}
				this.$request(params).then(res => {
					uni.setStorageSync('token', res.token);
					_this.$wskj.switchTab('/pages/home_index/home');
				})
				//_this.$wskj.switchTab('/pages/home_index/home');
			},
			// 密码隐藏显示
			showpassword() {
				this.type = this.type === 'password' ? 'text' : 'password';
			},
		}
	}
</script>

<style lang="less">
	.title {
		font-family: PingFangSC, PingFang SC;
		font-weight: 600;
		font-size: 42rpx;
		color: #333333;
		text-align: center;
	}

	.uist {
		padding: 0 60rpx;

		.item {
			display: flex;
			align-items: center;
			justify-content: space-between;
			line-height: 100rpx;
			border-bottom: 1px solid #ccc;

			text {
				font-family: PingFangSC, PingFang SC;
				font-weight: 400;
				font-size: 34rpx;
				color: #333333;
				font-weight: bold;
			}

			view {
				font-family: PingFangSC, PingFang SC;
				font-weight: 400;
				font-size: 30rpx;
				color: #333333;
			}
		}
	}

	/deep/ .u-input {
		border: 2rpx solid #dadbde;
		border-radius: 10rpx;
	}

	.wx_login {
		display: flex;
		justify-content: center;
		align-items: center;
		margin-top: 28px;

		.iv_login {
			width: 78rpx;
			height: 78rpx;
		}
	}

	.third_party {
		display: flex;
		margin-left: 66rpx;
		margin-top: 120rpx;
		align-items: center;

		.border {
			width: 238rpx;
			border-bottom: 2rpx solid #999999;
		}

		.third {
			padding: 0rpx 20rpx;
		}
	}

	.content {
		margin-top: 30rpx;

		.privacy_policy {
			margin-top: 20rpx;
			margin-left: 87rpx;
			font-size: 24rpx;

			.text {
				color: #FB4B4E;
			}

		}

		.login_bg {
			height: 180px;
			width: 100%;
			background: linear-gradient(91deg, #FCD4D2 0%, #FDEDEE 100%);
			// view 显示一行
			display: flex;
			// view 俩边撑开
			justify-content: space-around;

			.login_title {
				color: #FFFFFF;
				margin-top: 70px;
			}

			.iv_login {
				width: 100px;
				height: 100px;
				bottom: -30%;
			}

		}

		.but_login {
			width: 400rpx;
			text-align: center;
			justify-content: center;
			line-height: 88rpx;
			font-weight: 500;
			margin: 0 auto;
			color: white;
			height: 88rpx;
			border-radius: 44rpx 44rpx 44rpx 44rpx;
			background: linear-gradient(89deg, #FFBFBC 0%, #FB262A 100%);
		}

		.user_name {
			margin-top: -50rpx;
			background-color: wheat;
			border-radius: 20px 20px 0px 0px;
			background: white;
			padding: 100rpx 60rpx;

			.user_phone {
				font-size: 12px;
				padding-top: 38rpx;
				padding-bottom: 28rpx;
				border-bottom: 2rpx solid #999999;
			}

			.text_user {
				margin-top: 60rpx;
				font-size: 16px;
				font-weight: 600;
			}

			.user_pass {
				margin-top: 20rpx;
				font-size: 12px;
			}

			.password {
				display: flex;

				.iv_pass {
					height: 30rpx;
					width: 30rpx;
				}

				.user_pass {
					margin-top: 20rpx;
					font-size: 12px;
				}
			}
		}
	}
</style>