/**
* v_login.js
* Version: 0.1
* User: wy
* Date: 2017-09-06
* 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:{  
            numberTel: null,
            textPassword: null,  
            textErrorTip: null,
            //配置组件
            componentsConfig: initComponentsConfig()   
        },
        mounted(){           
			iot.business.sds.init({
			    data: {
			        oa: true
			    },
			    success: (response) => {
			    	console.log(response);
			    	iot.business.user.login({
					    data: {
					        sds: true
					    },
					    success: (response) => {
					    	console.log(response);
					    	let data = uPublic.checkResponseData(response.data);
					    	if(data){
					    		iot.business.user.login({
								    data: {
								        username: data.UserId,
								        pwd: data.UserId,
								    },
								    success: (response) => {
								    	let data = uPublic.checkResponseData(response.data);
					    				if(data){
					    					iot.navigator.openWindow({
										        url: '../device/index.html',
										        id: 'device'
										    });
					    				}else{
					    					
					    				}
								    },
								    error: (error) => {
								    	
								    },
								    complete: () => {}
								});
					    	}else{
					    		
					    	}
					    },
					    error: (error) => {
					    	console.log(error);
					    },
					    complete: () => {}
					});
			    },
			    error: (error) => {
			    	console.log(error);
			    }
			});
        },
        methods:{
            onTelChange(number){
                telChange(this,number);           
            },
            onPasswordChange(text){
                passwordChange(this,text);            
            },        
            //tap 登录
            onLoginButtonTap(){
                loginButtonTap();
            },
            //tap 快速注册
            onRegisterTap(){
                registerTap();
            }, 
            //tap 忘记密码
            onForgetPasswordTap(){
                forgetPasswordTap();
            }                      
        }   
    }).$mount('#app');
}

//配置组件参数
function initComponentsConfig() {
    return {
        telInput: {
            initParam: {
                class: 'custom-text',
                icon: '&#xe6e8;',
                placeholder: Vue.t('login.telInputTip'),
                maxlength: 11,
                minlength: 11
            }
        },
        passwordInput: {
            initParam: {
                class: 'custom-password-text',
                icon: '&#xe64d;',
                placeholder: Vue.t('login.passwordInputTip'),
                type: 'password'
            }
        },
        //登录 button 参数
        loginButton: {   
            initParam: {
                class: 'custom-button',
                text: Vue.t('btn.login')
            }
        },      
        dialog: {
            initParam: {
                class: 'custom-dialog'
            }
        },
        loading: {
            initParam: {
                class: 'custom-loading'
            }
        }               
    }
}

function telChange(self,number){
    self.numberTel = number;
    console.log('telChange: ' + self.numberTel);
}

function passwordChange(self,text){
    self.textPassword = text;
    console.log('passwordChange: ' + self.textPassword);
}

//tap 登录
function loginButtonTap(){
    iot.navigator.openWindow({
        url: '../device/index.html',
        id: 'device'
    });
}

//tap 快速注册
function registerTap(){
	
//  iot.navigator.openWindow({
//      url: './register.html',
//      id: 'register'
//  });
}

//tap 忘记密码
function forgetPasswordTap(){
    iot.navigator.openWindow({
        url: './forgetPassword.html',
        id: 'forgetPassword'
    });
}