/**
* v_addUser.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-button','u-dialog','u-loading']);
    uPublic.closeSlideBack();
    const app = new Vue({
        data:{  
            textName: null,  
            textErrorTip: '',
            //配置组件
            componentsConfig: initComponentsConfig(),
            extras: {}
        },
        mounted(){       
        	this.extras = iot.navigator.getExtras();
        	console.log(this.extras);
        },
        methods:{
        	getTextName(){
        		return this.textName;
        	},
        	setTextName(text){
        		this.textName = text;
        	},
        	getTextErrorTip(){
        		return this.textErrorTip;
        	},
        	setTextErrorTip(tip){
        		this.textErrorTip = tip
        	},
            //tap <
            onBackTap(){
                backTap();
            },            
            onNameChange(text){
                nameChange(this,text);            
            },        
            //tap 确定
            onConfirmButtonTap(){
                confirmButtonTap(this);
            }                      
        }   
    }).$mount('#app');
}

//配置组件参数
function initComponentsConfig() {
    return {
        nameInput: {
            initParam: {
                class: 'custom-text',
                icon: '&#xe6b0;',
                placeholder: Vue.t('addUser.nameInputTip'),
                maxlength: config.input.maxlength
            }
        },
        //确定 button 参数
        confirmButton: {   
            initParam: {
                class: 'custom-button',
                text: Vue.t('btn.confirm')
            }
        },      
        dialog: {
            initParam: {
                class: 'custom-dialog'
            }
        },
        loading: {
            initParam: {
                class: 'custom-loading'
            }
        }               
    }
}

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

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

//tap 确定
function confirmButtonTap(self){
	if(self.getTextName() == null || self.getTextName().trim() == ''){
		self.setTextErrorTip(Vue.t('addUser.error.nameCanNotEmpty'));
	}else if(uPublic.checkString(self.getTextName())){
		self.setTextErrorTip(Vue.t('textTip.textType'));
	}else if(self.getTextErrorTip() == ''){
		uComponents.showLoading(self);
		iot.business.api.sendCustom('lock/addLockUser',{
			data: {
				device_id: self.extras.deviceId,
				nickname: self.getTextName()
			},
			success: (response) => {
				console.log(response);
				if(response.data.id){
					uComponents.openAlert(self, Vue.t('addUser.addUserSuccess'), {
			        text: Vue.t('btn.confirm'), callback: function () {
			        		console.log("添加用户成功");
	      				//跳转到门锁用户界面
			        		iot.navigator.openWindow({
						        url: './doorlockUser.html',
						        id: 'doorlockUser',
						        extras: {
						        	id: response.data.id,
						        	lockId: self.extras.lockId,
						        	uuid: self.extras.uuid,
						        	role: self.extras.role
						        }
						    });
				        }
			    	});
				}
			},
			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('addUser.addUserFailure'), {
		        text: Vue.t('btn.confirm'), callback: function () {
		        		console.log("添加用户失败");
			        }
		    	});
			},
			complete: () => {
				uComponents.hideLoading(self);
			}
		});
	}
}
