<template>

	<view class="normal-login-container">
		<view class="logo-content align-center justify-center flex">
			<image :src="login_top" mode="widthFix" class="width100" style="margin-top: -40upx;"></image>
		
		</view>
		<!-- 	<view class="logo-content align-center justify-center flex">
			<image :src="login_top" mode="widthFix" class="width100" style="margin-top: -80upx;"></image> -->
		<!-- 	<image :src="description" mode="widthFix" class="img_top"
				style="position: absolute;right: 0;top: 120upx; z-index: 999;width: 346upx;height: 330upx;"></image> -->
		<!-- </view> -->

		<!-- 账号密码框 -->
		<view class="login-form-content">

			<text style="color: #98AEAF;">角色</text>
			<view style="display: flex;margin: 15px auto;" class="u-demo-block__content">
				<u-checkbox-group placement="row" v-model="checkboxValue1" @change="checkboxChange"
					activeColor="#4CB2B6">
					<u-checkbox :customStyle="{margin:'0 0 8px 15px'}" v-for="(item, index) in checkboxList1"
						:key="index" :label="item.name" :name="item.name">
					</u-checkbox>
				</u-checkbox-group>
			</view>
			<text style="color: #98AEAF;">单位</text>
			<view class="u-demo-block__content" style="margin: 15px auto;">
				<view class="u-page__tag-item">
					<u-search @search="searchUnit" @custom="searchUnit" showAction placeholder="请搜索单位名称进行选择"
						v-model="loginForm.unit">
					</u-search>
				</view>
			</view>
			<text style="color: #98AEAF;">真实姓名</text>
			<view class="input-item flex align-center">
				<view class="iconfont icon-user icon"></view>
				<input v-model="loginForm.nickname" class="input" type="text" placeholder="请输入姓名" maxlength="30" />
			</view>
			<text style="color: #98AEAF;">账号</text>
			<view class="input-item flex align-center">
				<view class="iconfont icon-user icon"></view>
				<input v-model="loginForm.username" class="input" type="text" placeholder="请输入账号" maxlength="30" />
			</view>
			<text style="color: #98AEAF;">手机号</text>
			<view class="input-item flex align-center">
				<view class="iconfont icon-user icon"></view>
				<input v-model="loginForm.mobile" class="input" type="text" placeholder="请输入手机号" maxlength="30" />
			</view>
			<text style="color: #98AEAF;">密码</text>
			<view class="input-item flex align-center">
				<view class="iconfont icon-password icon"></view>
				<input v-model="loginForm.password" type="password" class="input" placeholder="请输入密码" maxlength="20" />
			</view>
			<text style="color: #98AEAF;">再次确认密码</text>
			<view class="input-item flex align-center">
				<view class="iconfont icon-password icon"></view>
				<input v-model="loginForm.passwords" type="password" class="input" placeholder="请确认密码" maxlength="20" />
			</view>

			<Verify @success="pwdLogin" :mode="'pop'" :captchaType="'blockPuzzle'"
				:imgSize="{ width: '330px', height: '155px' }" ref="verify"></Verify>

			<!-- 注册按钮 -->
			<view class="login-form-content2">
				<button @click="handleLogin" class="login-btn cu-btn block lg">确认注册</button>
			</view>
		</view>
		<u-picker :show="shows" :columns="columns1" @cancel="cancel" @confirm="confirm"></u-picker>
		<!-- <view class="xieyi text-center">
			<text class="text-grey1">登录即代表同意</text>
			<text @click="handleUserAgrement" class="text-blue">《用户协议》</text>
			<text @click="handlePrivacy" class="text-blue">《隐私协议》</text>
		</view> -->
	</view>
</template>
<script>
	import {
		addUser
	} from '@/api/staff_info'; //添加用户
	import {
		wxbind
	} from '@/api/login.js'; //微信绑定
