<template>
  <div>
    <div>
      <tiny-button @click="loadData">加载数据</tiny-button>
    </div>
    <br />
    <tiny-grid :data="tableData" show-footer :footer-method="footerMethod" border>
      <tiny-grid-column type="index" width="60"></tiny-grid-column>
      <tiny-grid-column type="selection" width="60"></tiny-grid-column>
      <tiny-grid-column field="name" title="公司名称"></tiny-grid-column>
      <tiny-grid-column field="employees" title="员工数"></tiny-grid-column>
      <tiny-grid-column field="createdDate" title="创建日期"></tiny-grid-column>
      <tiny-grid-column field="city" title="城市"></tiny-grid-column>
    </tiny-grid>
  </div>
</template>

<script lang="jsx">
import { TinyGrid, TinyGridColumn, TinyButton } from '@opentiny/vue'

export default {
  components: {
    TinyButton,
    TinyGrid,
    TinyGridColumn
  },
  data() {
    return {
      tableData: []
    }
  },
  methods: {
    loadData() {
      this.tableData = [
        {
          id: '0',
          name: 'GFD 科技 YX 公司',
          city: '福州',
          employees: 800,
          createdDate: '2014-04-30 00:56:00'
        },
        {
          id: '2',
          name: 'WWW 科技 YX 公司',
          city: '深圳',
          employees: 300,
          createdDate: '2016-07-08 12:36:22'
        },
        {
          id: '3',
          name: 'RFV 有限责任公司',
          city: '中山',
          employees: 1300,
          createdDate: '2014-02-14 14:14:14'
        },
        {
          id: '4',
          name: 'TGB 科技 YX 公司',
          city: '龙岩',
          employees: 360,
          createdDate: '2013-01-13 13:13:13'
        },
        {
          id: '5',
          name: 'YHN 科技 YX 公司',
          city: '韶关',
          employees: 810,
          createdDate: '2012-12-12 12:12:12'
        },
        {
          id: '6',
          name: 'WSX 科技 YX 公司',
          city: '黄冈',
          employees: 800,
          createdDate: '2011-11-11 11:11:11'
        },
        {
          id: '7',
          name: 'KBG 物业 YX 公司',
          city: '赤壁',
          employees: 400,
          createdDate: '2016-04-30 23:56:00'
        },
        {
          id: '8',
          name: '深圳市福德宝网络技术 YX 公司',
          city: '厦门',
          employees: 540,
          createdDate: '2016-06-03 13:53:25'
        }
      ]
    },
    footerMethod({ columns, data }) {
      if (data.length === 0) {
        return [
          ['平均', '', '', 0],
          ['和值', '', '', 0]
        ]
      }

      return [
        columns.map((column, columnIndex) => {
          if (columnIndex === 0) {
            return '平均'
          }

          if (column.property === 'employees') {
            return Math.floor(data.map((item) => item[column.property]).reduce((acc, item) => acc + item) / data.length)
          }

          return null
        }),
        columns.map((column, columnIndex) => {
          if (columnIndex === 0) {
            return '和值'
          }

          if (column.property === 'employees') {
            return data.map((item) => item[column.property]).reduce((acc, item) => acc + item)
          }

          return null
        })
      ]
    }
  }
}
</script>
