/**
* v_editNickname.js
* Version: 0.1
* User: zjx
* Date: 2017-09-29
* 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-text']);
    const app = new Vue({
        data:{          
            //配置组件
            textNickName: iot.navigator.getExtras().nickname,
            textErrorTip: null,
            componentsConfig: initComponentsConfig()   
        },
        mounted(){
        },
        methods:{
        	getTextNickName(){
        		return this.textNickName;
        	},
        	setTextNickName(text){
        		this.textNickName = this;
        	},
        	onSaveTap(){
        		saveTap(this);
        	},
        	onBackTap(){
        		backTap();
        	},
        	onNicknameChange(text){
        		nicknameChange(this, text);
        	}
        }   
    }).$mount('#app');
}

//配置组件参数
function initComponentsConfig() {
    return {
    	nameInput: {
            initParam: {
                class: 'custom-editNickname-text',
                placeholder: Vue.t('editNickname.nickNameInputTip')
            }
        },
        //保存 button 参数
        saveButton: {   
            initParam: {
                class: 'custom-button',
                text: Vue.t('btn.save')
            }
        },
        dialog: {
            initParam: {
                class: 'custom-dialog'
            }
        },
        loading: {
            initParam: {
                class: 'custom-loading'
            }
        }
    }
}


//保存昵称
function saveTap(self){
	if(self.textNickName == null || self.textNickName.trim() == ''){
		self.textErrorTip = Vue.t('editNickname.nickNameInputTip');
	}else{
		uComponents.showLoading(self);
		iot.business.user.setInfo({
		    data: {
		        nickname: self.textNickName,
		        head: '',
		        info: {}
		    },
		    success: (response) => {
		    	if(response.data = 'success'){
					iot.navigator.fire(plus.webview.currentWebview().opener(),'returnPage',{ });
		    		iot.navigator.back();
		    	}
		    },
		    error: (error) => {
		    	console.log(error);
		    },
		    complete: () => {
				uComponents.hideLoading(self);
		    }
		});
	}
}

//返回上一页
function backTap(){
	iot.navigator.back();
}

function nicknameChange(self, text){
	self.textNickName = text;
	self.textErrorTip = '';
}
