<template>
	<view>
		<scroll-view scroll-y class="page">
			<cu-custom bgColor="bg-cyan"><block slot="content">注册</block></cu-custom>
			<view class="photoView">
				<view class="photoImage">
					<open-data type="userAvatarUrl"></open-data>
				</view>
			</view>
			<form action="">
				<view class="cu-form-group">
					<view class="title">医院名称</view>
					<picker @change="pickerHospitalChange" :value="indexHospital" :range="pickerHospital">
						<view class="picker">
							{{indexHospital>-1?pickerHospital[indexHospital]:'请选择医院'}}
						</view>
					</picker>
				</view>
				<view class="cu-form-group">
					<view class="title">分院名称</view>
					<picker @change="pickerSortChange" :value="indexSort" :range="pickerSort">
						<view class="picker">
							{{indexSort>-1?pickerSort[indexSort]:'请选择分院'}}
						</view>
					</picker>
				</view>
				<view class="cu-form-group margin-top">
					<view class="title">姓名</view>
					<input placeholder="请输入姓名" name="name" value="" @input="getName"></input>
				</view>
				<view class="cu-form-group">
					<view class="title">联系电话</view>
					<input placeholder="请输入联系电话" name="phone" value="" @input="getPhone"></input>
				</view>
			</form>
			<view class="padding flex flex-direction" style="padding-top: 40%;">
				<button class="cu-btn round bg-cyan  margin-tb-sm lg" @click="buttonClick">注册</button>
			</view>
		</scroll-view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				index: -1,
				indexHospital: -1,
				indexSort: -1,
				hospital: '',
				sortHospital: '',
				pickerHospital: [],
				pickerSort: [],
				name: '',
				idcard: '',
				openid: '',
				session_key: ''
			}
		},
		mounted() {
			// 获取用户登录信息
			let that = this;
			uni.login({
				success(res) {
					if(res.code){
						let new_that = that;
						uni.request({
							url: 'https://lekapi.opmonitor.com/?c=app_api&a=getToolOpenid',
							data: {code: res.code},
							header: {
								'Content-type': 'application/json'
							},
							success: function(res) {
								new_that.openid = res.data.data.openid
								new_that.session_key = res.data.data.session_key
							},
							fail: () => {
								console.info('小程序域名不正确，请检查域名正确性')
							}
						});
					}
				},
				fail(msg) {
					console.info('小程序登录失败！！！')
				}
			});
			this.getHospitalData();
		},
		methods: {
			getHospitalData(){
				let that = this;
				uni.request({
					url: 'https://lekapi.opmonitor.com/?c=app_api&a=getHospitalList',
					data: {},
					header: {
						'Content-type': 'application/json'
					},
					success: function(res) {
						that.pickerHospital = res.data.data
					},
					fail: () => {
						console.info('小程序域名不正确，请检查域名正确性')
					}
				});
			},
			getSortHospital() {
				let that = this;
				uni.request({
					url: 'https://lekapi.opmonitor.com/?c=app_api&a=getSortHospitalList',
					data: {hospital : that.hospital},
					header: {
						'Content-type': 'application/json'
					},
					success: function(res) {
						that.pickerSort = res.data.data
					},
					fail: () => {
						console.info('小程序域名不正确，请检查域名正确性')
					}
				});
			},
			pickerHospitalChange(e) {
				this.indexHospital = e.detail.value
				this.hospital = this.pickerHospital[this.indexHospital]
				this.getSortHospital();
			},
			pickerSortChange(e) {
				this.indexSort = e.detail.value
				this.sortHospital = this.pickerSort[this.indexSort]
			},
			getName: function(e){
				this.name = e.target.value
			},
			getPhone: function(e){
				this.phone = e.target.value
			},
			buttonClick(e) {
				let that = this;
				uni.showLoading({
					title: '页面跳转中',
					mask: true,
					success: function(res) {
						let new_that = that;
						uni.request({
							url: 'https://lekapi.opmonitor.com/?c=app_api&a=getRegisterToolInfo',
							data: {
								hospital: new_that.hospital,
								sortHospital: new_that.sortHospital,
								name: new_that.name,
								phone: new_that.phone,
								openid: new_that.openid
							},
							header: {
								'Content-type': 'application/x-www-form-urlencoded'
							},
							method: 'POST',
							success: function(res){
								if(res.data.data === 'false'){
									uni.navigateTo({
										url: '/pages/plugin/register'
									})
								}else{
									// 将这些变量放置globalData中
									getApp().globalData.hospital = new_that.hospital
									getApp().globalData.sortHospital = new_that.sortHospital
									getApp().globalData.name = new_that.name
									getApp().globalData.openid = new_that.openid
									getApp().globalData.phone = new_that.phone
									if(res.data.data === 'success'){
										uni.navigateTo({
											url: '/pages/index/index'
										})
									}
								}
							},
							fail: () => {
								console.info('小程序域名不正确，请检查域名正确性')
							}
						})
					}
				})
			}
		}
	}
</script>

<style>
	.page {
		width: 100vw;
		height: 120Vh;
	}
	.photoView {
		padding-top: 50rpx;
		position: relative;
		color: #fff;
		display: flex;
		flex-direction: column;
		align-items: center;
	}
	.photoImage {
		overflow:hidden;
		display: block;
		width: 200rpx;
		height: 200rpx;
		margin: 20rpx;
		border-radius: 50%;
		border: 2px solid #fff;
		box-shadow: 3px 3px 10px rgba(0, 0, 0, 0.2);
	}
	.cu-form-group .title{
		min-width: calc(4em + 15px);
		font-weight: 600;
	}
	.picker {
		text-align: left;
	}
</style>
