/**
* v_index.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';
import {projectMethods} from '../../public/components.js';

iot.ready(init);
function init() {
    //通用 按钮、dialog、loading组件
    uPublic.componentsInit(['u-button','u-dialog','u-loading']);
    //项目 list组件
    uPublic.componentsExtend(['grid-list']);
    const app = new Vue({
        data:{            
            //配置组件
            componentsConfig: initComponentsConfig(),
            //提示框是否显示
            stateShowFlag: false,
            //我的门锁
            doorlock: null,
            //提示框显示文字
            showMsg: '',
            textNickname: null,
            extras: {},
            unbindSucceess: false
            },
        mounted(){   
        	this.extras = iot.navigator.getExtras();
        	console.log(this.extras);
	    	//获取门锁
	    	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;
        	},
            //tap <
            onBackTap(){
                backTap();
            },       
            //tap 修改设备昵称
            onEditNameTap(){
            	editNameTap(this);
            },
            //tap 解绑门锁
            onUnbindDoorlockButtonTap(){
            	unbindDoorlockButtonTap(this);
            },
            //tap 隐藏提示框
            onUnbindStateDialogTap(){
            	unbindStateDialogTap(this);
            }
        }   
    }).$mount('#app');
}


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

function getDoorlockInfo(self){
	uComponents.showLoading(self);
	iot.business.api.send('device/getList',{
	    data: {
	        sds: false,
	        device_id: self.extras.deviceId,
	        product_id: 2,
	        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.back();
}

//tap 修改设备昵称
function editNameTap(self){
	iot.navigator.openWindow({
        url: './editName.html',
        id: 'editName',
        extras: {
            deviceId: self.getDoorlock().id,
            nickname: self.getTextNickname()
        }
    });
}

//tap 解绑门锁
function unbindDoorlockButtonTap(self){
uComponents.openConfirm(self, Vue.t('doorlockUser.unbindDialogTip'),
		{text: Vue.t('dialog.cancel'), callback: function () {}},
		{text: Vue.t('dialog.unbind'), callback: function () {
//			uComponents.showLoading(self);
//	    	//云端解绑
//	    	iot.business.device.unbind({
//			    data: {
//			        device_id: self.getDoorlock().id
//			    },
//			    success: (response) => {
//			    	console.log(response);
			    	iot.business.device.unbind({
						data: {
					        sds: true,
					        uuid: self.extras.uuid,
					        device_id: self.getDoorlock().id
						    },
					    success: (response) => {
					    	console.log(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);
					    }
					});
//			    },
//			    error: (error) => {
//			    	console.log(error);
//			    	self.setStateShowFlag(true);
//	    			self.setShowMsg(Vue.t('doorlockUser.unbindFailue'));
//	    			self.setUnbindSucceess(false);
//			    },
//			    complete: () => {
//			    	uComponents.hideLoading(self);
//			    }
//			});
		}}
	);
}

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