// pages/FirstPage/index.js
import {getOnlive} from '../../api/home.js'
import {getOpenidData} from '../../utils/getUserInfo.js'
import { env } from '../../environment.js'

Page({

    /**
     * 页面的初始数据
     */
    data: {
        areaName: '', // 当前区域位置
        announcementList: [], // 公告列表
        // 快捷入口
        entersList: [
            {
                title:'护河行动',
                iconPath: '../../assets/first/new.png',
                path: '/package_first/pages/announceList/index',
                type: 'page'
            },
            {
                title:'热点资讯',
                iconPath: '../../assets/first/ask.png',
                path: '/package_first/pages/askAdmin/index',
                type: 'page'
            },
            {
                title:'大好河山',
                iconPath: '../../assets/first/rivers.png',
                path: '/package_first/pages/beautyList/index',
                type: 'page'
            },
            {
                title:'积分商城',
                iconPath: '../../assets/first/stores.png',
                path: '/package_A/pages/goods-page/index',
                type: 'mini'
            },
            {
                title:'护河活动',
                iconPath: '../../assets/first/activity.png',
                path: '/package_first/pages/ProtectionActivity/index',
                type: 'page'
            },
            {
                title:'护河建议',
                iconPath: '../../assets/first/suggestion.png',
                path: '/package_first/pages/ProtectionSuggestion/index',
                type: 'page'
            },
            {
                title:'志愿者服务',
                iconPath: '../../assets/first/volunteer.png',
                path: '/package_A/pages/volunteer-page/index',
                type: 'page'
            },
            {
                title:'我要上报',
                iconPath: '../../assets/first/report.png',
                path: '/package_A/pages/report-page/index',
                type: 'page'
            }
        ],
        liveNum: 0, // 在线人数
        list: [], // 河道展示
        current: '',
        params: {
            page: 1,
            limit: 3
        }
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.getTabBar().setData({
            selected: 0
        })
        let tenant = wx.getStorageSync('tenant')
        if(!Object.keys(tenant).length) {
            const path = '/pages/FirstPage/index'
            const fromType = 'tabbar'
            wx.redirectTo({
                url: `/pages/login/login?path=${path}&fromType=${fromType}`,
            })
        } else {
            this.setData({current: '/stream/index',})
        }
    },
    
    onShow() {
        let tenant = wx.getStorageSync('tenant')
        if(Object.keys(tenant).length) {
            this.getOnliveNum()
        }
        let areaName = wx.getStorageSync('positionParams').areaName
        this.setData({areaName})
    },


     /* 更新数据 */
     updateList(e) {
        let list = this.data.list.concat(e.detail || [])
        this.setData({ list })
    },
    /* 重置数据 */
    resetList() {
        this.setData({ list: [] })
    },
    // 获取在线人数
    getOnliveNum() {
        getOnlive().then(res=> {
            if(res.code === 0) {
                this.setData({liveNum: res.data})
            }
        })
    },

    // 公告详情
    showAnnouncementDetail() {
        wx.navigateTo({
            url:"/package_first/pages/announceList/index"
        })
    },

    // 获取点击详情
    getCurrentData(dataKey, e) {
        let index = e.currentTarget.dataset.current;
        return this.data[dataKey][index]
    },

    // 进入对应页面
    goPage(e) {
        let title = e.currentTarget.dataset.title
        if(title !== '敬请期待') {
            // 判断是否有权限
            let tenant = wx.getStorageSync('tenant')
            let path = e.currentTarget.dataset.path
            let type = e.currentTarget.dataset.type
            if(Object.keys(tenant).length) {
                if(type === 'tabbar') {
                    wx.switchTab({
                      url: path,
                    })
                } else if(type === 'mini'){
                    const { tenantCode, tenantId } = wx.getStorageSync('tenant')
                    if(tenantId === 5){
                        wx.navigateToMiniProgram({
                            appId: 'wx57d8902569fbf5e1',
                            path: `pages/index/index?tenantCode=${tenantCode}`,
                            extraData: {
                                tenantCode
                            },
                            envVersion: env,
                            success(res) {
                            }
                          })
                    }else{
                        wx.navigateTo({
                            url: path,
                        })
                    }
                } else {
                    wx.navigateTo({
                      url: path,
                    })
                }
            } else {
                wx.redirectTo({
                    url: `/pages/login/login?path=${path}&fromType=${type}`,
                })
            }
        }
    },

    // 查看更多
    showMoreRivers() {
        wx.switchTab({
          url: '/pages/all/index',
        })
    },

    // 上报问题
    uploadProblem(e) {
        let detail = e.currentTarget.dataset.detail
        let openid = wx.getStorageSync('openid')
        if(openid) {
            wx.navigateTo({
                url: '/package_A/pages/report/index?data=' + JSON.stringify(detail),
              })
        } else {
            getOpenidData().then(res=> {
                wx.navigateTo({
                  url: '/package_A/pages/report/index?data=' + JSON.stringify(detail),
                })
            })
        }
    },

    /* 跳转至筛选页 */
    goSelectLocation(e) {
        const path = '/pages/FirstPage/index'
        wx.redirectTo({
          url: `/pages/login/login?path=${path}`,
        })
    },

     /* 跳转至详情页 */
     showDetail(e) {
        let data = e.currentTarget.dataset.item
        wx.navigateTo({
          url: '/package_A/pages/riverDetail/index?data='+ JSON.stringify(data),
        })
    },

    /* 跳转至积分规则页面 */
    showRules() {
        wx.navigateTo({
          url: '/package_first/pages/RulesPage/index',
        })
    }
})