<template>
	<view class="container" :style="{paddingTop:CustomBar+'px'}">
		<tui-skeleton v-if="skeletonShow" backgroundColor="#fafafa" borderRadius="10rpx" :isLoading	="true" :loadingType="7"></tui-skeleton>
		<template v-else>
			<view class="tui-page-title">登录</view>
			<view class="tui-form">
				<view class="tui-view-input">
					<tui-list-cell :hover="false" :lineLeft="false" backgroundColor="transparent">
						<view class="tui-cell-input">
							<tui-icon name="mobile" color="#6d7a87" :size="20"></tui-icon>
							<input
								:adjust-position="false"
								v-model="param.mobile"
								placeholder="请输入账号/手机号"
								placeholder-class="tui-phcolor"
								type="number"
								maxlength="11"
							/>
							<view class="" v-show="param.mobile" @click.stop="clearInput(1)">
								<tui-icon name="close-fill" :size="16" color="#bfbfbf" ></tui-icon>
							</view>
						</view>
					</tui-list-cell>
					<tui-list-cell :hover="false" :lineLeft="false" backgroundColor="transparent">
						<view class="tui-cell-input">
							<tui-icon name="pwd" color="#6d7a87" :size="20"></tui-icon>
							<input
								v-if="!isShowEye"
								:adjust-position="false"
								v-model="param.password"
								placeholder="请输入密码"
								placeholder-class="tui-phcolor"
								type="text"
								maxlength="36"
							/>
							<input
								v-if="isShowEye"
								:adjust-position="false"
								v-model="param.password"
								placeholder="请输入密码"
								placeholder-class="tui-phcolor"
								type="password"
								maxlength="36"
							/>
							<view class="" v-show="param.password" style="margin: 0 20rpx 0 0;" @click.stop="changInputType">
								<tui-icon :name="isShowEye ? 'unseen' : 'eye'" :size="22" color="#bfbfbf"></tui-icon>
							</view>
							<view class="" v-show="param.password" @click.stop="clearInput(2)">
								<tui-icon name="close-fill" :size="16" color="#bfbfbf" ></tui-icon>
							</view>
						</view>
					</tui-list-cell>
				</view>
				<view class="tui-btn-box">
					<tui-button type="primary" :disabledGray="true" :disabled="disabled" :shadow="true" shape="circle" @click="hanldeClick">登录</tui-button>
				</view>
			</view>
		</template>
	</view>
</template>

<script>
import { mapMutations } from 'vuex'
import authorize from '@/common/config/authorize.js' 
export default {
	computed: {
		disabled: function() {
			let bool = true
			if (this.param.mobile && this.param.password) {
				bool = false
			}
			return bool
		}
	},
	data() {
		return {
			CustomBar:this.CustomBar,// 顶部导航栏高度
			param:{
				code:null,
				encryptedData:null,
				iv:null ,
				mobile: '',
				password: '', 
			},
			popupShow: false,
			passwordType:'password',
			skeletonShow:true,
			isShowEye:true
		} 
	},
	onLoad(options) {
		
	},
	methods: {
		...mapMutations(['login', 'logout']),
		async getWxAuthorize(){// 初始化授权登录
			try{
				const wXCode = await authorize.getCode('weixin')
				const wEchat = await authorize.getUserInfo('weixin')
				const param = { code: wXCode,encryptedData: wEchat.encryptedData,iv: wEchat.iv}
				const res = await this.UserService.UserLoginAuthApplets(param)
				const data = res.data
				this.login(data)
				this.$store.commit('updateStatus',data)
				setTimeout(()=>{
					this.navigateLink(data.userType)
				},500)
			}catch(error){
				this.logout()
				this.skeletonShow = false
				this.$store.commit('updateStatus',error.data)
			}
		},
		async UserLoginReceiptPassword(){// 账号密码登录
			const WxCode = await authorize.getCode('weixin')
			const Wx = await authorize.getUserInfo('weixin')
			this.param.code = WxCode
			this.param.iv = Wx.iv
			this.param.encryptedData = Wx.encryptedData
			this.UserService.UserLoginReceiptPassword(this.param).then(response =>{
				this.login(response.data)
				this.$store.commit('updateStatus',response.data)
				this.navigateLink(response.data.userType)
			}).catch(err =>{
				this.$util.msg(err.msg,2000)
			})
		},
		hanldeClick(){//点击登录
			this.UserLoginReceiptPassword()
		},
		navigateLink(userType){// 根据用户权限跳转对应页面
			/**
			 * 用户类型 1服务商人员,2客服,3财务,4超级管理员
			 * 1. 服务商人员跳转到收款列表页面
			 * 2. 客服跳转到收款列表页面
			 * 3. 财务人员跳转到款项识别页面
			 * 4. 超级管理员跳转到款项识别页面
			 * */
			 switch(userType){
				 case 1:
					this.$api.navigateTo('/pages/collection/list')
					break
				 case 2:
					this.$api.navigateTo('/pages/collection/list')
					break
				 case 3:
					this.$api.navigateTo('/pages/collection/sms')	
					break
				 case 4:
					this.$api.navigateTo('/pages/collection/sms')
					break
					
			 }
		},
		changInputType(){// 控制显示密码
			this.isShowEye = !this.isShowEye
		},
		clearInput(type) {// 清除输入框信息
			if (type == 1) {
				this.param.mobile = ''
			} else {
				this.param.password = ''
			}
		}
	},
	onShow() {
		this.getWxAuthorize()
	}
}
</script>

