<template>
	<view class="container">
		<view class="bg bg-color-base margin-b20"></view>

		<u-modal v-model="tipShow" @confirm="pswShow=true" confirm-text="去修改" :show-cancel-button="true" cancel-text="暂不修改" content="检测到您的密码为初始密码，请修改密码后再登录!"></u-modal>

		<u-modal   @confirm="changeConfirm" title="修改密码" confirm-text="提交" :mask-close-able="true" v-model="pswShow" >
			<view class="slot-content" style="margin: 20rpx;">
				<u-form label-width="180"  ref="uForm">
					<u-form-item label="新密码"><u-input v-model="newPsw" type="password"/></u-form-item>
					<u-form-item :border-bottom="false" label="确认新密码"><u-input type="password" v-model="confirmPsw" /></u-form-item>
				</u-form>
			</view>
		</u-modal>

		<view class="tab vs-row vs-align-center">
			<image class="tab-bg" 
			src="https://6e69-niew6-1302638010.tcb.qcloud.la/denglu/%E7%99%BB%E5%BD%955/banner-icon.png?sign=d63f6b91aed3733b261cc0127d2cf702&t=1604049324"
			mode=""></image>

			<view class="vs-row vs-align-center">
				<view class="vs-column vs-align-center margin-r40" @click="cur = 0">
					<text class="font-50 margin-b20" style="color: #000000;">登录</text>
				</view>
			</view>
		</view>

		<view class="login margin-b80">
			<view class="input vs-row vs-align-center margin-b40">
				<image style="color: #88d8bc;width: 42rpx;height: 40rpx;" class="input-icon margin-r20" src="/static/login/a4.png"></image>
				<input class="vs-flex-item  font-30" v-model="username" type="text" value=""  placeholder="请输入您的账号"
				 placeholder-class="input-placeholder" />
			</view>
			<view class="input vs-row vs-align-center margin-b40">
				<image style="width: 40rpx;height: 40rpx;" class="input-icon margin-r20" src="/static/login/psw2.png"
				 ></image>
				<input class="vs-flex-item  font-30" v-model="pwd" type="text" password value="" placeholder="请输入您的密码"
				 placeholder-class="input-placeholder" />
			</view>
			<view style="margin-left: 20rpx;">
				<u-checkbox active-color="#7dc7ae" @change="checkboxChange" v-model="isSave" >记住密码</u-checkbox>
			</view>
		</view>
		<view @click="login" class="button bg-color-base vs-row vs-align-center vs-space-center margin-b20">
			<text class="color-white font-34">立即登录</text>
		</view>
		<u-modal v-model="show" :content="content"></u-modal>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				isSave: false,
				show: false,
				content: "",
				username: '', //手机号码
				pwd: '' ,//密码
				
				//修改密码
				tipShow:false,
				pswShow:false,
				newPsw:'',//新密码
				confirmPsw:''//确认新密码
			}
		},
		onLoad() {
			
			uni.getStorage({
			    key: 'account',
			    success: (res)=>{
			       this.username = res.data.account;
				   this.pwd = res.data.pwd;
				   this.isSave = true;
			    }
			});
		},
		methods:{
			checkboxChange(e){
				this.isSave=e.value
			},
			async handelLogin(){
				let that=this
				// let resp=await this.$api.getCurrentAccount()
				// if (resp.data.hadUpdatePwd==1) {
					if(this.isSave==true){
						 uni.setStorageSync('account',{account:that.username,pwd:that.pwd});					
					}else{
						uni.removeStorageSync("account");
					}
					uni.reLaunch({
						url:"../index/index"
					})
				// }else{
				// 	this.tipShow=true
				// }
			},
			changeConfirm(){
				if (this.$isEmpty(this.newPsw)) {
					this.$u.toast('新密码不能为空')
					this.pswShow=true
					return
				}
				if (this.$isEmpty(this.confirmPsw)) {
					this.pswShow=true
					this.$u.toast('确认密码不能为空')
					return
				}
				if (this.pwd===this.newPsw) {
					this.pswShow=true
					this.$u.toast('新密码不能和旧密码一致')
					return
				}
				if (this.newPsw!==this.confirmPsw) {
					this.pswShow=true
					this.$u.toast('两次输入的密码不一致')
					return
				}
				let params={
					oldPwd:this.pwd,
					newPwd:this.newPsw,
					confirmPwd:this.confirmPsw
				}
				this.$api.updatePsw(params).then(res=>{
					if (res.msg=='success') {
						uni.showToast({
							title:"修改成功，请重新登录",
							icon:"none",
							duration:2000
						})
						this.pwd=""
					}
				})
			},
		    login() {
				
				var that = this;
				if (!that.username) {
					that.content="请输入您的账号";
					that.show = true;
					return;
				}
				
				if (!that.pwd) {
					that.content="请输入您的密码";
					that.show = true;
					return;
				}
				
				this.$dialog.showLoading('登录中...')
				
				
				// this.$api.permissions.login({
				// 	loginName: that.phone,
				// 	loginPwd: that.pwd
				// },{"Content-Type":"application/x-www-form-urlencoded"}).then(res => {
				// 	if(res.code=="200"){
				// 		uni.setStorageSync('access-token',res.data);
				// 		this.handelLogin()
				// 	}else{
				// 		that.content=res.msg;
				// 		that.show = true;
				// 	}
				// 	uni.hideLoading()
					
				// }).catch(err=>{
				// 	uni.hideLoading()
				// });
				let params={
					tenant_id:"000000",
					username:that.username,
					password:this.$md5(that.pwd),
					grant_type: "password"
				}
				this.$cache.put('tokenObj',params)
				this.$api.getToken(params).then(res => {
					console.log(res)
					if(res.access_token != undefined){
						let token=res.token_type+" "+res.access_token
						uni.setStorageSync('token', token)
						this.handelLogin()
					}else{
						that.content = res.msg;
						that.show = true;
					}
					uni.hideLoading()
					
				}).catch(err=>{
					uni.hideLoading()
				});
			}
		}
	}
