<template>
	<view class="content">
		<view class="workTitle">
			<view class="workTitle-name">
				{{index == 0 ? '手机号' : '邀请码'}}
			</view>
			<view class="workTitle-text">
				{{index == 0 ? '请输入好友的手机号' : '请输入好友的邀请码'}}
			</view>
		</view>
		<view class="workCont">
			<view class="workPicker">
				<picker class="workPicker-picker" @change="bindPickerChange" :value="index" :range="array" range-key="name">
					<view class="uni-input">{{array[index].name}}</view>
				</picker>
				<image src="@/static/imgs/basic_down.png" mode="aspectFill"></image>
			</view>
			<view class="workPicker">
				<input class="workPicker-input" type="tel" maxlength="11" :placeholder="placeholder" v-model="invite" v-if="index == 0">
				<input class="workPicker-input" type="text" :placeholder="placeholder" v-model="invite" v-else>
			</view>
			<button class="idcardBtn" @click="basicSubmit">
				{{index == 0 ? '校验手机号' : '校验邀请码'}}
			</button>
			<view class="workUser" v-if="userData">
				<image class="workUser-head" :src="userData.avatar ? userData.avatar : '/static/imgs/default_myHead.png'" mode="aspectFill"></image>
				<view class="workUser-cont">
					<view class="workUser-cont-name">{{userData.nickname}}</view>
					<view class="workUser-cont-btn" @click="binding">
						立即绑定
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import { Verify, Bind } from '@/apis/interfaces/index'
	export default {
		data() {
			return {
				array: [
					{
						name: '手机号',
						type: 'mobile'
					},
					{
						name: '邀请码',
						type: 'verify'
					}
				],
				index: 0,
				placeholder: '输入手机号',
				invite : '',
				userData: ''
			}
		},
		onShow() {},
		methods: {
			bindPickerChange(e) {
				this.index = e.detail.value
				if(e.detail.value == 0) {
					this.placeholder = '输入手机号'
				} else {
					this.placeholder = '输入邀请码'
				}
			},
			
			// 提交
			basicSubmit() {
				Verify({
					invite	: this.invite,
					type	: this.array[this.index].type
				}).then(res => {
					this.userData = res
				}).catch(err => {
					uni.showToast({
						title: err.message,
						icon : 'none'
					})
				})
			},
			
			// 立即绑定
			binding() {
				Bind({
					invite	: this.invite,
					type	: this.array[this.index].type
				}).then(res => {
					uni.showToast({
						title: '绑定成功',
						icon : 'none'
					})
					
					setTimeout(()=>{
						// 跳到业务员搜索
						this.$Router.push({name: 'Index'})
					},3000)
				}).catch(err => {
					uni.showToast({
						title: err.message,
						icon : 'none'
					})
				})
			}
		},
	}
</script>

<style lang="scss" scoped>
	.content {
		padding: $padding*4 $padding*3;
		box-sizing: border-box;
	}
	
	.workTitle {
		text-align: center;
		.workTitle-name {
			font-weight: 600;
			font-size: $title-size + 6;
			line-height: 80rpx;
		}
		.workTitle-text {
			color: $text-color;
			font-size: $title-size-m;
		}
	}
	
	.workCont {
		margin: $margin * 2 0;
		.workPicker {
			background-color: #fbfbfb;
			border-radius: $radius-m;
			padding: 0 $padding;
			margin-bottom: $margin + 10;
			height: 88rpx;
			line-height: 88rpx;
			box-sizing: border-box;
			position: relative;
			image {
				width: 28rpx;
				height: 28rpx;
				position: absolute;
				right: $padding;
				top: $padding - 5;
			}
			picker {
				width: 100%;
				height: 100%;
			}
			.workPicker-input {
				height: 88rpx;
				width: 100%;
			}
		}
	}
	
	.workUser {
		background-color: #fbfbfb;
		border-radius: $radius-m;
		padding: $padding;
		box-sizing: border-box;
		display: flex;
		margin-top: $margin * 2;
		.workUser-head {
			width: 80rpx;
			height: 80rpx;
			border-radius: 50%;
		}
		.workUser-cont {
			line-height: 80rpx;
			width: calc(100% - 80rpx);
			padding-left: $padding;
			box-sizing: border-box;
			display: flex;
			.workUser-cont-name {
				font-size: $title-size-m;
				flex: 1;
				font-weight: 600;
			}
			.workUser-cont-btn {
				font-size: $title-size-sm;
				padding: 0 20rpx;
				background-color: #007df5;
				display: inline-block;
				color: #ffffff;
				border-radius: $radius-m;
				height: 62rpx;
				line-height: 62rpx;
				margin-top: 10rpx;
			}
		}
	}
	
	.idcardBtn {
		margin: $margin*2 0 0;
		background-color: $mian-color;
		color: #ffffff;
		border-radius: $radius-lg;
		line-height: 90rpx;
		text-align: center;
		&[disabled] {
			background-color: #eba5a5;
		}
	}
</style>