/**
* 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: {},
        	userListData: [],
        	startId: 0
        },
        mounted(){
        	this.extras = iot.navigator.getExtras();
        	//获取用户列表
        	getUserList(this);
//      	
        },
        methods:{
        	getUserList(){
 				return this.userList;       		
        	},
        	setUserList(list){
        		this.userList = list;
        	},
        	onBackTap(){
        		backTap();
        	},
        	onAddUserTap(){
        		addUserTap();
        	},
        	setLoadShowFlag(number){
                this.loadShowFlag = (number >= this.pagesize);
            },
            getUserListData(){
            	return this.userListData;
            },
            setUserListData(data){
            	this.userListData = data;
            },
            getStartId(){
            	return this.startId;
            },
            setStartId(id){
            	this.startId = id;
            },
            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){
	console.log(self.extras);
	uComponents.showLoading(self);
	iot.business.device.getUsers({
	    data: {
	        device_id: self.extras.deviceId
	    },
	    success: (response) => {
	    	console.log(response);
	    	let data =  [ { id: 2,
			       device_id: 'B0F89310D509',
			       user_id: 2,
			       role: 0,
			       nickname: '用户1',
			       username: '测试1' },
			     { id: 3,
			       device_id: 'B0F89310D509',
			       user_id: 1,
			       role: 1,
			       nickname: '用户2',
			       username: '测试2' },
			     { id: 4,
			       device_id: 'B0F89310D509',
			       user_id: null,
			       role: 1,
			       nickname: '用户3' },
			     { id: 5,
			       device_id: 'B0F89310D509',
			       user_id: null,
			       role: 1,
			       nickname: '用户4' },
			     { id: 6,
			       device_id: 'B0F89310D509',
			       user_id: null,
			       role: 1,
			       nickname: '用户5' } ];
			
//	    	let data = uPublic.checkResponseData(response.data);
			console.log(data);
	    	if(data){
	    		let list = [];
	    		for(let i = 0; i < data.length ; i++){
	    			list[i] = {
	    				value: data[i].id,
	    				role: data[i].role,
	    				image: data[i].role == 0 ? '../../resources/image/green_admin_icon.png' : data[i].user_id == null ? '../../resources/image/gray_unbindHead_icon.png' : '../../resources/image/green_bindhead_icon.png',
	    				title: data[i].nickname,
	    				subtitle: data[i].username
	    			}
	    		}
			       
			    self.setUserListData(self.getUserList().concat(data));
	    		self.setUserList(self.getUserList().concat(list));
	    		console.log(self.getUserList());
				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: './doorlockUser.html',
        id: 'doorlockUser',
        extras: {
        	id: self.getUserListData()[listIndex].id,
        	userId: self.getUserListData()[listIndex].user_id,
        	nickname: self.getUserListData()[listIndex].nickname,
        	username: self.getUserListData()[listIndex].username,
        	deviceId: self.getUserListData()[listIndex].device_id,
        	role: self.getUserListData()[listIndex].role,
        	uuid: self.extras.uuid
        }
    });
 	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;
    });
}