const DEFAULT_PAGE = 0;
const baseUrl = getApp();
const api = require('../../config/api.js');
const util = require('../../config/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
      id:1,
      title:'我的地址',
      listdata:[], //地址列表
      listdatamorei:[],//地址列表默认地址
      currentdata:[], //当前值
      isnewadd:1,//是否新增，1为新增，2为修改
      checked:true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      wx.setNavigationBarTitle({
        title: this.data.title,
      })
      baseUrl.islogin();
  },

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

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
      this.getaddresslist(wx.getStorageSync('wxuserid'));
      this.getaddresslistmorei(wx.getStorageSync('wxuserid'));
  },

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

  },

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

  },

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

  },

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

  },

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

  },
  onClickShow() {
    this.setData({ show: true });
  },

  onClickHide() {
    this.setData({ show: false });
  },
  submit:function(e){
    console.log(e);
    let isnewadd=this.data.isnewadd;
    let isdefault=this.data.checked?0:1;

    let name=e.detail.value.name;
    let areaname=e.detail.value.areaname;
    let phone=e.detail.value.phone;
    let address=e.detail.value.address;

    if(isnewadd==1){

  //修改
         //得到当前数据
         const that = this;
         let url = baseUrl.globalData.baseUrl + '/myaddress/addressadd';       
         var param = {
           name:name,
           phone:phone,
           region:areaname,
           addressinfo:address,
           isdefault:isdefault
         };
         //此处为使用封装的post请求
         util.post(url, param).then((res) => {
           // console.log(res);   
           // that.setData({
           //   currentdata:res.data
           // });
         }).catch((errMsg) => {
           console.log(errMsg);
         });
        

    }else{

        //修改
         //得到当前数据
         const that = this;
         let id=that.data.id;
         let url = baseUrl.globalData.baseUrl + '/myaddress/updateaddress';
       
         var param = {
           id:id,
           name:name,
           phone:phone,
           region:areaname,
           addressinfo:address,
           isdefault:isdefault
         };
         //此处为使用封装的post请求
         util.post(url, param).then((res) => {
           // console.log(res);   
           // that.setData({
           //   currentdata:res.data
           // });
         }).catch((errMsg) => {
           console.log(errMsg);
         });
    }
    this.setData({ show: false });
    wx.navigateTo({
      url: '/pages/myaddress/myaddress',
    })
  },
  modfiyclick:function(params) {
    console.log(params.currentTarget.id);
    let id=params.currentTarget.id;
    this.setData({ 
      show: true,
      isnewadd:2,
      id:id
     });
    this.getcurrentdata(id);

  },
  delclick:function(params) {
    console.log(params);
    this.deletefun(params.currentTarget.id);
    wx.navigateTo({
      url: '/pages/myaddress/myaddress',
    })
  },
  onChange({ detail }) {
    // 需要手动对 checked 状态进行更新
    console.log(detail)
    this.setData({ checked: detail });
  },
  getaddresslist:function(params) {
      const that = this;
      let url = baseUrl.globalData.baseUrl + '/myaddress/myaddresslist';
      var param = {
        user_id:params
      };
      //此处为使用封装的post请求
      util.post(url, param).then((res) => {
        console.log(res);   
        that.setData({
          listdata:res.data
        });
      }).catch((errMsg) => {
        console.log(errMsg);
      });
  },
  getaddresslistmorei:function(params) {
        const that = this;
        let url = baseUrl.globalData.baseUrl + '/myaddress/myaddresslistdefault';
        var param = {
          user_id:params
        };
        //此处为使用封装的post请求
        util.post(url, param).then((res) => {
          console.log(res);   
          that.setData({
            listdatamorei:res.data
          });
        }).catch((errMsg) => {
          console.log(errMsg);
        });
    },getcurrentdata:function(params) {
      //得到当前数据
        const that = this;
        let url = baseUrl.globalData.baseUrl + '/myaddress/getmyaddressbyid';
        var param = {
          id:params
        };
        //此处为使用封装的post请求
        util.post(url, param).then((res) => {
          console.log(res);   
          that.setData({
            currentdata:res.data
          });
        }).catch((errMsg) => {
          console.log(errMsg);
        });
    },
    deletefun:function(params) {

          //得到当前数据
        const that = this;
        let url = baseUrl.globalData.baseUrl + '/myaddress/myaddresslistdelete';
        var param = {
          id:params
        };
        //此处为使用封装的post请求
        util.post(url, param).then((res) => {
          // console.log(res);   
          // that.setData({
          //   currentdata:res.data
          // });
        }).catch((errMsg) => {
          console.log(errMsg);
        });
    }
})