/**
* v_scanCodeAdd.js
* Version: 0.1
* User: wy
* Date: 2017-09-07
* Copyright(c)  2017. U-GEN Tech.Co,Ltd. All Rights Reserved.
* 扫描绑定设备页面
*/

import {iot, uPublic, uComponents} from '../../public/public.js';

iot.ready(init);
function init() {
    //通用 dialog、loading组件
    uPublic.componentsInit(['u-dialog','u-loading']);
    const app = new Vue({
        data:{            
            //配置组件
            componentsConfig: initComponentsConfig()     
        },
        mounted(){  
        	openScanCode(this);
        },
        methods:{
            //tap <
            onBackTap(){
                backTap();
            }                  
        }
    }).$mount('#app');
}

//配置组件参数
function initComponentsConfig() {
    return {
        dialog: {
            initParam: {
                class: 'custom-dialog'
            }
        },
        loading: {
            initParam: {
                class: 'custom-loading'
            }
        }                
    }
}

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

function openScanCode(self){
	iot.utils.scanBarcode({
	    data: {
	        id: 'bcid',
	        styles: {
	        	frameColor: '#00ffff',
	        	scanbarColor: '#00ffff',
	        	background: '#242b35'
	        }
	    },
	    success: (response) => {
	    	let data = uPublic.checkResponseData(response.data);
	    	if(data){
	    		//扫描成功
		    	let qrKey = data.result;
		    	//绑定设备
	        	iot.business.sds.scanqr({
				    data: {
				        qrKey: qrKey
				    },
				    success: (response) => {
				    	let data = uPublic.checkResponseData(response.data);
	    				if(data){
					    	console.log(response);
						    	uComponents.openAlert(self, Vue.t('addDevice.bindSuccess'), {
					                text: Vue.t('btn.confirm'), callback: function () {
					                	iot.navigator.openWindow({
									        url: '../device/',
									        id: 'device'
									    }); 
					                }
					         });
						}else{}
				    },
				    error: (error) => {
				    	//请求失败
    					uPublic.openRequestErrorAlert(self);
				    }
				});
	    	}else{}
	    },
	    error: (error) => {
	    	console.log(error);
	    	//请求失败
    		uPublic.openRequestErrorAlert(self);
	    }
	});
}
