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

  /**
   * 页面的初始数据
   */
  data: {
    mask_state: false,
    more_state: false,
    tab_current: 1,
    open_user_id: '',
    look: 0,
    look_type: '',
    state: false,
    state2: false,
    page: 1,
    status: 1,
    secret_state: false
  },


  //点赞
  likeCharge(e) {
    var id = e.currentTarget.dataset.id
    var index = e.currentTarget.dataset.index
    var list = this.data.list
    var status = ''
    if (list[index].user_support == 1) {
      list[index].user_support = 0
      status = 2
    } else {
      list[index].user_support = 1
      status = 1
    }
    this.setData({
      list: list
    })
    this.like(id, status)
  },

  like(id, status) {
    let url = '/home/index/support'
    let params = {
      list_id: id,
      status: status
    }
    let header = {
      "XX-Token": wx.getStorageSync('token'),
      'XX-Device-Type': 'wxapp'
    }
    app.post(url, params, header).then((res) => {
      if (res.data.code == 20000) {
        this.getData()
      }
    })
  },


  //收藏,取消收藏
  collectInfo(e) {
    var id = e.currentTarget.dataset.id
    var index = e.currentTarget.dataset.index
    var list = this.data.list
    var status = ''
    if (list[index].user_collect == 1) {
      list[index].user_collect = 0
      status = 2
    } else {
      list[index].user_collect = 1
      status = 1
    }
    this.setData({
      list: list
    })
    this.collect(id, status)
  },

  collect(id, status) {
    let url = '/home/index/collect'
    let params = {
      list_id: id,
      status: status
    }
    let header = {
      "XX-Token": wx.getStorageSync('token'),
      'XX-Device-Type': 'wxapp'
    }
    app.post(url, params, header).then((res) => {
      console.log(res)
      if (res.data.code == 20000) {
        this.getData()
      }
    })
  },

  goDetail(e) {
    var id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/index/detail/detail?id=' + id,
    })
  },

  complain(e) {
    var id = e.currentTarget.dataset.id
    var status = e.currentTarget.dataset.status
    var list = this.data.list
    var index = e.currentTarget.dataset.index
    let url = '/home/index/updateFashion'
    let params = {
      list_id: id,
      status: status
    }
    let header = {
      "XX-Token": wx.getStorageSync('token'),
      'XX-Device-Type': 'wxapp'
    }
    app.post(url, params, header).then((res) => {
      if (res.data.code == 20000) {
        this.getData()
        wx.showToast({
          title: '设置成功',
        })
      }else{
          wx.showToast({
            title: '设置失败',
            icon:'none'
          })
      }
    })
  },

  cancleMask() {
    this.setData({
      mask_state: false
    })
  },

  editInfo() {
    wx.navigateTo({
      url: '/pages/index/info/info',
    })
  },

  showMore(e) {
    var index = e.currentTarget.dataset.index
    var list = this.data.list
    list[index].more_state = !(list[index].more_state)
    this.setData({
      list: list
    })
  },

  goBox() {
    wx.navigateTo({
      url: '/pages/my/draft/draft',
    })
  },

  goList(e) {
    var status = e.currentTarget.dataset.status
    wx.navigateTo({
      url: '/pages/index/fans/fans?status=' + status,
    })
  },

  goMessage() {
    wx.navigateTo({
      url: '/pages/index/message/message',
    })
  },

  //评论
  review(e) {
    var id = e.currentTarget.dataset.id
    var type = e.currentTarget.dataset.type
    wx.navigateTo({
      url: '/pages/index/comment/comment?id=' + id + '&type=' + type,
    })
  },

  chargeTime(e) {
    var current = e.currentTarget.dataset.current
    var tab_current = this.data.tab_current
    if (current == 1) {
      this.setData({
        state: !(this.data.state)
      })
    }
    if (current == 4) {
      this.setData({
        state2: true
      })
    } else {
      this.setData({
        state2: false
      })
    }
    if (tab_current == current) {
      return false
    } else {
      this.setData({
        tab_current: current,
        status: current,
      })
      this.getData()
    }
  },

  getData2() {
    let url = '/home/index/userPage'
    let params = {
      open_user_id: this.data.open_user_id,
      look: this.data.look,
      look_type: this.data.look_type,
      page: this.data.page
    }
    let header = {
      "XX-Token": wx.getStorageSync('token'),
      'XX-Device-Type': 'wxapp'
    }
    app.post(url, params, header).then((res) => {
      var user_id = res.data.data.open_user_id
      wx.setStorageSync('user_id', user_id)
      if (res.data.code == 20000) {
        this.setData({
          avatar: res.data.data.avatar,
          fans_num: res.data.data.fans_num,
          host: res.data.data.host,
          host_num: res.data.data.host_num,
          signature: res.data.data.signature,
          user_nickname: res.data.data.user_nickname
        })
      }
    })
  },

  getData() {
    let url = '/home/index/checkPage'
    let params = {
      open_user_id: this.data.open_user_id,
      status: this.data.status,
      page: this.data.page
    }
    let header = {
      "XX-Token": wx.getStorageSync('token'),
      'XX-Device-Type': 'wxapp'
    }
    app.post(url, params, header).then((res) => {
      if (res.data.code == 20000) {
        const list = res.data.data.list.map((item, index) => {
          if (item.open_time) {
            var time = item.open_time.split(' ');
            return { ...item,
              date: time
            }
          } else {
            return { ...item,
              time: item.time
            }
          }
        });
        for (var i in list) {
          list[i].secret_state = false
        }
        console.log('list', list)
        this.setData({
          list: list
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.getData()
    this.getData2()

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.getData()
    this.getData2()
  },

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

  },

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

  },

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

  },

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

  },

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

  }
})