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

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

  },

  /**
   * 组件的初始数据
   */
  data: {
    condition: '',

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

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

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

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

    /**
     * 切换筛选
     */
    tabSelect: function (params) {
      if (!this.data.canChange) return
      this.data.canChange = false;
      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();
    },

    /**
     * 获取定位
     */
    getLoc: function () {
      wx.getLocation({
        altitude: true,
        highAccuracyExpireTime: 0,
        isHighAccuracy: true,
        type: 'type',
        success: (result) => {
          this.data.latlng = result;
        },
        complete: () => {
          this.getList();
        }
      })
    },

    toGuid: function (params) {
      const item = params.currentTarget.dataset.item;
      console.log(item,'item===>');
      if (!item.lat || !item.lon) {
        wx.showToast({
          title: '未采集位置',
          icon: 'none',
          duration: 2000
        })
        return
      }
      wx.openLocation({
        latitude: item.lat,
        longitude: item.lon,
        scale: 18,
      });
    },

    /**
     * 获取列表
     */
    getList: function () {
      const params = {
        page: this.data.page,
        limit: this.data.pageSize,
        lat: this.data.latlng.latitude,
        lon: this.data.latlng.longitude,
        teamname: this.data.condition
      }
      app.axios("GET", "app", "/Othteam/getOthteamPage", 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);
        console.log(this.data.list,'this.data.list===>');
      })
    },
  }
})