<template>
  <div v-loading="loading">
    <div class="white-box">
      <div class="filter-box">
        <filter-user :filter-types.sync="filterTypes" @select-value="handleFilterUser"></filter-user>
      </div>
      <el-table class="table-box" ref="multipleTable" :data="tableData" stripe style="width: 100%;" :height="tool.getTableHeight()">
        <el-table-column v-for="(tableHeader, key) in tableHeaders" :key="key" :label="tableHeader.header" :width="tableHeader.other.width ? tableHeader.other.width : ''" :prop="tableHeader.other.prop ? tableHeader.other.prop : null">
          <template slot-scope="scope">
            <template v-if="scope.row[tableHeader.index].other.tag" >
              <el-tag :type="scope.row[tableHeader.index].other.tag.type ? scope.row[tableHeader.index].other.tag.type : null" :size="scope.row[tableHeader.index].other.tag.size ? scope.row[tableHeader.index].other.tag.size : null" :class="scope.row[tableHeader.index].other.tag.class ? scope.row[tableHeader.index].other.tag.class : null" >{{scope.row[tableHeader.index].value}}</el-tag>
            </template>
            <template v-else>
              <div v-html="scope.row[tableHeader.index].value"></div>
            </template>
          </template>
        </el-table-column>
      </el-table>
      <div class="white-box-footer">
        <el-button type="success" size="small" @click="handleExport" v-show="permission.hasPermission(`bonus/new-period-bonus-export`)">Export Excel<!-- 导出Excel --></el-button>
        <pagination :total="totalCount" :page_size="pageSize" @size-change="handleSizeChange" @current-change="handleCurrentChange"></pagination>
      </div>
    </div>
  </div>
</template>

<script>
import network from '@/utils/network'
import tool from '@/utils/tool'
import TraceDownYcFilter from './trace-down-yc-filter'
import FilterUser from '@/components/FilterUser'
import permission from '@/utils/permission'
import baseInfo from '@/utils/baseInfo'
import Pagination from '@/components/Pagination'
import filterHelper from '@/utils/filterHelper'

export default {
  name: 'bonus_new-period-bonus',
  components: {TraceDownYcFilter,FilterUser,Pagination},
  mounted () {
    this.getData()
  },
  data () {
    return {
      tableHeaders: null,
      tableData: null,
      sumData: {
        'SUM_PV_1L':null,
      },
      loading: true,
      multipleSelection: [],
      currentPage: 1,
      totalPages: 1,
      totalCount: 1,
      pageSize: 20,
      tool: tool,
      permission: permission,
      filterTypes: null,
      filterModel: {},
      baseDecLevels: baseInfo.decLevels(),
      baseEmpLevels: baseInfo.empLevels(),
    }
  },
  methods: {
    handleCurrentChange (page) {
      this.getData(page, this.pageSize)
    },
    handleSizeChange (pageSize) {
      this.getData(this.currentPage, pageSize)
    },
    handleFilterUser(filterData) {
      filterHelper.handleFilterUser(this, filterData)
    },
    handleFilter() {
      this.getData()
    },
    getData(page, pageSize) {
      let filterData = this.filterModel
      let vueObj = this
      network.getPageData(this, 'bonus/new-period-bonus', page, pageSize, filterData, function (response) {
        vueObj.filterTypes = response.filterTypes
        vueObj.allData = response
        // vueObj.sumData = response.sumData
      })
    },
    getSummaries(param) {
      const { columns, data } = param
      const sums = []
      if(columns && data && columns.length>0 && data.length>0) {
        columns.forEach((column, index) => {
          if (index === 0) {
            sums[index] = 'sub-total' + "\n" + 'Total' //'小计' + "\n" + '合计'
            return
          }
          const values = data.map(item => {
            if(typeof item[column.property] === 'object'){
              return Number(item[column.property].value)
            } else {
              return Number(undefined)
            }
          });
          if (!values.every(value => isNaN(value))) {
            sums[index] = values.reduce((prev, curr) => {
              const value = Number(curr);
              if (!isNaN(value)) {
                return prev + curr
              } else {
                return prev
              }
            }, 0);
            const field = 'SUM_' + column.property;
            if (typeof this.sumData[field] !== 'undefined') {
              sums[index] = tool.formatPrice(sums[index]) + '\n' + tool.formatPrice(this.sumData[field])
            } else {
              sums[index] = tool.formatPrice(sums[index])
            }
          } else {
            sums[index] = '-'
          }
        })
      }
      return sums
    },
    handleExport(){
      this.$confirm(`Are you sure you want to export the current data？`, 'Hint', {//`确定要导出当前数据吗？`, '提示'
        confirmButtonText: 'confirm', // 确定
        cancelButtonText: 'cancel', // 取消
        type: 'warning'
      }).then(() => {
        return network.getData(`bonus/new-period-bonus-export`, this.filterModel)
      }).then(response => {
        this.$message({
          message: response,
          type: 'success'
        })
      }).catch(response => {

      })
    },
  }
}

</script>

<style scoped>
  .table-box .el-form-item__label {
    width: 100px;
    color: #99a9bf;
  }

  .table-box .el-form-item {
    width: 30%;
    margin-right: 0;
    margin-bottom: 0;
  }
</style>
