<template>
	<view class="container">
		<view class="container_text">
			<image class="banner-img" src="/static/img/img019.png" mode="scaleToFill"></image>
		</view>
		<view class="loginTitle"><text>登录</text></view>
		<view class="login_text">
			<view class="login_input flex">
				<view class="login_img">
					<image src="/static/icon/img03.png"></image>
				</view>
				<view class="login_name"><input class="uni-input" v-model="username" focus placeholder="请输入手机号" />
				</view>
			</view>
			<view class="login_input flex">
				<view class="login_img">
					<image src="/static/icon/img04.png"></image>
				</view>
				<view class="login_name"><input class="uni-input" type="password" v-model="passward" focus
						placeholder="请输入密码" /></view>
			</view>
			<view><button type="green" class="uni-button uni-button-green" @click="toLogin">登录</button></view>
			<view><button type="green" class="uni-button uni-button-green uni-button-green-plain" plain="true"
					hover-class="none" @click="register">注册</button></view>
			<navigator url="./forget">
				<view class="forget">忘记密码</view>
			</navigator>
		</view>
	</view>
</template>

<script>
	import {
		mapState,
		mapMutations
	} from 'vuex';
	import {
		login,
		getUserInfo
	} from '@/api/login.js';
	export default {
		data() {
			return {
				username: '',
				passward: '',
				// #ifdef APP-PLUS
				// 保存当前是否为微信浏览器
				weixinB: false,
				// #endif
				chooseType: ''
			};
		},
		onLoad() {
			this.weixinB = uni.getStorageSync('weichatBrowser')
			this.chooseType = uni.getStorageSync('chooseType') || ''
		},
		methods: {
			...mapMutations(['setUserInfo', 'login']),
			//登录
			async toLogin() {
				let obj = this;
				obj.logining = true;
				if (obj.username == '') {
					obj.$api.msg('请输入手机号');
					return;
				}
				if (obj.passward == '') {
					obj.$api.msg('请输入密码');
					return;
				}
				login({
						account: obj.username,
						password: obj.passward
					})
					.then(function(e) {
						console.log('登录数据', e)
						uni.setStorageSync('token', e.data.token);
						// console.log('打印---token',token);

						obj.userInfo();
					})
					.catch(function(e) {
						console.log(e);
					});
			},
			//获取用户信息
			userInfo() {
				let obj = this;
				getUserInfo({})
					.then(function(e) {
						obj.setUserInfo(e.data);
						// if(e.data.ext_info == null){
						// 	uni.navigateTo({
						// 		url: `/pages/index/identity`
						// 	});
						// }
						// if(e.data.ext_info != null){
						// 	// 1是病人 2是医生 3是机构
						// 	uni.switchTab({
						// 		url: `/pages/index/home`
						// 	})
						// }
						// 没有选择身份或没有填写身份信息
						if (e.data.ext_info == null) {
							if (!obj.chooseType) {
								uni.navigateTo({
									url: '/pages/index/identity',
									fail() {
										uni.switchTab({
											url: '/pages/user/user',
											fail() {

											}
										})
									}
								});
							} else {
								if (obj.chooseType == 1) {
									uni.navigateTo({
										url: `/pages/public/PatRegister`
									});
								}
								if (obj.chooseType == 2) {
									uni.navigateTo({
										url: `/pages/public/DocRegister`
									});
								}
								if (obj.chooseType == 3) {
									uni.navigateTo({
										url: `/pages/public/MecRegister`
									});
								}
							}

						} else if (e.data.ext_info != null) {
							// 1是病人 2是医生 3是机构
							// 校验用户类型和入口是否一致
							if (e.data.type != obj.chooseType) {
								uni.showModal({
									title: '提示',
									content: '您当前的用户类型和入口类型不一致，请选择相应的入口进入！',
									success: function(res) {

										setTimeout(function() {
											//安卓手机
											document.addEventListener(
												"WeixinJSBridgeReady",
												function() {
													WeixinJSBridge.call("closeWindow");
												},
												false
											);
											//ios手机
											WeixinJSBridge.call("closeWindow");
										}, 100);

									}
								})
							} else {
								if (e.data.type == 2) {
									uni.setTabBarItem({
										index: 0,
										text: '首页',
										iconPath: 'static/tabBar/tab-home.png',
										selectedIconPath: 'static/tabBar/tab-home-current.png'
									});
									uni.setTabBarItem({
										index: 1,
										text: '我的病人',
										iconPath: 'static/tabBar/tab-cate.png',
										selectedIconPath: 'static/tabBar/tab-cate-current.png',
										visible: true
									});
									uni.setTabBarItem({
										index: 2,
										text: '科普学习',
										iconPath: 'static/tabBar/tab-study.png',
										selectedIconPath: 'static/tabBar/tab-study-current.png',
										visible: false
									});
									uni.setTabBarItem({
										index: 3,
										text: '接单',
										iconPath: 'static/tabBar/jd.png',
										selectedIconPath: 'static/tabBar/jd-act.png',
										visible: true
									})
									uni.setTabBarItem({
										index: 4,
										text: '我的',
										iconPath: 'static/tabBar/tab-my.png',
										selectedIconPath: 'static/tabBar/tab-my-current.png'
									});
								}
								if (e.data.type == 3) {
									uni.setTabBarItem({
										index: 0,
										text: '首页',
										iconPath: 'static/tabBar/tab-home.png',
										selectedIconPath: 'static/tabBar/tab-home-current.png'
									});
									uni.setTabBarItem({
										index: 1,
										text: '咨询记录',
										iconPath: 'static/tabBar/tab-cate.png',
										selectedIconPath: 'static/tabBar/tab-cate-current.png',
										visible: true
									});
									uni.setTabBarItem({
										index: 2,
										text: '科普学习',
										iconPath: 'static/tabBar/tab-study.png',
										selectedIconPath: 'static/tabBar/tab-study-current.png',
										visible: false
									});
									uni.setTabBarItem({
										index: 3,
										text: '发布订单',
										iconPath: 'static/tabBar/jd.png',
										selectedIconPath: 'static/tabBar/jd-act.png',
										pagePath: '/pages/dd/dd',
										visible: true
									})

									uni.setTabBarItem({
										index: 4,
										text: '我的',
										iconPath: 'static/tabBar/tab-my.png',
										selectedIconPath: 'static/tabBar/tab-my-current.png'
									});
								}
								if (e.data.type == 1) {
									uni.setTabBarItem({
										index: 0,
										text: '首页',
										iconPath: 'static/tabBar/tab-home.png',
										selectedIconPath: 'static/tabBar/tab-home-current.png'
									});
									uni.setTabBarItem({
										index: 1,
										text: '咨询记录',
										iconPath: 'static/tabBar/tab-cate.png',
										selectedIconPath: 'static/tabBar/tab-cate-current.png',
										visible: true
									});
									uni.setTabBarItem({
										index: 2,
										text: '科普学习',
										iconPath: 'static/tabBar/tab-study.png',
										selectedIconPath: 'static/tabBar/tab-study-current.png',
										visible: true
									});
									uni.setTabBarItem({
										index: 3,
										text: '接单',
										iconPath: 'static/tabBar/jd.png',
										selectedIconPath: 'static/tabBar/jd-act.png',
										visible: false
									})
									uni.setTabBarItem({
										index: 4,
										text: '我的',
										iconPath: 'static/tabBar/tab-my.png',
										selectedIconPath: 'static/tabBar/tab-my-current.png.png'
									});
								}
								uni.switchTab({
									url: '/pages/index/home',
									fail(e) {
										obj.$log.addLog('t4')
										uni.switchTab({
											url: '/pages/user/user',
											fail() {

											}
										})
									}
								})
							}

						} else {
							obj.$log.addLog('t3')
							uni.switchTab({
								url: '/pages/index/home',
								fail() {
									uni.switchTab({
										url: '/pages/user/user',
										fail() {

										}
									})
								}
							})
						}
					})
					.catch(function(e) {
						console.log(e)
					});
			},
			//跳转注册页
			register() {
				uni.navigateTo({
					url: `/pages/public/register`
				});
			},
			//跳转忘记密码
			forget() {
				uni.navigateTo({
					url: `/pages/public/forget`
				});
			},
			// 后退
			navBack() {
				uni.navigateBack();
			}
		}
	};
