// pages/listDel/index.js
import address from '../../../api/address'
const cache = require('../../../utils/cache.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    // x轴方向的偏移
    x: 0,
    // 当前x的值
    currentX: 0,
    addressList: [],
    isreturn: ''
  },

  handleMovableChange: function (e) {
    let index = e.currentTarget.dataset.idx;
    let x = e.currentTarget.dataset.x;

    this.data.addressList[index].currentX = x;
  },

  handleTouchend: function (e) {
    let index = e.currentTarget.dataset.idx;
    let x = e.currentTarget.dataset.x;

    this.isMove = true;
    if (this.data.addressList[index].currentX < -46) {
      this.data.addressList[index].x = -112;
      this.setData({
        addressList: this.data.addressList
      });
    } else {
      this.data.addressList[index].x = 0;
      this.setData({
        addressList: this.data.addressList
      });
    }
  },

  // 创建订单返回
  returncreateorder(e) {
    let addressvalue = e.currentTarget.dataset.address
    let addressId = e.currentTarget.dataset.idx
    console.log(addressId)
    if (this.data.isreturn) {
      
      wx.redirectTo({
     
        url: '../submitOrder/submitOrder?addressvalue='+addressvalue + '&addressId=' + addressId,
      })
    } else {
      return
    }

  },


  handleDeleteAddress(e) {
    console.log('进入删除')
    let index = e.currentTarget.dataset.index;

    address.deladdress({
      mobile: cache.getMobile(),
      addressId: index
    }).then(res => {
      if (res.code == 200) {
        wx.showToast({
          title: '删除成功',
          icon: 'none',
          duration: 3000
        })
        this.getAdressList()

      }

    })
  },
  handleTouchestart: function (e) {},
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    let isreturn = options.isreturn
    console.log(isreturn)
    if (isreturn) {
      this.setData({
        isreturn: isreturn
      })
    }
  },

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

  },

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

  },

  getAdressList() {
    address.getAdressList({
      mobile: cache.getMobile()
    }).then(res => {
      let data = res.data;
      data.forEach(item => {

        item.subName = item.name.substring(0, 1);
        item.x = 0;
        item.currentX = 0;

      });
      this.setData({
        addressList: res.data
      })


    })

  },


  goToAddAddress: function (e) {

    let way = e.currentTarget.dataset.way;
    let addressId = e.currentTarget.dataset.id;
    console.log(addressId)
    if (addressId >= 0) {
      wx.navigateTo({
        url: '/pages/groupmeallist/addOrEditaddress/addOrEditaddress?type=' + way + '&addressId=' + addressId,
      })
    } else {

      wx.navigateTo({
        url: '/pages/groupmeallist/addOrEditaddress/addOrEditaddress?type=' + way,
      })
    }
  },

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

  },

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

  },

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

  },

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

  },

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

  }
})