/**
* 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:{ 
        	//配置组件
            componentsConfig: initComponentsConfig(),
        	//用户列表
        	userList: [],
        	userListIndex: null,
        	loadShowFlag: false,
        	loadFlag: false,
        	loadText: 'loading',
        	pagesize: 10,
        	tip: '无数据',
        	extras: {}
        },
        mounted(){
        	self.extras = iot.navigator.getExtras();
        	//获取用户列表
        	getUserList();
//      	this.userList = [{value: 1, role: 0, image: '../../resources/image/green_admin_icon.png', title: '我是用户', subtitle: "MM"},
//      	{value: 1, role: 1, image: '../../resources/image/green_bindhead_icon.png', title: '我是用户', subtitle: "MM"}]
        },
        methods:{
        	getUserList(){
 				return this.userList;       		
        	},
        	setUserList(list){
        		this.userList = list;
        	},
        	onBackTap(){
        		backTap();
        	},
        	onAddUserTap(){
        		addUserTap();
        	},
        	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 initComponentsConfig() {
    return {
        dialog: {
            initParam: {
                class: 'custom-dialog'
            }
        },
        loading: {
            initParam: {
                class: 'custom-loading'
            }
        }                
    }
}

//获取用户列表
function getUserList(self){
	uComponents.showLoading(self);
	iot.business.device.getUsers({
	    data: {
	        device_id: self.extras.deviceId
	    },
	    success: (response) => {
	    	console.log(response);
	    	let data = uPublic.checkResponseData(response.data);
	    	if(data){
	    		let list = [];
	    		for(let i = 0; i < data.length ; i++){
	    			list[i] = {
	    				value: data.userId,
	    				role: data.role,
	    				image: data.role == 0 ? '../../resources/image/green_admin_icon.png' : data.name == null ? '../../resources/image/gray_unbindHead_icon.png' : '../../resources/image/green_bindhead_icon.png',
	    				title: data.nickname,
	    				subTitle: data.name
	    			}
	    		}
	    		self.setUserList(self.getUserList().concat(list));
				if(data.length > 0){
					self.setStartId(data[data.length-1].id);
				}
	    	}
	    },
	    error: (error) => {
	    	console.log(error);
	    	uPublic.openRequestErrorAlert(self);
	    },
	    complete: () => {
			uComponents.hideLoading(self);
	    }
	});
}

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

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

// 向左滑动用户列表，修改用户列表下标
function swipeLeftRow(self, index) {
	self.userListIndex = index;
}
// 向右滑动用户列表，修改用户列表下标
function swipeRightRow(self) {
	self.userListIndex = null;
}
// 点击组件列表 通知事件和当前点击列表下标
function onTapUserList(self, listIndex) {
	iot.navigator.openWindow({
        url: '../doorlockUsers/doorlockUsers.html',
        id: 'doorlockUsers',
    });
 	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;
    });
}