// pages/luckDraw/components/tabbar.js
Component({
    /**
     * 组件的属性列表
     */
    properties: {

    },

    /**
     * 组件的初始数据
     */
    data: {
        list: [
            {
                text: "首页",
                pagePath: "/pages/luckDraw/index",
                selectedIconPath: "../../../images/luck-draw/home-active.png",
                iconPath: "../../../images/luck-draw/home.png",
            },
            {
                text: "我的",
                pagePath: "/pages/luckDraw/profile",
                selectedIconPath: "../../../images/luck-draw/user-active.png",
                iconPath: "../../../images/luck-draw/user.png",
            }
        ]
    },

    attached: function() {
        var url =getCurrentPages()[getCurrentPages().length-1].route
        this.data.list.forEach(v => {
            if (this.getPathName(v.pagePath) == this.getPathName(url)) {
                v.active = true
            }
        })
        this.setData({
            list: this.data.list
        })
    },

    /**
     * 组件的方法列表
     */
    methods: {
        toPage: function(e) {
            const index = e.currentTarget.dataset.index;
            this.data.list.forEach((v, i) => {
                if (i == index) {
                    v.active = true
                } else {
                    v.active = false
                }
            })
            this.setData({
                list: this.data.list
            })
            if (index == 0) {
                wx.reLaunch({
                  url: this.data.list[index].pagePath,
                })
                return
            }
            wx.redirectTo({
              url: this.data.list[index].pagePath,
            })
        },
        getPathName(path) {
            let pos = path.indexOf("luckDraw/");
            pos += "luckDraw/".length;
            return path.substr(pos)
        }
    }
})
