/**
* v_editName.js
* Version: 0.1
* User: wy
* Date: 2017-10-09
* Copyright(c)  2017. U-GEN Tech.Co,Ltd. All Rights Reserved.
* 修改名称页面
*/
import config from '../../public/config.js';
import {iot, uPublic, uComponents} from '../../public/public.js';

iot.ready(init);
function init() {
    //通用 input、dialog、loading组件
    uPublic.componentsInit(['u-text','u-dialog','u-loading']);
    uPublic.closeSlideBack();
    const app = new Vue({
        data:{  
            textName: iot.navigator.getExtras().nickname,  
            textErrorTip: null,
            //配置组件
            componentsConfig: initComponentsConfig(),
            extras: {},
            role: null
        },
        mounted(){       
        	this.extras = iot.navigator.getExtras();
        	this.role = this.extras.role;
        },
        methods:{
        	getTextName(){
        		return this.textName;
        	},
        	setTextName(text){
        		this.textName = text;
        	},
        	getTextErrorTip(){
        		return this.textErrorTip;
        	},
        	setTextErrorTip(tip){
        		this.textErrorTip = tip;
        	},
            //tap <
            onBackTap(){
                backTap();
            }, 
            //tap 保存
            onSaveTap(){
                saveTap(this);
            },           
            onNameChange(text){
                nameChange(this,text);            
            }                     
        }   
    }).$mount('#app');
}

//配置组件参数
function initComponentsConfig() {
    return {
        nameInput: {
            initParam: {
                class: 'custom-editName-text',
                placeholder: Vue.t('editName.nickNameInputTip'),
                maxlength: config.input.maxlength
            }
        },      
        dialog: {
            initParam: {
                class: 'custom-dialog'
            }
        },
        loading: {
            initParam: {
                class: 'custom-loading'
            }
        }               
    }
}

iot.navigator.aback = function(){
    iot.navigator.fire(plus.webview.currentWebview().opener(),'returnPage',{});
	iot.navigator.back();
}

//tap <
function backTap(){
    iot.navigator.aback();
}

//tap 保存
function saveTap(self){
    if(self.getTextName() == null || self.getTextName().trim() == ''){
    	self.setTextErrorTip(Vue.t('editName.error.nicknameCanNotBeBlank'));
    }else{
    	uComponents.showLoading(self);
		iot.business.api.sendCustom('lock/setLockUserName',{
		    data: {
		        rel_id: self.extras.id,
		        nickname: self.getTextName(),
		        device_id: self.extras.deviceId
		    },
		    success: (response) => {
		    	console.log(response);
		    	if(response.data.success){
		    		uComponents.openAlert(self, Vue.t('editName.editNameSuccess'), {
			        text: Vue.t('btn.confirm'), callback: function () {
							let opener = plus.webview.currentWebview().opener();
							let view = plus.webview.getWebviewById(opener.id);
							console.log(view);
							iot.navigator.aback();
				        }
			    	});
		    	}
//		    	let data = uPublic.checkResponseData(response.data);
//	    		if(data){
//			    	uComponents.openAlert(self, Vue.t('editName.nicknameCanNotBeBlank'), {
//			        text: Vue.t('btn.confirm'), callback: function () {
//							let opener = plus.webview.currentWebview().opener();
//							let view = plus.webview.getWebviewById(opener.id);
//							console.log(view);
//							iot.navigator.fire(plus.webview.currentWebview().opener(),'returnPage',{});
//							console.log('returnPage');
//		    				iot.navigator.back();
//				        }
//			    	});
//	    		}
		    },
		    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('editName.editNameFailue'), {
		        text: Vue.t('btn.confirm'), callback: function () {
		        		let opener = plus.webview.currentWebview().opener();
	    				iot.navigator.back();
			        }
		    	});
		    },
		    complete: () => {
	    		uComponents.hideLoading(self);
		    }
		});
    }
}

function nameChange(self,text){
    self.textName = text;
    self.setTextErrorTip(null);
    console.log('nameChange: ' + self.textName);
}