Component({
	data: {
		activeUrl: '',
		active: 0,
		list: [],
		initList: [{
				icon: 'home-o',
				text: '首页',
				url: '/pages/index/index',
				hidden: false
			},
			{
				icon: 'bar-chart-o',
				text: '数据中心',
				url: '/pages/data/index',
				hidden: true
			},
			{
				icon: 'user-o',
				text: '个人中心',
				url: '/pages/account/index',
				hidden: false
			}
		],
		role: ''
	},

	lifetimes: {
		attached: function () {

		},
	},

	methods: {
		switchTab(e) {
			var url = e.currentTarget.dataset.url
			if (url && this.data.activeUrl != url) {
				wx.switchTab({
					url: url
				})
			}

		},
		onChange(e) {
			this.setData({
				active: e.detail
			});
			var active = e.detail
			var cnt = -1;
			var list = this.data.list
			var url = '';
			for (var i = 0; i < list.length; ++i) {
				if (!list[i].hidden) cnt = cnt + 1;
				if (cnt == active) {
					url = list[i].url
					break;
				}
			}
			wx.switchTab({
				url: url
			});
		},

		init() {
			const page = getCurrentPages().pop();
			var userInfo = getApp().globalData.userInfo
			var role = userInfo ? userInfo.topRole : ''
			var list = this.data.initList
			if (role && role.key == 'leader') {
				// list = this.data.initList.slice(1)
				list[0].hidden = true;
			} else {
				list[0].hidden = false;
			}
			if (role && role.rights && role.rights.dataView) {
				list[1].hidden = false;
			} else {
				list[1].hidden = true;
			}
			this.setData({
				list: list
			})
			var activeUrl = '';
			for (var i = 0; i < list.length; ++i) {
				if (list[i].url == '/' + page.route) {
					activeUrl = list[i].url
					break;
				};
			}
			this.setData({
				activeUrl
			});
			if (role) {
				this.setData({
					role
				})
			}
		}
	}
});