<template>
	<view class="content">
		<uni-icons class="setting" type="gear" @click="handleServerSettings" size="50" color="#fff"></uni-icons>
		<view class="login_logo">
			<!-- <image :src="logo || 'https://img.picgo.net/2024/04/18/login_logo45cb6a5eb95c8c1c.png'"></image> -->
			<view class="logo_bj">{{ logoName || '中赢' }}</view>
		</view>
		<view class="login-title">
			<label>工业互联网平台</label>

			<text>{{ companyName }}</text>
		</view>
		<view>
			<view class="login-content">
				<view>
					<view class="form">
						<view class="login-input">
							<label class="icon-lable"></label>
							<input name="input" v-model="userInfo.username" placeholder="请输入账号" />
						</view>
						<view class="login-input">
							<label class="icon-lable2"></label>
							<input class="" name="input" password v-model="userInfo.passwd" placeholder="请输入密码" />
						</view>
						<button @click="submit">登录</button>
						<view class="password-memo">
							<checkbox-group @change="checkboxChange">
								<label>
									<checkbox value="1" :checked="isMemo" />
									记住密码
								</label>
							</checkbox-group>
						</view>
					</view>
				</view>
			</view>
		</view>
		<ServerSetting ref="serverSettingRef" @setServerStatus="setServerStatus" />
	</view>
</template>

<script>
	import {
		postJ
	} from '@/utils/api.js'
	import {
		login,
		usName
	} from '@/api/common.js'
	import {
		getResourcesTree
	} from '@/api/pda/common.js'

	import {
		setMemo,
		isMemo,
		removeMemo,
		setPassword,
		getPassword
	} from '@/utils/passwordMemo.js'
	import ServerSetting from '@/components/ServerSetting/index'

	export default {
		components: {
			ServerSetting
		},
		data() {
			return {
				userInfo: {
					username: '',
					passwd: ''
				},
				isMemo: isMemo(),
				loginDisabled: false,

				companyName: null,
				logo: null,
				logoName: ''
			}
		},
		onLoad() {
			if (this.isMemo) {
				const userInfo = getPassword()
				if (userInfo?.username) this.userInfo = userInfo
			}

			this.getUsName()
		},
		mounted() {
			// #ifdef APP-PLUS
			this.$nextTick(() => {
				this.$refs.serverSettingRef && this.$refs.serverSettingRef.serverCheck()
			})
			// #endif
			// uni.reLaunch({
			//           url: "/pages/home/home",
			//         });
		},
		methods: {
			getUsName() {
				usName().then(res => {
					this.companyName = res.companyName
					this.logo = res.logo
					this.logoName = res.logoName
				})
			},
			setServerStatus(val) {
				this.loginDisabled = !val
			},
			//登录
			// submit() {
			//   // #ifdef APP-PLUS
			//   if (!this.apiUrl) {
			//     this.$refs.serverSettingRef.open();
			//     return;
			//   }
			//   // #endif

			//   const params = {
			//     loginName: this.userInfo.username,
			//     loginPwd: this.userInfo.passwd,
			//   };

			//   login(params)
			//     .then((res) => {
			//       if (this.isMemo) {
			//         setPassword(this.userInfo);
			//       }
			//       let data = res.data;
			//       uni.setStorageSync("token", data.token);
			//       uni.setStorageSync("userInfo", data);
			//       uni.showToast({
			//         title: "登录成功",
			//         icon: "success",
			//         duration: 2000,
			//       });
			//       setTimeout(() => {
			//         uni.reLaunch({
			//           url: "/pages/home/home",
			//         });
			//       }, 2000);
			//     })
			//     .catch((err) => {
			//       console.log(err, 222);
			//       uni.showToast({
			//         title: err.message || err,
			//         icon: "none",
			//         duration: 2000,
			//       });
			//     });
			// },
			//登录
			submit() {
				// #ifdef APP-PLUS
				if (!this.apiUrl) {
					this.$refs.serverSettingRef.open()
					return
				}
				// #endif

				let param = {
					loginName: this.userInfo.username,
					loginPwd: this.userInfo.passwd
				}

				postJ(this.apiUrl + '/main/user/login', param)
					.then(res => {
						if (this.isMemo) {
							setPassword(this.userInfo)
						}

						let data = res.data
						uni.setStorageSync('token', data.token)
						uni.setStorageSync("userInfo", data);
						uni.showToast({
							title: '登录成功',
							icon: 'success',
							duration: 2000
						})
						this.getTree()
						setTimeout(() => {
							uni.reLaunch({
								url: '/pages/home/home'
							})
						}, 2000)
					})
					.catch(err => {
						uni.showToast({
							title: err.message,
							icon: 'none',
							duration: 2000
						})
					})
			},
			handleServerSettings() {
				this.$refs.serverSettingRef.open()
			},
			checkboxChange(e) {
				this.isMemo = e.detail.value[0] === '1'
				if (this.isMemo) {
					setMemo()
				} else {
					removeMemo()
				}
			},
			formatRouter(list) {
				let authorities = [];

				const fn = (list) => {
					let arr = [];
					for (const p of list) {
						if (p.menuType === 2) {
							// p.children = [];
							authorities.push(p);
						}
						//  else {
						if (p.children?.length) {
							p.children = fn(p.children);
						} else {
							p.children = [];
						}
						arr.push(p);
						// }
					}

					return arr;
				};

				fn(list);

				return authorities
			},
			getTree() {
				getResourcesTree().then(res => {
					console.log(res)
					if (res.length == 0) {
						uni.showToast({
							title: '您还未配置权限',
							icon: 'none'
						})
					}
					let List = JSON.stringify(res || [])
					let authorities =this.formatRouter(res[0].children)
					uni.setStorageSync('treeList', List)
					uni.setStorageSync('authorities', JSON.stringify(authorities)) //按钮
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	@import 'login.scss';
</style>