/**
* v_addUser.js
* Version: 0.1
* User: wy
* Date: 2017-10-09
* Copyright(c)  2017. U-GEN Tech.Co,Ltd. All Rights Reserved.
* 添加用户页面
*/

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']);
    const app = new Vue({
        data:{  
            textName: null,  
            textErrorTip: null,
            //配置组件
            componentsConfig: initComponentsConfig()  
        },
        mounted(){           
        },
        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')
            }
        },
        //确定 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(null);
    console.log('nameChange: ' + self.textName);
}

//tap 确定
function confirmButtonTap(self){
	if(self.getTextName() == null || self.getTextName().trim() == ''){
		self.setTextErrorTip(Vue.t('addUser.error.nameCanNotEmpty'));
	}else{
		uComponents.showLoading(self);
		iot.business.api.sendCustom('lock/addUser',{
			data: {
				device_id: self.extras.deviceId
			},
			success: (response) => {
				console.log(response);
				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: {
					        	userId: response.data.id
					        }
					    });
			        }
		    	});
			},
			error: (error) => {
				console.log(error);
				uComponents.openAlert(self, Vue.t('addUser.addUserFailure'), {
		        text: Vue.t('btn.confirm'), callback: function () {
		        		console.log("添加用户失败");
			        }
		    	});
			},
			complete: () => {
				uComponents.hideLoading(self);
			}
		});
	}
}
