/**
* 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() {
    //通用 list、dialog、loading组件
    uPublic.componentsInit(['u-list','u-dialog','u-loading']);
    const app = new Vue({
        data:{                                   
            //配置组件
            componentsConfig: initComponentsConfig(),
            unlockInfoList: [],
            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
        	},
            //tap <
            onBackTap(){
                backTap();
            },
            //tap 添加
            onAddTap(){
                addTap(this);
            }                          
        }   
    }).$mount('#app');
}

//配置组件参数
function initComponentsConfig() {
    return {
        unlockInfoList:{   
            initParam: {
                class: 'custom-unlockInfo-list',
                list: [],
                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);
				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+'',
						rightText: 'ID:'+data[i].openId
					};
					j++;
					lastMode = data[i].mode;
				}
				self.setUnlockInfoList(self.getUnlockInfoList().concat(list));
				console.log(self.getUnlockInfoList());
				//更新列表数据
	            uComponents.changeList(self, self.getUnlockInfoList());
	    	}else{}
		},
		error: (err) => {
			console.log(err);
		},
		complete: () => {
			uComponents.hideLoading(self);
		}
	});
}
