<template>
  <view class="padding">
    <!-- 日期组件 -->
    <date-selector @change="getData" />
    <!-- 健康上报组件 -->
    <health-data />
    <!-- 校园轨迹组件 -->
    <location-map />
  </view>
</template>

<script>
import { mapActions } from 'vuex';
import DateSelector from './components/date-selector';
import HealthData from './components/health-data';
import LocationMap from './components/location-map';

export default {
  name: 'Statistics',
  components: { DateSelector, LocationMap, HealthData },

  onLoad() {
    // 初始加载今天的数据
    const start = this.$moment().format('YYYY-MM-DD');
    const end = this.$moment().format('YYYY-MM-DD');
    this.getData(start, end);
  },

  methods: {
    ...mapActions('statistics', ['getSchoolSigns', 'getSchoolSignsNumber']),
    /**
     * 获取数据
     * @param {string} start 开始时间 yy-mm-dd
     * @param {string} end 截止时间 yy-mm-dd
     */
    getData(start, end) {
      console.log('start, end: ', start, end);
      const startTime =
        this.$moment(start)
          .startOf('day')
          .format('x') - 0;
      const endTime =
        this.$moment(end)
          .endOf('day')
          .format('x') - 0;
      console.log('startTime,endTime: ', startTime, endTime);
      // 获取校园打卡的数据
      this.getSchoolSignsData(startTime, endTime);
      // 获取健康上报数目数据
      this.getSchoolSignsNumberData(startTime, endTime);
    },

    /**
     * 获取校园打卡的数据
     * @param {number} start 开始时间
     * @param {number} end 截止时间
     */
    getSchoolSignsData(start, end) {
      try {
        const params = { param: { start, end } };
        this.getSchoolSigns(params);
      } catch (error) {
        console.log('getSchoolSignsData error: ', error);
      }
    },

    /**
     * 获取健康上报的数目数据
     * @param {number} start 开始时间
     * @param {number} end 截止时间
     */
    getSchoolSignsNumberData(start, end) {
      try {
        const params = { param: { start, end } };
        this.getSchoolSignsNumber(params);
      } catch (error) {
        console.log('getSchoolSignsNumberData error: ', error);
      }
    },
  },
};
</script>
