/**
* v_index.js
* Version: 0.1
* User: wy
* Date: 2017-09-06
* Copyright(c)  2017. U-GEN Tech.Co,Ltd. All Rights Reserved.
* 门锁管理页面
*/

import config from '../../public/config.js';
import defaultConfig from '../../public/defaultConfig.js';
import {iot, uPublic, uComponents} from '../../public/public.js';
import {projectMethods} from '../../public/components.js';

iot.ready(init);
function init() {
    //通用 按钮、dialog、loading组件
    uPublic.componentsInit(['u-button','u-dialog','u-loading','u-switch']);
    //项目 list组件
    uPublic.componentsExtend(['grid-list']);
    uPublic.closeSlideBack();
    console.log(iot.navigator.getExtras());
    const app = new Vue({
        data:{            
            //配置组件
            componentsConfig: initComponentsConfig(),
            //提示框是否显示
            stateShowFlag: false,
            //我的门锁
            doorlock: null,
            //提示框显示文字
            showMsg: '',
            textNickname: iot.navigator.getExtras().lockInfo.device_name,
            extras: {},
            unbindSucceess: false,
            //门铃消息推送
            valueDoorbell: iot.navigator.getExtras().lockInfo.options.ring,
            //报警消息推送
            valueAlarm: iot.navigator.getExtras().lockInfo.options.alarm,
            //开发消息推送
            valueOpendoor: iot.navigator.getExtras().lockInfo.options.open,
            role: null,
            valueIsClear: true,
            unbindShowDialog: false
            },
        mounted(){   
//      	uComponents.openAlert(this, Vue.t('doorlockManage.unbindSuccess'), {
//		        text: Vue.t('dialog.confirm'), callback: function () {
//		        	iot.navigator.openWindow({
//				        url: '../device/index.html',
//				        id: 'device'
//				    });
//		       	}
//		    });
        	this.extras = iot.navigator.getExtras();
        	console.log(this.extras);
        	this.role = this.extras.lockInfo.role;
	    	//获取门锁
//	    	getDoorlockInfo(this);
	    	//监听returnPage返回事件，重新获取最新数据
	    	let self = this;
			window.addEventListener('returnPage',function(event){
				console.log("returnPage");
				getDoorlockInfo(self);
			});
        },
        methods:{
        	getStateShowFlag(){
        		return this.stateShowFlag;
        	},
        	setStateShowFlag(flag){
        		this.stateShowFlag = flag
        	},
        	getDoorlock(){
        		return this.doorlock;
        	},
        	setDoorlock(obj){
        		this.doorlock = obj;
        	},
        	getShowMsg(){
        		return this.showMsg;
        	},
        	setShowMsg(msg){
        		this.showMsg = msg;
        	},
        	getTextNickname(){
        		return this.textNickname;
        	},
        	setTextNickname(text){
        		this.textNickname = text;
        	},
        	getUnbindSucceess(){
        		return this.unbindSucceess;
        	},
        	setUnbindSucceess(bool){
        		this.unbindSucceess = bool;
        	},
        	getValueDoorbell(){
        		return this.valueDoorbell;
        	},
        	setValueDoorbell(value){
        		this.valueDoorbell = value;
        	},
        	getValueAlarm(){
        		return this.valueAlarm;
        	},
        	setValueAlarm(value){
        		this.valueAlarm = value;
        	},
        	getValueOpendoor(){
        		return this.valueOpendoor;
        	},
        	setValueOpendoor(value){
        		this.valueOpendoor = value;
        	},
        	getValueIsClear(){
        		return this.valueIsClear;
        	},
        	setValueIsClear(value){
        		this.valueIsClear = value;
        	},
        	getUnbindShowDialog(){
        		return this.unbindShowDialog;
        	},
        	setUnbindShowDialog(flag){
        		this.unbindShowDialog = flag;
        	},
            //tap <
            onBackTap(){
                backTap();
            },       
            //tap 修改设备昵称
            onEditNameTap(){
            	editNameTap(this);
            },
            //tap 解绑门锁
            onUnbindDoorlockButtonTap(){
            	unbindDoorlockButtonTap(this);
            },
            //tap 隐藏提示框
            onUnbindStateDialogTap(){
            	unbindStateDialogTap(this);
            },
            onDoorbellSwitchTap(value){
            	doorbellSwitchTap(this, value);
            },
            onAlarmSwitchTap(value){
            	alarmSwitchTap(this, value);
            },
            onOpendoorSwitchTap(value){
            	opendoorSwitchTap(this, value);
            },
            onCancelTap(){
            	cancelTap(this);
            },
            onConfirmTap(){
            	confirmTap(this);
            },
            onIsClearSwitchTap(value){
            	isClearSwitchTap(this, value);
            }
        }   
    }).$mount('#app');
}

//配置组件参数
function initComponentsConfig() {
    return { 
    	unbindDoorlockButton: {
    		initParam: {
                class: 'custom-button',
                text: Vue.t('btn.unbindDoorlock')
            }
    	},
        dialog: {
            initParam: {
                class: 'custom-dialog'
            }
        },
        isClearSwitch: {
            initParam: {
                class: 'custom-isClearSwitch-switch',
                text: Vue.t('doorlockManage.isClearRecord')
            }
        },
        loading: {
            initParam: {
                class: 'custom-loading'
            }
        },
        doorbellSwitch: {
        	initParam: {
	            class: 'custom-switch',
	        }
        },
        alarmSwitch: {
        	initParam: {
	            class: 'custom-switch',
	        }
        },
        opendoorSwitch: {
	    	initParam: {
	            class: 'custom-switch',
	        }
        }
    }
}

