var app = getApp();
Page({
  data: {
    appList:[],//当前app列表
    happyList: [],
    studyList: [],
    gameList: [],
    tabId: 0,
    currentType: 'entertainment',// 当前列表类型
  },
  onLoad: function (options) {
    console.log(options.currentType)
    this.setData({
      currentType: options.currentType
    })
    if(options.currentType=='entertainment'){
      this.setData({
        tabId: 0
      })
    }else if(options.currentType=='study'){
      this.setData({
        tabId: 1
      })
    }else{
      this.setData({
        tabId: 2
      })
    }
    this.appTypeList(options.currentType);
  },

  // 切换应用类型
  tabClick: function (e) {
    // console.log(e.currentTarget.dataset.tabid)
    this.setData({
      tabId: e.currentTarget.dataset.tabid
    })
    if(e.currentTarget.dataset.tabid==0){
      // 娱乐应用
      this.setData({
        currentType: 'entertainment'
      })
      // 获取列表
      this.appTypeList('entertainment');
    }else if(e.currentTarget.dataset.tabid==1){
      // 学习应用
      this.setData({
        currentType: 'study'
      })
      // 获取列表
      this.appTypeList('study');
    }else if(e.currentTarget.dataset.tabid==2){
      // 游戏应用
      this.setData({
        currentType: 'game'
      })
      // 获取列表
      this.appTypeList('game');
    }
  },
  appTypeList: function (type) {
    let me = this;
    // 请求前加Loading
    wx.showLoading({
      title: '加载中',
      mask: true
    })
    wx.request({
      url: app.globalData.apiUrl + 'apps/appList',
      method: 'POST',
      data: {
        token: wx.getStorageSync('token'),
        type: type // study：学习应用、entertainment：娱乐应用、game：游戏应用
      },
      success: function (res) {
        // 请求成功后关闭Loading
        wx.hideLoading();
        if(res.data.errno==200){
          me.setData({
            listUpdataStatus: false
          })
          if(type=='study'){
            me.setData({
              studyList: res.data.data.appList,
              appList: res.data.data.appList
            })
          }else if(type=='entertainment'){
            me.setData({
              happyList: res.data.data.appList,
              appList: res.data.data.appList
            })
          } else if(type=='game'){
            me.setData({
              gameList: res.data.data.appList,
              appList: res.data.data.appList
            })
          }
        }else{
          wx.showModal({
            title: '提示',
            showCancel: false,
            content: res.data.msg
          })
        }
      }
    })
  },
  appSwitch: function (e) {// 应用开关
    console.log(e.currentTarget.dataset.appid+'，'+e.detail.value)
    console.log('应用开关', e.detail.value)
    let me = this,
        setAppStatus = 1,
        appId = e.currentTarget.dataset.appid;
    if(e.detail.value){
      // app使用状态，1：启用、0、禁用
      setAppStatus = 1
    }else{
      setAppStatus = 0
    }
    // 请求前加Loading
    wx.showLoading({
      title: '加载中',
      mask: true
    })
    wx.request({
      url: app.globalData.apiUrl + 'Apps/setApp',
      method: 'POST',
      data: {
        token: wx.getStorageSync('token'),
        status: setAppStatus,
        appId: appId
      },
      success: function (res) {
        // 请求成功后关闭Loading
        wx.hideLoading();
        if(res.data.errno==200){
        }else{
          me.appTypeList(me.data.currentType)
          wx.showModal({
            title: '提示',
            showCancel: false,
            content: res.data.msg
          })
        }
      }
    })
  },
  // 完成设置
  settingDone: function (e){
    // 跳转首页
    wx.reLaunch({
      url: '/pages/index/index'
    })
  },
})