<template>

	<view style="padding: 20px;">

		<sms-box v-model="postForm" ref="smsBox" :type="type == '1'?1:2"></sms-box>
		<view>
			<button v-if="type == '1'" type="primary" @click="doBind">确认绑定</button>
			<button v-if="type == '0'" type="warn" @click="unBind">确认解绑</button>
		</view>

	</view>

</template>

<script>
	import {
		bindMobile,
		unBindMobile
	} from '@/api/sys/user/bind'
	import SmsBox from '@/pages/login/components/SmsBox.vue'

	export default {
		components: {
			SmsBox
		},
		data() {
			return {
				type: null,

				postForm: {
					mobile: '',
					smsCode: '',
					captchaKey: '',
					captchaValue: ''
				}
			}
		},

		onLoad: function(option) {

			this.type = option.type;
			this.postForm.mobile = option.mobile;
		},

		methods: {

			// 绑定手机
			async doBind() {
				
				// 消息框
				const smsCheck = await this.$refs.smsBox.validate().then(res=>{return res});
				
				console.log('smsCheck', smsCheck)
				
				if(!smsCheck){
					return;
				}
			
				uni.showLoading({
					title: '加载中..'
				})

				bindMobile(this.postForm).then(() => {
					this.handleSuccess('手机号绑定成功！')
				}).catch(() => {
					uni.hideLoading()
				})
				
			},

			// 解绑手机
			async unBind() {
				
				// 消息框
				const smsCheck = await this.$refs.smsBox.validate().then(res=>{return res});
				
				console.log('smsCheck', smsCheck)
				
				if(!smsCheck){
					return;
				}
				

				uni.showLoading({
					title: '加载中..'
				})
				
				unBindMobile(this.postForm).then(() => {
					this.handleSuccess('手机号解绑成功！')
				}).catch(() => {
					uni.hideLoading()
				})
			
			},


			handleSuccess(msg) {
				uni.hideLoading()
				uni.showToast({
					title: msg,
					icon: 'none'
				})
				
				setTimeout(()=>{
					uni.navigateBack()
				}, 1500)
				
			}

		}
	}
</script>

<style scoped>

</style>