// #ifdef H5
	import Verify from '@/pagesA/components/verifition/Verify';
		// #endif
	import {
		getDepartUnit
	} from '@/api/login.js'
	import config from '@/config'
	const baseUrlImg=config.baseUrlImg
	export default {
		name: 'Login',
		components: {
			// #ifdef H5
			Verify
			// #endif
		},
		data() {
			return {
				shows: false, //单位搜索后的下拉框
				columns1: [
					['未搜索到所属单位']
				],
				searchData: [],
				checkboxValue1: [],
				// 基本案列数据
				checkboxList1: [{
						name: '管理者',
						disabled: false,
					},
					{
						name: '执行者',
						disabled: false
					}
				],
				description: `${baseUrlImg}/login/description.png`, //登录页面顶部装饰图
				login_top:`${baseUrlImg}/login/login_top.png`, //登录页面顶部
				wx_login:`${baseUrlImg}/login/wx_login.png`, //登录按钮
				captchaEnabled: true, // 验证码开关 TODO 芋艿：需要抽到配置里
				canIUseGetUserProfile: false, // 用来判断用哪个登录
				globalConfig: getApp().globalData.config,
				userInfo: '',
				loginForm: {
					username: '',
					password: '',
					captchaVerification: '',
					socialCode: '',
					socialType: 34,
					socialState: '9b2ffbc1-7425-4155-9894-9d5c08541d62',
					orgId: '',
					unit: '',
					mobile: '',
					nickname: '', //真实姓名
					appRoleId: [],
					userId: '',
					realName: ''
				}
			};
		},
		onLoad(option) {
			this.loginForm.socialCode = option.socialCode
			//如果手机支持最新版登录就用新方法
			if (uni.getUserProfile) {
				this.canIUseGetUserProfile = true;
			}
		},
		methods: {
			checkboxChange(n) { //角色选择
				console.log(n, 'nnnnn');
				n.map(i => {
					if (i == '管理者') {
						this.loginForm.appRoleId.push('1')
					} else if (i == '执行者') {
						this.loginForm.appRoleId.push('2')
					} else {
						const arr = ["1", "2"]
						this.loginForm.appRoleId = arr
					}
				})
			},
			confirm(e) { //搜索单位确定
				console.log(e, 'eeeeeeeeee');
				this.loginForm.unit = e.value[0]
				this.searchData.map(i => {
					if (i.orgName == e.value[0] && i.orgName !== '未搜索到所属单位') {
						console.log(i.id, 'i.id');
						this.loginForm.orgId = i.id
					}
				})
				this.shows = false
			},
			cancel() {
				this.shows = false
			},
			//搜索单位
			searchUnit() {
				const org = this.loginForm.unit;
				getDepartUnit({
					orgName: org
				}).then(response => {
					console.log(response);
					// if (response.code == 0 && response.data.length == 0) {
					// 	this.$model.alert('暂无单位')
					// } else
					if (response.code == 0) {
						this.searchData = response.data
						this.shows = true
						response.data.map(i => {
							this.columns1[0].push(i.orgName)
						})
						console.log(this.columns1);
					}

				});


			},
			// 微信登录模块
			//老版登录接口(不再弹出登录弹框)
			bindGetUserInfo(e) {
				if (e.detail.userInfo) {
					//业务逻辑
				}
			},
			// 弹出登录弹框(新版)
			getUserProfiles() {},
			// 登录方法
			handleLogin(params) {
				if (this.loginForm.username === '') {
					this.$modal.msgError('请输入您的账号');
				} else if (this.loginForm.password === '') {
					this.$modal.msgError('请输入您的密码');
				} else if (this.loginForm.passwords === '') {
					this.$modal.msgError('请再次确认密码');
				} else if (this.loginForm.realname === '') {
					this.$modal.msgError('请输入您的真实姓名');
				} else if (this.loginForm.password != this.loginForm.passwords) {
					this.$modal.msgError('密码输入不一致,请重新输入');
				} else {
					this.pwdLogin({});
				}
			},
			// 密码登录
			async pwdLogin(captchaParams) {
				this.loginForm.appRoleId = [...new Set(this.loginForm.appRoleId)]
				console.log(this.loginForm, 'formData');
				addUser(JSON.stringify(this.loginForm)).then(reponse => {

					if (reponse.code == 0) {
						this.loginForm.userId = reponse.data
						this.loginForm.realName = this.loginForm.nickname
						console.log(this.loginForm.userId, '123456123456');
						wxbind(this.loginForm).then(res => {
							console.log(res, this.loginForm, '222222222222');
							// 注册成功后    不用路由
							// this.$modal.loading('登录中，请耐心等待...');
							// 执行登录
							// console.log(this.loginForm, 'this.loginForm');
							this.loginForm.captchaVerification = captchaParams.captchaVerification;
							this.$store.dispatch('Login', this.loginForm).then(() => {
								this.$modal.closeLoading(); //关闭遮罩层
								this.loginSuccess();
							});
						})
					}

				})


			},
			// 登录成功后，处理函数
			loginSuccess(result) {
				this.$store.dispatch('loadDictDatas');
				// 设置用户信息
				this.$store.dispatch('GetInfo').then(res => {
					this.$tab.reLaunch('/pages/index');
					// this.$tab.reLaunch('/pages/work/index');
				});
			}
		}
	};
</script>
<style lang="scss">
	page {
		background-color: #ffffff;
	}

	.normal-login-container {
		width: 100%;

		.logo-content {
			width: 100%;
			font-size: 21px;
			text-align: center;

			image {
				border-radius: 4px;
			}

			.title {
				margin-left: 10px;
			}
		}

		.login-form-content {
			text-align: left;
			margin: 0 0 0 40rpx;
			margin-top: 0;
			width: 90%;

			.input-item {
				margin: 15px auto;
				background-color: #f5f9f7;
				height: 35px;
				border-radius: 20px;

				.icon {
					font-size: 38rpx;
					margin-left: 10px;
					color: #999;
				}

				.input {
					width: 100%;
					font-size: 14px;
					line-height: 18px;
					text-align: left;
					padding-left: 15px;
				}
			}

			/deep/ .u-search__content,
			/deep/.u-search__content__input {
				background-color: rgb(245, 249, 247) !important;
			}

			.login-btn {
				margin-top: 30px;
				height: 80upx;
				border-radius: 356upx;
				background-color: #4cb2b6;
				color: #ffffff;
				box-shadow: 0px 2upx 6upx 0px rgba(123, 123, 123, 0.3);
			}

			.xieyi {
				color: #333;
				margin-top: 20px;
			}
		}

		.easyinput {
			width: 100%;
		}
	}

	// 登录按钮
	.login-form-content2 {
		text-align: center;
		margin: 10px auto;
		margin-top: 0%;
		margin-bottom: 30rpx;
		width: 75%;

		.wx_login {
			height: 100upx;
			width: 450upx;

			// font-size: 16px;
			// padding-top: 2px;
			// background-color: #f5f7f9;
			// color: #728f90;
			// border-radius: 356upx;
			// box-shadow: 0px 2upx 6upx 0px rgba(123, 123, 123, 0.3);
		}

		.or {
			font-size: 20upx;
			margin: 20upx;
			color: #98aeaf;
		}
	}

	.login-code-img {
		height: 45px;
	}

	.wx_icon {
		position: relative;
		margin-top: 27%;
		margin-left: 42%;
	}
</style>
