/**
* v_remoteOpendoor.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-commstatus-loading','u-dialog','u-loading']);
    const app = new Vue({
        data:{  
            textPassword: null,  
            textErrorTip: null,
            textLoading: 'loading...',
            status: 0,       
            //配置组件
            componentsConfig: initComponentsConfig(),
            //设备uuid
            uuid: null
        },
        mounted(){           
        	resolve(this);
        },
        methods:{
        	getTextPassword(){
        		return this.textPassword;
        	},
        	setTextPassword(pwd){
        		this.textPassword = pwd;
        	},
        	setTextErrorTip(tip){
        		this.textErrorTip = tip;
        	},
            getTextLoading(){
                return this.textLoading;
            },
            setTextLoading(text){
                this.textLoading = text;
            },
            getStatus(){
                return this.status;
            },
            setStatus(value){
                this.status = value;
            },
            onPasswordChange(text){
                passwordChange(this,text);            
            },
            onPasswordChange(text){
                passwordChange(this,text);            
            },      
            getUuid(){
            	return this.uuid;
            },
            setUuid(id){
            	this.uuid = id;
            },
            //tap <
            onBackTap(){
                backTap();
            },
            //tap 确定
            onConfirmButtonTap(){
                confirmButtonTap(this);
            }                 
        } 
    }).$mount('#app');
}

//配置组件参数
function initComponentsConfig() {
    return {
        passwordInput: {
            initParam: {
                class: 'custom-text',
                icon: '&#xe64d;',
                placeholder: Vue.t('remoteOpendoor.passwordInputTip')
            }
        },
        //确定 button 参数
        confirmButton: {   
            initParam: {
                class: 'custom-button',
                text: Vue.t('btn.confirm')
            }
        },
        dialog: {
            initParam: {
                class: 'custom-dialog'
            }
        },
        loading: {
            initParam: {
                class: 'custom-loading'
            }
        },
        ucommstatusloading: {
            initParam: {
                class: 'custom-commstatus-loading',
                buttonText: Vue.t('btn.confirm')
            }
        }                
    }
}

function resolve(self){
	let extras = iot.navigator.getExtras();
	self.setUuid(extras.uuid);
	console.log(self.getUuid());
}

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

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

//tap 确定
function confirmButtonTap(self){
	console.log(self.getTextPassword());
	if(self.getTextPassword() == null || self.getTextPassword().trim() == ''){
		self.setTextErrorTip(Vue.t('remoteOpendoor.error.noPasswordTip'));	
	}else{
		uComponents.showLoading(self);
		iot.business.sds.setDeviceStatus({
		    data: {
		        uuid: self.getUuid(),
		        setParams: {
		        	'action': {
		        		'value': 'openDoor'
		        	},
		            'remote_open_door': {
		                'value': self.getTextPassword()
		            }
		        }
		    },
		    success: (response) => {
		    	console.log(response);
		    	uComponents.openAlert(self, Vue.t('remoteOpendoor.openDoorSuccess'), {
			        text: Vue.t('dialog.confirm'), callback: function () {
			        	//返回首页
    					iot.navigator.back();
			        }
			    });
		    },
		    error: (error) => {
		    	console.log(error);
	    		uComponents.openAlert(self, Vue.t('remoteOpendoor.openDoorFailure'), {
			        text: Vue.t('dialog.confirm'), callback: function () {
			        }
				});
			},
		    complete(){
				uComponents.hideLoading(self);
		    }
		});	
	}
}
