// packageG/mycomponent/selfpick/selfpick.js
const app = getApp();
var location = require("../../../mybehaviors/location/location");
Component({
  behaviors: [location],
  /**
   * 组件的属性列表
   */
  properties: {
    datas: {
      type: null
    },
    component_id: {
      type: null
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    activeID: 0,
    deliverInfo: {
      deliver_name: "正在获取中...",
      loading: true //正在获取数据
    }
  },


  lifetimes: {
    // 生命周期函数，可以为函数，或一个在methods段中定义的方法名
    attached() {
      this.initData();
    },
    moved() {},
    detached() {},

  },
  pageLifetimes: {
    show: function() {
      // 页面被展示
      this.initData();
    },
    hide: function() {
      // 页面被隐藏
    },
    resize: function(size) {
      // 页面尺寸变化
    }
  },

  /**
   * 组件的方法列表
   */
  methods: {
    gotoUrl() {
      if (this.data.deliverInfo.loading) {
        return;
      }
      wx.navigateTo({
        url: '/packageC/selfCarry/selectSelfPickupPoint/selectSelfPickupPoint?tag=1'
      });
    },
    initData() {
      let deliverIDStorage = null;
      try {
        deliverIDStorage = wx.getStorageSync('deliverId'); //获取当前会话是否存储了自提点id
      } catch (e) {
        // Do something when catch error
      }
      if (deliverIDStorage) {
        // 当前会话有存储自提点id（可能是链接上储存起来有或者通过手动切换了自提点）
        this.data.activeID = deliverIDStorage;
      }
      this._getLocation(() => {
        this.getNowPackageDeliver();
      });
    },
    getNowPackageDeliver(lat, lng) {
      //获取自提点信息
      let _json = {
        lng: this.data.point.lng||'',
        lat: this.data.point.lat||''
      };
      if (this.data.activeID) {
        _json.deliver_id = this.data.activeID;
      }
      let urlStr = app.getNetAddresss('plugin.package-deliver.frontend.decorate-deliver.getNowPackageDeliver');
      app._postNetWork({
        url: urlStr,
        data: _json,
        success: (resdata) => {
          let res = resdata.data;
          if (res.result == 1) {
            this.setData({
              deliverInfo: res.data,
              total_page: res.data.last_page,
            });
            wx.setStorageSync("deliverId", res.data.id);
          } else {
            wx.showToast({
              title: res.msg,
              duration: 1000,
              icon: 'none'
            });
            this.setData({
              ['deliverInfo.deliver_name']: "请手动切换选择！"
            });
          }
          this.setData({
            ['deliverInfo.loading']: false
          });
        },
        fail: function (res) {
          console.log(res.msg);
        }
      });
    }
  }
});