/**
* 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 {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();
    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[0],
            //报警消息推送
            valueAlarm: iot.navigator.getExtras().lockInfo.options[1],
            //开发消息推送
            valueOpendoor: iot.navigator.getExtras().lockInfo.options[2],
            role: null,
            valueIsClear: true,
            unbindShowDialog: false
            },
        mounted(){   
        	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);
	let productId = config.model;
	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[0]);
	    		self.setValueAlarm(data.device_info.options[1]);
	    		self.setValueOpendoor(data.device_info.options[2]);
	    	}
	    },
	    error: (error) => {
	    	console.log(error);
//	    	if(window.iotDebug){
//              iotDebug.push('end: 获取门锁信息失败'+JSON.stringify(error));
//         	}
//	    	if (window.iotDebug) {
//              iotDebug.upload(iot,'获取门锁信息失败');
//          }
	    	uPublic.openRequestErrorAlert(self);
	    },
	    complete: () => {
	    	uComponents.hideLoading(self);
	    }
	});
//	iot.business.api.send('device/getList',{
//	    data: {
//	        sds: false,
//	        device_id: self.extras.deviceId,
//	        product_id: productId,
//	        parent_id: 0,
//	        start_id: 0,
//	        number: 10
//	    },
//	    success: (response) => {
//	    	console.log(response);
//	    	let data = uPublic.checkResponseData(response.data);
//	    	if(data){
//	    		self.setDoorlock(response.data[0]);
//	    		self.setTextNickname(self.getDoorlock().nickname);
//	    		console.log(self.getDoorlock());
//	    	}
//	    },
//	    error: (error) => {
//	    	//请求失败
//  		uPublic.openRequestErrorAlert(self);
//	    	console.log(error);
//	    },
//	    complete: () => {
//	    	uComponents.hideLoading(self);
//	    }
//	},true);
}

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

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

//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);
//uComponents.openConfirm(self, Vue.t('doorlockManage.unbindDialogTip'),
//		{text: Vue.t('dialog.cancel'), callback: function () {}},
//		{text: Vue.t('dialog.unbind'), callback: function () {
//			uComponents.showLoading(self);
//			iot.business.sds.unbindDevice({
//              data: {
//                  uuid: self.extras.uuid
//              },
//              success: (response) => {
//                  console.log(response);
//                  unbind(self);
//              },
//              error: (error) => {
//              	console.log(error)
//              },
//              complete: () => {
//              	
//              }
//          });
//		}}
//	);
}

//tap 隐藏提示框
function unbindStateDialogTap(self){
	self.setStateShowFlag(false);
	if(self.getUnbindSucceess){
		iot.navigator.openWindow({
	        url: '../device/index.html',
	        id: 'device'
	    });
	}
}

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);
	    	self.setStateShowFlag(true);
			self.setShowMsg(Vue.t('doorlockManage.unbindSuccess'));
			self.setUnbindSucceess(true);
	    },
	    error: (error) => {
	    	console.log(error);
//	    	if(window.iotDebug){
//              iotDebug.push('end: 云端解绑失败'+JSON.stringify(error));
//         	}
//	    	if (window.iotDebug) {
//              iotDebug.upload(iot,'云端解绑失败');
//          }
	    	self.setStateShowFlag(false);
	    	self.setShowMsg(Vue.t('doorlockManage.unbindFailue'));
	    	self.setUnbindSucceess(false);
	    },
	    complete: () => {
	    	uComponents.hideLoading(self);
	    }
	});
}

//function unbindUser(self){
//	iot.business.api.sendCustom('lock/unbindUser',{
//  	data: {
//  		rel_id:  self.getDoorlock().id,
//  		device_id: self.getDoorlock().device_id,
//  		user_id:  self.getDoorlock().user_device_id
//  	},
//  	success: (response) => {
//  		self.setStateShowFlag(true);
//			self.setShowMsg(Vue.t('doorlockUser.unbindSuccess'));
//			self.setUnbindSucceess(true);
//  	},
//  	error: (error) => {
//  		console.log(error);
//  		self.setStateShowFlag(false);
//	    	self.setShowMsg(Vue.t('doorlockUser.unbindFailue'));
//	    	self.setUnbindSucceess(false);
//  	},
//  	complete: () => {
//			uComponents.hideLoading(self);
//  	}
//  });
//}


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){
//	uComponents.showLoading(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) => {
			console.log();
		},
		error: (error) => {
			console.log(error); 
//			if(window.iotDebug){
//              iotDebug.push('end: 设置推送失败'+JSON.stringify(error));
//         	}
//	    	if (window.iotDebug) {
//              iotDebug.upload(iot,'设置推送失败');
//          }
           	uPublic.openRequestErrorAlert(self);
		},
		complete: () => {
//			uComponents.hideLoading(self);
		}
	});
}


function cancelTap(self){
	self.setUnbindShowDialog(false);
}

function confirmTap(self){
	self.setUnbindShowDialog(false);
	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: () => {
        	
        }
    });
//	unbind(self);
}

function isClearSwitchTap(self, value){
	self.setValueIsClear(value);
	console.log(self.getValueIsClear());
}

