import { getAidList } from "../../service/system";
import { alertInfo, alertSuccess, loadingFunc } from "../../utils/index";
import request from '../../utils/request'

const { OK } = request;

// components/aid/index.js
Component({

  /**
   * 组件的属性列表
   */
  properties: {
    type: Number,
    bannerType: Number,
  
  },
  lifetimes: {
    attached() {
      this.init();
    }
  },
  /**
   * 组件的初始数据
   */
  data: {
    park: {},
    list: [],
    total: 0,
    page: 1,
    size: 20,
  },
  lifetimes: {
    attached() {
      loadingFunc(async () => {
        await this.init();
      })
    }
  },
  /**
   * 组件的方法列表
   */
  methods: {
    async init() {
      const { type, page, size } = this.data;
      const { code, message, data = [], total } = await getAidList(page, size, type);
      if (code !== OK) {
        alertInfo(message)
        return
      }
      this.setData({
        list: data,
        total,
        totalPage: Math.ceil(total / size)
      })
    },
    onRefresh() {
      const that = this;
      this.setData({
        page: 1,
      }, () => {
        that.init();
      })
    },
    jumpToDetail(e) {
      const { id = "" } = e.currentTarget.dataset;
      const { type } = this.data;
      wx.navigateTo({
        url: `/pages/aid/detail/index?id=${id}&type=${type}`,
      })
    },
    consult(e) {
      const { id = "" } = e.currentTarget.dataset;
      const { type } = this.data;
      wx.navigateTo({
        url: `/pages/aid/consult/index?id=${id}&type=${type}`,
      })
    },
    async onChangePage(e) {
      const page = e.detail.currentIndex;
      const that = this;
      this.setData({
        page
      }, () => {
        that.init();
      })
    },
  }
})