// pages/emergency/resourse/index/index.js
const app = getApp();

Component({
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    condition: '',
    curTab: 0,
    tabs: [],

    canChange: false,
    freshIng: false,
    page: 1,
    pageSize: 10,
    list: []
  },

  pageLifetimes: {
    show: function () {
      this.getType();
    },
  },

  /**
   * 组件的方法列表
   */
  methods: {

    /**
     * 搜索
     */
    inputSearch: function (e) {
      this.data.condition = e.detail.value;
      this.refresh();
    },

    /**
     * 切换筛选
     */
    tabSelect: function (params) {
      const index = Number.parseInt(params.currentTarget.dataset.index);
      this.data.curTab = index;
      this.setData(this.data)
      this.refresh();
    },

    /**
     * 刷新
     * @param {*} params 
     */
    refresh: function (params) {
      this.data.page = 1;
      this.data.list = [];
      this.setData(this.data)
      this.getList();
    },

    /**
     * 获取列表
     */
    getList: function () {
      const params = {
        page: this.data.page,
        limit: this.data.pageSize,
        othtypeid: this.data.tabs[this.data.curTab].emerTypeId,
        planname: this.data.condition,
        userId: app.globalData.userInfo.userId
      }
      app.axios("GET", "app", "/Othplan/getOthplanPage", params, false).then(res => {
        this.data.freshIng = false;
        if (res.code == 1) {
          let page = Number.parseInt(res.data.pageNum);
          if (this.data.page == 1) {
            this.data.list = res.data.list;
          } else {
            var list = this.data.list;
            if (this.data.page == page) this.data.list = [...list, ...res.data.list]
          }
          if (res.data.list?.length > 0) this.data.page = page + 1
        }
        this.setData(this.data)
      })
    },

    /**
     * 获取预案类型
     */
    getType: function () {
      app.axios("GET", "app", "/Othplan/getOthtypeList", {}, false).then(res => {
        if (res.code == 1) {
          console.log(res,'res==');
          this.data.tabs = res.data;
          this.setData(this.data)
        }
        this.setData(this.data)
      })
    }
  }
})