/**
* v_alarmInfo.js
* Version: 0.1
* User: wy
* Date: 2017-09-06
* Copyright(c)  2017. U-GEN Tech.Co,Ltd. All Rights Reserved.
* 报警信息页面
*/
const PAGE_SIZE = 10;
import moment from 'moment';
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','u-refresh']);
    uPublic.closeSlideBack();
    const app = new Vue({
        data:{            
            //配置组件
            componentsConfig: initComponentsConfig(),
            list:[],
            startId: 0,
            deviceId: null,
            flag: true,
            refreshDisabled: true
        },
        mounted(){      
        	resolve(this);
        	let ulistDom = this.$refs.ulist.$el;
		    console.log(ulistDom);
		    let self = this;
			ulistDom.onscroll = function () {
				console.log(ulistDom.offsetHeight+'   '+ulistDom.offsetTop);
				console.log(ulistDom.scrollTop);
				console.log(ulistDom.scrollHeight);
	
				let sum = ulistDom.offsetHeight+ulistDom.scrollTop;
				console.log(sum);
				//滑到底了
				if(sum == ulistDom.scrollHeight){
					self.refreshDisabled = false;
					ulistDom.style.overflow = 'hidden';
				}
			}
        },
        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;
        	},
        	//tap 点击加载更多
        	onListLoadTap(){
        		listLoadTap(this);
        	},
        	onRefreshFresh(){
        		refreshFresh(this);
        	},
            //tap <
            onBackTap(){
                backTap();
            }                    
        }   
    }).$mount('#app');
}

//配置组件参数
function initComponentsConfig() {
    return {
        alarmInfoList:{   
            initParam: {
                class: 'custom-list',
				list: [],
                tip: Vue.t('alarmInfo.noInfoTip'),
//              pagesize: PAGE_SIZE
            }
        },
        dialog: {
            initParam: {
                class: 'custom-dialog'
            }
        },
        loading: {
            initParam: {
                class: 'custom-loading'
            }
        },
        refresh: {
            initParam: {
            	text: ['下拉刷新','松开加载','正在刷新','刷新成功'],
            	class: 'custom-refersh',
            	pullUp: false
            }
        }
    }
}

// 获取历史记录
function resolve(self) {
  	self.setDeviceId(iot.navigator.getExtras().deviceId);
	uComponents.showLoading(self);
	iot.business.api.sendCustom('lock/getHistoryRecord',
	{
		data: {
			device_id: self.getDeviceId(),
			action: 2,
			start_id: self.getStartId(),
			page_size: PAGE_SIZE
		},
		success: async (response) => {
			console.log(response);
			if(self.flag){
				let data = uPublic.checkResponseData(response.data);
				if(data && data.record.length > 0){
					let record = data.record;
					self.flag = await uPublic.upDateRead('notify', moment(record[0].time).unix());
				}
			}
			let data = uPublic.checkResponseData(response.data);
			if(data){
				let list = [];
				let record = data.record;
				for(let i=0; i<record.length; i++){
					list[i] = {
						value: record[i].openId,
						leftImage: '../../resources/image/green_alarmInfo'+record[i].mode+'_icon.png',
						subtitle: Vue.t('alarmInfo.'+config.alarmMode[record[i].mode]+''),
						rightText: moment(record[i].time).format("YYYY.MM.DD HH:mm:ss")
					};
				}
				self.setList(self.getList().concat(list));
				if(record.length > 0){
					self.setStartId(record[record.length-1].id);
				}
				//更新列表数据
				uComponents.hideRefresh(self);
				let ulistDom = self.$refs.ulist.$el;
		   		let sum = ulistDom.offsetHeight+ulistDom.scrollTop;
				//滑到底了
				ulistDom.style.overflow = 'auto';
				self.refreshDisabled = true;
		//		if(sum != ulistDom.scrollHeight){
		//		 	ulistDom.style.overflow = 'auto';
		//			self.refreshDisabled = true;
		//		}
	            uComponents.changeList(self, self.getList());
			}else{
			}
			uComponents.hideLoading(self);
		},
		error: (error) => {
			console.log(error); 
           	uPublic.openRequestErrorAlert(self);
		},
		complete: () => {
			uComponents.hideLoading(self);
		}
	});
}

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

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

//tap 点击加载更多
function listLoadTap(self) {
	resolve(self);
}

function refreshFresh(self){
	resolve(self);
}