/**
* v_wifiAdd.js
* Version: 0.1
* User: wy
* Date: 2017-09-07
* Copyright(c)  2017. U-GEN Tech.Co,Ltd. All Rights Reserved.
* 连接Wi-Fi页面
*/

import {iot, uPublic, uComponents} from '../../public/public.js';

iot.ready(init);
function init() {
    //通用 input、按钮、dialog、loading组件
    uPublic.componentsInit(['u-text','u-button','u-commstatus-loading','u-dialog','u-loading']);
    const app = new Vue({
        data:{     
            textPassword: null, 
            textErrorTip: null, 
            textLoading: 'loading...',
            status: 0,       
            //配置组件
            componentsConfig: initComponentsConfig()   
        },
        mounted(){                 
        },
        methods:{
            getTextLoading(){
                return this.textLoading;
            },
            setTextLoading(text){
                this.textLoading = text;
            },
            getStatus(){
                return this.status;
            },
            setStatus(value){
                this.status = value;
            },
            onPasswordChange(text){
                passwordChange(this,text);            
            },            
            //tap <
            onBackTap(){
                backTap();
            },
            //tap 连接
            onConnectButtonTap(){
                connectButtonTap(this);    
            },
            //tap loading button
            onCommstatusLoadingButtonTap(){
                commstatusLoadingButtonTap(this);
            }                           
        }   
    }).$mount('#app');
}

//配置组件参数
function initComponentsConfig() {
    return {
        passwordInput: {
            initParam: {
                class: 'custom-password-text',
                icon: '&#xe64d;',
                placeholder: Vue.t('wifiAdd.passwordInputTip'),
                type: 'password'
            }
        },
        //连接 button 参数
        connectButton: {   
            initParam: {
                class: 'custom-button',
                text: Vue.t('btn.connect')
            }
        }, 
        dialog: {
            initParam: {
                class: 'custom-dialog'
            }
        },
        loading: {
            initParam: {
                class: 'custom-loading'
            }
        },
        ucommstatusloading: {
            initParam: {
                class: 'custom-commstatus-loading',
                buttonText: Vue.t('btn.confirm')
            }
        }                        
    }
}

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

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

//tap 连接
function connectButtonTap(self){ 
    uComponents.showCommstatusloading(self);
    setTimeout(function(){
        self.setTextLoading(Vue.t('commstatusLoading.connectSuccess'));
        setTimeout(function(){
            //绑定成功
            self.setStatus(1);
            self.setTextLoading(Vue.t('commstatusLoading.bindSuccess'));
            //绑定失败
            //self.setStatus(2);
            //self.setTextLoading(Vue.t('commstatusLoading.bindFailed'));
            //uComponents.changeCommstatusButtonText(self,Vue.t('btn.reBound'));
        },3000);
    },3000);
}

//tap loading button
function commstatusLoadingButtonTap(self) {
    //绑定成功
    uComponents.hideCommstatusloading(self);
    iot.navigator.openWindow({
        url: '../device/',
        id: 'device'
    });
    //绑定失败
    //uComponents.hideCommstatusloading(self);
}