/**
* v_remoteOpendoor.js
* Version: 0.1
* User: wy
* Date: 2017-09-06
* Copyright(c)  2017. U-GEN Tech.Co,Ltd. All Rights Reserved.
* 远程开门页面
*/

import CryptoJS from "crypto-js";
import {iot, uPublic, uComponents} from '../../public/public.js';
import crypto from '../../public/crypto.js';

iot.ready(init);
function init() {
    //通用 input、按钮、dialog、loading组件
    uPublic.componentsInit(['u-text','u-button','u-comm-loading','u-dialog','u-loading']);
    const app = new Vue({
        data:{  
            textPassword: null,  
            textErrorTip: null,
            textLoading: 'loading...',
            status: 0,       
            //配置组件
            componentsConfig: initComponentsConfig(),
            //设备uuid
            uuid: null,
            deviceId: null
        },
        mounted(){           
        	resolve(this);
        	let a = {"mac":"B0F89310DDF2","productId":"10002","productSecret":"WABK8XXEEKKLL5MM"};
        },
        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;
            },
            getDeviceId(){
            	return this.deviceId;
            },
            setDeviceId(id){
            	this.deviceId = 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);
	self.setDeviceId(extras.deviceId);
	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);
		console.log(self.getDeviceId());
		let keyIv = crypto.enkey(self.getDeviceId().replace(/:/g,''));
		console.log(keyIv);
		let pwd = crypto.encode(self.getTextPassword(), keyIv);
		console.log(pwd);
//		let msg = crypto.decode(pwd, keyIv);
//		console.log(msg);
		iot.business.sds.setDeviceStatus({
		    data: {
		        uuid: self.getUuid(),
		        setParams: {
		        	'action': {
		        		'value': 'openDoor'
		        	},
		            'remote_open_door': {
		                'value': pwd
		            }
		        }
		    },
		    success: (response) => {
		    	console.log(response);
		    	uComponents.openAlert(self, Vue.t('remoteOpendoor.openDoorSuccess'), {
			        text: Vue.t('dialog.confirm'), callback: function () {
			        	//返回首页
//  					iot.navigator.back();
						iot.navigator.openWindow({
					        url: 'index.html',
					        id: 'device',
					    });	
			        }
			    });
		    },
		    error: (error) => {
		    	console.log(error);
	    		uComponents.openAlert(self, Vue.t('remoteOpendoor.openDoorFailure'), {
			        text: Vue.t('dialog.confirm'), callback: function () {
			        }
				});
			},
		    complete(){
				uComponents.hideLoading(self);
		    }
		});	
	}
}
