/**
* v_inviteBind.js
* Version: 0.1
* User: wy
* Date: 2017-10-09
* Copyright(c)  2017. U-GEN Tech.Co,Ltd. All Rights Reserved.
* 邀请绑定页面
*/

let QRCode = require('qrcode');
import {iot, uPublic, uComponents} from '../../public/public.js';

iot.ready(init);
function init() {
    //通用 按钮、dialog、loading组件
    uPublic.componentsInit(['u-button','u-dialog','u-loading']);
    const app = new Vue({
        data:{  
            //配置组件
            componentsConfig: initComponentsConfig(),
            extras: [],
            uuid: null,
            qrCodeDisabled: false
        },
        mounted(){           
        	this.extras = iot.navigator.getExtras();
        	console.log(this.extras);
        	this.setUuid(this.extras.uuid);
        	qrcodeButtonTap(this);
        },
        methods:{
        	getUuid(){
        		return this.uuid;
        	},
        	setUuid(id){
        		this.uuid = id;
        	},
        	setQrCodeDisabled(boolean){
        		this.qrCodeDisabled = boolean;
        	},
            //tap <
            onBackTap(){
                backTap();
            },          
            //tap 重新生成二维码
            onQRcodeButtonTap(){
                qrcodeButtonTap(this);
            }                      
        }   
    }).$mount('#app');
}

//配置组件参数
function initComponentsConfig() {
    return {
        //重新生成二维码 button 参数
        qrcodeButton: {   
            initParam: {
                class: 'custom-button',
                text: Vue.t('btn.qrcodeReset')
            }
        },      
        dialog: {
            initParam: {
                class: 'custom-dialog'
            }
        },
        loading: {
            initParam: {
                class: 'custom-loading'
            }
        }               
    }
}

//tap <
function backTap(){
	iot.navigator.fire(plus.webview.currentWebview().opener(),'returnPage',{});
	console.log('returnPage');
    iot.navigator.back();
}

//tap 重新生成二维码
function qrcodeButtonTap(self){
	self.setQrCodeDisabled(false);
	uComponents.showLoading(self);
	iot.business.sds.saveqr({
	    data: {
	        uuid: self.getUuid()
	    },
	    success: (response) => {
	    	let data = uPublic.checkResponseData(response.data);
	    	if(data){
	    		let qrKey = response.data.qrKey;
	    		let data = {
	    			qrKey: qrKey,
	    			lockId: self.extras.lockId,
	    			relId: self.extras.id
	    		}
	    		data = JSON.stringify(data);
				let qr = self.$refs.qrcode;
				QRCode.toCanvas(qr, data, {
					scale: 5,
					margin: 2
				},(error) => {
			        if (error) {
			          console.log(error)
			        } else {
			          console.log('success');
			          uComponents.hideLoading(self);
			          qr.style.width = "5.333rem";
			          qr.style.height = "5.333rem";
			          console.log(qr.style);
			          setTimeout(() => {
			          	self.setQrCodeDisabled(true);
			          },1000*60*10);
			        }
			    });
	    	}else{}
	    },
	    error: (error) => {
	    	console.log(error);
	    	//请求失败
    		uPublic.openRequestErrorAlert(self);
	    }
	});
	setTimeout(() => {
		self.setQrCodeDisabled(true);
	},config.qrcode_timeout)
}