<template>
	<view class="register">

		<view class="content">
			<!-- 头部logo -->
			<view class="header">
				<image :src="logoImage"></image>
			</view>
			<!-- 主体 -->
			<view class="main" @click="isShowOrgInfo()">
				<wInput v-model="orgInfo.organizationName" type="text" :isDisabled="true" placeholder="选择机构"
					@click="isShowOrgInfo()">
				</wInput>
				<u-picker keyName="organizationName" :show="show" :columns="columns" @confirm="confirm" @cancel="cancel"
					confirmColor="#53a591">
				</u-picker>
			</view>
			<wButton class="wbutton" bgColor="linear-gradient(to bottom right, rgb(55,186,189), #94e2c4)" text="提 交"
				:rotate="isRotate" @click="submitOrgInfo()"></wButton>
			<view class="uni-fixed-bottom">
				<div style="padding: 0 15px;">
					<u-row>
						<u-col span="5">
							<u-text text="技术支持：会山科技" color="#cacaca">
							</u-text>
						</u-col>
						<u-col span="3">
							<div></div>
						</u-col>
						<u-col span="5">
							<u-text style="position: absolute; right: 15px;bottom: 0;" text="hs@willalp.com"
								color="#cacaca">
							</u-text>
						</u-col>
					</u-row>
				</div>
			</view>
		</view>
	</view>
</template>

<script>
	let _this = this;
	import wInput from '../watch-login/watch-input.vue' //input
	import wButton from '../watch-login/watch-button.vue' //button

	export default {
		data() {
			return {
				//logo图片 base64
				//logoImage: 'data:image/png;base64,[encoded data, 23364 characters]',
				logoImage: '../../static/img/login/pns_app_icon.png',
				showAgree: true, //协议是否选择
				isRotate: false, //是否加载旋转
				orgInfo: {
					organizationName: '',
					organizationCode: '',
				},
				show: false,
				columns: [
					[]
				],
				form: {
					userName: '',
					organizationCode: ''
				},
				interval: null
			}
		},
		components: {
			wInput,
			wButton,
		},
		beforeDestroy() {
			clearInterval(this.interval)
		},
		mounted() {
			_this = this;
			this.form.userName = uni.getStorageSync('setUserName')
			this.getOrganization()
		},
		methods: {
			async getabData() {
				const {
					data: res
				} = await this.$httpRequest({
					url: '/api/ci/ab?un=all',
					method: 'get',
				})
				if (res.code == 200) {
					uni.setStorageSync("addbk", res.data)
				}
			},
			async subOrgLogin(orgCode) {
				let form = uni.getStorageSync("loginForm")
				form.orgCode = orgCode
				// 发送请求
				const {
					data: res
				} = await this.$httpRequest({
					url: '/api/subOrgLogin',
					method: 'post',
					urlType: 1,
					data: form,
					isNotToken: true
				});
				if (!res) {
					_this.isRotate = false
					this.$showModal('网络异常')
					return
				}
				if (res.code === 200) {
					const data = res.loginUser.user
					this.form.userName = data.userName
					this.form.OrganizationCode = orgCode
					uni.setStorageSync('setUserName', data.userName);
					uni.setStorageSync('token', res.token);
					uni.setStorageSync('files', res.files);
					uni.setStorageSync('QRcodeToken', res.loginUser.token);
					this.$getAllDicts()
					this.$getAllConfigData()
					this.getAppUserInfo()
					this.isRotate = false
				} else {
					uni.showModal({
						title: '提示',
						content: res.msg,
						showCancel: false
					});
				}
			},
			submitOrgInfo() {

				if (this.isRotate) {
					//判断是否加载中，避免重复点击请求
					return false;
				}
				this.isRotate = true
				uni.setStorageSync("orgInfo", this.orgInfo)
				this.form.organizationCode = this.orgInfo.organizationCode
				this.getabData()
				let port
				if (this.orgInfo.organizationCode === 'G00000003') {
					uni.setStorageSync("tabType", 2)
					port = 3010
				} else if (this.orgInfo.organizationCode === 'G00000007') {
					uni.setStorageSync("tabType", 3)
					port = 3005
				} else if (this.orgInfo.organizationCode === 'G00000005') {
					uni.setStorageSync("tabType", 4)
					port = 3012
				} else {
					uni.setStorageSync("tabType", 1)
				}
				//写入 【webSocket】 地址
				let ws = this.$BASE_SOCKET_URL + ':' + port
				uni.setStorageSync("wsServerUrl", ws)
				this.subOrgLogin(this.orgInfo.organizationCode)
			},

			async getAppUserInfo() {
				if (!this.orgInfo.organizationName) {
					uni.showToast({
						duration: 1500,
						icon: 'error',
						title: '请选择机构'
					});
					return;
				}
				this.isRotate = true
				// 发送请求
				const {
					data: res
				} = await this.$httpRequest({
					url: '/app/getUser',
					data: this.form,
					urlType: this.$getUrlType()
				});
				if (res.code === 200) {
					uni.showToast({
						duration: 1500,
						icon: 'checkmark'
					});
					const data = res.data
					uni.setStorageSync("userInfo", data)
					uni.reLaunch({
						url: '../../pages/tabbar/wpTabbar'
					})
				} else {
					uni.showModal({
						title: '提示',
						content: res.msg,
						showCancel: false
					});
				}
				this.isRotate = false
			},
			async getOrganization() {
				this.form.userName = uni.getStorageSync('loginForm').username
				// 发送请求
				const {
					data: res
				} = await this.$httpRequest({
					url: '/api/getOrganization',
					method: 'post',
					urlType: 1,
					data: this.form,
					isNotToken: true
				});
				if (res.code === 200) {
					const data = res.data
					if (data.length === 0) {
						uni.showModal({
							title: '提示',
							content: '请联系机构管理员激活账户',
							showCancel: false
						});
						return;
					}
					this.orgInfo.organizationName = data[0].orgName
					this.orgInfo.organizationCode = data[0].orgCode
					for (var i = 0; i < data.length; i++) {
						this.columns[0].push({
							organizationName: data[i].orgName,
							organizationCode: data[i].orgCode
						})
					}
					uni.setStorageSync("orglist", this.columns)
					if (data.length === 1) {
						this.submitOrgInfo();
					}
					uni.setStorageSync("orglist", this.columns)
				}
			},
			confirm(e) {
				//console.log('confirm', e)
				this.orgInfo = e.value[0];
				this.show = false
			},
			cancel() {
				uni.showTabBar({
					animation: true
				})
				this.show = false
			},
			isShowOrgInfo() {
				if (0 === this.columns[0].length) {
					uni.showModal({
						title: '提示',
						content: '请联系机构管理员激活账户',
						showCancel: false
					});
					return;
				}
				uni.hideTabBar({
					animation: true,
				})
				this.show = true
			}
		}
	}
</script>

<style>
	@import url("../watch-login/css/icon.css");
	@import url("./css/main.css");
</style>
