/**
* v_hijackRecord.js
* Version: 0.1
* User: wy
* Date: 2017-09-06
* Copyright(c)  2017. U-GEN Tech.Co,Ltd. All Rights Reserved.
* 劫持记录页面
*/
import moment from 'moment';
const PAGE_SIZE = 10;
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']);
    uPublic.closeSlideBack();
    const app = new Vue({
        data:{            
            //配置组件
            componentsConfig: initComponentsConfig(),
            list: [],
            startId: 0,
            deviceId: null,
            userInfo: []
        },
        mounted(){       
        	resolve(this);
        },
        methods:{
        	getList(){
        		return this.list;
        	},
        	setList(list){
        		this.list = list;
        	},
        	getStartId(){
        		return this.startId;
        	},
        	setStartId(id){
        		this.startId = id;
        	},
        	getDeviceId(){
        		return this.deviceId;
        	},
        	setDeviceId(id){
        		this.deviceId = id;
        	},
        	getUserInfo(){
        		return this.userInfo;
        	},
        	setUserInfo(info){
        		this.userInfo = info;
        	},
        	onListLoadTap(){
        		listLoadTap(this);
        	},
            //tap <
            onBackTap(){
                backTap();
            }                    
        }   
    }).$mount('#app');
}

//配置组件参数
function initComponentsConfig() {
    return {
        hijackRecordList:{   
            initParam: {
                class: 'custom-list',
				list: [],
                tip: Vue.t('hijackRecord.noRecordTip'),
                pagesize: PAGE_SIZE
            }
        },
        dialog: {
            initParam: {
                class: 'custom-dialog'
            }
        },
        loading: {
            initParam: {
                class: 'custom-loading'
            }
        }                
    }
}

 function resolve(self) {
	 	self.setDeviceId(iot.navigator.getExtras().deviceId);
		uComponents.showLoading(self);
    	iot.business.api.sendCustom('lock/getHistoryRecord',
		{
			data: {
				device_id: self.getDeviceId(),
				action: 3,
				start_id: self.getStartId(),
				page_size: PAGE_SIZE
			},
			success: (response) => {
				let data = uPublic.checkResponseData(response.data);
				if(data){
					console.log(data);
					let list = [];
					let record = data.record;
					if(data.hasOwnProperty('info')){
						self.setUserInfo(data.info);
					}
					console.log(config.hijackMode[20]);
					for(let i=0; i<record.length; i++){
						list[i] = {
							value: record[i].openId,
							title: "ID:"+record[i].openId,
							leftImage: '../../resources/image/green_hijackMode20_icon.png',
							subtitle: Vue.t('hijackRecord.'+config.hijackMode[record[i].mode]+''),
		//					title: ('ID:'+record[i].id),
							rightText: moment(record[i].time).format("YYYY.MM.DD HH:mm:ss")
						};
						for(let j=0; j<self.getUserInfo().length; j++){
							if(record[i].openId == self.getUserInfo()[j].openId && record[i].mode == self.getUserInfo()[j].mode){
								if(self.getUserInfo()[j].nickname != null){
									list[i].title = self.getUserInfo()[j].nickname;
								}
							}
						}
					}
					self.setList(self.getList().concat(list));
					if(record.length > 0){
						self.setStartId(record[record.length-1].id);
					}
					//更新列表数据
		            uComponents.changeList(self, self.getList());
				}else{
				}
				uComponents.hideLoading(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);
			}
		});
    }

//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 listLoadTap(self) {
	resolve(self);
}