/**
* v_editName.js
* Version: 0.1
* User: wy
* Date: 2017-09-06
* 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-button','u-dialog','u-loading']);
    uPublic.closeSlideBack();
    const app = new Vue({
        data:{
            textName: iot.navigator.getExtras().nickname,
            textErrorTip: null,
            //配置组件
            componentsConfig: initComponentsConfig(),
            extras: {}
        },
        mounted(){     
        	this.extras = iot.navigator.getExtras();
        	console.log(this.extras);
        },
        methods:{
        	getTextName(){
        		return this.textName;
        	},
        	setTextName(text){
        		this.textName = text;
        	},
        	setTextErrorTip(text){
        		this.textErrorTip = text;
        	},
            onNameChange(text){
                nameChange(this,text);           
            },
            //tap <
            onBackTap(){
                backTap();
            },
            //tap 保存
            onSaveTap(){
            	saveTap(this);
            }                    
        }   
    }).$mount('#app');
}

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

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

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

////重写mui.back
//mui.back = function(){
//  iot.navigator.fire(plus.webview.currentWebview().opener(),'returnPage',{});
//  console.log("fire");
//	iot.navigator.back();
//}

//tap 保存
function saveTap(self){
	if(self.getTextName() == null || self.getTextName().trim() == ''){
		self.setTextErrorTip(Vue.t('editName.error.nameCanNotBeBlank'));
	}else{
		uComponents.showLoading(self);
		iot.business.device.setName({
		    data: {
		        device_id: self.extras.lockId,
		        device_name: self.getTextName()
		    },
		    success: (response) => {
		    	console.log(response);
		    	let data = uPublic.checkResponseData(response.data);
	    		if(data){
	    			iot.navigator.fire(plus.webview.currentWebview().opener(),'returnPage',{});
					console.log('returnPage');
			    	uComponents.openAlert(self, Vue.t('editName.editNameSuccess'), {
			        text: Vue.t('btn.confirm'), callback: function () {
		    				iot.navigator.back();
				        }
			    	});
	    		}
		    },
		    error: (error) => {
		    	console.log(error);
		    	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);
		    }
		});	
	}
}