/**
* v_index.js
* Version: 0.1
* User: zjx
* Date: 2017-09-29
* 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']);
    const app = new Vue({
        data:{ 
        	//用户列表
        	userList: [],
        	userListIndex: null,
        	loadShowFlag: false,
        	loadFlag: false,
        	loadText: 'loading',
        	pagesize: 10,
        	tip: '无数据'
        },
        mounted(){
        	this.userList = [{value: 1, role: 0, image: '../../resources/image/green_head_icon.png', title: '我是用户', subtitle: "昵称： MM"},
        	{value: 1, role: 1, image: '../../resources/image/green_head_icon.png', title: '我是用户', subtitle: "昵称： MM"}]
        },
        methods:{
        	onBackTap(){
        		backTap();
        	},
        	onUserTap(){
        		addUser();
        	},
        	setLoadShowFlag(number){
                this.loadShowFlag = (number >= this.pagesize);
            },
            onSwipeLeftRow(index){
                swipeLeftRow(this, index);
            },
            onSwipeRightRow(){
                swipeRightRow(this);
            },
        	onTapLoad(){
        		tapLoad();
        	},
        	onTapUserList(index){
        		onTapUserList(this, index);
        	},
        	onTapButton(buttonIndex){
        		tapButton(buttonIndex);
        	},
        	changeUserList(list){
                changeUserList(this, list);
            }
		}
          
    }).$mount('#app');
}

//返回上一页
function backTap(){
	iot.navigator.back();
}

//添加用户
function addUser(){
//	iot.navigator.openWindow({
//      url: '../doorlockUsers/addUser',
//      id: 'addUser',
//  });
}

// 向左滑动用户列表，修改用户列表下标
function swipeLeftRow(self, index) {
	self.userListIndex = index;
}
// 向右滑动用户列表，修改用户列表下标
function swipeRightRow(self) {
	self.userListIndex = null;
}
// 点击组件列表 通知事件和当前点击列表下标
function onTapUserList(self, listIndex) {
 	if (self.userListIndex != null) {
        self.userListIndex = null;
    }
}
//点击加载更多
function tapLoad(){
	if (!self.loadFlag) {
        self.loadText = 'loading';
        self.loadFlag = true;
        //再次发送获取用户列表请求
    }
}

//点击按钮
function tapButton(buttonIndex){
	console.log(buttonIndex);
}

// 修改userList列表
function changeUserList(self, list) {
    self.setLoadShowFlag(list.length - self.swipeList.length);
    self.userList = list;
    self.$nextTick(function () {
        self.loadText = '点击加载更多';
        self.loadFlag = false;
    });
}