var app = getApp();
Page({
  data: {
    useArray: ['允许使用全部应用','完成训练计划，允许使用应用','不可使用应用'],
    useIndex: 0,//使用限制
    shieldArray: ['关闭','使用20分钟，护眼5分钟','使用30分钟，护眼5分钟','使用40分钟，护眼5分钟'],
    shieldIndex: 0,//强制护眼
    // shieldValue: [0],
    // shield: '',
    installApp: 0, //安装应用状态，1、允许安装、0、不允许安装


    trainTip: true,
    messageTip: true,
    serveTip: true,
    isShow: true,

    parallaxArray: ['关闭','右眼视差','左眼视差'],
    parallaxIndex: 0,//视差训练

    weakArray: ['关闭','双眼弱视','右眼弱视','左眼弱视'],
    weakIndex: 0,//弱视训练

    constraintTipShow: false,
    useLimitShow: false,
    installAppTipShow: false,
  },
  onLoad: function () {
    // this.demopicker = this.selectComponent("#shieldpicker");
    let me = this;
    // 请求前加Loading
    wx.showLoading({
      title: '加载中',
      mask: true
    })
    wx.request({
      url: app.globalData.apiUrl + 'Settings/index',
      method: 'POST',
      data: {
        token: wx.getStorageSync('token')
      },
      success: function (res) {
        // 请求成功后关闭Loading
        wx.hideLoading();
        if(res.data.errno==200){
          if(res.data.data.loopTrainingtime==40){
            me.setData({
              shieldIndex: 3
            })
          }else if(res.data.data.loopTrainingtime==30){
            me.setData({
              shieldIndex: 2
            })
          }else if(res.data.data.loopTrainingtime==20){
            me.setData({
              shieldIndex: 1
            })
          }else{
            me.setData({
              shieldIndex: 0
            })
          }
          me.setData({
            useIndex: res.data.data.appLockedStatus-1,
            installApp: res.data.data.moreAppStatus,
            parallaxIndex: res.data.data.parallaxStatus-1,
            weakIndex: res.data.data.redLightStatus-1,
          })
        }else{
          wx.showModal({
            title: '提示',
            showCancel: false,
            content: res.data.msg
          })
        }
      }
    })
  },
  useChange: function (e) {// 使用限制
    let me = this,
        useStatus = parseInt(e.detail.value)+1;
    console.log('使用限制')
    console.log(useStatus)
    // 请求前加Loading
    wx.showLoading({
      title: '加载中',
      mask: true
    })
    wx.request({
      url: app.globalData.apiUrl + 'apps/appManagement',
      method: 'POST',
      data: {
        token: wx.getStorageSync('token'),
        status: useStatus
      },
      success: function (res) {
        // 请求成功后关闭Loading
        wx.hideLoading();
        if(res.data.errno==200){
          me.setData({
            useIndex: useStatus-1
          })
        }else{
          me.setData({
            useIndex: me.data.useIndex
          })
          wx.showModal({
            title: '提示',
            showCancel: false,
            content: res.data.msg
          })
        }
      }
    })
  },
  parallaxChange: function (e) {// 视差训练
    let me = this,
        parallaxStatus = parseInt(e.detail.value)+1;
    console.log('视差训练')
    console.log(parallaxStatus)
    // 请求前加Loading
    wx.showLoading({
      title: '加载中',
      mask: true
    })
    wx.request({
      url: app.globalData.apiUrl + 'devices/setIntensiveTraining',
      method: 'POST',
      data: {
        token: wx.getStorageSync('token'),
        status: parallaxStatus
      },
      success: function (res) {
        // 请求成功后关闭Loading
        wx.hideLoading();
        if(res.data.errno==200){
          me.setData({
            parallaxIndex: parallaxStatus-1
          })
          if(parallaxStatus!=1){
            wx.showModal({
              title: '重要提示',
              showCancel: false,
              content: '视差训练仅适用于双眼视差大于0.2的情况'
            })
          }
        }else{
          me.setData({
            parallaxIndex: me.data.parallaxIndex
          })
          wx.showModal({
            title: '提示',
            showCancel: false,
            content: res.data.msg
          })
        }
      }
    })
  },
  weakChange: function (e) {// 弱视训练
    let me = this,
        weakStatus = parseInt(e.detail.value)+1;
    console.log('弱视训练')
    console.log(weakStatus)
    // 请求前加Loading
    wx.showLoading({
      title: '加载中',
      mask: true
    })
    wx.request({
      url: app.globalData.apiUrl + 'devices/setRedLightTraining',
      method: 'POST',
      data: {
        token: wx.getStorageSync('token'),
        status: weakStatus
      },
      success: function (res) {
        // 请求成功后关闭Loading
        wx.hideLoading();
        if(res.data.errno==200){
          me.setData({
            weakIndex: weakStatus-1
          })
          if(weakStatus!=1){
            wx.showModal({
              title: '重要提示',
              showCancel: false,
              content: '如无弱视情况，请勿开启此训练！\r\n（原理：使用红灯闪烁的方式,激活黄斑区视觉细胞感光能力）'
            })
          }
        }else{
          me.setData({
            weakIndex: me.data.weakIndex
          })
          wx.showModal({
            title: '提示',
            showCancel: false,
            content: res.data.msg
          })
        }
      }
    })
  },
  shieldChange: function (e) {// 强制护眼
    let me = this,
        shieldIndex = parseInt(e.detail.value)+1;
    console.log('强制护眼')
    console.log(shieldIndex)
    // 请求前加Loading
    wx.showLoading({
      title: '加载中',
      mask: true
    })
    wx.request({
      url: app.globalData.apiUrl + 'apps/eyeProtection',
      method: 'POST',
      data: {
        token: wx.getStorageSync('token'),
        status: shieldIndex
      },
      success: function (res) {
        // 请求成功后关闭Loading
        wx.hideLoading();
        if(res.data.errno==200){
          me.setData({
            shieldIndex: shieldIndex-1
          })
        }else{
          me.setData({
            shieldIndex: me.data.shieldIndex
          })
          wx.showModal({
            title: '提示',
            showCancel: false,
            content: res.data.msg
          })
        }
      }
    })
  },
  installApp: function (e) {// 安装应用
    console.log('安装应用', e.detail.value)
    let me = this,
        installAppStatus = 0;
    if(e.detail.value){
      // 安装应用状态，1、允许安装、0、不允许安装
      installAppStatus = 1
    }else{
      installAppStatus = 0
    }
    // 请求前加Loading
    wx.showLoading({
      title: '加载中',
      mask: true
    })
    wx.request({
      url: app.globalData.apiUrl + 'apps/installApp',
      method: 'POST',
      data: {
        token: wx.getStorageSync('token'),
        status: installAppStatus
      },
      success: function (res) {
        // 请求成功后关闭Loading
        wx.hideLoading();
        if(res.data.errno==200){
          me.setData({
            installApp: installAppStatus
          })
        }else{
          me.setData({
            installApp: me.data.installApp
          })
          wx.showModal({
            title: '提示',
            showCancel: false,
            content: res.data.msg
          })
        }
      }
    })
  },
  // 使用限制疑问
  useLimit: function () {
    this.setData({
      useLimitShow: true
    })
  },
  closeUseLimit: function () {
    this.setData({
      useLimitShow: false
    })
  },
  // 强制护眼疑问
  constraint: function () {
    this.setData({
      constraintTipShow: true
    })
  },
  closeConstraintTip: function () {
    this.setData({
      constraintTipShow: false
    })
  },
  // 安装应用疑问
  installAppTip: function () {
    this.setData({
      installAppTipShow: true
    })
  },
  closeInstallAppTip: function () {
    this.setData({
      installAppTipShow: false
    })
  },
  toWhiteList: function(e) {
    wx.navigateTo({
      url: '/pages/whiteList/whiteList?currentType='+e.currentTarget.id
    })
  },
  // 完成设置
  settingDone: function (e){

    wx.showModal({
      title: '重要提示',
      showCancel: true,
      content: '请确认是否保存',
      success (res) {
        if (res.confirm) {
          // console.log('用户点击确定')
          // 跳转首页
          wx.reLaunch({
            url: '/pages/index/index'
          })
        } else if (res.cancel) {
          // console.log('用户点击取消')
        }
      }
    })
  },

  // shieldTap: function (e) {
  //   this.demopicker.showPicker();
  // },
  // confirm:function(e){
  //   console.log(e.detail)
  //   this.setData({
  //     shieldValue: e.detail
  //   })
  //   // this.demopicker.hiddePicker();
  // },
  
})