</script>

<style lang="scss">
	page{
		background-color: #FFFFFF;
	}
	.container {
		position: relative;
	}

	.bg {
		position: relative;
		width: 750rpx;
		height: 380rpx;
	}

	.tab {
		position: absolute;
		top: 240rpx;
		left: 20rpx;
		right: 20rpx;
		height: 150rpx;
		padding: 0 50rpx;
		background-color: #fff;
		border-top-left-radius: 20rpx;
		border-top-right-radius: 20rpx;

		&-bg {
			position: absolute;
			top: -200rpx;
			right: -50rpx;
			width: 440rpx;
			height: 285rpx;
		}
	}

	.line {
		width: 25rpx;
		height: 7rpx;
	}

	.login,
	.register {
		padding: 0 60rpx;
	}

	.input {
		width: 580rpx;
		height: 90rpx;
		padding: 0 30rpx;
		background-color: rgba(80, 100, 235, 0.1);
		border-radius: 100rpx;

		&-icon {
			width: 30rpx;
			height: 38rpx;
		}

		&-placeholder {
			color: #909090;
		}
	}

	.button {
		width: 630rpx;
		height: 90rpx;
		margin-left: 60rpx;
		border-radius: 100rpx;
	}

	.separator {
		height: 2rpx;
		margin: 0 30rpx;
		background-color: #f5f5f5;
	}
	// 下边距
	.margin-b5 {
		margin-bottom: 5rpx;
	}

	.margin-b10 {
		margin-bottom: 10rpx;
	}

	.margin-b15 {
		margin-bottom: 15rpx;
	}

	.margin-b20 {
		margin-bottom: 20rpx;
	}

	.margin-b25 {
		margin-bottom: 25rpx;
	}

	.margin-b30 {
		margin-bottom: 30rpx;
	}

	.margin-b40 {
		margin-bottom: 40rpx;
	}

	.margin-b60 {
		margin-bottom: 60rpx;
	}

	.margin-b80 {
		margin-bottom: 80rpx;
	}

	.margin-b100 {
		margin-bottom: 100rpx;
	}

	// 右边距
	.margin-r5 {
		margin-right: 5rpx;
	}

	.margin-r10 {
		margin-right: 10rpx;
	}

	.margin-r15 {
		margin-right: 15rpx;
	}

	.margin-r20 {
		margin-right: 20rpx;
	}

	.margin-r25 {
		margin-right: 25rpx;
	}

	.margin-r30 {
		margin-right: 30rpx;
	}

	.margin-r40 {
		margin-right: 40rpx;
	}

	.margin-r60 {
		margin-right: 60rpx;
	}

	// 字体大小
	.font-18 {
		font-style: normal;
		font-size: 18rpx;
		font-family: Droid Sans Fallback;
	}

	.font-20 {
		font-style: normal;
		font-size: 20rpx;
		font-family: Droid Sans Fallback;
	}

	.font-22 {
		font-style: normal;
		font-size: 22rpx;
		font-family: Droid Sans Fallback;
	}

	.font-24 {
		font-style: normal;
		font-size: 24rpx;
		font-family: Droid Sans Fallback;
	}

	.font-26 {
		font-style: normal;
		font-size: 26rpx;
		font-family: Droid Sans Fallback;
	}

	.font-28 {
		font-style: normal;
		font-size: 28rpx;
		font-family: Droid Sans Fallback;
	}

	.font-30 {
		font-style: normal;
		font-size: 30rpx;
		font-family: Droid Sans Fallback;
	}

	.font-32 {
		font-style: normal;
		font-size: 32rpx;
		font-family: Droid Sans Fallback;
	}

	.font-34 {
		font-style: normal;
		font-size: 34rpx;
		font-family: Droid Sans Fallback;
	}

	.font-36 {
		font-style: normal;
		font-size: 36rpx;
		font-family: Droid Sans Fallback;
	}

	.font-38 {
		font-style: normal;
		font-size: 38rpx;
		font-family: Droid Sans Fallback;
	}

	.font-40 {
		font-style: normal;
		font-size: 40rpx;
		font-family: Droid Sans Fallback;
	}

	.font-46 {
		font-style: normal;
		font-size: 46rpx;
		font-family: Droid Sans Fallback;
	}

	.font-50 {
		font-style: normal;
		font-size: 50rpx;
		font-family: Droid Sans Fallback;
	}

	.font-60 {
		font-style: normal;
		font-size: 60rpx;
		font-family: Droid Sans Fallback;
	}

	.font-80 {
		font-style: normal;
		font-size: 80rpx;
		font-family: Droid Sans Fallback;
	}

	// 字体对齐
	.text-left {
		text-align: left;
	}

	.text-center {
		text-align: center;
	}

	.text-right {
		text-align: right;
	}

	// color相关
	.color-white {
		color: #FFFFFF;
	}

	.color-red {
		color: #dc0000;
	}

	// 黑色色阶向下
	.color-black {
		color: #000;
	}

	.color-black-3 {
		color: #333;
	}

	.color-black-6 {
		color: #666;
	}

	.color-black-9 {
		color: #999;
	}

	// 字体宽度
	.font-weight-400 {
		font-weight: 400;
	}

	.font-weight-500 {
		font-weight: bold;
	}

	// 间隔
	.spacing-20 {
		width: 750rpx;
		height: 20rpx;
		background-color: #f8f8f8;
	}

	// 圆角
	.radius-10 {
		border-radius: 10rpx;
	}

	.radius-20 {
		border-radius: 20rpx;
	}

	.radius-30 {
		border-radius: 30rpx;
	}

	.radius-circle {
		border-radius: 50%;
	}

	.radius-height {
		border-radius: 10000px;
	}

	// flex相关
	.vs-flex-item {
		flex: 1;
	}

	.vs-space-between {
		justify-content: space-between;
	}

	.vs-space-around {
		justify-content: space-around;
	}

	.vs-space-center {
		justify-content: center;
	}

	.vs-space-end {
		justify-content: flex-end;
	}

	.vs-row {
		flex-direction: row;
	}

	.vs-column {
		flex-direction: column;
	}

	.vs-align-end {
		align-items: flex-end;
	}

	.vs-align-center {
		display: flex;
		align-items: center;
	}

	.vs-align-start {
		align-items: flex-start;
	}

	.vs-item-hover {
		background-color: rgba(0, 0, 0, 0.05);
	}

	.vs-btn-hover {
		opacity: 0.8;
	}

	.color-base {
		color: #7dc7ae;
	}

	.bg-color-base {
		background-color: #7dc7ae;
	}
</style>