<style lang="scss">
.container {
	.tui-status-bar {
		width: 100%;
		height: var(--status-bar-height);
	}

	.tui-header {
		width: 100%;
		padding: 40rpx;
		display: flex;
		align-items: center;
		justify-content: space-between;
		box-sizing: border-box;
	}

	.tui-page-title {
		width: 100%;
		font-size: 48rpx;
		font-weight: bold;
		color: $uni-text-color;
		line-height: 42rpx;
		padding: 40rpx;
		box-sizing: border-box;
	}

	.tui-form {
		padding-top: 50rpx;

		.tui-view-input {
			width: 100%;
			box-sizing: border-box;
			padding: 0 40rpx;

			.tui-cell-input {
				width: 100%;
				display: flex;
				align-items: center;
				padding-top: 48rpx;
				padding-bottom: $uni-spacing-col-base;

				input {
					flex: 1;
					padding-left: $uni-spacing-row-base;
				}

				.tui-icon-close {
					margin-left: auto;
				}
			}
		}

		.tui-cell-text {
			width: 100%;
			padding: $uni-spacing-col-lg $uni-spacing-row-lg;
			box-sizing: border-box;
			font-size: $uni-font-size-sm;
			color: $uni-text-color-grey;
			display: flex;
			align-items: center;
			justify-content: space-between;

			.tui-color-primary {
				color: $uni-color-primary;
			}
		}

		.tui-btn-box {
			width: 100%;
			padding: 0 $uni-spacing-row-lg;
			box-sizing: border-box;
			margin-top: 80rpx;
		}
	}

	.tui-login-way {
		width: 100%;
		font-size: 26rpx;
		color: $uni-color-primary;
		display: flex;
		justify-content: center;
		position: fixed;
		left: 0;
		bottom: 80rpx;

		view {
			padding: 12rpx 0;
		}
	}

	.tui-auth-login {
		width: 100%;
		display: flex;
		align-items: center;
		justify-content: center;
		padding-bottom: 80rpx;
		padding-top: 20rpx;

		.tui-icon-platform {
			width: 90rpx;
			height: 90rpx;
			display: flex;
			align-items: center;
			justify-content: center;
			position: relative;
			margin-left: 40rpx;

			&::after {
				content: '';
				position: absolute;
				width: 200%;
				height: 200%;
				transform-origin: 0 0;
				transform: scale(0.5, 0.5) translateZ(0);
				box-sizing: border-box;
				left: 0;
				top: 0;
				border-radius: 180rpx;
				border: 1rpx solid $uni-text-color-placeholder;
			}
		}

		.tui-login-logo {
			width: 60rpx;
			height: 60rpx;
		}
	}
}
</style>
