<template>
	<view class="userBinding">
		<view class="cInputBox">
			<input class="cInput" v-model="name" type="text" placeholder="请输入就诊人姓名">
		</view>
		<view class="cInputBox">
			<input class="cInput" v-model="idCard" type="idcard" placeholder="请输入就诊人身份证号码">
		</view>
		<view class="marginTop">
			<view class="cInputBox">
				<input class="cInput" v-model="mobile" type="tel" placeholder="请输入绑定人手机号码">
			</view>
			<van-field :value="code" placeholder="请输入短信验证码" clearable @change="codeInput" use-button-slot>
				<button slot="button" :disabled="disabled" class="send" @click="send">{{ text }}</button>
			</van-field>
		</view>
		
		<!-- <van-field :value="name" placeholder="请输入就诊人姓名" clearable @change="nameInput" />
		<van-field :value="idCard" type="idcard" placeholder="请输入就诊人身份证号码" clearable @change="userIdInput" />
		<view class="marginTop">
			<van-field  :value="mobile" type="number" placeholder="请输入就诊人手机号码" clearable @change="numberInput" />
			<van-field :value="code" placeholder="请输入短信验证码" clearable @change="codeInput" use-button-slot>
				<button slot="button" :disabled="disabled" class="send" @click="send">{{ text }}</button>
			</van-field>
		</view> -->
		<view class="bind bindActive" @click="bind">绑定</view>
		<van-toast id="van-toast" />
	</view>
</template>

<script>
	import IdentityCodeValid from '@/utils/validateIdent'
	import Toast from '@/wxcomponents/vant/toast/toast';
	import sendVerifyCode from "@/mixins/SendVerifyCode";
	import { getCode, login, sendCode, bindMobile, getFieldConfig, getDataConfig } from '@/api/api.js';
	import {mapGetters,mapMutations} from 'vuex'
	export default {
		mixins:[sendVerifyCode],
		data(){
			return{
				name:'',
				idCard:'',
				mobile:'',
				code:'',
				aespass:''
			}
		},
		onLoad(){
			this.getCode()
		},
		methods:{
			...mapMutations(['setIsBand','setConfigList', 'setFiledList', 'setOrg']),
			nameInput(event){
				this.name = event.detail
			},
			userIdInput(event){
				this.idCard = event.detail
			},
			numberInput(event){
				this.mobile = event.detail
			},
			codeInput(event){
				this.code = event.detail
			},
			send(){
				if (!this.mobile) return Toast('请填写手机号码');
				if (!/^1(3|4|5|7|8|9|6)\d{9}$/i.test(this.mobile)) return Toast('请输入正确的手机号码')
				this.sendCodes()
			},
			// 获取验证码前获取密钥
			async getCode(){
				let res = await getCode({}, true)
				// console.log(res,'res')
				this.aespass = res.data.data.aespass
				
			},
			async sendCodes(){
				let mobile = this.mobile
				let aespass = this.aespass
				let params = {
					phone: mobile.toString(),
					aespass: aespass
				}
				this.sendCode()
				let res = await sendCode(params, true)
				console.log(res,'oop')
			},
			async bind(){
				if (!this.name) return Toast('请填写患者姓名');
				let data = this.idCard.toUpperCase() //转换成大写
				// if(!IdentityCodeValid( data )){
				// 	Toast('请输入正确的身份证号')
				// 	return
				// }
				if (!this.mobile) return Toast('请填写手机号码');
				if (!/^1(3|4|5|7|8|9|6)\d{9}$/i.test(this.mobile)) return Toast('请输入正确的手机号码')
				if (!this.code) return Toast('请填写验证码');
				
				let params = {
					name: this.name,
					id_card_no:this.idCard,
					mobile:this.mobile,
					code:this.code,
					openid:'opE6O5ODuaN9U6viDrAlGH87h6yM',
					relationship:'父子'
				}
				let res = await bindMobile(params ,true)
				console.log('res',res)
				if(res.data.data.result){
					Toast('验证码错误')
				}else{
					if(res.data.data.is_bind == true){
						let data = { 
							is_bind:true,
							userInfo:res.data.data.patient
						}
						this.setIsBand(data)
						// if(res.data.data.patient.id > 0){
						// 	this.getDataConfig(res.data.data.patient.id)
						// 	this.getFieldConfig(res.data.data.patient.id)
						// }
						if(res.data.data.role.patient_id > 0){
							this.getDataConfig(res.data.data.role.patient_id)
							this.getFieldConfig(res.data.data.role.patient_id)
						}
						// if(res.data.data.template_id.id > 0){
						// 	let data = {
						// 		org: res.data.data.template_id
						// 	}
						// 	this.setOrg(data)
						// }
						Toast('绑定成功')
						console.log()
						setTimeout(() => {
							uni.navigateBack()
						},1000)
					}else{
						Toast(res.data.data.msg)
					}
				}
				
			},
			async getFieldConfig(patient_id){
				let params = {
					patient_id: patient_id
				}
				let res = await getFieldConfig(params)
				let data = {
					filedList:res.data.data
				}
				this.setFiledList(data)
			},
			async getDataConfig(patient_id){
				let params = {
					patient_id: patient_id
				}
				let res = await getDataConfig(params)
				let data = {
					configList:res.data.data.list
				}
				this.setConfigList(data)
			},
		}
	}
</script>

<style>
	page{
		height: 100%;
	}
	.cInputBox{
		
		padding: 20rpx 30rpx;
		box-sizing: border-box;
		width: 100%;
		background-color: #fff;
	}
	.cInput{
		height: 48rpx;
		line-height: 48rpx;
		font-size: 28rpx;
		color: #333;
		background-color: #fff;
		
	}
	.userBinding{
		height: 100%;
		background: #f7f7f7;
	}
	.marginTop{
		margin-top: 20rpx;
	}
	.send{
		color: #01BD5D;
		font-size: 30rpx;
		background: #FFFFFF;
	}
	button::after {
	  border: none;
	}
	.bind{
		width: 690rpx;
		height: 90rpx;
		background: #D8D8D8;
		box-shadow: 0px 0px 24rpx 0px rgba(216, 216, 216, 0.4);
		border-radius: 45rpx;
		line-height: 90rpx;
		text-align: center;
		font-size: 36rpx;
		color: #FFFFFF;
		margin: 80rpx auto 0;
	}
	.bindActive{
		background: linear-gradient(-90deg, #01BD5D, #28D87D);
	}
	
</style>
