/**
* 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() {
	console.log("init" + new Date().getTime());
    //通用 dialog、loading组件
    uPublic.componentsInit(['u-dialog','u-loading']);
//  uPublic.closeSlideBack();
    const app = new Vue({
        data:{            
            //配置组件
            componentsConfig: initComponentsConfig(),
            barcode: null 
        },
        mounted(){  
        	let self = this;
        	if (plus.os.name == 'Android') {
                iot.permission.checkPermission(['CAMERA'], function (res) {
                    console.log('success', res);
                    console.log("openScanCode"+ new Date().getTime());
                    openScanCode(self);
                }, function (err) {
                    console.log('error', err);
                });
            }else{
            	openScanCode(self);
            }
        },
        methods:{
            //tap <
            onBackTap(){
                backTap(this);
            }                  
        }
    }).$mount('#app');
}

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

//tap <
function backTap(self){
    iot.navigator.back();
    console.log(self.barcode);
    iot.utils.closeScanBarcode(self.barcode);
}

function openScanCode(self){
	self.barcode = iot.utils.scanBarcode({
	    data: {
	        id: 'bcid',
	        styles: {
	        	frameColor: '#00ffff',
	        	scanbarColor: '#00ffff',
	        	background: '#3a3d54'
	        }
	    },
	    success: (response) => {
	    	console.log("openScanCodeSuccess"+ new Date().getTime());
	    	console.log(response);
	    	let data = uPublic.checkResponseData(response.data);
	    	if(data){
	    		//扫描成功
	    		let result = JSON.parse(data.result);
		    	let qrKey = result.qrKey;
		    	let lockId = result.lockId;
		    	let relId = result.relId;
		    	//绑定设备
	        	iot.business.sds.scanqr({
				    data: {
				        qrKey: qrKey
				    },
				    success: (response) => {
					    console.log(response);
				    	let data = uPublic.checkResponseData(response.data);
	    				if(data){
					    	//云端绑定
					    	deviceBind(self, lockId, relId);
						}else{}
				    },
				    error: (error) => {
				    	//绑定失败
				    	console.log(error);
//					    if(window.iotDebug){
//			                iotDebug.push('end: 绑定失败'+JSON.stringify(error));
//			           	}
//				    	if (window.iotDebug) {
//			                iotDebug.upload(iot,'绑定失败');
//			            }
    					uComponents.openAlert(self, Vue.t('addDevice.bindFailure'), {
					        text: Vue.t('dialog.confirm'), callback: function () {
					        }
					    });
				    }
				});
	    	}else{}
	    },
	    error: (error) => {
	    	//请求失败
	    	console.log(error);
//  		if(window.iotDebug){
//              iotDebug.push('end: 扫描失败'+JSON.stringify(error));
//         	}
//	    	if (window.iotDebug) {
//              iotDebug.upload(iot,'扫描失败');
//          }
    		uPublic.openRequestErrorAlert(self);
	    }
	});
}

function deviceBind(self, lockId, relId){
	iot.business.api.sendCustom('lock/bindUser',{
	    data: {
	        device_id: lockId,
	        rel_id: relId,
//		    mac: parameters.mac,
	        nickname: ''
	    },
	    success: (response) => {
	    	console.log(response);
	    	let data = response.data;
			if(data.success){
				uComponents.openAlert(self, Vue.t('addDevice.bindSuccess'), {
			        text: Vue.t('dialog.confirm'), callback: function () {
			        	iot.navigator.fire(plus.webview.getWebviewById('device'),'returnPage',{
			        		needClose: true
			        	});
			        	iot.navigator.openWindow({
					        url: '../device/index.html',
					        id: 'device'
					    });
			        }
			    });
			}
	    },
	    error: (error) => {
	    	console.log(error);
//	    	if(window.iotDebug){
//              iotDebug.push('end: 云端绑定失败'+JSON.stringify(error));
//         	}
//	    	if (window.iotDebug) {
//              iotDebug.upload(iot,'云端绑定失败');
//          }
	    	uComponents.openAlert(self, Vue.t('addDevice.bindFailure'), {
		        text: Vue.t('dialog.confirm'), callback: function () {
		        }
		    });
	    },
	    complete: () => {}
	});
}
