/*
 * @Descripttion: HJ
 * @Company: 思宏科技
 * @version: v1.0
 * @Author: HJ
 * @Date: 2021-06-17 10:37:48
 * @LastEditors: HJ
 * @LastEditTime: 2021-06-25 14:05:14
 */
import { fhGetAddressList } from "../../api/address/index";

const componentOptions = {
  // 组件选项
  options: {
    multipleSlots: true,
  },
  behaviors: [],
  properties: {
    customStyle: {
      type: String,
      value: "",
    },
    selectId: {
      type: [Number, String],
      value: null,
      observer(id) {
        this.setData({
          currentId: id,
        });
      },
    },
    showRadio: {
      type: Boolean,
      value: true
    },
  },
  // 组件数据
  data: {
    isPageHidden: false, // 页面是否处于隐藏状态
    // 收货地址列表
    addressList: [],
    // 加载中
    loading: false,
    // 当前选中的id
    currentId: null,
  },
  // 数据监听器
  observers: {},
  // 组件方法
  methods: {
    init() {},
    // 获取所有收货地址
    async getAddressList() {
      this.setData({
        loading: true,
      });
      try {
        const { results } = await fhGetAddressList();
        this.setData({
          addressList: results,
        });
      } catch (e) {
        console.log("debug axios", e);
      }
      this.setData({
        loading: false,
      });
    },
    // 跳转
    nav({ currentTarget: { dataset } }) {
      const { type = "add", info = "" } = dataset;
      wx.$routerTo({
        url: "/pages/address/manage/index",
        query: {
          type,
          ...info,
        },
      });
    },
    // 点击一个地址
    clickItem({ currentTarget: { dataset } }) {
      const { info } = dataset;
      this.triggerEvent("change", info);
    },
  },
  // 组件生命周期
  lifetimes: {
    created() {},
    attached() {
      this.init();
    },
    ready() {},
    moved() {},
    detached() {},
  },
  definitionFilter() {},
  // 页面生命周期
  pageLifetimes: {
    // 页面被展示
    show() {
      this.getAddressList();
      const { isPageHidden } = this.data;
      // show事件发生前，页面不是处于隐藏状态时
      if (!isPageHidden) {
        return;
      }
      // 重新执行定时器等操作
    },
    // 页面被隐藏
    hide() {
      this.setData({
        isPageHidden: true,
      });

      // 清除定时器等操作
    },
    // 页面尺寸变化时
    resize() {},
  },
};

Component(componentOptions);
