// pages/addressBook/index.js
var app = getApp();
import { customRequest } from '../../utils/request.js';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    items: [
      { value: '1', name: '设为默认' },
      { value: '2', name: '已设为默认', checked: 'true' },
      { value: '3', name: '设为默认' },
    ],
    hasAddress: false,
    recordList: [],
  },

  getList () {
    var that = this;
    customRequest("/user/address/getUserAddressList", { header: 'headers', method: 'POST', data: that.data.searchForm }).then((res) => {
      if (app.isNotEmptyCheck(res.data.data)) {

        that.setData({
          recordList: res.data.data,
          hasAddress: true
        });
      } else {
        that.setData({
          recordList: [],
          hasAddress: false
        });
      }
    })
    // wx.request({
    //     url: app.globalData.ip + "/user/address/getUserAddressList",
    //     method: "POST",
    //     header: app.globalData.headers,
    //     data: that.data.searchForm,
    //     success: function(res) {
    //         console.log(res);


    //     }

    // });
  },
  radioChange (e) {
    console.log('radio发生change事件，携带value值为：', e.detail.value)

    var that = this;
    that.data.recordList.forEach(item => {
      item.defaultFlag = 0;
      if (item.id == e.detail.value) {
        item.defaultFlag = 1;
      }
    });

    this.setData({
      recordList: that.data.recordList
    })
    customRequest("/user/address/updateDefaultFlag", {
      header: 'headers', method: 'POST', data: {
        id: e.detail.value,
        defaultFlag: 1,
      }
    }).then((res) => {

    })
    // wx.request({
    //   url: app.globalData.ip + "/user/address/updateDefaultFlag",
    //   method: "POST",
    //   header: app.globalData.headers,
    //   data: {
    //     id: e.detail.value,
    //     defaultFlag: 1,
    //   },
    //   success: function (res) {
    //     console.log(res);
    //   }

    // });

  },
  addAddress: function () {
    wx.navigateTo({
      url: '/subPage/addAddress/index',
    })
  },
  delAddress: function (e) {
    var that = this;
    var record = e.currentTarget.dataset.record;
    console.log(record);

    wx.showModal({
      title: '提示',
      content: `确定要删除该地址吗？`,
      cancelColor: "#666666",
      confirmColor: "#1890ff",
      success (res) {
        if (res.confirm) {
          console.log('用户点击确定')
          customRequest("/user/address/deleteUserAddress", { header: 'headers', method: 'POST', data: { id: record.id } }).then((res) => {
            that.getList();

          })
          // wx.request({
          //   url: app.globalData.ip + "/user/address/deleteUserAddress",
          //   method: "POST",
          //   header: app.globalData.headers,
          //   data: {
          //     id: record.id
          //   },
          //   success: function (res) {
          //     console.log(res);
          //   }

          // });

        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

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

  },

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

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

  },

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

  },

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

  },

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

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
  return app.sharePageImage()
},
})