import {BASE_URL} from '../../utils/request';
import { uploadImage } from '../../utils/upload.js';
import { MAKE_PHONE_CALL } from '../../utils/util.js';
import {userModify} from '../../api/user';
const app = getApp();
Page({
  data: {
    menuList: [
      { icon: '/static/image/me-1.png', title: '活动规则' },
      { icon: '/static/image/me-2.png', title: '免责声明' },
      { icon: '/static/image/me-3.png', title: '报名记录' },
      { icon: '/static/image/me-4.png', title: '优惠券' },
      { icon: '/static/image/me-5.png', title: '设置' },
      { icon: '/static/image/me-6.png', title: '联系客服' }
    ],
    userInfo: {},
    programConfig:{},
    url:BASE_URL,
    showAgreementModal:false
  },
  goPage(e){
    const index = e.currentTarget.dataset.index;
    const pageMap = {
      0: `/pages/rules/rules?type=${index}`,
      1: `/pages/rules/rules?type=${index}`,
      2: `/pages/registrationRecords/registrationRecords`,
      3: `/pages/coupon/coupon`,
      4: `/pages/setting/setting`
    };
    if (pageMap[index]) {
      wx.navigateTo({ url: pageMap[index] });
    } else if (index === 5) {
      this.setData({ showAgreementModal: true });
    }
  },
  onLoad(options) {
    this.setData({
      userInfo: app.globalData.userInfo,
      programConfig:app.globalData.programConfig
    });
  },
  onShow() {
    if (typeof this.getTabBar === 'function' && this.getTabBar()) {
      this.getTabBar().setData({ selected: 2 })
    }
    this.setData({
      userInfo: app.globalData.userInfo
    });
  },
   // 头像
   async onChooseAvatar(e) {
    const { avatarUrl } = e.detail;
    // 上传到服务器
    const res = await uploadImage(avatarUrl);
    this.setData({
      "userInfo.avatar":avatarUrl
    });
    this.onSave(res.path);
  },
  async onSave(pathUrl){
    const res = await userModify({avatar:pathUrl});
    if(res.code == 200){
      wx.showToast({
        title: '更新成功',
        icon: 'none',
        duration: 2000
      });
      app.globalData.userInfo = res.data;
    }else{
      wx.showToast({
        title: res.message || '更新失败',
        icon: 'none',
        duration: 2000
      });
    }
  },
  onPhone(){
    MAKE_PHONE_CALL('15527606226');
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})