// pages/myAddress/myAddress.js
const app = getApp()
const wxService = require('../../utils/wxService')
const utils = require('../../utils/util')
const envInfo = require('../../config/index').envInfo

wxService.page({

  /**
   * 页面的初始数据
   */
  data: {
    isSelect:0,
    isScroll: true,
    addressList:[],
    startX: '',
    delBtnWidth: 132,//删除按钮宽度单位（rpx）
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.hideShareMenu()
  },
  handelCheckShopAddress(e) {
    const index = e.currentTarget.dataset.index
    const item = this.data.addressList[index]
    item.fullArea = item.province + ' ' + item.city + ' ' + item.district
    wx.setStorageSync('_defalutAddress', item)
    let pages = getCurrentPages();
    let beforePage = pages[pages.length - 2];
    beforePage.setData({
      wxAddress: false
    })
    console.log('beforePage+++++++', beforePage)
    setTimeout(() => {
      wxService.router().back()
    }, 300)
  },

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

  // 获取地址列表
  getAddressList(){ 
    const { memberId } = wx.getStorageSync('_baseUserInfo')
    wxService.post(`/member/addressManage/getall`,{
      userId: memberId
    }).then(res => {
      if (!res) return false
      const { result, data } = res.data
      if (result == 0) {
        this.setData({
          addressList: data
        })
      }

    }).finally(() => {
    })
  },


  //获取微信地址
  chooseWxAddress() {
    wx.chooseAddress({
      success: (res) => {
        console.log(res)
        this.setData({
          addressInfo: res
        })
      },
      fail: function (err) {
        console.log(err)
      }
    })
  },

  // 编辑地址
  editAddress(e){
    if (e.currentTarget.dataset.id){
      wxService.router(`/pages/addAddress/addAddress?id=${e.currentTarget.dataset.id}`)
    }
  },

  //设置默认地址
  settingDefalutAddress(e){
    const { memberId } = wx.getStorageSync('_baseUserInfo')
    console.log(e)
    let params = {
      userId: memberId,
      id: e.currentTarget.dataset.id
    }
    if(!e.currentTarget.dataset.type){
      wxService.post(`/member/addressManage/setTolerant`,params).then(res => {
        if (!res) return false
        const { result, data } = res.data
        if (result == 0) {
          this.getAddressList()
        }

      }).finally(() => {
      })
    }
  },

  drawStart: function (e) {
    var touch = e.touches[0]

    for (var index in this.data.addressList) {
      var item = this.data.addressList[index]
      item.right = 0
    }
    this.setData({
      addressList: this.data.addressList,
      startX: touch.clientX,
    })

  },
  drawMove: function (e) {
    var touch = e.touches[0]
    var item = this.data.addressList[e.currentTarget.dataset.index]
    var disX = this.data.startX - touch.clientX

    if (disX >= 20) {
      if (disX > this.data.delBtnWidth) {
        disX = this.data.delBtnWidth
      }
      item.right = disX
      this.setData({
        isScroll: false,
        addressList: this.data.addressList
      })
    } else {
      item.right = 0
      this.setData({
        isScroll: true,
        addressList: this.data.addressList
      })
    }
  },
  drawEnd: function (e) {
    var item = this.data.addressList[e.currentTarget.dataset.index]
    if (item.right >= this.data.delBtnWidth / 2) {
      item.right = this.data.delBtnWidth
      this.setData({
        isScroll: true,
        addressList: this.data.addressList,
      })
    } else {
      item.right = 0
      this.setData({
        isScroll: true,
        addressList: this.data.addressList,
      })
    }
  },

  deleteAddress(e){
    console.log(e)
    var that = this
    let id = e.currentTarget.dataset.id
    wx.showModal({
      title: '提示',
      content: '确认删除该地址吗？',
      success: function (res) {
        if (res.confirm) {
          console.log('用户点击确定')
          wxService.post(`/member/addressManage/delete`,{
            id: id
          }).then(res => {
            const { result, data } = res.data
            if (result == 0) {
              wx.showToast({
                title: `删除成功`,
                icon: 'none'
              })
              that.getAddressList()
            }
          }).finally(() => {
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  }
})