const { api, imgName, getUserInfo, wxLogin, alertTip, authAgainByUser, userLocation } = require('../../lib/util.js')
Page({
  data: {
    imgHardOn: imgName('security_settings_on_active.png'),
    imgHardOff: imgName('security_settings_on_black.png'),
    imgStandardOn: imgName('security_settings_standard_active.png'),
    imgStandardOff: imgName('security_settings_standard_black.png'),
    imgOn: imgName('security_settings_off_active.png'),
    imgOff: imgName('security_settings_off_black.png'),
    imgWxOn: imgName('security_settings_wx_active.png'),
    imgWxOff: imgName('security_settings_wx_black.png'),
    imgPhoneOn: imgName('security_settings_telephone_active.png'),
    imgPhoneOff: imgName('security_settings_telephone_black.png'),
    checkOn: imgName('security_settings_unchecked_active.png'),
    checkOff: imgName('security_settings_unchecked.png'),
    imgMore: imgName('security_settings_more_white.png'),
    imgMoreAction: imgName('chevrons_white.png'),
    imgVoiceLow: imgName('voice.png'),
    imgVoiceHign: imgName('voice2.png'),
    settings:{
      remindermode:1,
      weixin:true,
      phone:false
    },
    voiceNum: 50,
  },
  onLoad: function () {
    console.log('welcome')
  },
  onShow: function () {
    this.setData({
      voiceNum: wx.getStorageSync('voiceNum')
    })
    this.getAlarmSetting()
  },
  getAlarmSetting(){
    let that = this
    api.post('api/user/userConfigInfo.json')
    .then(res=>{
      console.log(res)
      let datamodel = res.data
      if (!datamodel.telephonealarmnoti){
        api.post('api/user/updateUserConfig.json', { userid: wx.getStorageSync('user').userId, telephonealarmnoti: true })
          .then(res => {
            that.getAlarmSetting()
          })
          .catch(err => {
            alertTip(err)
          })
      }
      that.setData({
        settings: {
          remindermode: datamodel.remindermode,
          weixin: datamodel.wechatnotice,
          phone: datamodel.telephonealarmnoti
        }
      })
    })
    .catch(err=>{
      alertTip(err)
    })
  },
  getWxAlarm(){
    api.post('api/user/isFirstOpenWeChat.json')
    .then(res=>{
      console.log(res.data)
      if(!res.data){
        alertTip('未关注公众号')
      }
    })
    .catch(err=>{
      alertTip(err)
    })

  },
  setSecure(e){
    let This=this
    api.post('api/user/updateUserConfig.json', {userid: wx.getStorageSync('user').userId,...e.currentTarget.dataset})
      .then(res => {
        This.getAlarmSetting()
      })
      .catch(err => {
        alertTip(err)
      })
  },
  //声音变化
  voiceChange(e){
    let that=this
    api.post('api/vehicle/volume.json', { para: String(e.detail.value)})
      .then(res => {
        wx.setStorageSync('voiceNum', e.detail.value)
      })
      .catch(err => {
        wx.setStorageSync('voiceNum', e.detail.value)
      })
  },
  settingDetail(){
    wx.navigateTo({
      url: '/pages/user_alarm_detail_setting/user_alarm_detail_setting',
    })
  }
})