</script>

<style lang="scss">
	page {
		height: 100%;
	}

	.container {
		width: 100%;
		height: 100%;
		background-size: 100%;
	}

	.container_text {
		width: 100%;
		height: 500rpx;
		top: 0rpx;

		image {
			width: 100%;
		}
	}

	.login_text {
		margin: auto 30rpx;
		position: relative;
		padding: 100rpx 102rpx;
		background-color: #ffffff;
		margin-top: -180rpx;
		border-radius: 20rpx;

		.login_input {
			border-bottom: 1px solid #f0f0f0;
			margin-bottom: 65rpx;

			.login_img image {
				height: 35rpx;
				width: 29rpx;
				margin-right: 20rpx;
			}

			.uni-input {
				text-align: left;
				width: 470rpx;
				font-size: 28rpx !important;
			}

			.login_name {
				color: #333333;
			}
		}

		.other {
			margin-top: 60rpx;

			.fenge {
				width: 30%;
				height: 2rpx;
				background-color: #eeeeee;
			}

			.qita {
				font-size: 28rpx;
				color: #999999;
			}
		}

		.weixin {
			width: 75rpx;
			height: 75rpx;
			margin: 25rpx auto;
		}

		.weixin image {
			width: 100%;
			height: 100%;
		}

		.weixin_text {
			text-align: center;
			font-size: 28rpx;
			color: #999999;
		}

		.forget {
			font-size: 28rpx;
			width: 100%;
			text-align: right;
			color: #999999;
		}

		.uni-button-green {
			color: #ffffff;
			background-color: #6786FB;
			margin: 40rpx 10rpx;
			border-radius: 50rpx;
		}

		.uni-button-green-plain {
			border: 1px solid #6786FB;
			margin: 40rpx 10rpx;
			border-radius: 50rpx;
			color: #6786FB;
			background-color: #ffffff;
		}

		.uni-button {
			height: 85rpx;
			line-height: 85rpx;
		}
	}

	.loginTitle {
		position: absolute;
		top: 250rpx;
		width: 100%;
		text-align: center;
		color: white;
		font-size: 40rpx;
	}
</style>
