<template>
	<view class="main">
		<view class="form">
			<view class="form_group">
				<view class="lable">绑定方式</view>
				<picker :range="bindType" @change="picker($event, 'type')">
					<input placeholder="请选择" :value="bindType[item.type]" :disabled="true" />
					<view class="icon more">&#xe62b;</view>
				</picker>
			</view>
			<view class="form_group">
				<view class="lable">姓名</view>
				<input v-model="item.name" placeholder="请输入姓名" />
			</view>
			<view class="form_group">
				<view class="lable">{{ item.type == 0 ? '手机号' : '身份证号' }}</view>
				<input type="tel" v-model="item.value" placeholder="请输入手机号" v-if="item.type == 0" />
				<input type="idcard" v-model="item.value" placeholder="请输入身份证" v-else />
			</view>
			<view class="form_group">
				<view class="lable">就诊人关系</view>
				<picker :range="relationship" range-key="dictLabel" @change="picker($event, 'relationship')">
					<input placeholder="请选择" v-model="item.relationship" :disabled="true" />
					<view class="icon more">&#xe62b;</view>
				</picker>
			</view>
		</view>
		<button class="btn" @click="add()">确认</button>
	</view>
</template>

<script>
export default {
	data() {
		return {
			item: { type: 1, relationship: '自己' },
			bindType: ['手机号', '身份证'],
			relationship: []
		};
	},
	onLoad() {
		this.getRelationship();
	},
	methods: {
		picker(e, tag) {
			if (tag == 'type') {
				this.item.type = e.detail.value;
			}
			if (tag == 'relationship') {
				this.item.relationship = this.relationship[e.detail.value].dictLabel;
			}
			this.$forceUpdate();
		},
		getRelationship() {
			this.http.request({
				url: '/app/common/type/nk_kinship',
				loading: 'false',
				success: (res) => {
					this.relationship = res.data.data;
				}
			});
		},
		add() {
			let rule = [
				{ name: 'name', checkType: 'notnull', errorMsg: '请输入姓名' },
				{ name: 'value', checkType: 'notnull', errorMsg: this.item.type == 0 ? '请输入手机号' : '请输入身份证' }
			];
			if (!this.verify.check(this.item, rule)) {
				uni.showModal({ content: this.verify.error, showCancel: false });
				return false;
			}
			//通知模板订阅消息
			uni.requestSubscribeMessage({
				tmplIds: ['1Jvx8F22na-tG2Q6HFX_3vRtbiv7zZko6NwX8ICIFXc', 'S6JARkInVBjID8QMpgZdSHNyxeMDZQq7lur8YrNK0oY'],
				complete: (c) => {
					this.http.request({
						url: '/app/user/bind',
						method: 'POST',
						data: this.item,
						success: (res) => {
							uni.showModal({
								title: '提示',
								content: '绑定成功',
								showCancel: false,
								success: (res) => {
									uni.$emit('bind');
									uni.navigateBack();
								}
							});
						}
					});
				}
			});
		}
	}
};
</script>

<style lang="scss"></style>
