/**
* v_unlockInfo.js
* Version: 0.1
* User: wy
* Date: 2017-10-09
* 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';

iot.ready(init);
function init() {
    //通用 侧滑列表、dialog、loading组件
    uPublic.componentsInit(['u-swipe-list','u-dialog','u-loading']);
    uPublic.closeSlideBack();
    const app = new Vue({
        data:{                                   
            //配置组件
            componentsConfig: initComponentsConfig(),
            unlockInfoList: [],
            openLockInfoList: [],
            extras: {},
            role: null
        },
        mounted(){           
        	this.extras = iot.navigator.getExtras();
        	this.role = this.extras.role;
        	console.log(this.extras);
        	//获取已绑定的开锁信息
        	getBindedLockInfo(this);
        	let self = this;
        	window.addEventListener('returnPage',function(event){
				console.log("returnPage");
				self.unlockInfoList = [];
				getBindedLockInfo(self);
			});
        },
        methods:{
        	getUnlockInfoList(){
        		return this.unlockInfoList;
        	},
        	setUnlockInfoList(list){
        		this.unlockInfoList = list
        	},
        	getOpenLockInfoList(){
        		return this.openLockInfoList;
        	},
        	setOpenLockInfoList(list){
        		this.openLockInfoList = list;
        	},
            //tap <
            onBackTap(){
                backTap();
            },
            onAddTap(){
            //tap 添加
                addTap(this);
            },
            //tap list row btn 
            onUnlockInfoListButtonTap(value, buttonValue){
                unlockInfoListButtonTap(this, value, buttonValue);
            }                         
        }   
    }).$mount('#app');
}

//配置组件参数
function initComponentsConfig() {
    return {
        unlockInfoList: {
            initParam: {
//              class: 'custom-unlockInfo-list',
                list: [],
                class: 'custom-swipe-list',
//              list: [
//                  {title: '指纹1', subtitle: 'ID:123456', buttons: [{'text': Vue.t('unlockInfo.setHijack')},{'text': Vue.t('unlockInfo.cancelAssociated')}]},
//                  {title: '指纹2', subtitle: 'ID:123456', buttons: [{'text': Vue.t('unlockInfo.setHijack')},{'text': Vue.t('unlockInfo.cancelAssociated')}]},
//                  {title: '劫持指纹1', subtitle: 'ID:123456', buttons: [{'text': Vue.t('unlockInfo.cancelHijack')},{'text': Vue.t('unlockInfo.cancelAssociated')}]}
//              ],
                tip: Vue.t('unlockInfo.noInfoTip')
            }
        },
        dialog: {
            initParam: {
                class: 'custom-dialog'
            }
        },
        loading: {
            initParam: {
                class: 'custom-loading'
            }
        }               
    }
}

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

//tap 添加 
function addTap(self){
    iot.navigator.openWindow({
        url: './addUnlockInfo.html',
        id: 'addUnlockInfo',
        extras: {
        	deviceId: self.extras.deviceId,
        	id: self.extras.id
        }
    });
}

//点击加载更多
function tapLoad(){
    if (!self.loadFlag) {
        self.loadText = Vue.t('scroll.loading');
        self.loadFlag = true;
        //再次发送获取用户列表请求
    }
}

//获取已绑定的开锁信息
function getBindedLockInfo(self){
	uComponents.showLoading(self);
	iot.business.api.sendCustom('lock/getBindOpen',{
		data: {
			rel_id: self.extras.id,
			device_id: self.extras.deviceId
		},
		success: (response) => {
			console.log(response);
			let data = uPublic.checkResponseData(response.data);
	    	if(data){
	    		console.log(data);
	    		self.setOpenLockInfoList(data);
				let list = [];
				let j = 1;
				let lastMode = null;
				for(let i=0; i<data.length; i++){
					if(data[i].mode != lastMode){
						j = 1;
					}
					list[i] = {
						title: Vue.t('unlockInfo.'+config.openDoorMode[data[i].mode]+'')+j+'',
						subtitle: 'ID:'+data[i].openId,
						buttons: self.extras.role ? [] : [{'text': data[i].mode == 20 ?  Vue.t('unlockInfo.cancelHijack') :  data[i].mode == 2 ? Vue.t('unlockInfo.setHijack') : ''},{'text': Vue.t('unlockInfo.cancelAssociated')}]
					};
					j++;
					lastMode = data[i].mode;
				}
				self.setUnlockInfoList(self.getUnlockInfoList().concat(list));
				console.log(self.getUnlockInfoList());
				//更新列表数据
	            uComponents.changeSwipeList(self, self.getUnlockInfoList());
	    	}else{
	    		uComponents.changeSwipeList(self, []);
	    	}
		},
		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 unlockInfoListButtonTap(self, value, buttonValue) {
    //设为劫持&取消劫持
    if(buttonValue == '0'){
    	if(self.getOpenLockInfoList()[value].mode == 2){
    		uComponents.openConfirm(self, Vue.t('unlockInfo.setHijackTip'), 
	           {text: Vue.t('dialog.cancel'), callback: function () {}},
	           {text: Vue.t('dialog.confirm'), callback: function () {
	           		console.log("设为劫持");
	           		uComponents.showLoading(self);
	           		iot.business.api.sendCustom('lock/updateBindLog',{
						data: {
							mode: 20,
							id: self.getOpenLockInfoList()[value].id,
							rel_id: self.extras.id
						},
						success: (response) => {
							console.log(response);
							uComponents.openAlert(self, Vue.t('unlockInfo.setHijackSuccess'), {
						        text: Vue.t('dialog.confirm'), callback: function () {
						        	self.unlockInfoList = [];
						        	getBindedLockInfo(self);
						        }
						    });
						},
						error: (error) => {
							console.log(error);
//							if(window.iotDebug){
//				                iotDebug.push('end: 设为劫持失败'+JSON.stringify(error));
//				           	}
//					    	if (window.iotDebug) {
//				                iotDebug.upload(iot,'设为劫持失败');
//				            }
							uComponents.openAlert(self, Vue.t('unlockInfo.setHijackFailue'), {
						        text: Vue.t('dialog.confirm'), callback: function () {
						        }
						    });
						},
						complete: () => {
							uComponents.hideLoading(self);
						}
					});
	        }});
    	}else{
    		uComponents.openConfirm(self, Vue.t('unlockInfo.cancelHijackTip'), 
	           {text: Vue.t('dialog.cancel'), callback: function () {}},
	           {text: Vue.t('dialog.confirm'), callback: function () {
	           		console.log("取消劫持");
	           		uComponents.showLoading(self);
	           		iot.business.api.sendCustom('lock/updateBindLog',{
						data: {
							mode: 2,
							id: self.getOpenLockInfoList()[value].id,
							rel_id: self.extras.id
						},
						success: (response) => {
							console.log(response);
							uComponents.openAlert(self, Vue.t('unlockInfo.cancelHijackSuccess'), {
						        text: Vue.t('dialog.confirm'), callback: function () {
						        	self.unlockInfoList = [];
						        	getBindedLockInfo(self);
						        }
						    });
						},
						error: (error) => {
							console.log(error);
//							if(window.iotDebug){
//				                iotDebug.push('end: 取消劫持失败'+JSON.stringify(error));
//				           	}
//					    	if (window.iotDebug) {
//				                iotDebug.upload(iot,'取消劫持失败');
//				            }
							uComponents.openAlert(self, Vue.t('unlockInfo.cancelHijackFailue'), {
						        text: Vue.t('dialog.confirm'), callback: function () {
						        }
						    });
						},
						complete: () => {
							uComponents.hideLoading(self);
						}
					});
	        }});
    	}
    }
    //取消关联
    else if(buttonValue == '1'){
        console.log(self.getOpenLockInfoList()[value]);
        uComponents.openConfirm(self, Vue.t('unlockInfo.cancelAssociatedTip'), 
           {text: Vue.t('dialog.cancel'), callback: function () {}},
           {text: Vue.t('dialog.confirm'), callback: function () {
           		uComponents.showLoading(self);
           		iot.business.api.sendCustom('lock/unbindOpenLog',{
					data: {
						device_id: self.extras.deviceId,
						mode: self.getOpenLockInfoList()[value].mode,
						openId: self.getOpenLockInfoList()[value].openId,
						rel_id: self.extras.id
					},
					success: (response) => {
						console.log(response);
						uComponents.openAlert(self, Vue.t('unlockInfo.cancelAssociatedSuccess'), {
					        text: Vue.t('dialog.confirm'), callback: function () {
					        	self.unlockInfoList = [];
					        	getBindedLockInfo(self);
					        }
					    });
					},
					error: (error) => {
						console.log(error);
//						if(window.iotDebug){
//			                iotDebug.push('end: 取消关联失败'+JSON.stringify(error));
//			           	}
//				    	if (window.iotDebug) {
//			                iotDebug.upload(iot,'取消关联失败');
//			            }
						uComponents.openAlert(self, Vue.t('unlockInfo.cancelAssociatedFailue'), {
					        text: Vue.t('dialog.confirm'), callback: function () {
					        }
					    });
					},
					complete: () => {
						uComponents.hideLoading(self);
					}
				});
           }}
        ); 
    }
}
