import { getEncyclopediaList } from "../../../../service/system";
import { alertInfo, alertSuccess, loadingFunc, wxLogin } from "../../../../utils/index";
import request from "../../../../utils/request"
const { OK } = request;
// pages/encyclopedia/components/item/index.js
Component({

  /**
   * 组件的属性列表
   */
  properties: {
    id: String
  },

  /**
   * 组件的初始数据
   */
  data: {
    page: 1,
    list: [],
  },
  lifetimes: {
    attached() {
      this.getData();
    }
  },
  /**
   * 组件的方法列表
   */
  methods: {
    async init() {
      const page = this.data.page;
      const id = this.id;
      const { code, data, message, total } = await getEncyclopediaList(id, page);
      if (code !== OK) {
        alertInfo(message)
        return;
      }
      this.setData({ list: data, totalPage: Math.ceil(total / 20) });
    },
    async getData() {
      loadingFunc(async() => {
        await this.init();
      })
    },
    jumpToDetail(e) {
      wx.navigateTo({
        url: `/pages/encyclopediaDetail/index?id=${e.currentTarget.dataset.data.id}`,
      })
    },
    async onChangePage(e) {
      const page = e.detail.currentIndex;
      const that = this;
      this.setData({
        page
      }, () => {
        that.getData();
      })
    },
  }
})