<template>
	<view>
		<scroll-view scroll-y class="page">
			<cu-custom bgColor="bg-cyan" :isBack="true"><block slot="backText">返回</block><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  margin-top">
					<view class="title">医院名称</view>
					<input name="hospital" v-model="hospital"></input>
				</view>
				<view class="cu-form-group">
					<view class="title">分院名称</view>
					<input name="sortHospital" v-model="sortHospital"></input>
				</view>
				<view class="cu-form-group">
					<view class="title">科室编码</view>
					<input name="departmentCode" :value="departmentCode" disabled="true"></input>
				</view>
				<view class="cu-form-group">
					<view class="title">科室名称</view>
					<input placeholder="请输入科室名称" v-model="department" @input="getDepartment"></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 {
				departmentCode: '',
				department: '',
				hospital: '',
				sortHospital: ''
			}
		},
		onLoad(e) {
			this.departmentCode = e.qrcode
			this.getDepartmentIsExist()
		},
		methods: {
			getDepartment: function(e){
				this.department = e.target.value
			},
			getDepartmentIsExist() {
				let that = this;
				uni.request({
					url: 'https://lekapi.opmonitor.com/?c=app_api&a=getDepartmentIsExist',
					data: {
						departmentCode: this.departmentCode
					},
					header: {
						'Content-type': 'application/json'
					},
					success: function(res){
						if(res.data.data.hospital === null || res.data.data.hospital === '') {
							that.hospital = getApp().globalData.hospital
						} else {
							that.hospital = res.data.data.hospital
						}
						if(res.data.data.sortHospital === null || res.data.data.sortHospital === ''){
							that.sortHospital = getApp().globalData.sortHospital
						} else {
							that.sortHospital = res.data.data.sortHospital
						}
						that.department = res.data.data.department
					},
					fail: () => {
						console.info('小程序域名不正确，请检查域名正确性')
					}
				})
			},
			buttonClick(e) {
				let that = this;
				uni.showLoading({
					title: '页面跳转中',
					mask: true,
					success: function(res) {
						let new_that = that;
						// console.info(new_that.hospital,new_that.sortHospital,new_that.department,new_that.departmentCode)
						uni.request({
							url: 'https://lekapi.opmonitor.com/?c=app_api&a=postByDepartment',
							data: {
								hospital: new_that.hospital,
								sortHospital: new_that.sortHospital,
								department: new_that.department,
								departmentCode: new_that.departmentCode
							},
							header: {
								'Content-type': 'application/x-www-form-urlencoded'
							},
							method: 'POST',
							success: function(res){
								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>