//获取设备信息
function getDoorlockInfo(self){
	uComponents.showLoading(self);
	iot.business.device.getInfo({
	    data: {
	        device_id: self.extras.lockInfo.lock_id
	    },
	    success: (response) => {
	    	console.log(response);
	    	let data = uPublic.checkResponseData(response.data);
	    	if(data){
	    		self.setTextNickname(data.device_name);
	    		self.setValueDoorbell(data.device_info.options.ring);
	    		self.setValueAlarm(data.device_info.options.alarm);
	    		self.setValueOpendoor(data.device_info.options.open);
	    	}
	    },
	    error: (error) => {
	    	console.log(error);
	    	uPublic.openRequestErrorAlert(self);
	    },
	    complete: () => {
	    	uComponents.hideLoading(self);
	    }
	});
}


//tap 修改设备昵称
function editNameTap(self){
	if(!self.role){
		iot.navigator.openWindow({
	        url: './editName.html',
	        id: 'editName',
	        extras: {
	            lockId: self.extras.lockInfo.lock_id,
	            nickname: self.getTextNickname()
	        }
	    });
	}
}

//tap ‘解绑门锁’ 显示解绑门锁弹框
function unbindDoorlockButtonTap(self){
	self.setUnbindShowDialog(true);
}

//tap 确认解绑门锁
function confirmTap(self){
	self.setUnbindShowDialog(false);
	// sds解绑门锁
	iot.business.sds.unbindDevice({
        data: {
            uuid: self.extras.uuid
        },
        success: (response) => {
            console.log(response);
            unbind(self);
        },
        error: (error) => {
	    	console.log(error);
	    	if(window.iotDebug){
                iotDebug.push('end: sds解绑设备失败'+JSON.stringify(error));
           	}
	    	if (window.iotDebug) {
                iotDebug.upload(iot,'sds解绑设备失败');
            }
        	uPublic.openRequestErrorAlert(self);
        },
        complete: () => {
        	
        }
    });
}

// 云端解绑门锁
function unbind(self){
	iot.business.api.sendCustom('lock/unbind',{
	    data: {
	    	device_sn: self.extras.deviceId,
	        device_id: self.extras.lockInfo.lock_id,
	        needDelInfo: self.getValueIsClear()
	    },
	    success: (response) => {
	    	console.log(response);
	    	iot.storage.delMaps(['historyId','history'], (response) => {
	        	console.log("clearMap"+response);
		    }, (error) => {
		    	console.log(error);
				uPublic.openRequestErrorAlert(self);  
		    });
//	    	self.setStateShowFlag(true);
//			self.setShowMsg(Vue.t('doorlockManage.unbindSuccess'));
//			self.setUnbindSucceess(true);
			uComponents.openAlert(self, Vue.t('doorlockManage.unbindSuccess'), {
		        text: Vue.t('dialog.confirm'), callback: function () {
		        	iot.navigator.openWindow({
				        url: '../device/index.html',
				        id: 'device'
				    });
		       	}
		    });
			iot.navigator.fire(plus.webview.currentWebview().opener(),'returnPage',{});
	    },
	    error: (error) => {
	    	console.log(error);
	    	self.setStateShowFlag(false);
	    	self.setShowMsg(Vue.t('doorlockManage.unbindFailue'));
	    	self.setUnbindSucceess(false);
	    },
	    complete: () => {
	    	uComponents.hideLoading(self);
	    }
	});
}

//选择是否清除所有信息，默认选择为‘清除所有信息’
function isClearSwitchTap(self, value){
	self.setValueIsClear(value);
	console.log(self.getValueIsClear());
}

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

////重写mui.back
mui.back = function(){
    iot.navigator.fire(plus.webview.currentWebview().opener(),'returnPage',{});
    console.log("fire");
	iot.navigator.back();
}

// 选择： 是否接收门铃消息推送
function doorbellSwitchTap(self, value){
	self.setValueDoorbell(value);
	setOption(self);
}

// 选择： 是否接收报警消息推送
function alarmSwitchTap(self, value){
	self.setValueAlarm(value);
	setOption(self);
}

// 选择： 是否接收开门消息推送
function opendoorSwitchTap(self, value){
	self.setValueOpendoor(value);
	setOption(self);
}

// 调用设置推送接口
function setOption(self){
	iot.business.api.sendCustom('lock/setPushOption',
	{
		data: {
			device_id: self.extras.lockInfo.lock_id,
			opt_ring: self.getValueDoorbell(),
			opt_alarm: self.getValueAlarm(),
			opt_open: self.getValueOpendoor()
		},
		success: (response) => {
			iot.navigator.fire(plus.webview.currentWebview().opener(),'returnPage',{});
			console.log();
		},
		error: (error) => {
			console.log(error); 
           	uPublic.openRequestErrorAlert(self);
		},
		complete: () => {
		}
	});
}

//tap 取消解绑门锁
function cancelTap(self){
	self.setUnbindShowDialog(false);
}

//tap 绑定成功弹框提示-->回到首页
function unbindStateDialogTap(self){
//	self.setStateShowFlag(false);
//	if(self.getUnbindSucceess){
//		iot.navigator.openWindow({
//	        url: '../device/index.html',
//	        id: 'device'
//	    });
//	}
}
