/**
* v_qrcode.js
* Version: 0.1
* User: wy
* Date: 2017-09-06
* Copyright(c)  2017. U-GEN Tech.Co,Ltd. All Rights Reserved.
* 生成绑定二维码页面
*/

var QRCode = require('qrcode');
console.log(QRCode);
import {iot, uPublic, uComponents} from '../../public/public.js';
iot.ready(init);
function init() {
    //通用 按钮、dialog、loading组件
    uPublic.componentsInit(['u-button','u-dialog','u-loading']);
    uPublic.closeSlideBack();
    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);
        },
        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.qrcode')
            }
        },
        dialog: {
            initParam: {
                class: 'custom-dialog'
            }
        },
        loading: {
            initParam: {
                class: 'custom-loading'
            }
        }                
    }
}

//tap <
function backTap(){
    iot.navigator.back();
}

//tap 生成二维码
function qrcodeButtonTap(self){
	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 qr = self.$refs.qrcode;
				QRCode.toCanvas(qr, qrKey, {
					scale: 5,
					margin: 2
				},(error) => {
			        if (error) {
			          console.log(error)
			        } else {
			          console.log('success');
			          uComponents.hideLoading(self);
			          qr.style.width = "5.280rem";
			          qr.style.height = "5.280rem";
			          console.log(qr.style);
			          setTimeout(() => {
			          	self.setQrCodeDisabled(true);
			          },1000*60*10);
			        }
			    });
	    	}else{}
	    },
	    error: (error) => {
	    	console.log(error);
//	    	if(window.iotDebug){
//              iotDebug.push('end: 生成二维码失败'+JSON.stringify(error));
//         	}
//	    	if (window.iotDebug) {
//              iotDebug.upload(iot,'生成二维码失败');
//          }
	    	//请求失败
    		uPublic.openRequestErrorAlert(self);
	    }
	});
}