/**
* v_index.js
* Version: 0.1
* User: wy
* Date: 2017-09-06
* 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','u-button']);
    const app = new Vue({
        data:{          
            //配置组件
            componentsConfig: initComponentsConfig(),
            clientId: null
        },
        mounted(){
			var clientInfo = plus.push.getClientInfo();
			this.setClientId(clientInfo.clientid);
			loginButtonTap(this);
        },
        methods:{
        	onLoginButtonTap(){
        		loginButtonTap(this);
        	},
        	getClientId(){
        		return this.clientId;
        	},
        	setClientId(id){
        		this.clientId = id;
        	}
        }   
    }).$mount('#app');
}

//配置组件参数
function initComponentsConfig() {
    return {
    	//登录 button 参数
        loginButton: {   
            initParam: {
                class: 'custom-guide-button',
                text: Vue.t('btn.guideLogin')
            }
        },        
        dialog: {
            initParam: {
                class: 'custom-dialog'
            }
        },
        loading: {
            initParam: {
                class: 'custom-loading'
            }
        }
    }
}

function loginButtonTap(self){
	uComponents.showLoading(self);
	iot.business.sds.init({
	    data: {
	        oa: true
	    },
	    success: (response) => {
	    	console.log(response);
	    	iot.business.user.login({
			    data: {
			        sds: true
			    },
			    success: (response) => {
//			    	console.log(response);
//					alert(JSON.stringify(response)); 
			    	var data = uPublic.checkResponseData(response.data);
			    	if(data){
			    		if(data.hasOwnProperty('UserID')){
			    			
			    			cloudsLogin(self, data.UserID);
			    		}
//			    		iot.navigator.openWindow({
//					        url: '../device/index.html',
//					        id: 'device'
//					    });
			    	}else{
			    		
			    	}
			    },
			    error: (error) => {
			    	console.log(error);
		    		if(window.iotDebug){
		                iotDebug.push('end: sds登陆失败'+JSON.stringify(error));
		           	}
			    	if (window.iotDebug) {
		                iotDebug.upload(iot,'sds登陆失败');
		            }
			    	//请求失败
//					uPublic.openRequestErrorAlert(self);
			    },
			    complete: () => {
			    	uComponents.hideLoading(self);
			    }
			});
	    },
	    error: (error) => {
	    	console.log(error);
    		if(window.iotDebug){
                iotDebug.push('end: sds登陆初始化失败'+JSON.stringify(error));
           	}
	    	if (window.iotDebug) {
                iotDebug.upload(iot,'sds登陆初始化失败');
            }
	    	//请求失败
			uPublic.openRequestErrorAlert(self);
	    },
	    complete: () => {
	    	uComponents.hideLoading(self);
	    }
	});
}

function cloudsLogin(self, id){
//	uComponents.showLoading(self);
	iot.business.user.simpleLogin({
		data: {
			username: id,
			clienId: self.getClientId()
		},
		success: (response) => {
//			iot.navigator.openWindow({
//		        url: '../device/index.html',
//		        id: 'device',
//		        styles: {
//		        	'background': 'transparent'
//		        }
//		    });
		    uPublic.openWindow({
		    	url: '../device/index.html',
		        id: 'device',
		        styles: {
		        	'background': 'transparent'
		        }
		    });
		},
		error: (error) => {
			console.log(error);
//  		if(window.iotDebug){
//              iotDebug.push('end: 云端登陆失败'+JSON.stringify(error));
//         	}
//	    	if (window.iotDebug) {
//              iotDebug.upload(iot,'云端登陆失败');
//          }
			uPublic.openRequestErrorAlert(self);
		},
		complete: () => {
			uComponents.hideLoading(self);
		}
	});
}