/**
* 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()   
        },
        mounted(){
//          setTimeout(function(){
//              iot.navigator.openWindow({
//                  url: './login.html',
//                  id: 'login'
//              });
//          },2000)   
			let self = this;
        },
        methods:{
        	onLoginButtonTap(){
        		loginButtonTap(this);
        	}
        }   
    }).$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);
			    	let data = uPublic.checkResponseData(response.data);
			    	if(data){
			    		console.log("云端登陆");
			    		cloudsLogin(self, data.UserID);
//			    		iot.navigator.openWindow({
//					        url: '../device/index.html',
//					        id: 'device'
//					    });
			    	}else{
			    		
			    	}
			    },
			    error: (error) => {
			    	console.log(error);
			    	//请求失败
					uPublic.openRequestErrorAlert(self);
			    },
			    complete: () => {}
			});
	    },
	    error: (error) => {
	    	console.log(error);
	    	//请求失败
			uPublic.openRequestErrorAlert(self);
	    }
	});
}

//云端登陆
function cloudsLogin(self, id){
	iot.business.user.autoLogin({
	    success: (response) => {
	    	console.log(response);
	    	let data = uPublic.checkResponseData(response.data);
			if(data){
				uComponents.hideLoading(self);
				iot.navigator.openWindow({
			        url: '../device/index.html',
			        id: 'device'
			    });
			}else{
				
			}
	    },
	    error: (error) => {
	    	console.log(error);
	    	iot.business.user.login({
		    data: {
		        username: id,
		        pwd: id,
		    },
		    success: (response) => {
		    	console.log(response);
		    	let data = uPublic.checkResponseData(response.data);
				if(data){
					uComponents.hideLoading(self);
					iot.navigator.openWindow({
				        url: '../device/index.html',
				        id: 'device'
				    });
				}else{
					
				}
		    },
		    error: (error) => {
		    	console.log(error);
		    	//不存在此用户
//		    	if(code == 10002){
		    		//用户注册
		    		iot.business.user.reg({
					    data: {
					        username: id,
					        pwd: id,
					        vcode: id
					    },
					    success: (response) => {
					    	console.log(response);
					    	//注册成功
					    	let data = uPublic.checkResponseData(response.data);
							if(data){
								uComponents.hideLoading(self);
								iot.navigator.openWindow({
							        url: '../device/index.html',
							        id: 'device'
							    });
							}else{
								
							}
					    },
					    error: (error) => {
					    	console.log(error);
					    	//注册失败
					    	uComponents.openAlert(self, Vue.t('login.failure'), {
						        text: Vue.t('dialog.confirm'), callback: function () {
						        	plus.webview.currentWebview().reload();
						        }
						    });
					    },
					    complete: () => {}
					});
//		    	}
		    	//请求失败
				uPublic.openRequestErrorAlert(self);
		    },
		    complete: () => {}
		});
	    },
	    complete: () => {}
	});
//	
}
