// pages/mine/index/index.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    nickName: '昵称',
    name: '用户名称',
    userIcon: '../../../images/ic_user_default.png',
    menuList: [],
    imgUrl: app.urls.baseImgUrl,
    Height: 0,
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var _self = this
    _self.setData({
      nickName: app.globalData.userInfo.name,
      name: app.globalData.userInfo.name
    })
  },
  getItemList() {
    wx.showLoading({
      title: '加载中...',
    })
    var _self = this
    app.http.get(app.urls.getMenuList, {
        header: {
          token: app.globalData.token
        }
      })
      .then(res => {
        var data = res.data
        wx.stopPullDownRefresh({})
        wx.hideLoading({})
        data.forEach(it => {
          it.configColumnList.sort((a, b) => a.configColumnOrder - b.configColumnOrder)
        })
        _self.setData({
          menuList: data
        })
      })
      .catch(err => {
        console.log(err)
        wx.stopPullDownRefresh({})
        wx.hideLoading({})
      })
  },
  checkPermission() {
    var _self = this
    try {
      var names = wx.getStorageSync('name')
      var icon = wx.getStorageSync('userIcon')
      if (names) {
        this.setData({
          name: names,
          userIcon: icon
        })
      } else {
        wx.showModal({
          title: '提示',
          content: '小程序需要您微信头像以及昵称用于展示,请授权.',
          showCancel: false,
          success(res) {
            console.log(res)
            if (res.confirm) {
              // _self.getUserProfile(3)
            }
          }
        })
      }
    } catch (error) {
      wx.showToast({
        title: '获取信息失败',
        icon: 'error'
      })
    }
  },
  getUserProfile(e) {
    let _self = this
    wx.getUserProfile({
      desc: '获取微信头像以及昵称用于展示',
      success: (res) => {
        wx.setStorage({
          data: res.userInfo.nickName,
          key: 'name',
        })
        wx.setStorage({
          data: res.userInfo.avatarUrl,
          key: 'userIcon',
        })
        _self.setData({
          name: res.userInfo.nickName,
          userIcon: res.userInfo.avatarUrl
        })
      },
      fail(err) {
        //判断是否绑定手机号
        wx.showToast({
          title: '获取个人信息失败',
          icon: 'error',
        })
      }
    })
  },
  addShop() {
    wx.navigateTo({
      url: '../shop/addshop',
    })
  },
  onShow() {
    var name = wx.getStorageSync('name')
    var userIcon = wx.getStorageSync('userIcon')
    if (name) {
      this.setData({
        name: name,
        userIcon: userIcon
      })
    }
    this.getItemList()
  },
  //item 状态  1 待付款 2待发货  3待收货  4售后/退款
  showOrder(e) {
    var item = e.currentTarget.dataset.item
    console.log(item)
    var path = '/pages/mine/order/orderlist?status=' + item
    wx.navigateTo({
      url: path,
    })
  },
  choosePage(e) {
    // 栏目列表页跳转方式1:小程序页面,2:其他小程序,3:网址
    var path = e.currentTarget.dataset.path
    var type = e.currentTarget.dataset.type
    var item = e.currentTarget.dataset.item
    switch (type) {
      case '1': //跳转小程序内部
      case '3': //链接
        wx.navigateTo({
          url: path + '?id=' + item.configColumnId + '&mode=' + item.configTableMode + '&type=' + item.configColumnType + '&title=' + item.configColumnName
        })
        break
      case '2': //其他小程序
        wx.navigateToMiniProgram({
          appId: '',
          success(res) {
            // 打开成功
          }
        })
        break
    }
  },
  onPullDownRefresh() {
    this.getItemList()
  },
  //跳转到二级栏目页面
  goColumnList(e) {
    var item = e.currentTarget.dataset.item
    wx.navigateTo({
      url: '../columnlist/columnlist?id=' + item.configColumnGroupId + '&title=' + item.configColumnGroupName,
    })
  },
  // 跳转到 我的名片
  goMyCard: function () {
    wx.navigateTo({
      url: '../myCard/myCard',
    })
  },
  //跳转设置页面
  showSetting() {
    wx.navigateTo({
      url: '../../mine/setting/columnsetting'
    })
  },
  imgHeight(e) {
    var winWid = wx.getSystemInfoSync().windowWidth; //获取当前屏幕的宽度
    var imgh = e.detail.height; //图片高度
    var imgw = e.detail.width; //图片宽度
    var swiperH = winWid * imgh / imgw * 0.88 + "px"

    this.setData({
      Height: swiperH //设置高度
    })
  },
  //引导跳转
  toFunction(e) {
    var _self = this
    var path = ''
    _self.data.menuList.forEach(it => {
      //判断引导
      if (it.configColumnGroupDirection == '3') {
        //引导
        path = it.configColumnGroupTargetUrl
      }
    })
    wx.navigateTo({
      url: path,
    })
  }
})