<template>
	<view class="main">
		<view class="one">
			<upload class='upload' :list="form.icon" name="icon" :count="1" @uplSuc="uplSuc" @uplDel="uplDel">
			</upload>
		</view>
		<view class="two">
			<view class="two_1">
				<view class="left">姓名</view>
				<view class="right">
					<input v-model="form.name" placeholder="请输入真实姓名" />
					<text class="iconfont icon-dayuhao"></text>
				</view>
			</view>
			<view class="two_1">
				<view class="left">手机号</view>
				<view class="right">
					<input v-model="form.mobile" placeholder="请输入手机号" />
					<text class="iconfont icon-dayuhao"></text>
				</view>
			</view>
			<view class="two_1" v-if="user.role!='Patient'">
				<view class="left">医院名称</view>
				<view class="right">
					<input v-model="form.hos_name" placeholder="请输入医院名称" />
					<text class="iconfont icon-dayuhao"></text>
				</view>
			</view>
			<view class="two_1" v-if="user.role!='Patient'">
				<view class="left">科室名称</view>
				<view class="right">
					<input v-model="form.dept_name" placeholder="请输入科室名称" />
					<text class="iconfont icon-dayuhao"></text>
				</view>
			</view>
			<view class="two_1" v-if="user.role!='Patient'">
				<view class="left">职务</view>
				<view class="right">
					<input v-model="form.title" placeholder="请输入职务" />
					<text class="iconfont icon-dayuhao"></text>
				</view>
			</view>
			<view class="two_1" v-if="user.role!='Patient'">
				<view class="left">职称</view>
				<view class="right">
					<input v-model="form.post" placeholder="请输入职称" />
					<text class="iconfont icon-dayuhao"></text>
				</view>
			</view>
			<view class="two_1" v-if="user.role!='Patient'">
				<view class="left">简介</view>
				<view class="right">
					<input v-model="form.content" placeholder="请输入简介" />
					<text class="iconfont icon-dayuhao"></text>
				</view>
			</view>
			<view class="two_1" v-if="user.role=='Patient'">
				<view class="left">就诊编号/卡号</view>
				<view class="right">
					<input v-model="form.card_no" placeholder="请输入就诊编号/卡号" />
					<text class="iconfont icon-dayuhao"></text>
				</view>
			</view>
			<view class="two_1" v-if="user.role=='Patient'">
				<view class="left">性别</view>
				<view class="right">
					<picker class="picker" mode="selector" :range="genderList" @change="genderChange" range-key="label">
						<view>{{form.gender||'请选择性别'}}</view>
					</picker>
					<text class="iconfont icon-dayuhao"></text>
				</view>
			</view>
			<view class="two_1" v-if="user.role=='Patient'">
				<view class="left">年龄</view>
				<view class="right">
					<input v-model="form.age" placeholder="请输入年龄" />
					<text class="iconfont icon-dayuhao"></text>
				</view>
			</view>
			<view class="two_1" v-if="user.role=='Patient'">
				<view class="left">住址</view>
				<view class="right">
					<input v-model="form.address" placeholder="请输入住址" />
					<text class="iconfont icon-dayuhao"></text>
				</view>
			</view>
			<view class="two_1" v-if="user.role=='Patient'">
				<view class="left">紧急联系人</view>
				<view class="right">
					<input v-model="form.urgent_name" placeholder="请输入紧急联系人" />
					<text class="iconfont icon-dayuhao"></text>
				</view>
			</view>
			<view class="two_1" v-if="user.role=='Patient'">
				<view class="left">紧急联系人电话</view>
				<view class="right">
					<input v-model="form.urgent_mobile" placeholder="请输入紧急联系人电话" />
					<text class="iconfont icon-dayuhao"></text>
				</view>
			</view>
			<view class="two_1" v-if="user.role=='Patient'">
				<view class="left">病例</view>
				<view class="right">
					<upload class='upload' :list="form.emrs" name="emrs" :count="1" @uplSuc="uplSuc" @uplDel="uplDel">
					</upload>
				</view>
			</view>
		</view>
		<view class="thr">
			<button class="button" type="primary" size="mini" @click="onSubmit()">保存</button>
		</view>
	</view>
</template>

<script>
	import upload from '../../components/upload/index.vue';
	export default {
		components: {
			upload
		},
		data() {
			return {
				user: {},
				form: {},
				// 字典表
				genderList: [{
						label: '男',
						value: '男'
					},
					{
						label: '女',
						value: '女'
					}
				],
			}
		},
		onLoad: async function(e) {
			const that = this;
			await that.searchToken();
			await that.search();
		},
		methods: {
			// 用户信息
			searchToken() {
				const that = this;
				try {
					const res = uni.getStorageSync('token');
					if (res) {
						const user = that.$jwt(res);
						that.$set(that, `user`, user);
					}
				} catch (e) {}
			},
			// 查询
			async search() {
				const that = this;
				const user = that.user
				if (user) {
					let res;
					if (user.role == 'Doctor') res = await that.$api(`/doctor/${user._id}`, 'GET', {})
					else if (user.role == 'Nurse') res = await that.$api(`/nurse/${user._id}`, 'GET', {})
					else res = await that.$api(`/patient/${user._id}`, 'GET', {})
					if (res.errcode == '0') {
						that.$set(that, `form`, res.data)
					} else {
						uni.showToast({
							title: res.errmsg,
							icon: 'none'
						});
					}
				}
			},
			// 图片上传
			uplSuc(e) {
				const that = this;
				that.$set(that.form, e.name, [e.data]);
			},
			// 图片删除
			uplDel(e) {
				const that = this;
				that.$set(that.form, e.name, [])
			},
			// 性别选择
			genderChange(e) {
				const that = this;
				let data = that.genderList[e.detail.value];
				if (data) that.$set(that.form, `gender`, data.value)
			},
			// 保存
			async onSubmit() {
				const that = this;
				const user = that.user
				const form = that.form
				if (!user) {
					uni.showToast({
						title: "缺少用户信息 无法保存！",
						icon: 'none'
					})
					return
				}
				let res;
				if (user.role == 'Doctor') res = await that.$api(`/doctor/${form._id}`, 'POST', form)
				else if (user.role == 'Nurse') res = await that.$api(`/nurse/${user._id}`, 'POST', form)
				else res = await that.$api(`/patient/${user._id}`, 'POST', form)
				if (res.errcode == '0') {
					uni.showToast({
						title: '维护信息成功',
						icon: 'none'
					})
					that.search()
				} else {
					uni.showToast({
						title: res.errmsg,
						icon: 'none'
					});
				}
			},
		}
	}
</script>

<style lang="scss" scoped>
	.main {
		.one {
			display: flex;
			justify-content: center;
			padding: 2vw;
		}

		.two {
			padding: 2vw;

			.two_1 {
				display: flex;
				justify-content: space-between;
				padding: 4vw;
				border-bottom: 1px solid var(--f9Color);
				font-size: var(--font14Size);
				color: var(--f69Color);

				.right {
					display: flex;
					align-items: center;

					input {
						text-align: right;
						padding: 0 1vw;
					}
				}
			}
		}

		.thr {
			text-align: center;

			.button {
				margin: 2vw 0 0 0;
				background-color: var(--f3CColor);
				color: var(--mainColor);
				font-size: var(--font14Size);
			}
		}
	}
</style>