/**
* v_index.js
* Version: 0.1
* User: zjx
* Date: 2017-09-26
* 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-button','u-dialog','u-loading']);
    uPublic.closeSlideBack();
    const app = new Vue({
        data:{          
            //配置组件
            nickname: null,
            componentsConfig: initComponentsConfig()   
        },
        mounted(){
        	let self  = this;
        	getInfo(this);
			window.addEventListener('returnPage',function(event){
				console.log("in");
				getInfo(self);
			});
			let wv = iot.navigator.getCurrentWebView();
			console.log(wv);
        },
        methods:{
        	//tap 修改昵称
        	onEditNicknameTap(){
        		editNicknameTap(this);
        	},
        	//tap 修改密码
        	onEditPasswordTap(){
        		editPasswordTap();
        	},
        	onBackTap(){
        		backTap();
        	},
        	//tap 退出系统
            onLogoutButtonTap(){
                logoutButtonTap(this);
            }
        }   
    }).$mount('#app');
}

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

//tap 修改昵称
function editNicknameTap(self){
	iot.navigator.openWindow({
        url: '../myInfo/editNickname.html',
        id: 'editNickname',
		extras: {
			nickname: self.nickname
		}
    });
}

//tap 修改密码
function editPasswordTap(){
	iot.navigator.openWindow({
        url: '../myInfo/editPassword.html',
        id: 'editPassword'
    });
}

function backTap(){
	iot.navigator.back();
}

function getInfo(self){
	iot.business.user.getInfo({
	    success: (response) => {
	    	let data = uPublic.checkResponseData(response.data);
			if(data){
				console.log(data);
	    		self.nickname = data.nickname;
			}else{
				
			}
	    },
	    error: (error) => {
	    	console.log(error);
			uPublic.openRequestErrorAlert(self);
	    },
	    complete: () => {}
	});
}

//tap 退出系统
function logoutButtonTap(self){
    uComponents.openConfirm(self, Vue.t('myInfo.logoutDialogTip'), 
       {text: Vue.t('dialog.cancel'), callback: function () {}}, 
       {text: Vue.t('dialog.confirm'), callback: function () {
			iot.business.user.logout({
			    data: {
//			    	sds: false
			    },
			    success: (response) => {
			    	console.log("sdsLogout:"+response);
			    	let sdsResponse = response;
	    			iot.business.websql.delMaps(['utoken', 'userinfo'], (response) => {
				        iot.storage.delMaps(['historyId','history'], (response) => {
				        	console.log("clearMap"+response);
				        	//打开首页
							iot.navigator.openWindow({
						        url: '../login/index.html',
						        id: 'login',
						        extras: {
						        	needClose: true
						        }
						    });
					    }, (error) => {
					    	console.log(error);
							uPublic.openRequestErrorAlert(self);
					    });
				    }, (error) => {
				    	console.log(error);
						uPublic.openRequestErrorAlert(self);
				    });
			    },
			    error: (error) => {
			    	console.log(error);
					uPublic.openRequestErrorAlert(self);
			    },
			    complete: () => {}
			});
       }}
    );
}